mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-27 02:42:25 +08:00
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:
@@ -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',
|
name: 'Search With AFFiNE AI',
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
generateObject,
|
generateObject,
|
||||||
generateText,
|
generateText,
|
||||||
streamText,
|
streamText,
|
||||||
|
ToolSet,
|
||||||
} from 'ai';
|
} from 'ai';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -178,12 +179,20 @@ export class OpenAIProvider
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getTools(options: CopilotChatOptions) {
|
private getTools(options: CopilotChatOptions): ToolSet | undefined {
|
||||||
if (options?.webSearch) {
|
if (options?.tools?.length) {
|
||||||
return {
|
const tools: ToolSet = {};
|
||||||
web_search_preview: openai.tools.webSearchPreview(),
|
for (const tool of options.tools) {
|
||||||
};
|
switch (tool) {
|
||||||
|
case 'webSearch': {
|
||||||
|
tools.web_search_preview = openai.tools.webSearchPreview();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tools;
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,6 +233,7 @@ export class OpenAIProvider
|
|||||||
providerOptions: {
|
providerOptions: {
|
||||||
openai: options.user ? { user: options.user } : {},
|
openai: options.user ? { user: options.user } : {},
|
||||||
},
|
},
|
||||||
|
toolChoice: options.webSearch ? 'required' : 'auto',
|
||||||
tools: this.getTools(options),
|
tools: this.getTools(options),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -246,16 +256,16 @@ export class OpenAIProvider
|
|||||||
|
|
||||||
const [system, msgs] = await chatToGPTMessage(messages);
|
const [system, msgs] = await chatToGPTMessage(messages);
|
||||||
|
|
||||||
const modelInstance = this.#instance(model, {
|
const modelInstance = this.#instance.responses(model);
|
||||||
structuredOutputs: Boolean(options.jsonMode),
|
|
||||||
user: options.user,
|
|
||||||
});
|
|
||||||
|
|
||||||
const { fullStream } = streamText({
|
const { fullStream } = streamText({
|
||||||
model: modelInstance,
|
model: modelInstance,
|
||||||
system,
|
system,
|
||||||
messages: msgs,
|
messages: msgs,
|
||||||
tools: this.getTools(options),
|
tools: this.getTools(options),
|
||||||
|
providerOptions: {
|
||||||
|
openai: options.user ? { user: options.user } : {},
|
||||||
|
},
|
||||||
frequencyPenalty: options.frequencyPenalty || 0,
|
frequencyPenalty: options.frequencyPenalty || 0,
|
||||||
presencePenalty: options.presencePenalty || 0,
|
presencePenalty: options.presencePenalty || 0,
|
||||||
temperature: options.temperature || 0,
|
temperature: options.temperature || 0,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export enum CopilotCapability {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const PromptConfigStrictSchema = z.object({
|
export const PromptConfigStrictSchema = z.object({
|
||||||
|
tools: z.enum(['webSearch']).array().nullable().optional(),
|
||||||
// openai
|
// openai
|
||||||
jsonMode: z.boolean().nullable().optional(),
|
jsonMode: z.boolean().nullable().optional(),
|
||||||
frequencyPenalty: z.number().nullable().optional(),
|
frequencyPenalty: z.number().nullable().optional(),
|
||||||
|
|||||||
Reference in New Issue
Block a user