Cohere Rerank on LangChain (Integration Guide)

Cohere supports various integrations with LangChain, a large language model (LLM) framework which allows you to quickly create applications based on Cohere’s models. This doc will guide you through how to leverage Rerank with LangChain.

Prerequisites

Running Cohere Rerank with LangChain doesn’t require many prerequisites, consult the top-level document for more information.

Cohere ReRank with LangChain

To use Cohere’s rerank functionality with LangChain, start with instantiating a CohereRerank object as follows: cohere_rerank = CohereRerank(cohere_api_key="{API_KEY}").

You can then use it with LangChain retrievers, embeddings, and RAG. The example below uses the vector DB chroma, for which you will need to install pip install chromadb. Other vector DB’s from this list can also be used. After reranking, we pass the top documents to ChatCohere through its documents argument to get a grounded answer with citations.

PYTHON
1from langchain_classic.retrievers import (
2 ContextualCompressionRetriever,
3)
4from langchain_cohere import (
5 ChatCohere,
6 CohereEmbeddings,
7 CohereRerank,
8)
9from langchain_text_splitters import CharacterTextSplitter
10from langchain_community.vectorstores import Chroma
11from langchain_community.document_loaders import WebBaseLoader
12
13user_query = "what is Cohere Toolkit?"
14
15# Define the Cohere LLM
16llm = ChatCohere(
17 cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
18)
19
20# Define the Cohere embedding model
21embeddings = CohereEmbeddings(
22 cohere_api_key="COHERE_API_KEY", model="embed-english-light-v3.0"
23)
24
25# Load text and split into chunks, you can also use data gathered elsewhere in your application
26raw_documents = WebBaseLoader(
27 "https://docs.cohere.com/docs/cohere-toolkit"
28).load()
29text_splitter = CharacterTextSplitter(
30 chunk_size=1000, chunk_overlap=0
31)
32documents = text_splitter.split_documents(raw_documents)
33
34# Create a vector store from the documents
35db = Chroma.from_documents(documents, embeddings)
36
37# Create Cohere's reranker with the vector DB using Cohere's embeddings as the base retriever
38reranker = CohereRerank(
39 cohere_api_key="COHERE_API_KEY", model="rerank-english-v3.0"
40)
41
42compression_retriever = ContextualCompressionRetriever(
43 base_compressor=reranker, base_retriever=db.as_retriever()
44)
45compressed_docs = compression_retriever.invoke(user_query)
46# Print the reranked documents from using the embeddings and reranker
47print(compressed_docs)
48
49# Ground the answer in the reranked documents
50response = llm.invoke(user_query, documents=compressed_docs)
51
52# Print the answer
53print("Answer:")
54print(response.content)
55# Print the citations that ground the answer in the documents
56print("Citations:")
57print(response.additional_kwargs.get("citations"))

Using LangChain on Private Deployments

You can use LangChain with privately deployed Cohere models. To use it, specify your model deployment URL in the base_url parameter.

PYTHON
1llm = CohereRerank(
2 base_url="<YOUR_DEPLOYMENT_URL>",
3 cohere_api_key="COHERE_API_KEY",
4 model="MODEL_NAME",
5)