Skip to main content

Cohere

caution

You are currently on a page documenting the use of Cohere models as text completion models. Many popular Cohere models are chat completion models.

You may be looking for this page instead.

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.

Overviewโ€‹

Integration detailsโ€‹

ClassPackageLocalSerializableJS supportPackage downloadsPackage latest
Coherelangchain_communityโŒbetaโœ…PyPI - DownloadsPyPI - Version

Setupโ€‹

The integration lives in the langchain-community package. We also need to install the cohere package itself. We can install these with:

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

Invocationโ€‹

Cohere supports all LLM functionality:

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

Chainingโ€‹

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: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 referenceโ€‹

For detailed documentation of all Cohere llm features and configurations head to the API reference: https://python.langchain.com/api_reference/community/llms/langchain_community.llms.cohere.Cohere.html


Was this page helpful?