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

# ChatCloudflareWorkersAI 集成

> 集成 ChatCloudflareWorkersAI 对话模型，使用 LangChain JavaScript。

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

这将帮助您开始使用 `ChatCloudflareWorkersAI` [对话模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。有关所有 `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 - 下载](https://img.shields.io/npm/dm/@langchain/cloudflare?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/cloudflare?style=flat-square\&label=%20&) |

### 模型功能

请查看下表标题中的链接，了解如何使用特定功能。

| [工具调用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md) | [结构化输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/structured-output.md) | [图像输入](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/messages.md#多模态) | 音频输入 | 视频输入 | [Token 级流式输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/streaming.md) | [Token 使用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#token-使用) | [Logprobs](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ |

## 设置

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

### 凭证

前往 [Cloudflare Workers AI](https://developers.cloudflare.com/workers-ai/) 注册 Cloudflare 并生成 API 密钥。完成后，请在环境中设置您的凭证：

```bash theme={null}
export CLOUDFLARE_ACCOUNT_ID="your-account-id"
export CLOUDFLARE_API_TOKEN="your-api-token"
```

在 Cloudflare Worker 中传递绑定尚不支持。

### 安装

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

```bash npm theme={null}
npm install @langchain/cloudflare @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/cloudflare @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/cloudflare @langchain/core
```

## 实例化

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

```typescript theme={null}
// @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={null}
import { ChatCloudflareWorkersAI } from "@langchain/cloudflare";

const llm = new ChatCloudflareWorkersAI({
  model: "@cf/meta/llama-2-7b-chat-int8", // Default value
  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/`,
});
```

## 调用

```typescript theme={null}
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={null}
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={null}
console.log(aiMsg.content)
```

```text theme={null}
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)。

***

> 官方原文：[src/oss/javascript/integrations/chat/cloudflare\_workersai.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/javascript/integrations/chat/cloudflare_workersai.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.