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

# ChatCerebras 集成

> 集成 ChatCerebras 对话模型使用 LangChain JavaScript.

[Cerebras](https://cerebras.ai/) 是一个以速度为重点的模型提供者，提供开源模型。由 Wafer-Scale Engine-3 (WSE-3) 驱动的 Cerebras CS-3 系统，代表了一种新型 AI 超级计算机，以其无与伦比的性能和可扩展性，为生成式 AI 训练和推理设定了标准。

使用 Cerebras 作为您的推理提供者，您可以：

* 实现前所未有的 AI 推理工作负载速度
* 以高吞吐量进行商业构建
* 通过我们无缝的集群技术轻松扩展您的 AI 工作负载

我们的 CS-3 系统可以快速轻松地集群化，创建世界上最大的 AI 超级计算机，使部署和运行最大模型变得简单。领先的 corporations、研究机构以及政府已经开始使用 Cerebras 解决方案来开发专有模型和训练流行的开源模型。

这将帮助您开始使用 `ChatCerebras` [对话模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。有关所有 `ChatCerebras` 功能和配置的详细文档，请访问 [API reference](https://reference.langchain.com/javascript/langchain-cerebras/ChatCerebras)。

## 概述

### 集成细节

| 类别 | 包 | 可序列化 | [PY 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/chat/cerebras.md) | 下载 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`ChatCerebras`](https://reference.langchain.com/javascript/langchain-cerebras/ChatCerebras) | [`@langchain/cerebras`](https://www.npmjs.com/package/@langchain/cerebras) | ❌ | ✅ | ![NPM - 下载](https://img.shields.io/npm/dm/@langchain/cerebras?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/cerebras?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#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ |

## 设置

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

### 凭证

从 [cloud.cerebras.ai](https://cloud.cerebras.ai) 获取 API 密钥，并将其添加到您的环境变量中：

```bash theme={null}
export CEREBRAS_API_KEY="your-api-key"
```

如果您希望自动跟踪您的模型调用，您也可以通过取消注释以下内容来设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```bash theme={null}
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
```

### 安装

LangChain ChatCerebras 集成位于 `@langchain/cerebras` 包中：

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

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

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

## 实例化

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

```typescript theme={null}
import { ChatCerebras } from "@langchain/cerebras"

const llm = new ChatCerebras({
    model: "llama-3.3-70b",
    temperature: 0,
    maxTokens: undefined,
    maxRetries: 2,
    // other params...
})
```

## 调用

```typescript theme={null}
const aiMsg = await llm.invoke([
    {
      role: "system",
      content: "You are a helpful assistant that translates English to French. Translate the user sentence.",
    },
    { role: "user", content: "I love programming." },
])
aiMsg
```

```text theme={null}
AIMessage {
  "id": "run-17c7d62d-67ac-4677-b33a-18298fc85e35",
  "content": "J'adore la programmation.",
  "additional_kwargs": {},
  "response_metadata": {
    "id": "chatcmpl-2d1e2de5-4239-46fb-af2a-6200d89d7dde",
    "created": 1735785598,
    "model": "llama-3.3-70b",
    "system_fingerprint": "fp_2e2a2a083c",
    "object": "chat.completion",
    "time_info": {
      "queue_time": 0.00009063,
      "prompt_time": 0.002163031,
      "completion_time": 0.012339628,
      "total_time": 0.01640915870666504,
      "created": 1735785598
    }
  },
  "tool_calls": [],
  "invalid_tool_calls": [],
  "usage_metadata": {
    "input_tokens": 55,
    "output_tokens": 9,
    "total_tokens": 64
  }
}
```

```typescript theme={null}
console.log(aiMsg.content)
```

```text theme={null}
J'adore la programmation.
```

## JSON 调用

```typescript theme={null}
const messages = [
  {
    role: "system",
    content: "You are a math tutor that handles math exercises and makes output in json in format { result: number }.",
  },
  { role: "user",  content: "2 + 2" },
];

const aiInvokeMsg = await llm.invoke(messages, { response_format: { type: "json_object" } });

// if you want not to pass response_format in every invoke, you can bind it to the instance
const llmWithResponseFormat = llm.bind({ response_format: { type: "json_object" } });
const aiBindMsg = await llmWithResponseFormat.invoke(messages);

// they are the same
console.log({ aiInvokeMsgContent: aiInvokeMsg.content, aiBindMsg: aiBindMsg.content });
```

```json theme={null}
{ aiInvokeMsgContent: '{"result":4}', aiBindMsg: '{"result":4}' }
```

***

## API 参考

有关所有 `ChatCerebras` 功能和配置的详细文档，请访问 [API reference](https://reference.langchain.com/javascript/langchain-cerebras/ChatCerebras)。

***

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


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