跳转到内容
搜索文档

使用 AI 模型

最后更新 查看 MarkdownAgent 设置

Agent 可以调用任意提供商的 AI 模型。Workers AI 内置且无需 API 密钥。你也可以使用 OpenAI ↗、Anthropic ↗、Google Gemini ↗,或任何提供 OpenAI 兼容 API 的服务。

AI SDK ↗ 为这些提供商提供统一接口,AIChatAgent 和入门模板在底层使用它。你还可以使用 AI Gateway 中的模型路由功能跨提供商路由、评估响应并管理速率限制。

调用 AI 模型

你可以在 Agent 的任何方法中调用模型,包括使用 onRequest 处理程序处理 HTTP 请求时、调度任务运行时、在 onMessage 处理程序中处理 WebSocket 消息时,或在你自己的任何方法中。

Agent 可以自主调用 AI 模型,并能处理需要数分钟(或更久)才能完整响应的长时间运行响应。如果客户端在流式传输中途断开,Agent 会继续运行,并在客户端重新连接时为其补发内容。

通过 WebSocket 流式传输

现代推理模型生成响应和将响应流式传回客户端都需要一定时间。你可以通过 WebSocket 流式传回,而不是缓冲整个响应。

src/index.jsjs
import { Agent } from "agents";
import { streamText } from "ai";
import { createWorkersAI } from "workers-ai-provider";

export class MyAgent extends Agent {
	async onConnect(connection, ctx) {
		//
	}

	async onMessage(connection, message) {
		let msg = JSON.parse(message);
		await this.queryReasoningModel(connection, msg.prompt);
	}

	async queryReasoningModel(connection, userPrompt) {
		try {
			const workersai = createWorkersAI({ binding: this.env.AI });
			const result = streamText({
				model: workersai("@cf/zai-org/glm-4.7-flash"),
				prompt: userPrompt,
			});

			for await (const chunk of result.textStream) {
				if (chunk) {
					connection.send(JSON.stringify({ type: "chunk", content: chunk }));
				}
			}

			connection.send(JSON.stringify({ type: "done" }));
		} catch (error) {
			connection.send(JSON.stringify({ type: "error", error: error }));
		}
	}
}
src/index.tsts
import { Agent } from "agents";
import { streamText } from "ai";
import { createWorkersAI } from "workers-ai-provider";

interface Env {
	AI: Ai;
}

export class MyAgent extends Agent<Env> {
	async onConnect(connection: Connection, ctx: ConnectionContext) {
		//
	}

	async onMessage(connection: Connection, message: WSMessage) {
		let msg = JSON.parse(message);
		await this.queryReasoningModel(connection, msg.prompt);
	}

	async queryReasoningModel(connection: Connection, userPrompt: string) {
		try {
			const workersai = createWorkersAI({ binding: this.env.AI });
			const result = streamText({
				model: workersai("@cf/zai-org/glm-4.7-flash"),
				prompt: userPrompt,
			});

			for await (const chunk of result.textStream) {
				if (chunk) {
					connection.send(JSON.stringify({ type: "chunk", content: chunk }));
				}
			}

			connection.send(JSON.stringify({ type: "done" }));
		} catch (error) {
			connection.send(JSON.stringify({ type: "error", error: error }));
		}
	}
}

你还可以使用 this.setState 将 AI 模型响应持久化到 Agent 状态。如果用户断开连接,读取消息历史并在用户重新连接时发送给他们。

Workers AI

你可以通过在 Agent 中配置绑定使用 Workers AI 中可用的任意模型。无需 API 密钥。

Workers AI 通过设置 stream: true 支持流式响应。使用流式传输可避免缓冲和延迟响应,尤其适用于较大模型或推理模型。

src/index.jsjs
import { Agent } from "agents";

export class MyAgent extends Agent {
	async onRequest(request) {
		const stream = await this.env.AI.run(
			"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
			{
				prompt: "Build me a Cloudflare Worker that returns JSON.",
				stream: true,
			},
		);

		return new Response(stream, {
			headers: { "content-type": "text/event-stream" },
		});
	}
}
src/index.tsts
import { Agent } from "agents";

interface Env {
	AI: Ai;
}

export class MyAgent extends Agent<Env> {
	async onRequest(request: Request) {
		const stream = await this.env.AI.run(
			"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
			{
				prompt: "Build me a Cloudflare Worker that returns JSON.",
				stream: true,
			},
		);

		return new Response(stream, {
			headers: { "content-type": "text/event-stream" },
		});
	}
}

你的 Wrangler 配置需要 ai 绑定:

{
	"ai": {
		"binding": "AI",
	},
}
[ai]
binding = "AI"

模型路由

你可以通过在调用 AI 绑定时指定 gateway 配置,从 Agent 直接使用 AI Gateway。模型路由允许你根据可用性、速率限制或成本预算跨提供商路由请求。

src/index.jsjs
import { Agent } from "agents";

export class MyAgent extends Agent {
	async onRequest(request) {
		const response = await this.env.AI.run(
			"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
			{
				prompt: "Build me a Cloudflare Worker that returns JSON.",
			},
			{
				gateway: {
					id: "{gateway_id}",
					skipCache: false,
					cacheTtl: 3360,
				},
			},
		);

		return Response.json(response);
	}
}
src/index.tsts
import { Agent } from "agents";

interface Env {
	AI: Ai;
}

export class MyAgent extends Agent<Env> {
	async onRequest(request: Request) {
		const response = await this.env.AI.run(
			"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
			{
				prompt: "Build me a Cloudflare Worker that returns JSON.",
			},
			{
				gateway: {
					id: "{gateway_id}",
					skipCache: false,
					cacheTtl: 3360,
				},
			},
		);

		return Response.json(response);
	}
}

Wrangler 配置中的 ai 绑定在 Workers AI 和 AI Gateway 之间共享。

{
	"ai": {
		"binding": "AI",
	},
}
[ai]
binding = "AI"

访问 AI Gateway 文档 了解如何配置 gateway 并获取 gateway ID。

AI SDK

AI SDK ↗ 为文本生成、工具调用、结构化响应等提供统一 API。它适用于任何具有 AI SDK 适配器的提供商,包括通过 workers-ai-provider ↗ 使用 Workers AI。

npm i ai workers-ai-provider
src/index.jsjs
import { Agent } from "agents";
import { generateText } from "ai";
import { createWorkersAI } from "workers-ai-provider";

export class MyAgent extends Agent {
	async onRequest(request) {
		const workersai = createWorkersAI({ binding: this.env.AI });
		const { text } = await generateText({
			model: workersai("@cf/zai-org/glm-4.7-flash"),
			prompt: "Build me an AI agent on Cloudflare Workers",
		});

		return Response.json({ modelResponse: text });
	}
}
src/index.tsts
import { Agent } from "agents";
import { generateText } from "ai";
import { createWorkersAI } from "workers-ai-provider";

interface Env {
	AI: Ai;
}

export class MyAgent extends Agent<Env> {
	async onRequest(request: Request): Promise<Response> {
		const workersai = createWorkersAI({ binding: this.env.AI });
		const { text } = await generateText({
			model: workersai("@cf/zai-org/glm-4.7-flash"),
			prompt: "Build me an AI agent on Cloudflare Workers",
		});

		return Response.json({ modelResponse: text });
	}
}

你可以切换提供商以使用 OpenAI、Anthropic 或任何其他 AI SDK 兼容适配器:

npm i ai @ai-sdk/openai
src/index.jsjs
import { Agent } from "agents";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

export class MyAgent extends Agent {
	async onRequest(request) {
		const { text } = await generateText({
			model: openai("gpt-4o"),
			prompt: "Build me an AI agent on Cloudflare Workers",
		});

		return Response.json({ modelResponse: text });
	}
}
src/index.tsts
import { Agent } from "agents";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

export class MyAgent extends Agent {
	async onRequest(request: Request): Promise<Response> {
		const { text } = await generateText({
			model: openai("gpt-4o"),
			prompt: "Build me an AI agent on Cloudflare Workers",
		});

		return Response.json({ modelResponse: text });
	}
}

OpenAI 兼容端点

Agent 可以调用任何支持 OpenAI API 的服务上的模型。例如,你可以使用 OpenAI SDK 从 Agent 直接调用 Google 的 Gemini 模型之一 ↗。

Agent 可以在 onRequest 处理程序中通过 HTTP 使用 Server-Sent Events (SSE) 流式传回响应,或使用原生 WebSocket API 向客户端流式传输响应。

src/index.jsjs
import { Agent } from "agents";
import { OpenAI } from "openai";

export class MyAgent extends Agent {
	async onRequest(request) {
		const client = new OpenAI({
			apiKey: this.env.GEMINI_API_KEY,
			baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
		});

		let { readable, writable } = new TransformStream();
		let writer = writable.getWriter();
		const textEncoder = new TextEncoder();

		this.ctx.waitUntil(
			(async () => {
				const stream = await client.chat.completions.create({
					model: "gemini-2.0-flash",
					messages: [
						{ role: "user", content: "Write me a Cloudflare Worker." },
					],
					stream: true,
				});

				for await (const part of stream) {
					writer.write(
						textEncoder.encode(part.choices[0]?.delta?.content || ""),
					);
				}
				writer.close();
			})(),
		);

		return new Response(readable);
	}
}
src/index.tsts
import { Agent } from "agents";
import { OpenAI } from "openai";

export class MyAgent extends Agent {
	async onRequest(request: Request): Promise<Response> {
		const client = new OpenAI({
			apiKey: this.env.GEMINI_API_KEY,
			baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
		});

		let { readable, writable } = new TransformStream();
		let writer = writable.getWriter();
		const textEncoder = new TextEncoder();

		this.ctx.waitUntil(
			(async () => {
				const stream = await client.chat.completions.create({
					model: "gemini-2.0-flash",
					messages: [
						{ role: "user", content: "Write me a Cloudflare Worker." },
					],
					stream: true,
				});

				for await (const part of stream) {
					writer.write(
						textEncoder.encode(part.choices[0]?.delta?.content || ""),
					);
				}
				writer.close();
			})(),
		);

		return new Response(readable);
	}
}

这篇文档对您有帮助吗?