Skip to main content

Cohere

Cohere is a Canadian startup that provides natural language processing models that help companies improve human-machine interactions.

Installation and Setup​

  • Install the Python SDK :
pip install cohere

Get a Cohere api key and set it as an environment variable (COHERE_API_KEY)

Cohere langchain integrations​

APIdescriptionEndpoint docsImportExample usage
ChatBuild chat botschatfrom langchain_community.chat_models import ChatCoherecohere.ipynb
LLMGenerate textgeneratefrom langchain_community.llms import Coherecohere.ipynb
RAG RetrieverConnect to external data sourceschat + ragfrom langchain.retrievers import CohereRagRetrievercohere.ipynb
Text EmbeddingEmbed strings to vectorsembedfrom langchain_community.embeddings import CohereEmbeddingscohere.ipynb
Rerank RetrieverRank strings based on relevancererankfrom langchain.retrievers.document_compressors import CohereRerankcohere.ipynb

Quick copy examples​

Chat​

from langchain_community.chat_models import ChatCohere
from langchain_core.messages import HumanMessage
chat = ChatCohere()
messages = [HumanMessage(content="knock knock")]
print(chat(messages))

LLM​

from langchain_community.llms import Cohere

llm = Cohere(model="command")
print(llm.invoke("Come up with a pet name"))

RAG Retriever​

from langchain_community.chat_models import ChatCohere
from langchain.retrievers import CohereRagRetriever
from langchain_core.documents import Document

rag = CohereRagRetriever(llm=ChatCohere())
print(rag.get_relevant_documents("What is cohere ai?"))

Text Embedding​

from langchain_community.embeddings import CohereEmbeddings

embeddings = CohereEmbeddings(model="embed-english-light-v3.0")
print(embeddings.embed_documents(["This is a test document."]))