Просмотр исходного кода

Merge pull request #9410 from weseek/imprv/knowedge-assistant-model-setting

imprv(ai): Knowedge Assistant model configuration by env var
mergify[bot] 1 год назад
Родитель
Сommit
293aa603b3

+ 15 - 4
apps/app/src/features/openai/server/services/assistant/assistant.ts

@@ -10,6 +10,16 @@ const AssistantType = {
   CHAT: 'Chat',
   CHAT: 'Chat',
 } as const;
 } as const;
 
 
+const AssistantDefaultModelMap: Record<AssistantType, OpenAI.Chat.ChatModel> = {
+  [AssistantType.SEARCH]: 'gpt-4o-mini',
+  [AssistantType.CHAT]: 'gpt-4o-mini',
+};
+
+const getAssistantModelByType = (type: AssistantType): OpenAI.Chat.ChatModel => {
+  const configKey = `openai:assistantModel:${type.toLowerCase()}`;
+  return configManager.getConfig('crowi', configKey) ?? AssistantDefaultModelMap[type];
+};
+
 type AssistantType = typeof AssistantType[keyof typeof AssistantType];
 type AssistantType = typeof AssistantType[keyof typeof AssistantType];
 
 
 
 
@@ -34,22 +44,23 @@ const findAssistantByName = async(assistantName: string): Promise<OpenAI.Beta.As
   return findAssistant(storedAssistants);
   return findAssistant(storedAssistants);
 };
 };
 
 
-const getOrCreateAssistant = async(type: AssistantType): Promise<OpenAI.Beta.Assistant> => {
+const getOrCreateAssistant = async(type: AssistantType, nameSuffix?: string): Promise<OpenAI.Beta.Assistant> => {
   const appSiteUrl = configManager.getConfig('crowi', 'app:siteUrl');
   const appSiteUrl = configManager.getConfig('crowi', 'app:siteUrl');
-  const assistantNameSuffix = configManager.getConfig('crowi', 'openai:assistantNameSuffix');
-  const assistantName = `GROWI ${type} Assistant for ${appSiteUrl}${assistantNameSuffix != null ? ` ${assistantNameSuffix}` : ''}`;
+  const assistantName = `GROWI ${type} Assistant for ${appSiteUrl}${nameSuffix != null ? ` ${nameSuffix}` : ''}`;
+  const assistantModel = getAssistantModelByType(type);
 
 
   const assistant = await findAssistantByName(assistantName)
   const assistant = await findAssistantByName(assistantName)
     ?? (
     ?? (
       await openaiClient.beta.assistants.create({
       await openaiClient.beta.assistants.create({
         name: assistantName,
         name: assistantName,
-        model: 'gpt-4o',
+        model: assistantModel,
       }));
       }));
 
 
   // update instructions
   // update instructions
   const instructions = configManager.getConfig('crowi', 'openai:chatAssistantInstructions');
   const instructions = configManager.getConfig('crowi', 'openai:chatAssistantInstructions');
   openaiClient.beta.assistants.update(assistant.id, {
   openaiClient.beta.assistants.update(assistant.id, {
     instructions,
     instructions,
+    model: assistantModel,
     tools: [{ type: 'file_search' }],
     tools: [{ type: 'file_search' }],
   });
   });
 
 

+ 2 - 2
apps/app/src/server/service/config-loader.ts

@@ -803,9 +803,9 @@ Guideline as a RAG:
     ].join(''),
     ].join(''),
   },
   },
   /* eslint-enable max-len */
   /* eslint-enable max-len */
-  OPENAI_ASSISTANT_NAME_SUFFIX: {
+  OPENAI_CHAT_ASSISTANT_MODEL: {
     ns: 'crowi',
     ns: 'crowi',
-    key: 'openai:assistantNameSuffix',
+    key: 'openai:assistantModel:chat',
     type: ValueType.STRING,
     type: ValueType.STRING,
     default: null,
     default: null,
   },
   },