Cohere
已弃用
Cohere 已将其 LLM 的 generate
端点标记为已弃用。请按照他们的 迁移指南 开始通过 ChatCohere
集成使用他们的 Chat API。
这将帮助您开始使用 LangChain 的 Cohere 补全模型 (LLM)。有关 Cohere
功能和配置选项的详细文档,请参阅 API 参考。
概述
集成详情
类 | 包 | 本地 | 可序列化 | PY 支持 | 包下载 | 包最新 |
---|---|---|---|---|---|---|
Cohere | @langchain/cohere | ❌ | ✅ | ✅ | ![]() | ![]() |
设置
要访问 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
- yarn
- pnpm
npm i @langchain/cohere @langchain/core
yarn add @langchain/cohere @langchain/core
pnpm add @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.
链接
我们可以像这样使用提示模板链接我们的补全模型
import { PromptTemplate } from "@langchain/core/prompts";
const prompt = new PromptTemplate({
template: "How to say {input} in {output_language}:\n",
inputVariables: ["input", "output_language"],
});
const chain = prompt.pipe(llm);
await chain.invoke({
output_language: "German",
input: "I love programming.",
});
Ich liebe Programming.
But for day to day purposes Ich mag Programming. would be enough and perfectly understood.
I love programming is "Ich liebe Programming" and I like programming is "Ich mag Programming" respectively.
There are also other ways to express this feeling, such as "Ich habe Spaß mit Programming", which means "I enjoy programming". But "Ich mag" and "Ich liebe" are the most common expressions for this.
Let me know if I can be of further help with something else!
API 参考
有关所有 Cohere 功能和配置的详细文档,请访问 API 参考:https://api.js.langchain.com/classes/langchain_cohere.Cohere.html