From e3c18bb12335bbc631828a78e662b7468ad74681 Mon Sep 17 00:00:00 2001 From: glay Date: Tue, 5 Nov 2024 18:53:12 +0800 Subject: [PATCH] =?UTF-8?q?=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20ap?= =?UTF-8?q?p/api/bedrock.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/bedrock.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/app/api/bedrock.ts b/app/api/bedrock.ts index aeee8eb55..57ab082e3 100644 --- a/app/api/bedrock.ts +++ b/app/api/bedrock.ts @@ -32,6 +32,11 @@ export interface ConverseRequest { topP?: number; stopSequences?: string[]; }; + tools?: { + name: string; + description?: string; + input_schema: any; + }[]; } function formatRequestBody( @@ -83,13 +88,31 @@ function formatRequestBody( : [{ text: msg.content || ";" } as ContentBlock], })); - return { + const input: ConverseStreamCommandInput = { modelId: request.modelId, messages, ...(request.inferenceConfig && { inferenceConfig: request.inferenceConfig, }), }; + + // 添加工具配置 + if (request.tools?.length) { + input.toolConfig = { + tools: request.tools.map((tool) => ({ + toolSpec: { + name: tool.name, + description: tool.description, + inputSchema: { + json: tool.input_schema, // 直接使用对象,不需要 JSON.stringify + }, + }, + })), + toolChoice: { auto: {} }, + }; + } + + return input; } export async function handle( @@ -137,8 +160,6 @@ export async function handle( }); const body = (await req.json()) as ConverseRequest; - console.log("[Bedrock] Request:", body.modelId); - const command = new ConverseStreamCommand(formatRequestBody(body)); const response = await client.send(command);