Skip to main content
您目前所在的页面是关于将 Cohere 模型用作文本补全模型的文档。许多流行的 Cohere 模型是 聊天完成模型您可能正在寻找 此页面
Cohere 是一家加拿大初创公司,提供自然语言处理模型,帮助企业改善人机交互。
有关所有属性和方法的详细文档,请前往 API 参考

概述

集成详情

本地可序列化JS 支持下载量版本
Coherelangchain-communitybetaPyPI - 下载量PyPI - 版本

设置

该集成位于 langchain-community 包中。我们还需要安装 cohere 包本身。我们可以使用以下命令安装:

凭据

我们需要获取一个 Cohere API 密钥 并设置 COHERE_API_KEY 环境变量:
import getpass
import os

if "COHERE_API_KEY" not in os.environ:
    os.environ["COHERE_API_KEY"] = getpass.getpass()

安装

pip install -U langchain-community langchain-cohere
设置 LangSmith 以获得一流的观测能力也很有帮助(但不是必需的)
os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()

调用

Cohere 支持所有 LLM 功能:
from langchain_cohere import Cohere
from langchain.messages import HumanMessage
model = Cohere(max_tokens=256, temperature=0.75)
message = "Knock knock"
model.invoke(message)
" Who's there?"
await model.ainvoke(message)
" Who's there?"
for chunk in model.stream(message):
    print(chunk, end="", flush=True)
 Who's there?
model.batch([message])
[" Who's there?"]

API 参考

有关所有 Cohere llm 功能和配置的详细文档,请前往 API 参考