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.

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: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?"]

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? '

Was this page helpful?


You can also leave detailed feedback on GitHub.