本指南将指导你设置并部署第一个带嵌入式 function calling 的 Workers AI 项目。你将使用 Workers、Workers AI 绑定(binding)、ai-utils 包 ↗ 和大型语言模型(LLM),在 Cloudflare 全球网络上部署你的第一个 AI 驱动应用。
按照 Workers AI 快速入门指南 执行至第 2 步。
接下来,在项目仓库中运行以下命令安装 Worker AI 工具包。
npm i @cloudflare/ai-utilsyarn add @cloudflare/ai-utilspnpm add @cloudflare/ai-utilsbun add @cloudflare/ai-utils将应用目录中的 index.ts 文件更新为以下代码:
import { runWithTools } from "@cloudflare/ai-utils";
export default {
async fetch(request, env, ctx) {
// Define function
const sum = (args) => {
const { a, b } = args;
return Promise.resolve((a + b).toString());
};
// Run AI inference with function calling
const response = await runWithTools(
env.AI,
// Model with function calling support
"@hf/nousresearch/hermes-2-pro-mistral-7b",
{
// Messages
messages: [
{
role: "user",
content: "What the result of 123123123 + 10343030?",
},
],
// Definition of available tools the AI model can leverage
tools: [
{
name: "sum",
description: "Sum up two numbers and returns the result",
parameters: {
type: "object",
properties: {
a: { type: "number", description: "the first number" },
b: { type: "number", description: "the second number" },
},
required: ["a", "b"],
},
// reference to previously defined function
function: sum,
},
],
},
);
return new Response(JSON.stringify(response));
},
};import { runWithTools } from "@cloudflare/ai-utils";
type Env = {
AI: Ai;
};
export default {
async fetch(request, env, ctx) {
// Define function
const sum = (args: { a: number; b: number }): Promise<string> => {
const { a, b } = args;
return Promise.resolve((a + b).toString());
};
// Run AI inference with function calling
const response = await runWithTools(
env.AI,
// Model with function calling support
"@hf/nousresearch/hermes-2-pro-mistral-7b",
{
// Messages
messages: [
{
role: "user",
content: "What the result of 123123123 + 10343030?",
},
],
// Definition of available tools the AI model can leverage
tools: [
{
name: "sum",
description: "Sum up two numbers and returns the result",
parameters: {
type: "object",
properties: {
a: { type: "number", description: "the first number" },
b: { type: "number", description: "the second number" },
},
required: ["a", "b"],
},
// reference to previously defined function
function: sum,
},
],
},
);
return new Response(JSON.stringify(response));
},
} satisfies ExportedHandler<Env>;此示例通过 import { runWithTools} from "@cloudflare/ai-utils" 导入工具,并遵循下方 API 参考。
此外,在本示例中我们定义并描述 LLM 可用于响应用户查询的 tool 列表。此处列表仅包含一个 tool,即 sum 函数。
由 runWithTools 函数抽象,将发生以下步骤:
sequenceDiagram
participant Worker as Worker
participant WorkersAI as Workers AI
Worker->>+WorkersAI: Send messages, function calling prompt, and available tools
WorkersAI->>+Worker: Select tools and arguments for function calling
Worker-->>-Worker: Execute function
Worker-->>+WorkersAI: Send messages, function calling prompt and function result
WorkersAI-->>-Worker: Send response incorporating function output
ai-utils 包 也在 Github ↗ 开源。
按照 Workers AI 快速入门指南 的第 4 和第 5 步进行本地开发和部署。
更多详情,请参阅 API 参考。