体验Cloudflare Workers AI

介绍

Workers AI 允许您在 Cloudflare 网络上运行机器学习模型,无论是来自 WorkersPages 还是通过 Cloudflare API 的任何地方编写的代码。

::: warning 注意 Workers AI 将于 2024 年 4 月 1 日后对以下型号的所有使用超过每天 10,000 个神经元的部分收取每 1,000 个神经元 0.011 美元的费用

  • bge-small-en-v1.5

  • bge-base-en-v1.5

  • bge-large-en-v1.5

  • distilbert-sst-2-int8

  • llama-2-7b-chat-int8

  • llama-2-7b-chat-fp16

  • mistral-7b-instruct-v0.1

  • m2m100-1.2b

  • resnet-50

  • whisper

其他标记为 beta 版本的模型目前不收费。

有关更多详情,请参考 价格页面 :::

在线体验

playground

CLI 运行

准备条件

  1. 注册 cloudflare 账号

  2. 安装 nodejsnpm

创建 worker 项目

::: code-group

npm create cloudflare@latest
yarn create cloudflare

:::

配置环境变量

wrangler.toml 文件末尾添加配置, 将 Workers AI 绑定到您的 Worker

[ai]
binding = "AI"

运行一个推理任务

这里我们使用 @cf/qwen/qwen1.5-14b-chat-awq 尝试问答

调整 src/index.ts

export interface Env {
  AI: Ai;
}

export default {
  async fetch(request, env): Promise<Response> {

    const messages = [
      { role: "system", content: "You are a friendly assistant" },
      {
        role: "user",
        content: "对比Vue和React,并给出未来前端的趋势",
      },
    ];
    const response = await env.AI.run("@cf/qwen/qwen1.5-14b-chat-awq", { messages });

    return Response.json(response);
  },
} satisfies ExportedHandler<Env>;

使用 npm run dev 运行推理,再打开的网页使用 cloudflare 授权后,即可看到返回结果。

CLI

curl 运行

# account_id 在 左侧 workers ai => 使用 REST API => 获取帐户 ID

# token 在 左侧 workers ai => 使用 REST API => 创建 Workers AI API 令牌

curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/@cf/qwen/qwen1.5-14b-chat-awq \
  -X POST \
  -H "Authorization: Bearer $CLOUDFLARE_AUTH_TOKEN" \
  -d '{ "messages": [{ "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "对比Vue和React,并给出未来前端的趋势" }]}'
curl

REST API

const axios = require('axios');

const account_id = 'xx'
const token = 'xx'
const model_name = '@cf/qwen/qwen1.5-14b-chat-awq'
const options = {
	method: 'POST',
	url: `https://api.cloudflare.com/client/v4/accounts/${account_id}/ai/run/${model_name}`,
	headers: {
		'Content-Type': 'application/json',
		Authorization: `Bearer ${token}`
	},
	data: {
		text: '比较一下Vue和React,以及未来web前端趋势'
	}
};

axios.request(options).then(function(response) {
	console.log(response.data);
}).catch(function(error) {
	console.error(error);
});

图片生成

  1. 使用 node 实现

    export interface Env {
      AI: Ai;
    }
    
    export default {
      async fetch(request, env): Promise<Response> {
    
        const inputs = {
          prompt: "a lovely korea girl, with curly hair",
        };
    
        const response = await env.AI.run(
          "@cf/stabilityai/stable-diffusion-xl-base-1.0",
          inputs
        );
    
        return new Response(response, {
          headers: {
            "content-type": "image/png",
          },
        });
      },
    } satisfies ExportedHandler<Env>;
    img1
  2. 使用 culr 实现

    # -o 将二进制图片保存到文件
    curl https://api.cloudflare.com/client/v4/accounts/${account_id}/ai/run/@cf/stabilityai/stable-diffusion-xl-base-1.0 \
      -X POST \
      -H "Authorization: Bearer ${token}" \
      -d '{ "prompt": "a lovely korea girl, with curly hair" }' \
      -o test2.jpg
    img2

AI GATEWAY

  1. AI => AI Gateway , 创建一个网关

  2. 调整 curl 命令

curl https://gateway.ai.cloudflare.com/v1/${account_id}/ai-test/workers-ai/@cf/qwen/qwen1.5-14b-chat-awq \
-X POST \
-H "Authorization: Bearer ${token}" \
-d '{ "messages": [{ "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "分析在编程领域为什么偏向使用英语而不是其他语言" }]}'

然后可以在面板中查看分析和实时日志

gateway

参考

Last updated