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.chat_models import ChatCoherecohere.ipynb
LLMGenerate textgeneratefrom langchain.llms import Coherecohere.ipynb
RAG RetrieverConnect to external data sourceschat + ragfrom langchain.retrievers import CohereRagRetrievercohere.ipynb
Text EmbeddingEmbed strings to vectorsembedfrom langchain.embeddings import CohereEmbeddingscohere.ipynb
Rerank RetrieverRank strings based on relevancererankfrom langchain.retrievers.document_compressors import CohereRerankcohere.ipynb

Quick copy examples​

Chat​

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

LLM​

from langchain.llms import Cohere

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

RAG Retriever​

from langchain.chat_models import ChatCohere
from langchain.retrievers import CohereRagRetriever
from langchain.schema.document import Document

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

Text Embedding​

from langchain.chat_models import ChatCohere
from langchain.retrievers import CohereRagRetriever
from langchain.schema.document import Document

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