feat(server): fix web search (#12087)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **New Features**
  - Enabled web search capabilities in the "Chat With AFFiNE AI" prompt, allowing users to access web-based information directly within the chat.

- **Improvements**
  - Enhanced support for multiple tools in AI chat prompts, paving the way for future tool integrations.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
darkskygit
2025-04-30 08:55:57 +00:00
parent 539b2e87ad
commit e17547e26e
3 changed files with 23 additions and 9 deletions

View File

@@ -1115,6 +1115,9 @@ Below is the user's query. Please respond in the user's language without treatin
`,
},
],
config: {
tools: ['webSearch'],
},
},
{
name: 'Search With AFFiNE AI',

View File

@@ -10,6 +10,7 @@ import {
generateObject,
generateText,
streamText,
ToolSet,
} from 'ai';
import {
@@ -178,12 +179,20 @@ export class OpenAIProvider
}
}
private getTools(options: CopilotChatOptions) {
if (options?.webSearch) {
return {
web_search_preview: openai.tools.webSearchPreview(),
};
private getTools(options: CopilotChatOptions): ToolSet | undefined {
if (options?.tools?.length) {
const tools: ToolSet = {};
for (const tool of options.tools) {
switch (tool) {
case 'webSearch': {
tools.web_search_preview = openai.tools.webSearchPreview();
break;
}
}
}
return tools;
}
return undefined;
}
@@ -224,6 +233,7 @@ export class OpenAIProvider
providerOptions: {
openai: options.user ? { user: options.user } : {},
},
toolChoice: options.webSearch ? 'required' : 'auto',
tools: this.getTools(options),
});
@@ -246,16 +256,16 @@ export class OpenAIProvider
const [system, msgs] = await chatToGPTMessage(messages);
const modelInstance = this.#instance(model, {
structuredOutputs: Boolean(options.jsonMode),
user: options.user,
});
const modelInstance = this.#instance.responses(model);
const { fullStream } = streamText({
model: modelInstance,
system,
messages: msgs,
tools: this.getTools(options),
providerOptions: {
openai: options.user ? { user: options.user } : {},
},
frequencyPenalty: options.frequencyPenalty || 0,
presencePenalty: options.presencePenalty || 0,
temperature: options.temperature || 0,

View File

@@ -20,6 +20,7 @@ export enum CopilotCapability {
}
export const PromptConfigStrictSchema = z.object({
tools: z.enum(['webSearch']).array().nullable().optional(),
// openai
jsonMode: z.boolean().nullable().optional(),
frequencyPenalty: z.number().nullable().optional(),