0
0
LangChainframework~30 mins

Source citation in RAG responses in LangChain - Mini Project: Build & Apply

Choose your learning style9 modes available
Source citation in RAG responses
📖 Scenario: You are building a simple Retrieval-Augmented Generation (RAG) system using LangChain. This system answers questions by retrieving relevant documents and then generating answers that include citations to the sources.
🎯 Goal: Create a LangChain script that retrieves documents for a query and generates an answer that includes source citations from the retrieved documents.
📋 What You'll Learn
Create a list called documents with three strings representing document texts.
Create a variable called query with the exact string 'What is LangChain?'.
Use LangChain's FAISS vector store to index the documents.
Create a retriever from the FAISS index with k=2 to get top 2 documents.
Use LangChain's RetrievalQA chain with a dummy LLM that returns a fixed answer including citations.
Generate an answer for the query that includes citations from the retrieved documents.
💡 Why This Matters
🌍 Real World
RAG systems are used in chatbots, virtual assistants, and search engines to provide accurate answers with references to trusted sources.
💼 Career
Understanding how to build RAG pipelines with source citation is valuable for AI developers, data scientists, and software engineers working on intelligent applications.
Progress0 / 4 steps
1
DATA SETUP: Create documents and query
Create a list called documents with these exact strings: 'LangChain is a framework for building applications with LLMs.', 'It helps connect LLMs with external data sources.', and 'Source citation is important in RAG systems.'. Also create a variable called query with the exact string 'What is LangChain?'.
LangChain
Need a hint?

Use a Python list for documents and assign the exact strings. Then assign the exact string to query.

2
CONFIGURATION: Create FAISS index and retriever
Import FAISS and OpenAIEmbeddings from LangChain. Create an embeddings variable using OpenAIEmbeddings(). Then create a FAISS index called index by calling FAISS.from_texts(documents, embeddings). Finally, create a retriever called retriever from index.as_retriever() with search_kwargs={'k': 2}.
LangChain
Need a hint?

Import the classes, create embeddings, then create the FAISS index and retriever with k=2.

3
CORE LOGIC: Create RetrievalQA chain with dummy LLM
Import RetrievalQA from LangChain. Create a dummy LLM class called DummyLLM with a __call__ method that takes prompt and returns the string 'LangChain is a framework for LLM apps. [Sources: Doc1, Doc2]'. Then create a llm instance of DummyLLM. Finally, create a qa variable by calling RetrievalQA.from_chain_type(llm=llm, retriever=retriever).
LangChain
Need a hint?

Create a dummy LLM class that returns a fixed answer with citations. Then create the RetrievalQA chain using this LLM and the retriever.

4
COMPLETION: Generate answer with citations
Call qa.run(query) and assign the result to a variable called answer. This will generate the answer including source citations.
LangChain
Need a hint?

Use qa.run(query) to get the answer with citations and assign it to answer.