mono项目开发模板,内置cli管理构建
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

45 lines
1.5 KiB

import { forceToolFailure, toolErrorStrategy } from "../env";
import type { DemoLog, XllmClient } from "./types";
export async function runStreamWithToolsDemo(client: XllmClient, log: DemoLog): Promise<void> {
log("========== streamWithTools() 流式 + 工具闭环 ==========");
log(`[streamWithTools] toolErrorStrategy: ${toolErrorStrategy}`);
log(`[streamWithTools] forceToolFailure: ${forceToolFailure}`);
let text = "";
for await (const event of client.streamWithTools(
{
messages: [{ role: "user", content: [{ type: "text", text: "查询北京天气,并简短回复。" }] }],
tools: [{ name: "get_weather", parameters: { type: "object", properties: { city: { type: "string" } } } }],
toolChoice: "auto",
toolErrorStrategy,
thinking: { type: "enabled" },
reasoningEffort: "high",
},
{
get_weather: (args) => {
if (forceToolFailure) {
throw new Error("forced tool failure from VITE_XLLM_FORCE_TOOL_FAILURE=1");
}
return {
city:
typeof args === "object" &&
args !== null &&
"city" in args &&
typeof (args as { city?: unknown }).city === "string"
? (args as { city: string }).city
: "Beijing",
weather: "晴",
temperature: 26,
};
},
},
)) {
if (event.type === "text.delta") {
text += event.text;
log(`[streamWithTools] delta: ${event.text}`);
}
}
log(`[streamWithTools] final: ${text}`);
}