Cohere
Cohere is a Canadian startup that provides natural language processing models that help companies improve human-machine interactions.
Head to the API reference for detailed documentation of all attributes and methods.
Setupβ
The integration lives in the langchain-community
package. We also need
to install the cohere
package itself. We can install these with:
pip install -U langchain-community langchain-cohere
Weβll also need to get a Cohere API key and set
the COHERE_API_KEY
environment variable:
import getpass
import os
os.environ["COHERE_API_KEY"] = getpass.getpass()
Β·Β·Β·Β·Β·Β·Β·Β·
Itβs also helpful (but not needed) to set up LangSmith for best-in-class observability
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
Usageβ
Cohere supports all LLM functionality:
from langchain_cohere import Cohere
from langchain_core.messages import HumanMessage
API Reference:
model = Cohere(model="command", 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?"]
You can also easily combine with a prompt template for easy structuring of user input. We can do this using LCEL
from langchain_core.prompts import PromptTemplate
prompt = PromptTemplate.from_template("Tell me a joke about {topic}")
chain = prompt | model
API Reference:
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? '