Cohere
Cohere 是一家加拿大新創公司,提供自然語言處理模型,協助企業改善人機互動。
前往 API 參考文件 以取得所有屬性和方法的詳細文件。
概觀
整合詳細資訊
類別 | 套件 | 本地 | 可序列化 | JS 支援 | 套件下載次數 | 套件最新版本 |
---|---|---|---|---|---|---|
Cohere | langchain_community | ❌ | beta | ✅ |
設定
整合功能位於 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_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