فهرست منبع

rm vectorStoreRelation field

Shun Miyazawa 1 سال پیش
والد
کامیت
4db73bac85

+ 0 - 2
apps/app/src/features/openai/interfaces/thread-relation.ts

@@ -1,11 +1,9 @@
 import type { IUser, Ref, HasObjectId } from '@growi/core';
 
 import type { AiAssistant } from './ai-assistant';
-import type { IVectorStore } from './vector-store';
 
 export interface IThreadRelation {
   userId: Ref<IUser>
-  vectorStore: Ref<IVectorStore>
   aiAssistant: Ref<AiAssistant>
   threadId: string;
   title?: string;

+ 0 - 5
apps/app/src/features/openai/server/models/thread-relation.ts

@@ -30,11 +30,6 @@ const schema = new Schema<ThreadRelationDocument, ThreadRelationModel>({
     ref: 'AiAssistant',
     required: true,
   },
-  vectorStore: {
-    type: Schema.Types.ObjectId,
-    ref: 'VectorStore',
-    required: true,
-  },
   threadId: {
     type: String,
     required: true,

+ 1 - 2
apps/app/src/features/openai/server/routes/thread.ts

@@ -50,8 +50,7 @@ export const createThreadHandlersFactory: CreateThreadFactory = (crowi) => {
           return res.apiv3Err(new ErrorV3('The specified AI assistant is not usable'), 400);
         }
 
-        const vectorStoreRelation = await openaiService.getVectorStoreRelation(aiAssistantId);
-        const thread = await openaiService.createThread(req.user._id, aiAssistantId, vectorStoreRelation, initialUserMessage);
+        const thread = await openaiService.createThread(req.user._id, aiAssistantId, initialUserMessage);
 
         return res.apiv3(thread);
       }

+ 6 - 16
apps/app/src/features/openai/server/services/openai.ts

@@ -65,9 +65,7 @@ const convertPathPatternsToRegExp = (pagePathPatterns: string[]): Array<string |
 };
 
 export interface IOpenaiService {
-  createThread(
-    userId: string, aiAssistantId: string, vectorStoreRelation: VectorStoreDocument, initialUserMessage: string
-  ): Promise<ThreadRelationDocument>;
+  createThread(userId: string, aiAssistantId: string, initialUserMessage: string): Promise<ThreadRelationDocument>;
   getThreadsByAiAssistant(aiAssistantId: string): Promise<ThreadRelationDocument[]>
   deleteThread(threadRelationId: string): Promise<ThreadRelationDocument>;
   deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
@@ -122,9 +120,9 @@ class OpenaiService implements IOpenaiService {
     return threadTitle;
   }
 
-  async createThread(
-      userId: string, aiAssistantId: string, vectorStoreRelation: VectorStoreDocument, initialUserMessage: string,
-  ): Promise<ThreadRelationDocument> {
+  async createThread(userId: string, aiAssistantId: string, initialUserMessage: string): Promise<ThreadRelationDocument> {
+    const vectorStoreRelation = await this.getVectorStoreRelation(aiAssistantId);
+
     let threadTitle: string | null = null;
     if (initialUserMessage != null) {
       try {
@@ -141,7 +139,6 @@ class OpenaiService implements IOpenaiService {
         userId,
         aiAssistant: aiAssistantId,
         threadId: thread.id,
-        vectorStore: vectorStoreRelation._id,
         title: threadTitle,
       });
       return threadRelation;
@@ -151,24 +148,17 @@ class OpenaiService implements IOpenaiService {
     }
   }
 
-  async updateThreads(aiAssistantId: string, vectorStoreId: string, vectorStoreRelationId: string): Promise<void> {
+  async updateThreads(aiAssistantId: string, vectorStoreId: string): Promise<void> {
     const threadRelations = await this.getThreadsByAiAssistant(aiAssistantId);
-    const updatedThreadRelationIds: string[] = [];
     for await (const threadRelation of threadRelations) {
       try {
         const updatedThreadResponse = await this.client.updateThread(threadRelation.threadId, vectorStoreId);
         logger.debug('Update thread', updatedThreadResponse);
-        updatedThreadRelationIds.push(threadRelation._id);
       }
       catch (err) {
         logger.error(err);
       }
     }
-
-    await ThreadRelationModel.updateMany(
-      { _id: { $in: updatedThreadRelationIds } },
-      { vectorStore: vectorStoreRelationId },
-    );
   }
 
   async getThreadsByAiAssistant(aiAssistantId: string): Promise<ThreadRelationDocument[]> {
@@ -835,7 +825,7 @@ class OpenaiService implements IOpenaiService {
 
       newVectorStoreRelation = await this.createVectorStore(data.name);
 
-      this.updateThreads(aiAssistantId, newVectorStoreRelation.vectorStoreId, newVectorStoreRelation._id);
+      this.updateThreads(aiAssistantId, newVectorStoreRelation.vectorStoreId);
 
       // VectorStore creation process does not await
       this.createVectorStoreFileWithStream(newVectorStoreRelation, conditions);