本节介绍故障排除工具并说明常见错误。
Workers 的通用日志功能同样适用于嵌入式 function calling。
tool 的调用可以像任何 Worker 一样使用 console.log() 记录:
export default {
async fetch(request, env, ctx) {
const sum = (args: { a: number; b: number }): Promise<string> => {
const { a, b } = args;
// Logging from within embedded function invocations
console.log(`The sum function has been invoked with the arguments a: ${a} and b: ${b}`)
return Promise.resolve((a + b).toString());
};
...
}
}runWithTools 函数有 verbose 模式,会输出有助于调试 function call 以及输入输出统计的日志。
const response = await runWithTools(
env.AI,
'@hf/nousresearch/hermes-2-pro-mistral-7b',
{
messages: [
...
],
tools: [
...
],
},
// Enable verbose mode
{ verbose: true }
);要使用嵌入式 function 响应 LLM prompt,可能需要多次 AI 推理请求和 function 调用,这可能影响用户体验。
考虑以下方式提升性能:
- 缩短 prompt(减少 input 处理时间)
- 减少提供的 tool 数量
- 向最终用户流式传输最终响应(最小化交互时间)。参见下方示例:
async fetch(request, env, ctx) {
const response = (await runWithTools(
env.AI,
'@hf/nousresearch/hermes-2-pro-mistral-7b',
{
messages: [
...
],
tools: [
...
],
},
{
// Enable response streaming
streamFinalResponse: true,
}
)) as ReadableStream;
// Set response headers for streaming
return new Response(response, {
headers: {
'content-type': 'text/event-stream',
},
});
}如果收到 BadInput 错误,你的输入可能超出了我们模型当前的 context window。尝试减少 input token 以解决此错误。