Skip to main content

Cohere

caution

您当前正在查看文档,介绍了将 Cohere 模型用作 文本补全模型 的方法。许多流行的 Cohere 模型是 聊天补全模型

您可能想查看 此页面

Cohere 是一家加拿大初创公司,提供自然语言处理模型,帮助公司改善人机交互。

请前往 API 参考 获取所有属性和方法的详细文档。

概述

集成细节

类别包名本地可序列化JS 支持包下载包最新
Coherelangchain_communitybetaPyPI - DownloadsPyPI - Version

设置

集成位于 LangChain 社区 包中。我们还需要安装 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["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

调用

Cohere 支持所有 大型语言模型 功能:

<!--IMPORTS:[{"imported": "HumanMessage", "source": "langchain_core.messages", "docs": "https://python.langchain.com/api_reference/core/messages/langchain_core.messages.human.HumanMessage.html", "title": "Cohere"}]-->
from langchain_cohere import Cohere
from langchain_core.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?"]

链接

您还可以轻松地与提示词模板结合,以便于用户输入的结构化。我们可以使用 LangChain表达式 (LCEL) 来实现这一点

<!--IMPORTS:[{"imported": "PromptTemplate", "source": "langchain_core.prompts", "docs": "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.prompt.PromptTemplate.html", "title": "Cohere"}]-->
from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template("Tell me a joke about {topic}")
chain = prompt | model
chain.invoke({"topic": "bears"})
' Why did the teddy bear cross the road?\nBecause he had bear crossings.\n\nWould you like to hear another joke? '

API 参考

有关所有 Cohere 大型语言模型功能和配置的详细文档,请访问 API 参考: https://python.langchain.com/api_reference/community/llms/langchain_community.llms.cohere.Cohere.html

相关


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书