Retrieval-Augmented Generation with LangChain

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

Lesson 03: Building a Basic RAG System with LangChain

Conversational RAG App 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

Demo

SportsBuddy is already in good shape. However, one immediate concern is its limited knowledge of current sports events. Try this out by asking SportsBuddy what it knows about Jamaica’s participation in the 2024 Summer Olympics.

rag_chain.invoke("What does the retrieved context say about Jamaica in 
  the 2024 Olympics?")
  the 2024 Olympics."
pip install wikipedia
from langchain_community.document_loaders import WikipediaLoader
wiki_loader = WikipediaLoader('Jamaica_at_the_2024_Summer_Olympics')

docs = []
web_loader = WebBaseLoader(
    web_paths=("https://en.wikipedia.org/wiki/2024_Summer_Olympics",)
)
wiki_loader = WikipediaLoader('Jamaica_at_the_2024_Summer_Olympics')

loaders = [web_loader, wiki_loader]

for loader in loaders:
    docs.extend(loader.load())
  are entries in track and field events as well as swimming events.'

Remembering Previous Chats

Currently, SportsBuddy lacks memory of past conversations. When asked a follow-up question, it simply indicates that it doesn’t know. To address this, introduce a memory store and enhance the prompt to incorporate previous messages.

from langchain.chains import create_history_aware_retriever
from langchain_core.prompts import MessagesPlaceholder
from langchain_core.prompts import ChatPromptTemplate

contextualize_q_system_prompt = (
  "Given a chat history and the latest user question "
  "which might reference context in the chat history, "
  "formulate a standalone question which can be understood "
  "without the chat history. Do NOT answer the question, "
  "just reformulate it if needed and otherwise return it as is."
)

contextualize_q_prompt = ChatPromptTemplate.from_messages(
  [
    ("system", contextualize_q_system_prompt),
    MessagesPlaceholder("chat_history"),
    ("human", "{input}"),
  ]
)
history_aware_retriever = create_history_aware_retriever(
  llm, retriever, contextualize_q_prompt
)
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain

system_prompt = (
  "You are an assistant for question-answering tasks. "
  "Use the following pieces of retrieved context to answer "
  "the question. If you don't know the answer, say that you "
  "don't know. Use three sentences maximum and keep the "
  "answer concise."
  "\n\n"
  "{context}"
)

qa_prompt = ChatPromptTemplate.from_messages(
  [
    ("system", system_prompt),
    MessagesPlaceholder("chat_history"),
    ("human", "{input}"),
  ]
)

question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)

rag_chain = create_retrieval_chain(history_aware_retriever, 
  question_answer_chain)
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_community.chat_message_histories import ChatMessageHistory

store = {}

def get_session_history(session_id: str) -> BaseChatMessageHistory:
  if session_id not in store:
    store[session_id] = ChatMessageHistory()
  return store[session_id]

conversational_rag_chain = RunnableWithMessageHistory(
  rag_chain,
  get_session_history,
  input_messages_key="input",
  history_messages_key="chat_history",
  output_messages_key="answer",
)
conversational_rag_chain.invoke(
  {"input": "What does the retrieved context say about Jamaica
     in the 2024 Olympics?"},
  config={
    "configurable": {"session_id": "sports-buddy-session"}
  },
)["answer"]
  delegation to the previous Olympics in 2016, with 56 athletes.'
conversational_rag_chain.invoke(
  {"input": "Is it their eighteenth appearance as an independent state?"},
  config={"configurable": {"session_id": "sports-buddy-session"}},
)["answer"]
  as an independent state."
See forum comments
Cinema mode Download course materials from Github
Previous: Enhancing a RAG App Next: Conclusion