跳至主要內容

Cohere

注意

您目前正在瀏覽一個頁面,該頁面記錄了將 Cohere 模型用作文字完成模型的使用方法。許多流行的 Cohere 模型是聊天完成模型

您可能正在尋找這個頁面

Cohere 是一家加拿大新創公司,提供自然語言處理模型,協助公司改善人機互動。

請前往API 參考,以取得所有屬性和方法的詳細文件。

概述

整合詳細資訊

類別套件本機可序列化JS 支援套件下載套件最新版本
Coherelangchain_communitybetaPyPI - DownloadsPyPI - Version

設定

該整合位於 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["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

調用

Cohere 支援所有 LLM 功能

from langchain_cohere import Cohere
from langchain_core.messages import HumanMessage
API 參考: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?"]

鏈接

您還可以輕鬆地將其與提示範本結合使用,以輕鬆構建使用者輸入。 我們可以使用 LCEL 來做到這一點

from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template("Tell me a joke about {topic}")
chain = prompt | model
API 參考:PromptTemplate
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 llm 功能和組態的詳細文件,請前往 API 參考: https://langchain-python.dev.org.tw/api_reference/community/llms/langchain_community.llms.cohere.Cohere.html


這個頁面有幫助嗎?