Cohere
This notebook covers how to get started with Cohere chat models.
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 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
ChatCohere supports all ChatModel functionality:
from langchain_community.chat_models import ChatCohere
from langchain_core.messages import HumanMessage
chat = ChatCohere(model="command", max_tokens=256, temperature=0.75)
messages = [HumanMessage(content="knock knock")]
chat.invoke(messages)
AIMessage(content="Who's there?")
await chat.ainvoke(messages)
AIMessage(content="Who's there?")
for chunk in chat.stream(messages):
print(chunk.content, end="", flush=True)
Who's there?
chat.batch([messages])
[AIMessage(content="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 ChatPromptTemplate
prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")
chain = prompt | chat
chain.invoke({"topic": "bears"})
AIMessage(content="Why did the bear go to the chiropractor?\n\nBecause she was feeling a bit grizzly!\n\nHope you found that joke about bears to be a little bit amusing! If you'd like to hear another one, just let me know. In the meantime, if you have any other questions or need assistance with a different topic, feel free to let me know. \n\nJust remember, even if you have a sore back like the bear, it's always best to consult a licensed professional for injuries or pain you may be experiencing. \n\nWould you like me to tell you another joke?")