尧图网络科技YAOTU DIGITAL 获取报价
获取报价
首页 / 资讯中心 / 文章详情

如何用 puter.ai.chat() 的 function calling 定义工具、执行本地函数并回传结果?

发布时间:2026/9/11 5:07:54

资讯中心
01
ARTICLE

如何用 puter.ai.chat() 的 function calling 定义工具、执行本地函数并回传结果?

如何用 puter.ai.chat() 的 function calling 定义工具、执行本地函数并回传结果?
如何用 puter.ai.chat() 的 function calling 定义工具、执行本地函数并回传结果【免费下载链接】puter The Internet Computer! Free, Open-Source, and Self-Hostable.项目地址: https://gitcode.com/GitHub_Trending/pu/puter本文解决的问题是在 Puter 网页应用或网站中调用puter.ai.chat()时如何告诉 AI 模型你本地定义了哪些函数工具在模型决定调用某个函数时执行它并把执行结果回传给模型让它基于结果生成最终回答。文档说明puter.ai.chat()适用于 websites、apps、nodejs、workers 等平台本文示例按文档给出的网页HTML script场景编写。Function Calling 的完整流程Puter 文档将 function calling也称 tool calling描述为五个步骤Define tools—— 在传给puter.ai.chat()的tools数组中创建函数规格AI requests a tool call—— 如果模型判断需要调用函数它会返回一个tool_calls数组而不是文本消息Execute the function—— 你的代码根据请求匹配到对应函数并执行Send the result back—— 用role: tool把函数结果传回给模型AI responds—— 模型基于工具结果生成最终回答。也就是说puter.ai.chat()本身不会替你执行函数模型只发出调用请求执行和回传都由你的代码完成。第一步定义工具tools 参数工具是tools数组中的函数规格对象字段如下来自puter.ai.chat()文档typeString—— 必须是functionfunction.nameString—— 函数名如get_weatherfunction.descriptionString—— 说明函数做什么、何时使用function.parametersObject—— 用 JSON Schema 描述函数入参function.strictBoolean可选—— 是否强制严格的参数校验。文档示例中定义的天气工具const tools [{ type: function, function: { name: get_weather, description: Get current weather for a location, parameters: { type: object, properties: { location: { type: string, description: City name } }, required: [location] } } }];parameters就是标准的 JSON Schema 对象required指明必填参数。替换为你自己的函数时保持这一结构不变只修改name、description和properties。第二步准备页面并加载 Puter JS文档所有示例都通过同一个方式引入 SDK。在你的 HTML 页面中加入script srchttps://js.puter.com/v2//script第三步发起带 tools 的调用并执行本地函数下面这段代码取自文档的 Function Calling 示例ai-function-calling.html可以整体放入页面script中直接运行。其中getWeather是文档给出的 mock 函数替换成你自己的本地函数即可script srchttps://js.puter.com/v2//script script // Mock weather function function getWeather(location) { return location : 22°C, Sunny; } // Define the tool const tools [{ type: function, function: { name: get_weather, description: Get current weather for a location, parameters: { type: object, properties: { location: { type: string, description: City name } }, required: [location] } } }]; (async () { const question Whats the weather in Paris?; puter.print(Question: question br/); puter.print((Loading...)br/); // Call AI with tools const response await puter.ai.chat(question, { tools }); // Check if AI wants to call a function if (response.message.tool_calls?.length 0) { const toolCall response.message.tool_calls[0]; const args JSON.parse(toolCall.function.arguments); const weatherData getWeather(args.location); // Send result back to AI const finalResponse await puter.ai.chat([ { role: user, content: question }, response.message, { role: tool, tool_call_id: toolCall.id, content: weatherData } ]); puter.print(Answer: finalResponse); } else { // If the AI responds directly without calling a tool, print its message puter.print(Answer: response); } })(); /script注意两点均来自文档toolCall.function.arguments是一个 JSON 字符串执行前需要JSON.parse如果模型没有发起工具调用tool_calls为空会走else分支直接返回文本回答代码必须同时处理这两种情况。第四步回传结果时 messages 的构造规则第二次调用puter.ai.chat()传入的是 messages 数组文档要求每条消息带role和contentrole取值system、assistant、user或tool。回传工具结果的消息必须包含role—— 必须是tooltool_call_id—— 取第一个 tool call 中的id该 id 在 ToolCall 对象中定义为每次工具调用的唯一标识content—— 函数结果作为字符串。上面的示例中第二次调用的 messages 由三部分组成原始 user 消息、模型发出的response.message含tool_calls的 assistant 消息、以及那条role: tool的结果消息。可选分支流式模式下的 function calling如果同时开了stream: true响应不再是单个对象而是用for await...of迭代的 ChatResponseChunk 流。工具调用以type tool_use的 chunk 出现此时字段名与非流式不同part.name是函数名part.input是已解析好的参数对象不需要再JSON.parsepart.id是调用 id。文档示例ai-streaming-function-calling.html的处理骨架const response await puter.ai.chat(question, { tools, stream: true }); for await (const part of response) { if (part.type text) { puter.print(part.text); } else if (part.type tool_use) { const funcName part.name; const args part.input; // 已解析: { location: Paris } let result; if (funcName get_weather) { result getWeather(args.location); } const finalResponse await puter.ai.chat([ { role: user, content: question }, { role: assistant, tool_calls: [{ id: part.id, type: function, function: { name: funcName, arguments: JSON.stringify(args) } }]}, { role: tool, tool_call_id: part.id, content: result } ], { stream: true }); for await (const finalPart of finalResponse) { if (finalPart.text) puter.print(finalPart.text); } } }这里回传时手动重建了 assistant 的tool_calls消息arguments需要重新JSON.stringify这是流式示例与上面非流式示例的主要差异。验证方式与已知边界判断模型是否发起了工具调用非流式时检查response.message.tool_calls?.length 0文档示例的判断方式流式时监听part.type tool_use。finish_reasonChatResponse 文档说明在规范化OpenAI 格式响应中当本轮以工具调用结束时finish_reason为tool_calls否则为stop等值。纯工具调用轮的 contentChatResponse 文档指出当模型只返回工具调用而没有任何文本时message.content为null此时内容全部在message.tool_calls中。模型选择示例未显式指定model即使用文档说明的默认模型gpt-5-nano需要时可在 options 中传入model500 可选模型或provider固定供应商。调试puter.ai.chat(prompt, testMode false, options)支持testMode参数用于在不消耗 API credits 的情况下测试代码。以上完整实现还可参考文档指向的两个 playground 页面源码ai-function-calling.html非流式与 ai-streaming-function-calling.html流式以及 API 主文档 chat.md 的 Function Calling 章节。【免费下载链接】puter The Internet Computer! Free, Open-Source, and Self-Hostable.项目地址: https://gitcode.com/GitHub_Trending/pu/puter创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
02
RELATED NEWS

相关资讯

更多网站建设与数字化升级内容

03
WHY YAOTU

想打造同款高转化官网?

懂行业、懂生意,从建站到增长一站式陪跑

场景化定制

不做模板站,围绕你的业务场景量身设计,小众不撞款。

营销型架构

以转化目标组织内容与路径,让官网真正带来询盘。

全周期服务

设计、开发、运营、运维一体,上线只是开始。

免费获取你的建站方案

留下需求,专属顾问 24 小时内为你输出方案建议。