このガイドでは、Workers AI プロジェクトのセットアップとデプロイを説明します。Workers、AI Gateway バインディング、大規模言語モデル(LLM)を使い、Cloudflare のグローバルネットワーク上に最初の AI アプリケーションをデプロイします。
- Cloudflare アカウント ↗ に登録します。
Node.js↗ をインストールします。
Node.js のバージョンマネージャー
権限の問題を避け、Node.js のバージョンを切り替えられるよう、Volta ↗ や nvm ↗ などの Node バージョンマネージャーを使います。このガイドの後半で説明する Wrangler には、Node バージョン 16.17.0 以降が必要です。
create-Cloudflare CLI(C3)で新しい Worker プロジェクトを作成します。C3 は、Cloudflare への新規アプリケーションのセットアップとデプロイを支援するコマンドラインツールです。
次を実行し、hello-ai という名前の新規プロジェクトを作成します。
npm create cloudflare@latest -- hello-aiyarn create cloudflare hello-aipnpm create cloudflare@latest hello-ainpm create cloudflare@latest を実行すると、create-cloudflare パッケージのインストールを求められ、セットアップが進みます。C3 は Cloudflare Developer Platform の CLI である Wrangler もインストールします。
セットアップでは、次のオプションを選びます。
- 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 ディレクトリには次が含まれます。
src/index.tsにある "Hello World" Worker- Wrangler 設定ファイル
アプリケーションディレクトリへ移動します。
cd hello-aiWorker を Workers AI に接続するには、AI バインディングを作成します。バインディングにより、Worker は Cloudflare Developer Platform 上の Workers AI などのリソースとやり取りできます。
Worker に Workers AI をバインドするには、Wrangler 設定ファイル の末尾に次を追加します。
{
"ai": {
"binding": "AI",
},
}[ai]
binding = "AI"バインディングは Worker コード内の env.AI で 利用できます。
次の手順では、ゲートウェイ ID に "default" を使えます。AI Gateway は、最初の認証済みリクエストでデフォルトゲートウェイを自動作成します。または、手動でゲートウェイを作成 し、その ID を使います。
Worker で推論タスクを実行する準備ができました。ここでは LLM の llama-3.1-8b-instruct-fast を使い、質問に答えます。
hello-ai アプリケーションディレクトリの index.ts を、次のコードに更新します。
export interface Env {
// If you set another name in the [Wrangler configuration file](/workers/wrangler/configuration/) as the value for 'binding',
// replace "AI" with the variable name you defined.
AI: Ai;
}
export default {
async fetch(request, env): Promise<Response> {
// Specify the gateway label and other options here
const response = await env.AI.run(
"@cf/meta/llama-3.1-8b-instruct-fast",
{
prompt: "What is the origin of the phrase Hello, World",
},
{
gateway: {
id: "default", // Uses the default gateway, or replace with your gateway ID
skipCache: true, // Optional: Skip cache if needed
},
},
);
// Return the AI response as a JSON object
return new Response(JSON.stringify(response), {
headers: { "Content-Type": "application/json" },
});
},
} satisfies ExportedHandler<Env>;ここまでで、Worker に AI バインディングを作成し、Llama 3.1 モデルを実行できるよう Worker を設定しました。グローバルにデプロイする前に、ローカルでプロジェクトをテストできます。
プロジェクトディレクトリで wrangler dev を実行し、Workers AI をローカルでテストします。
npx wrangler devwrangler dev の実行後、ログインを求められます。npx wrangler dev を実行すると、Wrangler は Worker を確認する URL(多くの場合 localhost:8787)を表示します。Wrangler が示す URL を開くと、次のようなメッセージが表示されます。
{
"response": "A fascinating question!\n\nThe phrase \"Hello, World!\" originates from a simple computer program written in the early days of programming. It is often attributed to Brian Kernighan, a Canadian computer scientist and a pioneer in the field of computer programming.\n\nIn the early 1970s, Kernighan, along with his colleague Dennis Ritchie, were working on the C programming language. They wanted to create a simple program that would output a message to the screen to demonstrate the basic structure of a program. They chose the phrase \"Hello, World!\" because it was a simple and recognizable message that would illustrate how a program could print text to the screen.\n\nThe exact code was written in the 5th edition of Kernighan and Ritchie's book \"The C Programming Language,\" published in 1988. The code, literally known as \"Hello, World!\" is as follows:\n\n main()\n {\n printf(\"Hello, World!\");\n }\n\nThis code is still often used as a starting point for learning programming languages, as it demonstrates how to output a simple message to the console.\n\nThe phrase \"Hello, World!\" has since become a catch-all phrase to indicate the start of a new program or a small test program, and is widely used in computer science and programming education.\n\nSincerely, I'm glad I could help clarify the origin of this iconic phrase for you!"
}AI Worker をグローバルにデプロイする前に、次を実行して Cloudflare アカウントでログインします。
npx wrangler loginCloudflare ダッシュボードへのログインを求める Web ページに移動します。ログイン後、Wrangler が Cloudflare アカウントを変更してよいか確認されます。下へスクロールし、Allow を選んで続行します。
最後に Worker をデプロイし、プロジェクトをインターネットから利用できるようにします。デプロイするには次を実行します。
npx wrangler deployデプロイ後、Worker は次のような URL で利用できます。
https://hello-ai.<YOUR_SUBDOMAIN>.workers.devWorker は、カスタムの workers.dev サブドメインにデプロイされます。URL を開いて AI Worker を実行できます。
このチュートリアルを完了すると、Worker を作成し、AI Gateway バインディング経由で Workers AI に接続し、Llama 3.1 モデルで推論タスクを実行できます。
- Workers バインディング — サードパーティモデルの呼び出し、ゲートウェイメソッドへのアクセス、AI SDK との連携。