Cohere
遗留
Cohere 已将他们的 LLM 的 generate
端点标记为已弃用。请遵循他们的 迁移指南,开始通过 ChatCohere
集成使用他们的聊天 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 @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...
});
调用
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