## Cohere

> [Cohere](https://cohere.ai/about) is a Canadian startup that provides natural language processing models that help companies improve human-machine interactions.

## Installation and Setup[​](#installation-and-setup "Direct link to Installation and Setup")

*   Install the Python SDK :

```
pip install langchain-cohere
```

Get a [Cohere api key](https://dashboard.cohere.ai/) and set it as an environment variable (`COHERE_API_KEY`)

## Cohere langchain integrations[​](#cohere-langchain-integrations "Direct link to Cohere langchain integrations")

| API | description | Endpoint docs | Import | Example usage |
| --- | --- | --- | --- | --- |
| Chat | Build chat bots | [chat](https://docs.cohere.com/reference/chat) | `from langchain_cohere import ChatCohere` | [cohere.ipynb](https://python.langchain.com/docs/integrations/chat/cohere/) |
| LLM | Generate text | [generate](https://docs.cohere.com/reference/generate) | `from langchain_cohere.llms import Cohere` | [cohere.ipynb](https://python.langchain.com/docs/integrations/llms/cohere/) |
| RAG Retriever | Connect to external data sources | [chat + rag](https://docs.cohere.com/reference/chat) | `from langchain.retrievers import CohereRagRetriever` | [cohere.ipynb](https://python.langchain.com/docs/integrations/retrievers/cohere/) |
| Text Embedding | Embed strings to vectors | [embed](https://docs.cohere.com/reference/embed) | `from langchain_cohere import CohereEmbeddings` | [cohere.ipynb](https://python.langchain.com/docs/integrations/text_embedding/cohere/) |
| Rerank Retriever | Rank strings based on relevance | [rerank](https://docs.cohere.com/reference/rerank) | `from langchain.retrievers.document_compressors import CohereRerank` | [cohere.ipynb](https://python.langchain.com/docs/integrations/retrievers/cohere-reranker/) |

## Quick copy examples[​](#quick-copy-examples "Direct link to Quick copy examples")

### Chat[​](#chat "Direct link to Chat")

```
from langchain_cohere import ChatCoherefrom langchain_core.messages import HumanMessagechat = ChatCohere()messages = [HumanMessage(content="knock knock")]print(chat.invoke(messages))
```

Usage of the Cohere [chat model](https://python.langchain.com/docs/integrations/chat/cohere/)

### LLM[​](#llm "Direct link to LLM")

```
from langchain_cohere.llms import Coherellm = Cohere()print(llm.invoke("Come up with a pet name"))
```

Usage of the Cohere (legacy) [LLM model](https://python.langchain.com/docs/integrations/llms/cohere/)

### ReAct Agent[​](#react-agent "Direct link to ReAct Agent")

```
from langchain_community.tools.tavily_search import TavilySearchResultsfrom langchain_cohere import ChatCohere, create_cohere_react_agentfrom langchain_core.prompts import ChatPromptTemplatefrom langchain.agents import AgentExecutorllm = ChatCohere()internet_search = TavilySearchResults(max_results=4)internet_search.name = "internet_search"internet_search.description = "Route a user query to the internet"prompt = ChatPromptTemplate.from_template("{input}")agent = create_cohere_react_agent(    llm,    [internet_search],    prompt)agent_executor = AgentExecutor(agent=agent, tools=[internet_search], verbose=True)agent_executor.invoke({    "input": "In what year was the company that was founded as Sound of Music added to the S&P 500?",})
```

### RAG Retriever[​](#rag-retriever "Direct link to RAG Retriever")

```
from langchain_cohere import ChatCoherefrom langchain.retrievers import CohereRagRetrieverfrom langchain_core.documents import Documentrag = CohereRagRetriever(llm=ChatCohere())print(rag.get_relevant_documents("What is cohere ai?"))
```

Usage of the Cohere [RAG Retriever](https://python.langchain.com/docs/integrations/retrievers/cohere/)

### Text Embedding[​](#text-embedding "Direct link to Text Embedding")

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

Usage of the Cohere [Text Embeddings model](https://python.langchain.com/docs/integrations/text_embedding/cohere/)

### Reranker[​](#reranker "Direct link to Reranker")

Usage of the Cohere [Reranker](https://python.langchain.com/docs/integrations/retrievers/cohere-reranker/)