Skip to content

Commit

Permalink
Merge pull request #9410 from weseek/imprv/knowedge-assistant-model-s…
Browse files Browse the repository at this point in the history
…etting

imprv(ai): Knowedge Assistant model configuration by env var
  • Loading branch information
mergify[bot] authored Nov 15, 2024
2 parents a675171 + 6fe56ad commit 293aa60
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const AssistantType = {
CHAT: 'Chat',
} 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];


Expand All @@ -34,22 +44,23 @@ const findAssistantByName = async(assistantName: string): Promise<OpenAI.Beta.As
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 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)
?? (
await openaiClient.beta.assistants.create({
name: assistantName,
model: 'gpt-4o',
model: assistantModel,
}));

// update instructions
const instructions = configManager.getConfig('crowi', 'openai:chatAssistantInstructions');
openaiClient.beta.assistants.update(assistant.id, {
instructions,
model: assistantModel,
tools: [{ type: 'file_search' }],
});

Expand Down
4 changes: 2 additions & 2 deletions apps/app/src/server/service/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,9 @@ Guideline as a RAG:
].join(''),
},
/* eslint-enable max-len */
OPENAI_ASSISTANT_NAME_SUFFIX: {
OPENAI_CHAT_ASSISTANT_MODEL: {
ns: 'crowi',
key: 'openai:assistantNameSuffix',
key: 'openai:assistantModel:chat',
type: ValueType.STRING,
default: null,
},
Expand Down

0 comments on commit 293aa60

Please sign in to comment.