跳到主要内容
旧版Cohere 已将他们的 LLM `generate` 终结点标记为已弃用。请遵循他们的迁移指南,通过`ChatCohere`集成开始使用他们的聊天 API。
您当前所在的页面记录了将 Cohere 模型用作文本补全模型。Cohere 上提供的许多流行模型都是聊天补全模型您可能正在寻找此页面
这将帮助您开始使用 LangChain 的 Cohere 补全模型(LLM)。有关 `Cohere` 功能和配置选项的详细文档,请参阅API 参考

概览

集成详情

类别本地可序列化PY 支持下载量版本
Cohere@langchain/cohereNPM - DownloadsNPM - Version

设置

要访问 Cohere 模型,您需要创建一个 Cohere 帐户,获取 API 密钥,并安装 `@langchain/cohere` 集成包。

凭据

访问cohere.com注册 Cohere 并生成 API 密钥。完成后,设置 `COHERE_API_KEY` 环境变量。
export COHERE_API_KEY="your-api-key"
如果您想获取模型调用的自动化跟踪,您还可以通过取消注释下方来设置您的 LangSmith API 密钥
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain Cohere 集成位于 `@langchain/cohere` 包中。
npm install @langchain/cohere @langchain/core

实例化

现在我们可以实例化我们的模型对象并生成聊天完成
import { Cohere } from "@langchain/cohere"

const llm = new Cohere({
  model: "command",
  temperature: 0,
  maxTokens: undefined,
  maxRetries: 2,
  // other params...
})

Azure 上的 Cohere、AWS Bedrock 上的 Cohere 和独立的 Cohere 实例的自定义客户端

我们可以实例化一个自定义的 `CohereClient` 并将其传递给 ChatCohere 构造函数。 注意:如果提供了自定义客户端,构造函数中的 `COHERE_API_KEY` 环境变量和 `apiKey` 参数都将被忽略。
import { Cohere } from "@langchain/cohere";
import { CohereClient } from "cohere-ai";

const client = new CohereClient({
  token: "<your-api-key>",
  environment: "<your-cohere-deployment-url>", //optional
  // other params
});

const llmWithCustomClient = new Cohere({
  client,
  // other params...
});

调用

const inputText = "Cohere is an AI company that "

const completion = await llm.invoke(inputText)
completion
Cohere is a company that provides natural language processing models that help companies improve human-machine interactions. Cohere was founded in 2019 by Aidan Gomez, Ivan Zhang, and Nick Frosst.

API 参考

有关所有 Cohere 功能和配置的详细文档,请访问API 参考
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.