← Cloudflare Workers AI / workers-ai / features / function-calling
Tradiční
Tato stránka ukazuje, jak provádět tradiční volání funkcí podle standardů běžných v oboru. Workers AI také nabízí vestavěné volání funkcí, což je výrazně jednodušší než tradiční volání funkcí.
U tradičního volání funkcí definujete pole nástrojů s názvem, popisem a argumenty nástroje. Příklad níže ukazuje, jak byste předali nástroj s názvem getWeather v inferenčním požadavku na model.
Příklad tradičního volání funkcí
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 poté vrátí objekt JSON s požadovanými argumenty a názvem volaného nástroje. Tento objekt JSON pak můžete použít k volání API.
[
{
"arguments": { "latitude": "51.5074", "longitude": "-0.1278" },
"name": "getWeather"
}
]Praktický příklad function calling najdete v našem demo aplikace ↗.