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β
API | description | Endpoint docs | Import | Example usage |
---|---|---|---|---|
Chat | Build chat bots | chat | from langchain.chat_models import ChatCohere | cohere.ipynb |
LLM | Generate text | generate | from langchain.llms import Cohere | cohere.ipynb |
RAG Retriever | Connect to external data sources | chat + rag | from langchain.retrievers import CohereRagRetriever | cohere.ipynb |
Text Embedding | Embed strings to vectors | embed | from langchain.embeddings import CohereEmbeddings | cohere.ipynb |
Rerank Retriever | Rank strings based on relevance | rerank | from langchain.retrievers.document_compressors import CohereRerank | cohere.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?"))