Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Workers AI allows you to run machine learning models, on the Cloudflare network, from your own code. This will help you getting started with ChatCloudflareWorkersAI chat models. For detailed documentation of all ChatCloudflareWorkersAI features and configurations head to the API reference.

概述

集成详情

ClassPackageSerializablePY supportDownloadsVersion
ChatCloudflareWorkersAI@langchain/cloudflareNPM - DownloadsNPM - Version

模型功能

请参阅下表标题中的链接,了解如何使用特定功能。

设置

要访问 Cloudflare Workers AI models,你需要create a Cloudflare account, get an API key, and install the @langchain/cloudflare integration package.

凭证

Head to this page to sign up to Cloudflare and generate an API key. Once you’ve done this, note your CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN. Passing a binding within a Cloudflare Worker is not yet supported.

安装

LangChain 的 ChatCloudflareWorkersAI 集成位于 @langchain/cloudflare 包中:
npm install @langchain/cloudflare @langchain/core

实例化

现在我们可以实例化模型对象并生成聊天补全:
// @lc-docs-hide-cell

// @ts-expect-error Deno is not recognized
const CLOUDFLARE_ACCOUNT_ID = Deno.env.get("CLOUDFLARE_ACCOUNT_ID");
// @ts-expect-error Deno is not recognized
const CLOUDFLARE_API_TOKEN = Deno.env.get("CLOUDFLARE_API_TOKEN");
import { ChatCloudflareWorkersAI } from "@langchain/cloudflare";

const llm = new ChatCloudflareWorkersAI({
  model: "@cf/meta/llama-2-7b-chat-int8", // 默认值
  cloudflareAccountId: CLOUDFLARE_ACCOUNT_ID,
  cloudflareApiToken: CLOUDFLARE_API_TOKEN,
  // Pass a custom base URL to use Cloudflare AI Gateway
  // baseUrl: `https://gateway.ai.cloudflare.com/v1/{YOUR_ACCOUNT_ID}/{GATEWAY_NAME}/workers-ai/`,
});

调用

const aiMsg = await llm.invoke([
  [
    "system",
    "You are a helpful assistant that translates English to French. Translate the user sentence.",
  ],
  ["human", "I love programming."],
])
aiMsg
AIMessage {
  lc_serializable: true,
  lc_kwargs: {
    content: 'I can help with that! The translation of "I love programming" in French is:\n' +
      "\n" +
      `"J'adore le programmati`... 4 more characters,
    tool_calls: [],
    invalid_tool_calls: [],
    additional_kwargs: {},
    response_metadata: {}
  },
  lc_namespace: [ "langchain_core", "messages" ],
  content: 'I can help with that! The translation of "I love programming" in French is:\n' +
    "\n" +
    `"J'adore le programmati`... 4 more characters,
  name: undefined,
  additional_kwargs: {},
  response_metadata: {},
  tool_calls: [],
  invalid_tool_calls: []
}
console.log(aiMsg.content)
I can help with that! The translation of "I love programming" in French is:

"J'adore le programmation."

API 参考

有关所有 ChatCloudflareWorkersAI 功能和配置的详细文档,请前往 API 参考