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:
The langchain-community package is no longer maintained. Examples that import from langchain_community may be outdated or broken. Use with caution.
Credentials
We’ll need to get a Cohere API key and set the COHERE_API_KEY environment variable:
import getpass
import os
if "COHERE_API_KEY" not in os.environ:
os.environ["COHERE_API_KEY"] = getpass.getpass()
Installation
pip install -U langchain-community langchain-cohere
It’s also helpful (but not needed) to set up LangSmith for best-in-class observability
os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()
Invocation
Cohere supports all 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)
await model.ainvoke(message)
stream = model.stream_events(message, version="v3")
for token in stream.text:
print(token, end="", flush=True)
API reference
For detailed documentation of all Cohere llm features and configurations head to the API reference