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

Building a Basic 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

In this demo, you’ll build a basic RAG app harnessing the power of OpenAI and LangChain.

pip install langchain langchain_community langchain_chroma
pip install -qU langchain-openai
export OPENAI_API_KEY="<insert-your-api-key-here>"
import os

os.environ['USER_AGENT'] = 'sports-buddy-demo'
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")
from langchain import hub
from langchain_chroma import Chroma
from langchain_community.document_loaders import WebBaseLoader
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
loader = WebBaseLoader(
  web_paths=("https://en.wikipedia.org/wiki/2024_Summer_Olympics",),
)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, 
  chunk_overlap=200)
splits = text_splitter.split_documents(docs)
database = Chroma.from_documents(documents=splits, 
  collection_name="sports_collection", embedding=OpenAIEmbeddings())
retriever = database.as_retriever()
prompt = hub.pull("rlm/rag-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, just say that you don't know. Use three sentences
  maximum and keep the answer concise.
Question: {question} 
Context: {context} 
Answer:
def format_docs(docs):
  return "\n\n".join(doc.page_content for doc in docs)
rag_chain = (
  {"context": retriever | format_docs, "question": RunnablePassthrough()}
  | prompt
  | llm
  | StrOutputParser()
)
rag_chain.invoke("Which programmes were dropped from the 2024 Olympics?")
See forum comments
Cinema mode Download course materials from Github
Previous: Introducing SportsBuddy Next: Enhancing a RAG App