跳转到内容
搜索文档

传统

最后更新 查看 MarkdownAgent 设置

本页展示如何按行业标准执行传统 function calling。Workers AI 还提供嵌入式 function calling,比传统 function calling 简单得多。

使用传统 function calling 时,你定义包含名称、描述和 tool 参数的 tool 数组。以下示例展示如何在模型的推理请求中传递名为 getWeather 的 tool。

Traditional function calling examplejs
const response = await env.AI.run("@hf/nousresearch/hermes-2-pro-mistral-7b", {
	messages: [
		{
			role: "user",
			content: "what is the weather in london?",
		},
	],
	tools: [
		{
			name: "getWeather",
			description: "Return the weather for a latitude and longitude",
			parameters: {
				type: "object",
				properties: {
					latitude: {
						type: "string",
						description: "The latitude for the given location",
					},
					longitude: {
						type: "string",
						description: "The longitude for the given location",
					},
				},
				required: ["latitude", "longitude"],
			},
		},
	],
});

return new Response(JSON.stringify(response.tool_calls));

LLM 将返回包含所需参数和被调用 tool 名称的 JSON 对象。然后你可以传递此 JSON 对象来发起 API 调用。

[
	{
		"arguments": { "latitude": "51.5074", "longitude": "-0.1278" },
		"name": "getWeather"
	}
]

有关 function calling 的工作示例,请参阅我们的演示应用

这篇文档对您有帮助吗?