Retrieval-Augmented Generation with LangChain

Nov 12 2024 · Python 3.12, LangChain 0.3.x, JupyterLab 4.2.4

Lesson 04: Advanced RAG Techniques

OpenAI & LangChain Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Open the starter notebook file for Lesson 4 to try this out. The starter notebook has been modified to reflect the basic RAG implementation. It currently doesn’t use the default RAG prompt. This specific prompt conditions the response, which doesn’t help showcase what you’re trying to prove here. Because the response will be long, the response’s length is displayed instead of the response itself. Finally, the chunk_overlap has been reduced since this is a relatively small dataset. Run the code as is to see how many documents are returned based on the given query:

4
retriever = database.as_retriever(search_kwargs={"k": 1})
print(len(response))
print(response)
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor

compressor = LLMChainExtractor.from_llm(llm)
compression_retriever = ContextualCompressionRetriever(
  base_compressor=compressor, base_retriever=retriever
)

compressed_docs = compression_retriever.invoke(
  "How was security during the 2024 Olympics"
)

compressed_docs
[Document(metadata={'source': 'https://en.wikipedia.org/wiki/Concerns_
  and_controversies_at_the_2024_Summer_Olympics', 'summary': 
  'Various concerns and controversies arose in relation to the 
  2024 Summer Olympics, including security concerns, human rights 
  issues, and controversy over allowing Israel to participate 
  amidst the Israel–Hamas war, and allowing Russian and Belarusian 
  athletes to compete as neutrals amidst the Russian invasion of 
  Ukraine. Despite the nominal Olympic Truce in place, the wars in 
  Ukraine and Palestine already set a more conflicted political 
  background to the 2024 Summer Olympics, before considering domestic
  and sporting issues.\n\n', 'title': 'Concerns and controversies 
  at the 2024 Summer Olympics'}, page_content='Various concerns and 
  controversies arose in relation to the 2024 Summer Olympics, 
  including security concerns, human rights issues, and controversy 
  over allowing Israel to participate amidst the Israel–Hamas war, 
  and allowing Russian and Belarusian athletes to compete as neutrals 
  amidst the Russian invasion of Ukraine.'),
Document(metadata={'source': 'https://en.wikipedia.org/wiki/Concerns_
  and_controversies_at_the_2024_Summer_Olympics', 'summary': 'Various 
  concerns and controversies arose in relation to the 2024 Summer 
  Olympics, including security concerns, human rights issues, and 
  controversy over allowing Israel to participate amidst the Israel–Hamas 
  war, and allowing Russian and Belarusian athletes to compete as 
  neutrals amidst the Russian invasion of Ukraine. Despite the nominal 
  Olympic Truce in place, the wars in Ukraine and Palestine already set 
  a more conflicted political background to the 2024 Summer Olympics, 
  before considering domestic and sporting issues.\n\n', 'title': 
  'Concerns and controversies at the 2024 Summer Olympics'}, 
  page_content='In February 2024, the French government announced that, 
  as a security precaution, the number of spectators for the opening 
  ceremony along the Seine would be reduced from 600,000 to 300,000. 
  This plan was proposed by Minister of the Interior Gérald Darmanin 
  in 2022. A security perimeter around the area designated for spectator 
  access was planned to be erected in the days leading up to the games, 
  limiting access for the public. In July 2024, it was reported that 
  there would be an expected 220,000 spectators and 45,000 police and 
  security officers present.')]

Introducing Re-ranking

A popular re-ranking tool is the Cohere API. Visit the developer page at https://dashboard.cohere.com/welcome/register to obtain a free API key. Visit the API Keys page from left menu items on the dashboard, and copy the default API key under the Trials keys section. In a new cell, install Cohere or, better still, langchain-cohere:

pip install cohere
pip install langchain-cohere
import os
import getpass

os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")
from langchain.retrievers.contextual_compression import 
  ContextualCompressionRetriever
from langchain_cohere import CohereRerank
from langchain_community.llms import Cohere

llm = Cohere(temperature=0)
compressor = CohereRerank(model="rerank-english-v3.0")
compression_retriever = ContextualCompressionRetriever(
  base_compressor=compressor, base_retriever=retriever
)

compressed_docs = compression_retriever.invoke(
  "How was security during the 2024 Olympics"
)

compressed_docs
See forum comments
Cinema mode Download course materials from Github
Previous: Advanced RAG Techniques Next: Enhancing a Basic RAG App