|
@@ -61,10 +61,11 @@ const convertPathPatternsToRegExp = (pagePathPatterns: string[]): Array<string |
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface IOpenaiService {
|
|
export interface IOpenaiService {
|
|
|
- getOrCreateThread(userId: string, vectorStoreId?: string, threadId?: string): Promise<OpenAI.Beta.Threads.Thread | undefined>;
|
|
|
|
|
|
|
+ getOrCreateThread(userId: string, vectorStoreRelation: VectorStoreDocument, threadId?: string): Promise<OpenAI.Beta.Threads.Thread | undefined>;
|
|
|
// getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
|
|
// getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
|
|
|
deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
|
|
deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
|
|
|
deleteObsolatedVectorStoreRelations(): Promise<void> // for CronJob
|
|
deleteObsolatedVectorStoreRelations(): Promise<void> // for CronJob
|
|
|
|
|
+ getVectorStoreRelation(aiAssistantId: string): Promise<VectorStoreDocument>
|
|
|
createVectorStoreFile(vectorStoreRelation: VectorStoreDocument, pages: PageDocument[]): Promise<void>;
|
|
createVectorStoreFile(vectorStoreRelation: VectorStoreDocument, pages: PageDocument[]): Promise<void>;
|
|
|
deleteVectorStoreFile(vectorStoreRelationId: Types.ObjectId, pageId: Types.ObjectId): Promise<void>;
|
|
deleteVectorStoreFile(vectorStoreRelationId: Types.ObjectId, pageId: Types.ObjectId): Promise<void>;
|
|
|
deleteObsoleteVectorStoreFile(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
|
|
deleteObsoleteVectorStoreFile(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
|
|
@@ -82,11 +83,11 @@ class OpenaiService implements IOpenaiService {
|
|
|
return getClient({ openaiServiceType });
|
|
return getClient({ openaiServiceType });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public async getOrCreateThread(userId: string, vectorStoreId?: string, threadId?: string): Promise<OpenAI.Beta.Threads.Thread> {
|
|
|
|
|
- if (vectorStoreId != null && threadId == null) {
|
|
|
|
|
|
|
+ public async getOrCreateThread(userId: string, vectorStoreRelation: VectorStoreDocument, threadId?: string): Promise<OpenAI.Beta.Threads.Thread> {
|
|
|
|
|
+ if (threadId == null) {
|
|
|
try {
|
|
try {
|
|
|
- const thread = await this.client.createThread(vectorStoreId);
|
|
|
|
|
- await ThreadRelationModel.create({ userId, threadId: thread.id });
|
|
|
|
|
|
|
+ const thread = await this.client.createThread(vectorStoreRelation.vectorStoreId);
|
|
|
|
|
+ await ThreadRelationModel.create({ userId, threadId: thread.id, vectorStore: vectorStoreRelation._id });
|
|
|
return thread;
|
|
return thread;
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
@@ -172,6 +173,15 @@ class OpenaiService implements IOpenaiService {
|
|
|
// return newVectorStoreDocument;
|
|
// return newVectorStoreDocument;
|
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
|
|
+ async getVectorStoreRelation(aiAssistantId: string): Promise<VectorStoreDocument> {
|
|
|
|
|
+ const aiAssistant = await AiAssistantModel.findById({ _id: aiAssistantId }).populate('vectorStore');
|
|
|
|
|
+ if (aiAssistant == null) {
|
|
|
|
|
+ throw createError(404, 'AiAssistant document does not exist');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return aiAssistant.vectorStore as VectorStoreDocument;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private async createVectorStore(name: string): Promise<VectorStoreDocument> {
|
|
private async createVectorStore(name: string): Promise<VectorStoreDocument> {
|
|
|
try {
|
|
try {
|
|
|
const newVectorStore = await this.client.createVectorStore(name);
|
|
const newVectorStore = await this.client.createVectorStore(name);
|