このページでは、業界標準に沿った従来方式の function calling の手順を説明します。Workers AI では 埋め込み function calling も使えます。従来方式より大幅に簡単です。
従来方式の function calling では、名前、説明、ツール引数を持つツールの配列を定義します。次の例は、推論リクエストで getWeather というツールをモデルに渡す方法です。
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 は、必要な引数と呼び出されたツール名を含む JSON オブジェクトを返します。この JSON オブジェクトを渡して、API を呼び出せます。
[
{
"arguments": { "latitude": "51.5074", "longitude": "-0.1278" },
"name": "getWeather"
}
]function calling の動作例は、デモアプリ ↗ を参照してください。