ChatCerebras
Cerebras 是一家模型提供商,提供开源模型,强调速度。Cerebras CS-3 系统由 Wafer-Scale Engine-3 (WSE-3) 提供支持,代表了一类新的 AI 超级计算机,以其无与伦比的性能和可扩展性,为生成式 AI 的训练和推理设立了标准。
通过 Cerebras 作为您的推理提供商,您可以:
- 实现 AI 推理工作负载前所未有的速度
- 以高吞吐量进行商业开发
- 通过我们无缝的集群技术轻松扩展 AI 工作负载
我们的 CS-3 系统可以快速轻松地集群,以创建世界上最大的 AI 超级计算机,从而轻松部署和运行最大的模型。领先的企业、研究机构和政府已经在使用 Cerebras 解决方案来开发专有模型并训练流行的开源模型。
这将帮助您开始使用 ChatCerebras 聊天模型。有关所有 ChatCerebras 功能和配置的详细文档,请前往 API 参考。
概览
集成详情
| 类 | 包 | 本地 | 可序列化 | PY 支持 | 包下载量 | 最新包 |
|---|---|---|---|---|---|---|
| ChatCerebras | @langchain/cerebras | ❌ | ❌ | ✅ | ![]() | ![]() |
模型特性
请参阅下表标题中的链接,了解如何使用特定功能的指南。
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | 逐令牌流 | 令牌使用情况 | 对数概率 |
|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ |
安装设置
要访问 ChatCerebras 模型,您需要创建一个 Cerebras 账户,获取 API
密钥,并安装 @langchain/cerebras 集成包。
凭证信息
从 cloud.cerebras.ai 获取 API 密钥,并将其添加到您的环境变量中:
export CEREBRAS_API_KEY="your-api-key"
如果您希望自动追踪模型调用,也可以通过取消下面注释设置 LangSmith API 密钥:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
安装
LangChain ChatCerebras 集成位于 @langchain/cerebras 包中:
:::提示 请参阅安装集成包的一般说明部分。 :::
- npm
- yarn
- pnpm
npm i @langchain/cerebras @langchain/core
yarn add @langchain/cerebras @langchain/core
pnpm add @langchain/cerebras @langchain/core
实例化
现在我们可以实例化我们的模型对象并生成聊天补全:
import { ChatCerebras } from "@langchain/cerebras";
const llm = new ChatCerebras({
model: "llama-3.3-70b",
temperature: 0,
maxTokens: undefined,
maxRetries: 2,
// other params...
});
调用
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;
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
}
}
console.log(aiMsg.content);
J'adore la programmation.
JSON 调用
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,
});
{ aiInvokeMsgContent: '{"result":4}', aiBindMsg: '{"result":4}' }
链式调用
我们可以像这样将模型与提示模板进行链式调用:
import { ChatPromptTemplate } from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
],
["human", "{input}"],
]);
const chain = prompt.pipe(llm);
await chain.invoke({
input_language: "English",
output_language: "German",
input: "I love programming.",
});
AIMessage {
"id": "run-5c8a9f25-0f57-499b-9c2b-87bd07135feb",
"content": "Ich liebe das Programmieren.",
"additional_kwargs": {},
"response_metadata": {
"id": "chatcmpl-abd1e9eb-b873-492e-9e30-0d13dfc3a145",
"created": 1735785607,
"model": "llama-3.3-70b",
"system_fingerprint": "fp_2e2a2a083c",
"object": "chat.completion",
"time_info": {
"queue_time": 0.00009499,
"prompt_time": 0.002095266,
"completion_time": 0.008807576,
"total_time": 0.012718439102172852,
"created": 1735785607
}
},
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 50,
"output_tokens": 7,
"total_tokens": 57
}
}
API 参考文档
如需了解 ChatCerebras 所有功能和配置的详细文档,请访问 API 参考页面: https://api.js.langchain.com/classes/\_langchain_cerebras.ChatCerebras.html
Related
- Chat model conceptual guide
- Chat model how-to guides

