> ## 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 集成

> 使用 LangChain Python 集成 ChatCerebras 对话模型。

本指南为您提供快速入门Cerebras [对话模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)的概述。如需了解所有 `ChatCerebras` 功能和配置的详细文档，请访问 [API参考](https://reference.langchain.com/python/langchain-cerebras/chat_models/ChatCerebras)。

在Cerebras，我们开发了全球最大且速度最快的AI处理器——Wafer-Scale Engine-3（WSE-3）。搭载WSE-3的Cerebras CS-3系统，代表了新一代AI超级计算机，以无与伦比的性能和可扩展性树立了生成式AI训练与推理的新标准。

使用Cerebras作为推理服务提供商，您可：

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

Cerebras CS-3系统可通过快速简易的集群配置，构建全球最大的AI超级计算机，轻松部署和运行世界级大模型。领先企业、研究机构和政府机构正使用Cerebras解决方案开发专有模型并训练流行开源模型。

想体验Cerebras的强大性能？访问我们的 [官网](https://cerebras.ai) 获取更多资源，通过Cerebras云服务或本地部署探索技术接入选项！

如需了解Cerebras云服务详情，请访问 [cloud.cerebras.ai](https://cloud.cerebras.ai/)。API参考文档位于 [inference-docs.cerebras.ai](https://inference-docs.cerebras.ai/)。

## 概览

### 集成细节

| 类别 | 包名 | 可序列化 | [JS支持](https://langchain-docs-zh.mintlify.site/langchain/oss/javascript/integrations/chat/cerebras.md) | 下载量 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`ChatCerebras`](https://reference.langchain.com/python/langchain-cerebras/chat_models/ChatCerebras) | [`langchain-cerebras`](https://reference.langchain.com/python/langchain-cerebras) | beta | ❌ | ![PyPI - 下载量](https://img.shields.io/pypi/dm/langchain-cerebras?style=flat-square\&label=%20) | ![PyPI - 版本](https://img.shields.io/pypi/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-使用) | [对数概率](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |

## 配置

```bash theme={null}
pip install langchain-cerebras
```

### 凭据

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

```
export CEREBRAS_API_KEY="your-api-key-here"
```

```python theme={null}
import getpass
import os

if "CEREBRAS_API_KEY" not in os.environ:
    os.environ["CEREBRAS_API_KEY"] = getpass.getpass("Enter your Cerebras API key: ")
```

```text theme={null}
Enter your Cerebras API key:  ········
```

为您的模型调用启用自动化追踪，设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API密钥：

```python theme={null}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

LangChain Cerebras集成位于 `langchain-cerebras` 包中：

```python theme={null}
pip install -qU langchain-cerebras
```

## 实例化

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

```python theme={null}
from langchain_cerebras import ChatCerebras

llm = ChatCerebras(
    model="llama-3.3-70b",
    # other params...
)
```

## 调用

```python theme={null}
messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={null}
AIMessage(content='Je adore le programmation.', response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 35, 'total_tokens': 42}, 'model_name': 'llama3-8b-8192', 'system_fingerprint': 'fp_be27ec77ff', 'finish_reason': 'stop'}, id='run-e5d66faf-019c-4ac6-9265-71093b13202d-0', usage_metadata={'input_tokens': 35, 'output_tokens': 7, 'total_tokens': 42})
```

## 流式输出

```python theme={null}
from langchain_cerebras import ChatCerebras
from langchain_core.prompts import ChatPromptTemplate

llm = ChatCerebras(
    model="llama-3.3-70b",
    # other params...
)

system = "You are an expert on animals who must answer questions in a manner that a 5 year old can understand."
human = "I want to learn more about this animal: {animal}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])

chain = prompt | llm

for chunk in chain.stream({"animal": "Lion"}):
    print(chunk.content, end="", flush=True)
```

```text theme={null}
OH BOY! Let me tell you all about LIONS!

Lions are the kings of the jungle! They're really big and have beautiful, fluffy manes around their necks. The mane is like a big, golden crown!

Lions live in groups called prides. A pride is like a big family, and the lionesses (that's what we call the female lions) take care of the babies. The lionesses are like the mommies, and they teach the babies how to hunt and play.

Lions are very good at hunting. They work together to catch their food, like zebras and antelopes. They're super fast and can run really, really fast!

But lions are also very sleepy. They like to take long naps in the sun, and they can sleep for up to 20 hours a day! Can you imagine sleeping that much?

Lions are also very loud. They roar really loudly to talk to each other. It's like they're saying, "ROAR! I'm the king of the jungle!"

And guess what? Lions are very social. They like to play and cuddle with each other. They're like big, furry teddy bears!

So, that's lions! Aren't they just the coolest?
```

## 异步

```python theme={null}
from langchain_cerebras import ChatCerebras
from langchain_core.prompts import ChatPromptTemplate

llm = ChatCerebras(
    model="llama-3.3-70b",
    # other params...
)

prompt = ChatPromptTemplate.from_messages(
    [
        (
            "human",
            "Let's play a game of opposites. What's the opposite of {topic}? Just give me the answer with no extra input.",
        )
    ]
)
chain = prompt | llm
await chain.ainvoke({"topic": "fire"})
```

```text theme={null}
AIMessage(content='Ice', response_metadata={'token_usage': {'completion_tokens': 2, 'prompt_tokens': 36, 'total_tokens': 38}, 'model_name': 'llama3-8b-8192', 'system_fingerprint': 'fp_be27ec77ff', 'finish_reason': 'stop'}, id='run-7434bdde-1bec-44cf-827b-8d978071dfe8-0', usage_metadata={'input_tokens': 36, 'output_tokens': 2, 'total_tokens': 38})
```

## 异步流式

```python theme={null}
from langchain_cerebras import ChatCerebras
from langchain_core.prompts import ChatPromptTemplate

llm = ChatCerebras(
    model="llama-3.3-70b",
    # other params...
)

prompt = ChatPromptTemplate.from_messages(
    [
        (
            "human",
            "Write a long convoluted story about {subject}. I want {num_paragraphs} paragraphs.",
        )
    ]
)
chain = prompt | llm

async for chunk in chain.astream({"num_paragraphs": 3, "subject": "blackholes"}):
    print(chunk.content, end="", flush=True)
```

```text theme={null}
In the distant reaches of the cosmos, there existed a peculiar phenomenon known as the "Eclipse of Eternity," a swirling vortex of darkness that had been shrouded in mystery for eons. It was said that this blackhole, born from the cataclysmic collision of two ancient stars, had been slowly devouring the fabric of space-time itself, warping the very essence of reality. As the celestial bodies of the galaxy danced around it, they began to notice a strange, almost imperceptible distortion in the fabric of space, as if the blackhole's gravitational pull was exerting an influence on the very course of events itself.

As the centuries passed, astronomers from across the galaxy became increasingly fascinated by the Eclipse of Eternity, pouring over ancient texts and scouring the cosmos for any hint of its secrets. One such scholar, a brilliant and reclusive astrophysicist named Dr. Elara Vex, became obsessed with unraveling the mysteries of the blackhole. She spent years pouring over ancient texts, deciphering cryptic messages and hidden codes that hinted at the existence of a long-lost civilization that had once thrived in the heart of the blackhole itself. According to legend, this ancient civilization had possessed knowledge of the cosmos that was beyond human comprehension, and had used their mastery of the universe to create the Eclipse of Eternity as a gateway to other dimensions.

As Dr. Vex delved deeper into her research, she began to experience strange and vivid dreams, visions that seemed to transport her to the very heart of the blackhole itself. In these dreams, she saw ancient beings, their faces twisted in agony as they were consumed by the void. She saw stars and galaxies, their light warped and distorted by the blackhole's gravitational pull. And she saw the Eclipse of Eternity itself, its swirling vortex of darkness pulsing with an otherworldly energy that seemed to be calling to her. As the dreams grew more vivid and more frequent, Dr. Vex became convinced that she was being drawn into the heart of the blackhole, and that the secrets of the universe lay waiting for her on the other side.
```

***

## API参考

如需了解所有 `ChatCerebras` 功能和配置的详细文档，请访问 [API参考](https://reference.langchain.com/python/langchain-cerebras/chat_models/ChatCerebras)

***

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


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