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

+ 0 - 4
apps/app/src/features/openai/server/routes/index.ts

@@ -19,10 +19,6 @@ export const factory = (crowi: Crowi): express.Router => {
   }
   // enabled
   else {
-    import('./rebuild-vector-store').then(({ rebuildVectorStoreHandlersFactory }) => {
-      router.post('/rebuild-vector-store', rebuildVectorStoreHandlersFactory(crowi));
-    });
-
     import('./thread').then(({ createThreadHandlersFactory }) => {
       router.post('/thread', createThreadHandlersFactory(crowi));
     });

+ 0 - 47
apps/app/src/features/openai/server/routes/rebuild-vector-store.ts

@@ -1,47 +0,0 @@
-import { ErrorV3 } from '@growi/core/dist/models';
-import type { Request, RequestHandler } from 'express';
-import type { ValidationChain } from 'express-validator';
-
-import type Crowi from '~/server/crowi';
-import { accessTokenParser } from '~/server/middlewares/access-token-parser';
-import { apiV3FormValidator } from '~/server/middlewares/apiv3-form-validator';
-import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-response';
-import loggerFactory from '~/utils/logger';
-
-import { getOpenaiService } from '../services/openai';
-
-import { certifyAiService } from './middlewares/certify-ai-service';
-
-const logger = loggerFactory('growi:routes:apiv3:openai:rebuild-vector-store');
-
-type RebuildVectorStoreFactory = (crowi: Crowi) => RequestHandler[];
-
-export const rebuildVectorStoreHandlersFactory: RebuildVectorStoreFactory = (crowi) => {
-  const loginRequiredStrictly = require('~/server/middlewares/login-required')(crowi);
-  const adminRequired = require('~/server/middlewares/admin-required')(crowi);
-
-  const validator: ValidationChain[] = [
-    //
-  ];
-
-  return [
-    accessTokenParser, loginRequiredStrictly, adminRequired, certifyAiService, validator, apiV3FormValidator,
-    async(req: Request, res: ApiV3Response) => {
-
-      const openaiService = getOpenaiService();
-      if (openaiService == null) {
-        return res.apiv3Err(new ErrorV3('GROWI AI is not enabled'), 501);
-      }
-
-      try {
-        // await openaiService?.rebuildVectorStoreAll();
-        return res.apiv3({});
-
-      }
-      catch (err) {
-        logger.error(err);
-        return res.apiv3Err(new ErrorV3('Vector Store rebuild failed'));
-      }
-    },
-  ];
-};

+ 0 - 72
apps/app/src/features/openai/server/services/openai.ts

@@ -35,7 +35,6 @@ import AiAssistantModel, { type AiAssistantDocument } from '../models/ai-assista
 import { convertMarkdownToHtml } from '../utils/convert-markdown-to-html';
 
 import { getClient } from './client-delegator';
-// import { splitMarkdownIntoChunks } from './markdown-splitter/markdown-token-splitter';
 import { openaiApiErrorHandler } from './openai-api-error-handler';
 import { replaceAnnotationWithPageLink } from './replace-annotation-with-page-link';
 
@@ -45,7 +44,6 @@ const BATCH_SIZE = 100;
 
 const logger = loggerFactory('growi:service:openai');
 
-// const isVectorStoreForPublicScopeExist = false;
 
 type VectorStoreFileRelationsMap = Map<string, VectorStoreFileRelation>
 
@@ -233,38 +231,6 @@ class OpenaiService implements IOpenaiService {
     return messages;
   }
 
-  // TODO: https://redmine.weseek.co.jp/issues/160332
-  // public async getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument> {
-  //   const vectorStoreDocument: VectorStoreDocument | null = await VectorStoreModel.findOne({ scopeType: VectorStoreScopeType.PUBLIC, isDeleted: false });
-
-  //   if (vectorStoreDocument != null && isVectorStoreForPublicScopeExist) {
-  //     return vectorStoreDocument;
-  //   }
-
-  //   if (vectorStoreDocument != null && !isVectorStoreForPublicScopeExist) {
-  //     try {
-  //       // Check if vector store entity exists
-  //       // If the vector store entity does not exist, the vector store document is deleted
-  //       await this.client.retrieveVectorStore(vectorStoreDocument.vectorStoreId);
-  //       isVectorStoreForPublicScopeExist = true;
-  //       return vectorStoreDocument;
-  //     }
-  //     catch (err) {
-  //       await oepnaiApiErrorHandler(err, { notFoundError: vectorStoreDocument.markAsDeleted });
-  //       throw new Error(err);
-  //     }
-  //   }
-
-  //   const newVectorStore = await this.client.createVectorStore(VectorStoreScopeType.PUBLIC);
-  //   const newVectorStoreDocument = await VectorStoreModel.create({
-  //     vectorStoreId: newVectorStore.id,
-  //     scopeType: VectorStoreScopeType.PUBLIC,
-  //   }) as VectorStoreDocument;
-
-  //   isVectorStoreForPublicScopeExist = true;
-
-  //   return newVectorStoreDocument;
-  // }
 
   async getVectorStoreRelation(aiAssistantId: string): Promise<VectorStoreDocument> {
     const aiAssistant = await AiAssistantModel.findById({ _id: aiAssistantId }).populate('vectorStore');
@@ -344,22 +310,6 @@ class OpenaiService implements IOpenaiService {
     }
   }
 
-  // TODO: https://redmine.weseek.co.jp/issues/160332
-  // TODO: https://redmine.weseek.co.jp/issues/156643
-  // private async uploadFileByChunks(pageId: Types.ObjectId, body: string, vectorStoreFileRelationsMap: VectorStoreFileRelationsMap) {
-  //   const chunks = await splitMarkdownIntoChunks(body, 'gpt-4o');
-  //   for await (const [index, chunk] of chunks.entries()) {
-  //     try {
-  //       const file = await toFile(Readable.from(chunk), `${pageId}-chunk-${index}.md`);
-  //       const uploadedFile = await this.client.uploadFile(file);
-  //       prepareVectorStoreFileRelations(pageId, uploadedFile.id, vectorStoreFileRelationsMap);
-  //     }
-  //     catch (err) {
-  //       logger.error(err);
-  //     }
-  //   }
-  // }
-
   private async uploadFile(pageId: Types.ObjectId, pagePath: string, revisionBody: string): Promise<OpenAI.Files.FileObject> {
     const convertedHtml = await convertMarkdownToHtml({ pagePath, revisionBody });
     const file = await toFile(Readable.from(convertedHtml), `${pageId}.html`);
@@ -542,28 +492,6 @@ class OpenaiService implements IOpenaiService {
     }
   }
 
-  // TODO: https://redmine.weseek.co.jp/issues/160332
-  // async rebuildVectorStoreAll() {
-  //   await this.deleteVectorStore(VectorStoreScopeType.PUBLIC);
-
-  //   // Create all public pages VectorStoreFile
-  //   const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
-  //   const pagesStream = Page.find({ grant: PageGrant.GRANT_PUBLIC }).populate('revision').cursor({ batch_size: BATCH_SIZE });
-  //   const batchStrem = createBatchStream(BATCH_SIZE);
-
-  //   const createVectorStoreFile = this.createVectorStoreFile.bind(this);
-  //   const createVectorStoreFileStream = new Transform({
-  //     objectMode: true,
-  //     async transform(chunk: HydratedDocument<PageDocument>[], encoding, callback) {
-  //       await createVectorStoreFile(chunk);
-  //       this.push(chunk);
-  //       callback();
-  //     },
-  //   });
-
-  //   await pipeline(pagesStream, batchStrem, createVectorStoreFileStream);
-  // }
-
   async filterPagesByAccessScope(aiAssistant: AiAssistantDocument, pages: HydratedDocument<PageDocument>[]) {
     const isPublicPage = (page :HydratedDocument<PageDocument>) => page.grant === PageGrant.GRANT_PUBLIC;
 

+ 0 - 5
apps/app/src/features/rate-limiter/config/index.ts

@@ -56,11 +56,6 @@ export const defaultConfig: IApiRateLimitEndpointMap = {
     method: 'GET',
     maxRequests: MAX_REQUESTS_TIER_3,
   },
-  '/_api/v3/openai/rebuild-vector-store': {
-    method: 'POST',
-    maxRequests: 1,
-    usersPerIpProspection: 1,
-  },
 };
 
 const isDev = process.env.NODE_ENV === 'development';