Shun Miyazawa 1 год назад
Родитель
Сommit
0b307ca12b

+ 4 - 18
apps/app/src/server/service/openai/client-delegator/azure-openai-client-delegator.ts

@@ -10,8 +10,6 @@ export class AzureOpenaiClientDelegator implements IOpenaiClientDelegator {
 
   private client: AzureOpenAI;
 
-  private openaiVectorStoreId: string;
-
   constructor() {
     // Retrieve Azure OpenAI related values from environment variables
     const credential = new DefaultAzureCredential();
@@ -30,28 +28,16 @@ export class AzureOpenaiClientDelegator implements IOpenaiClientDelegator {
     return this.client.files.create({ file, purpose: 'assistants' });
   }
 
-  async createVectorStoreFileBatch(fileIds: string[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch> {
-    return this.client.beta.vectorStores.fileBatches.create(this.openaiVectorStoreId, { file_ids: fileIds });
-  }
-
-  async getFileList(): Promise<OpenAI.Files.FileObjectsPage> {
-    return this.client.files.list();
-  }
-
-  async getVectorStoreFiles(): Promise<OpenAI.Beta.VectorStores.Files.VectorStoreFilesPage> {
-    return this.client.beta.vectorStores.files.list(this.openaiVectorStoreId);
-  }
-
-  async deleteVectorStoreFiles(fileId: string): Promise<OpenAI.Beta.VectorStores.Files.VectorStoreFileDeleted> {
-    return this.client.beta.vectorStores.files.del(this.openaiVectorStoreId, fileId);
+  async createVectorStoreFileBatch(vectorStoreId: string, fileIds: string[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch> {
+    return this.client.beta.vectorStores.fileBatches.create(vectorStoreId, { file_ids: fileIds });
   }
 
   async deleteFile(fileId: string): Promise<OpenAI.Files.FileDeleted> {
     return this.client.files.del(fileId);
   }
 
-  async uploadAndPoll(files: Uploadable[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch> {
-    return this.client.beta.vectorStores.fileBatches.uploadAndPoll(this.openaiVectorStoreId, { files });
+  async uploadAndPoll(vectorStoreId: string, files: Uploadable[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch> {
+    return this.client.beta.vectorStores.fileBatches.uploadAndPoll(vectorStoreId, { files });
   }
 
 }

+ 1 - 5
apps/app/src/server/service/openai/client-delegator/interfaces.ts

@@ -4,10 +4,6 @@ import type { Uploadable } from 'openai/uploads';
 export interface IOpenaiClientDelegator {
   createVectorStore(): Promise<OpenAI.Beta.VectorStores.VectorStore>
   uploadFile(file: Uploadable): Promise<OpenAI.Files.FileObject>
-  createVectorStoreFileBatch(fileIds: string[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch>
-  getVectorStoreFiles(): Promise<OpenAI.Beta.VectorStores.Files.VectorStoreFilesPage>;
-  deleteVectorStoreFiles(fileId: string): Promise<OpenAI.Beta.VectorStores.Files.VectorStoreFileDeleted>;
-  getFileList(): Promise<OpenAI.Files.FileObjectsPage>;
+  createVectorStoreFileBatch(vectorStoreId: string, fileIds: string[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch>
   deleteFile(fileId: string): Promise<OpenAI.Files.FileDeleted>;
-  uploadAndPoll(files: Uploadable[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch>;
 }

+ 6 - 23
apps/app/src/server/service/openai/client-delegator/openai-client-delegator.ts

@@ -10,54 +10,37 @@ export class OpenaiClientDelegator implements IOpenaiClientDelegator {
 
   private client: OpenAI;
 
-  private openaiVectorStoreId: string;
-
   constructor() {
     // Retrieve OpenAI related values from environment variables
     const apiKey = configManager.getConfig('crowi', 'app:openaiApiKey');
-    const vectorStoreId = configManager.getConfig('crowi', 'app:openaiVectorStoreId');
 
-    const isValid = [apiKey, vectorStoreId].every(value => value != null);
+    const isValid = [apiKey].every(value => value != null);
     if (!isValid) {
       throw new Error("Environment variables required to use OpenAI's API are not set");
     }
 
-    this.openaiVectorStoreId = vectorStoreId;
-
     // initialize client
     this.client = new OpenAI({ apiKey });
   }
 
   async createVectorStore(): Promise<OpenAI.Beta.VectorStores.VectorStore> {
-    return this.client.beta.vectorStores.create({});
+    return this.client.beta.vectorStores.create({ name: 'growi-vector-store' });
   }
 
   async uploadFile(file: Uploadable): Promise<OpenAI.Files.FileObject> {
     return this.client.files.create({ file, purpose: 'assistants' });
   }
 
-  async createVectorStoreFileBatch(fileIds: string[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch> {
-    return this.client.beta.vectorStores.fileBatches.create(this.openaiVectorStoreId, { file_ids: fileIds });
-  }
-
-  async getVectorStoreFiles(): Promise<OpenAI.Beta.VectorStores.Files.VectorStoreFilesPage> {
-    return this.client.beta.vectorStores.files.list(this.openaiVectorStoreId);
-  }
-
-  async deleteVectorStoreFiles(fileId: string): Promise<OpenAI.Beta.VectorStores.Files.VectorStoreFileDeleted> {
-    return this.client.beta.vectorStores.files.del(this.openaiVectorStoreId, fileId);
-  }
-
-  async getFileList(): Promise<OpenAI.Files.FileObjectsPage> {
-    return this.client.files.list();
+  async createVectorStoreFileBatch(vectorStoreId: string, fileIds: string[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch> {
+    return this.client.beta.vectorStores.fileBatches.create(vectorStoreId, { file_ids: fileIds });
   }
 
   async deleteFile(fileId: string): Promise<OpenAI.Files.FileDeleted> {
     return this.client.files.del(fileId);
   }
 
-  async uploadAndPoll(files: Uploadable[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch> {
-    return this.client.beta.vectorStores.fileBatches.uploadAndPoll(this.openaiVectorStoreId, { files });
+  async uploadAndPoll(vectorStoreId: string, files: Uploadable[]): Promise<OpenAI.Beta.VectorStores.FileBatches.VectorStoreFileBatch> {
+    return this.client.beta.vectorStores.fileBatches.uploadAndPoll(vectorStoreId, { files });
   }
 
 }