Rerank on LangChain

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.

PYTHON
1from langchain.retrievers import ContextualCompressionRetriever, CohereRagRetriever
2from langchain.retrievers.document_compressors import CohereRerank
3from langchain_community.embeddings import CohereEmbeddings
4from langchain_community.chat_models import ChatCohere
5from langchain.text_splitter import CharacterTextSplitter
6from langchain_community.document_loaders import TextLoader
7from langchain_community.vectorstores import Chroma
8
9user_query = "When was Cohere started?"
10# Create cohere's chat model and embeddings objects
11cohere_chat_model = ChatCohere(cohere_api_key="{API_KEY}")
12cohere_embeddings = CohereEmbeddings(cohere_api_key="{API_KEY}")
13# Load text files and split into chunks, you can also use data gathered elsewhere in your application
14raw_documents = TextLoader('demofile.txt').load()
15text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
16documents = text_splitter.split_documents(raw_documents)
17# Create a vector store from the documents
18db = Chroma.from_documents(documents, cohere_embeddings)
19
20# Create Cohere's reranker with the vector DB using Cohere's embeddings as the base retriever
21cohere_rerank = CohereRerank(cohere_api_key="{API_KEY}")
22compression_retriever = ContextualCompressionRetriever(
23 base_compressor=cohere_rerank,
24 base_retriever=db.as_retriever()
25)
26compressed_docs = compression_retriever.get_relevant_documents(user_query)
27# Print the relevant documents from using the embeddings and reranker
28print(compressed_docs)
29
30# Create the cohere rag retriever using the chat model
31rag = CohereRagRetriever(llm=cohere_chat_model)
32docs = rag.get_relevant_documents(
33 user_query,
34 source_documents=compressed_docs,
35)
36# Print the documents
37for doc in docs[:-1]:
38 print(doc.metadata)
39 print("\n\n" + doc.page_content)
40 print("\n\n" + "-" * 30 + "\n\n")
41# Print the final generation
42answer = docs[-1].page_content
43print(answer)
44# Print the final citations
45citations = docs[-1].metadata['citations']
46print(citations)