> ## 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.

# CloudflareWorkersAI 集成

> 使用 LangChain JavaScript 与 CloudflareWorkersAI LLM 集成。

这将帮助您使用 LangChain 开始使用 Cloudflare Workers AI 文本完成模型（LLMs）。有关 `CloudflareWorkersAI` 功能和配置选项的详细文档，请参阅 [API 参考](https://reference.langchain.com/javascript/langchain-cloudflare/CloudflareWorkersAI)。

## 概述

### 集成详情

| 类 | 包 | 本地 | 可序列化 | Python 支持 | 下载 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: | :-: |
| [`CloudflareWorkersAI`](https://reference.langchain.com/javascript/langchain-cloudflare/CloudflareWorkersAI) | [`@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&) |

## 设置

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

### 凭证

前往 [此页面](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"
```

### 安装

LangChain Cloudflare 集成位于 `@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 { CloudflareWorkersAI } from "@langchain/cloudflare";

const llm = new CloudflareWorkersAI({
  model: "@cf/meta/llama-3.1-8b-instruct", // 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 inputText = "Cloudflare is an AI company that "

const completion = await llm.invoke(inputText);
completion
```

```text theme={null}
"Cloudflare is not an AI company, but rather a content delivery network (CDN) and security company. T"... 876 more characters
```

***

## API 参考

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

***

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


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