以编程方式使用文档

Cerebras 是一个模型提供商,提供以速度为重点的开源模型。Cerebras CS-3 系统由晶圆级引擎-3 (WSE-3) 驱动,代表了一类新型 AI 超级计算机,以无与伦比的性能和可扩展性为生成式 AI 训练和推理树立了新标准。

使用 Cerebras 作为您的推理提供商,您可以:

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

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

这将帮助您开始使用 ChatCerebras 聊天模型。有关所有 ChatCerebras 功能和配置的详细文档,请前往 API 参考.

概述

集成详情

可序列化PY 支持下载量版本
ChatCerebras@langchain/cerebras!NPM - 下载量!NPM - 版本

模型功能

请参阅下方表格标题中的链接以获取有关如何使用特定功能的指南。

工具调用结构化输出图像输入音频输入视频输入令牌级流式处理令牌使用量对数概率

设置

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

凭证

cloud.cerebras.ai 获取 API 密钥并将其添加到您的环境变量中:

如果您想获取模型调用的自动追踪,您还可以通过取消注释下方内容来设置您的 LangSmith API 密钥:

# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain ChatCerebras 集成位于 @langchain/cerebras package:

npm install @langchain/cerebras @langchain/core
yarn add @langchain/cerebras @langchain/core
pnpm add @langchain/cerebras @langchain/core

实例化

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

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}' }

API 参考

有关所有 ChatCerebras 功能和配置的详细文档,请前往 API 参考.