以编程方式使用文档

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

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

设置

集成位于 langchain-community 包中。我们还需要安装 cohere 包本身。我们可以通过以下方式安装这些包:

凭证

我们需要获取 Cohere API 密钥 并设置 COHERE_API_KEY 环境变量:

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 functionality:

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?"
stream = model.stream_events(message, version="v3")
for token in stream.text:
    print(token, end="", flush=True)
 Who's there?
model.batch([message])
[" Who's there?"]

API 参考

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