跳转到内容
搜索文档

Workers 绑定(binding)

最后更新 查看 MarkdownAgent 设置

本指南将指导你设置并部署第一个 Workers AI 项目。你将使用 Workers、Workers AI 绑定(binding)和大型语言模型(LLM),在 Cloudflare 全球网络上部署你的第一个 AI 驱动应用。

  1. 注册 Cloudflare 账户
  2. 安装 Node.js

Node.js 版本管理器

使用 Voltanvm 等 Node 版本管理器,以避免权限问题并切换 Node.js 版本。本指南后续将介绍的 Wrangler 需要 Node 版本 16.17.0 或更高。

1. 创建 Worker 项目

你将使用 create-cloudflare CLI(C3)创建新的 Worker 项目。C3 是一个命令行工具,旨在帮助你设置新应用并部署到 Cloudflare。

运行以下命令创建名为 hello-ai 的新项目:

npm create cloudflare@latest -- hello-ai

运行 npm create cloudflare@latest 会提示你安装 create-cloudflare,并引导你完成设置。C3 还会安装 Wrangler,即 Cloudflare Developer Platform CLI。

进行设置时,请选择以下选项:

  • 对于 What would you like to start with?,选择 Hello World example
  • 对于 Which template would you like to use?,选择 Worker only
  • 对于 Which language do you want to use?,选择 TypeScript
  • 对于 Do you want to use git for version control?,选择 Yes
  • 对于 Do you want to deploy your application?,选择 No(部署前我们还会做一些修改)。

这将创建新的 hello-ai 目录。新的 hello-ai 目录将包含:

进入应用目录:

cd hello-ai

2. 将 Worker 连接到 Workers AI

Worker 必须创建 AI 绑定(binding)才能连接到 Workers AI。绑定(binding) 允许 Worker 与 Cloudflare Developer Platform 上的资源(如 Workers AI)交互。

要将 Workers AI 绑定到 Worker,在 Wrangler 文件末尾添加以下内容:

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

绑定在 Worker 代码中可用,通过 env.AI

你也可以将 Workers AI 绑定到 Pages Function。更多信息,请参阅 Functions 绑定

3. 在 Worker 中运行推理任务

你现在可以在 Worker 中运行推理任务。在此示例中,你将使用 LLM llama-3.1-8b-instruct 来回答问题。

hello-ai 应用目录中的 index.ts 文件更新为以下代码:

index.jsjs
export default {
	async fetch(request, env) {
		const response = await env.AI.run("@cf/meta/llama-3.1-8b-instruct", {
			prompt: "What is the origin of the phrase Hello, World",
		});

		return new Response(JSON.stringify(response));
	},
};
index.tsts
export interface Env {
	// If you set another name in the Wrangler config file as the value for 'binding',
	// replace "AI" with the variable name you defined.
	AI: Ai;
}

export default {
	async fetch(request, env): Promise<Response> {
		const response = await env.AI.run("@cf/meta/llama-3.1-8b-instruct", {
			prompt: "What is the origin of the phrase Hello, World",
		});

		return new Response(JSON.stringify(response));
	},
} satisfies ExportedHandler<Env>;

至此,你已为 Worker 创建了 AI 绑定,并配置 Worker 能够执行 Llama 3.1 模型。你现在可以在全球部署之前在本地测试项目。

4. 使用 Wrangler 本地开发

在项目目录中,通过运行 wrangler dev 在本地测试 Workers AI:

npx wrangler dev

运行 wrangler dev 后,系统会提示你登录。运行 npx wrangler dev 时,Wrangler 会提供一个 URL(很可能是 localhost:8787)供你查看 Worker。访问 Wrangler 提供的 URL 后,将显示类似以下示例的消息:

{
	"response": "Ah, a most excellent question, my dear human friend! *adjusts glasses*\n\nThe origin of the phrase \"Hello, World\" is a fascinating tale that spans several decades and multiple disciplines. It all began in the early days of computer programming, when a young man named Brian Kernighan was tasked with writing a simple program to demonstrate the basics of a new programming language called C.\nKernighan, a renowned computer scientist and author, was working at Bell Labs in the late 1970s when he created the program. He wanted to showcase the language's simplicity and versatility, so he wrote a basic \"Hello, World!\" program that printed the familiar greeting to the console.\nThe program was included in Kernighan and Ritchie's influential book \"The C Programming Language,\" published in 1978. The book became a standard reference for C programmers, and the \"Hello, World!\" program became a sort of \"Hello, World!\" for the programming community.\nOver time, the phrase \"Hello, World!\" became a shorthand for any simple program that demonstrated the basics"
}

5. 部署 AI Worker

在全球部署 AI Worker 之前,运行以下命令使用 Cloudflare 账户登录:

npx wrangler login

你将跳转到网页,要求登录 Cloudflare 仪表板。登录后,系统会询问 Wrangler 是否可以对 Cloudflare 账户进行更改。向下滚动并选择 Allow(允许) 继续。

最后,部署 Worker 以使项目可在 Internet 上访问。要部署 Worker,运行:

npx wrangler deploy
https://hello-ai.<YOUR_SUBDOMAIN>.workers.dev

Worker 将部署到你的自定义 workers.dev 子域名。你现在可以访问该 URL 来运行 AI Worker。

完成本教程后,你已创建 Worker,通过 AI 绑定将其连接到 Workers AI,并从 Llama 3 模型运行了推理任务。

相关资源

这篇文档对您有帮助吗?