> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# ChatCloudflareWorkersAI 集成

> 使用 LangChain JavaScript 集成 ChatCloudflareWorkersAI 聊天模型。

[Workers AI](https://developers.cloudflare.com/workers-ai/) 允许您在自己的代码中运行 Cloudflare 网络上的机器学习模型。

本文将帮助您开始使用 `ChatCloudflareWorkersAI` [聊天模型](/oss/javascript/langchain/models)。有关 `ChatCloudflareWorkersAI` 所有功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/javascript/langchain-cloudflare/ChatCloudflareWorkersAI)。

## 概述

### 集成详情

| 类                                                                                                                    | 包                                                                  | 可序列化 | Python 支持 |                                                  下载量                                                  |                                                 版本                                                 |
| :------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------- | :--: | :-------: | :---------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------: |
| [`ChatCloudflareWorkersAI`](https://reference.langchain.com/javascript/langchain-cloudflare/ChatCloudflareWorkersAI) | [`@langchain/cloudflare`](https://npmjs.com/@langchain/cloudflare) |   ✅  |     ❌     | ![NPM - Downloads](https://img.shields.io/npm/dm/@langchain/cloudflare?style=flat-square\&label=%20&) | ![NPM - Version](https://img.shields.io/npm/v/@langchain/cloudflare?style=flat-square\&label=%20&) |

### 模型特性

下表标题中的链接提供了如何使用特定功能的指南。

| [工具调用](/oss/javascript/langchain/tools) | [结构化输出](/oss/javascript/langchain/structured-output) | [图像输入](/oss/javascript/langchain/messages#multimodal) | 音频输入 | 视频输入 | [令牌级流式传输](/oss/javascript/langchain/streaming/) | [令牌使用量](/oss/javascript/langchain/models#token-usage) | [对数概率](/oss/javascript/langchain/models#log-probabilities) |
| :-------------------------------------: | :--------------------------------------------------: | :---------------------------------------------------: | :--: | :--: | :---------------------------------------------: | :---------------------------------------------------: | :--------------------------------------------------------: |
|                    ❌                    |                           ❌                          |                           ✅                           |   ❌  |   ❌  |                        ✅                        |                           ❌                           |                              ❌                             |

## 设置

要访问 Cloudflare Workers AI 模型，您需要创建一个 Cloudflare 账户、获取 API 密钥，并安装 `@langchain/cloudflare` 集成包。

### 凭证

请访问[此页面](https://developers.cloudflare.com/workers-ai/)注册 Cloudflare 并生成 API 密钥。完成后，请记下您的 `CLOUDFLARE_ACCOUNT_ID` 和 `CLOUDFLARE_API_TOKEN`。

目前尚不支持在 Cloudflare Worker 内部传递绑定。

### 安装

LangChain ChatCloudflareWorkersAI 集成位于 `@langchain/cloudflare` 包中：

<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/cloudflare @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/cloudflare @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/cloudflare @langchain/core
  ```
</CodeGroup>

## 实例化

现在我们可以实例化模型对象并生成聊天补全：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
// @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");
```

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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,
  // 传递自定义基础 URL 以使用 Cloudflare AI Gateway
  // baseUrl: `https://gateway.ai.cloudflare.com/v1/{YOUR_ACCOUNT_ID}/{GATEWAY_NAME}/workers-ai/`,
});
```

## 调用

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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: []
}
```

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
console.log(aiMsg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
I can help with that! The translation of "I love programming" in French is:

"J'adore le programmation."
```

***

## API 参考

有关 `ChatCloudflareWorkersAI` 所有功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/javascript/langchain-cloudflare/ChatCloudflareWorkersAI)。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\javascript\integrations\chat\cloudflare_workersai.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
