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

# ChatCerebras 集成

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

[Cerebras](https://cerebras.ai/) 是一家模型提供商，专注于提供高速的开源模型。Cerebras CS-3 系统由晶圆级引擎-3（WSE-3）驱动，代表了一类新型 AI 超级计算机，以其无与伦比的性能和可扩展性，为生成式 AI 训练和推理设定了新标准。

选择 Cerebras 作为您的推理提供商，您可以：

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

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

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

## 概述

### 集成详情

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

## 设置

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

### 凭证

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

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export CEREBRAS_API_KEY="your-api-key"
```

如果您希望自动追踪模型调用，还可以通过取消注释以下行来设置您的 [LangSmith](/langsmith/home) API 密钥：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
```

### 安装

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

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

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

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

## 实例化

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

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatCerebras } from "@langchain/cerebras"

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

## 调用

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const aiMsg = await llm.invoke([
    {
      role: "system",
      content: "你是一个将英语翻译成法语的助手。请翻译用户的句子。",
    },
    { role: "user", content: "I love programming." },
])
aiMsg
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
console.log(aiMsg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
J'adore la programmation.
```

## JSON 调用

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const messages = [
  {
    role: "system",
    content: "你是一个数学导师，处理数学练习并以 JSON 格式输出，格式为 { result: number }。",
  },
  { role: "user",  content: "2 + 2" },
];

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

// 如果您不想在每次调用时都传递 response_format，可以将其绑定到实例
const llmWithResponseFormat = llm.bind({ response_format: { type: "json_object" } });
const aiBindMsg = await llmWithResponseFormat.invoke(messages);

// 它们是相同的
console.log({ aiInvokeMsgContent: aiInvokeMsg.content, aiBindMsg: aiBindMsg.content });
```

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{ aiInvokeMsgContent: '{"result":4}', aiBindMsg: '{"result":4}' }
```

***

## API 参考

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

***

<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\cerebras.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>
