Generative AI and Agentic AILangChain 1.4 · LangGraph 1.2 · Python 3.12 or 3.13
Dashboard
0%

Project: chat with your documents

A document chat app is a RAG pipeline with two entry points: an indexing function that turns files into a saved vector index once, and a chat function that condenses each question with the history, retrieves, answers from the chunks only and returns the sources.

Last updated: 08 Oct, 2026 · LangChain 1.4

This project joins the parts of the section: Loading and chunking documents, Embeddings and vector stores, Building a RAG pipeline and Conversational RAG. It runs on three public arXiv papers that the code downloads, keeps only a few pages of each so that indexing stays small, and needs two keys: GROQ_API_KEY for the chat model and GOOGLE_API_KEY for the embeddings.

bash
pip install langchain langchain-groq langchain-google-genai langchain-community langchain-text-splitters faiss-cpu pypdf
build_index downloads the PDFs, loads and splits them, embeds the chunks with Gemini and saves a FAISS index to disk. chat takes a question, condenses it with the history, retrieves the top chunks from the saved index, answers from those chunks only and returns the answer with its sources.

Planning the files

The project layout: a data loader, a vector store, embeddings and a search file · from the Complete Agentic AI Course video · 6:49:49 to 6:52:25

The video makes a src folder with one file per job: a data loader that reads the documents, a vector store file, an embedding file and a search file, with an app.py that wires them. The same four jobs appear below as pieces of one script, so that the whole app runs as a single example. Splitting them into files later is a matter of moving functions.

Video's fileJobIn this project
data_loaderRead every documentThe loop over PAPERS in build_index
embeddingSplit and embed the chunksThe splitter and GoogleGenerativeAIEmbeddings
vectorstoreBuild, save and load the indexFAISS.from_documents, save_local, load_local
searchRetrieve and answerThe chat function

Building the index once

Building the FAISS index and saving it to disk · from the Complete Agentic AI Course video · 7:03:13 to 7:06:57

The video builds the FAISS index the first time and saves it in a folder, then loads it on later runs, so that the chunks are not embedded again. The video's store holds hand-made vectors from a local model with an L2 index; here LangChain's FAISS does the same work with Gemini embeddings. build_index returns the loaded store when the folder exists.

Loading and splitting the pages

python
pages = []
for name, arxiv_id in PAPERS.items():
    if not os.path.exists(f"{name}.pdf"):
        urllib.request.urlretrieve(f"https://arxiv.org/pdf/{arxiv_id}", f"{name}.pdf")
    docs = PyPDFLoader(f"{name}.pdf").load()
    pages += [docs[i] for i in PICK[name]]
chunks = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(pages)

Embedding, saving and reloading

python
store = FAISS.from_documents(chunks, embeddings, ids=[f"c{i}" for i in range(len(chunks))])
store.save_local(INDEX_DIR)       # next run: FAISS.load_local(INDEX_DIR, embeddings, ...) instead

Chatting against the saved index

Search and answer on top of the saved index · from the Complete Agentic AI Course video · 7:08:15 to 7:10:20

The video ends by adding a search step that queries the store and sends the context to a Groq model, and it calls the result the whole crash course in one pipeline. The chat function below does that with the history step from the conversational lesson. It numbers pages from 1 for people, tells the model to say when the documents hold no answer, and returns the sources next to the reply.

The chat function

python
def chat(question):
    standalone = (condense | model).invoke({"history": history, "question": question}).content if history else question
    docs = retriever.invoke(standalone)
    context = "\n\n".join(f"[{d.metadata['source']} p.{d.metadata['page'] + 1}] {d.page_content}" for d in docs)
    reply = (answer | model).invoke({"context": context, "history": history, "question": question}).content
    history.extend([HumanMessage(question), AIMessage(reply)])
    sources = sorted({(d.metadata["source"], d.metadata["page"] + 1) for d in docs})
    return reply, sources

The app end to end

The conversation below is scripted so that it can run without typing. It asks about the Transformer paper, follows up with a pronoun, switches to the object detection review, and ends with a question no document answers.

ExampleAPI keyDownloads three arXiv PDFs by URL and keeps 5 pages; run on Groq openai/gpt-oss-120b and gemini-embedding-001
import os, urllib.request
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import HumanMessage, AIMessage
from langchain.chat_models import init_chat_model

PAPERS = {"attention": "1706.03762", "qzhou": "2508.21632", "objdet": "1807.05511"}
PICK = {"attention": [3, 4], "qzhou": [0, 1], "objdet": [0]}
INDEX_DIR = "paper_index"
embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")
model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0)

def build_index():
    if os.path.exists(INDEX_DIR):
        print("loading the saved index")
        return FAISS.load_local(INDEX_DIR, embeddings, allow_dangerous_deserialization=True)
    pages = []
    for name, arxiv_id in PAPERS.items():
        if not os.path.exists(f"{name}.pdf"):
            urllib.request.urlretrieve(f"https://arxiv.org/pdf/{arxiv_id}", f"{name}.pdf")
        docs = PyPDFLoader(f"{name}.pdf").load()
        pages += [docs[i] for i in PICK[name]]
    chunks = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(pages)
    store = FAISS.from_documents(chunks, embeddings, ids=[f"c{i}" for i in range(len(chunks))])
    store.save_local(INDEX_DIR)
    print("built an index of", len(chunks), "chunks from", len(pages), "pages")
    return store

store = build_index()
retriever = store.as_retriever(search_kwargs={"k": 3})
condense = ChatPromptTemplate.from_messages([
    ("system", "Rewrite the final question as one standalone question that makes sense without the conversation. Do not answer it. Return only the rewritten question."),
    ("placeholder", "{history}"), ("human", "{question}")])
answer = ChatPromptTemplate.from_messages([
    ("system", "Answer in at most three sentences, using only the context. If the answer is not in the context, say you cannot find it in the documents.\n\nContext:\n{context}"),
    ("placeholder", "{history}"), ("human", "{question}")])
history = []

def chat(question):
    standalone = (condense | model).invoke({"history": history, "question": question}).content if history else question
    docs = retriever.invoke(standalone)
    context = "\n\n".join(f"[{d.metadata['source']} p.{d.metadata['page'] + 1}] {d.page_content}" for d in docs)
    reply = (answer | model).invoke({"context": context, "history": history, "question": question}).content
    history.extend([HumanMessage(question), AIMessage(reply)])
    sources = sorted({(d.metadata["source"], d.metadata["page"] + 1) for d in docs})
    return reply, sources

for q in ["What is scaled dot-product attention?", "Why is it divided by that square root?",
          "What does the object detection review say about handcrafted features?", "What is the capital of Australia?"]:
    reply, sources = chat(q)
    print("YOU:", q)
    print("AI :", reply)
    print("sources:", sources)
    print()
print(len(history), "messages in history")

Reading the conversation

  • The first run built the index: 23 chunks from 5 pages, saved to paper_index. A later run prints loading the saved index and embeds nothing.
  • The first answer defines scaled dot-product attention, with the source attention.pdf page 4. Pages count from 1 here, because chat adds 1 to the loader's page index.
  • The pronoun follow-up "Why is it divided by that square root?" was condensed with the history, retrieved the same page, and the answer explains the key dimension and the softmax.
  • The third question moves to another document: the source is objdet.pdf page 1, and the answer repeats the abstract's point about handcrafted features and shallow trainable architectures.
  • The last question has no answer in any document. The reply is I cannot find that information in the provided documents., yet the sources line still lists two chunks, because retrieval always returns something. A UI should hide the sources when the model declines.
  • Eight messages are in the history after four turns, two for each turn.

Typing the questions yourself

Swap the scripted list for a loop that reads from the keyboard. It uses the same chat function and stops on an empty line.

python
while True:
    question = input("You: ").strip()
    if not question:
        break
    reply, sources = chat(question)
    print("AI:", reply)
    print("Sources:", sources)

The video's app vs this app

In the videoIn this project
EmbeddingsA local sentence-transformers modelHosted gemini-embedding-001
IndexA hand-made FAISS index with L2 distance, saved as a file with a metadata pickleFAISS through LangChain, saved with save_local
ModelA Groq Gemma model, since retiredGroq openai/gpt-oss-120b
QuestionsOne query per runA conversation with history
DocumentsPDFs placed in a data folderThree public papers downloaded by URL
KeysPasted into the fileRead from the environment

Where you use a document chat app

  • Team knowledge bases. Point PAPERS at policies or manuals and keep the index folder next to the app.
  • Study tools. Questions over lecture notes or papers, with the page of each answer.
  • A base to grow. Add the hybrid search from the better-retrieval lesson, or move retrieval into a tool as in the agentic lesson.
Watch out. The saved index is not updated when a PDF changes. Delete the paper_index folder, or index into a new folder, after changing PAPERS or PICK. allow_dangerous_deserialization=True is needed because FAISS stores its metadata as a pickle, which can run code when loaded, so only load index folders you created yourself.
Try it yourself
  • Change PICK to keep pages 5 and 6 of the Transformer paper, delete the paper_index folder, and ask about the feed-forward network.
  • Ask a follow-up that depends on the previous answer, then print history to see what the condense step was given.
  • Change k to 1 and ask the object detection question again, then read the sources line.

Every expert started right here.