以编程方式使用文档

这将帮助您开始使用 LangChain 的 Cohere 补全模型(LLMs)。有关详细文档,请参阅 Cohere 功能和配置选项,请参阅 API 参考文档.

概述

集成详情

本地可序列化PY 支持下载量版本
Cohere@langchain/cohere!NPM - 下载量!NPM - 版本

设置

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

凭证

前往 cohere.com 注册 Cohere 并生成 API 密钥。完成此操作后,请设置 COHERE_API_KEY 环境变量:

如果您希望获取模型调用的自动跟踪,还可以设置您的 LangSmith API 密钥(取消下方注释):

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

安装

LangChain Cohere 集成位于 @langchain/cohere package:

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

实例化

现在我们可以实例化模型对象并生成文本补全:

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

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

我们可以实例化一个自定义 CohereClient 并将其传递给 Cohere constructor.

Note: 如果提供了自定义客户端,则 COHERE_API_KEY 环境变量和 apiKey 构造函数中的参数将被忽略。

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 参考文档.