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 LANGCHAIN_TRACING_V2="true"
# export LANGCHAIN_API_KEY="your-api-key"
安装
LangChain Cohere 集成位于@langchain/cohere
包中
提示
有关安装集成包的一般说明,请参阅此部分。
- npm
- yarn
- pnpm
npm i @langchain/cohere
yarn add @langchain/cohere
pnpm add @langchain/cohere
实例化
现在我们可以实例化我们的模型对象并生成聊天补全
import { Cohere } from "@langchain/cohere";
const llm = new Cohere({
model: "command",
temperature: 0,
maxTokens: undefined,
maxRetries: 2,
// 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