DEV Community

Cover image for ๐Ÿค–๐Ÿ“š Build Your Own AI-Powered Book Chatbot using Python, Flask, Lang Chain, and Pinecone!
ujjwal
ujjwal

Posted on

๐Ÿค–๐Ÿ“š Build Your Own AI-Powered Book Chatbot using Python, Flask, Lang Chain, and Pinecone!

Hey Devs! ๐Ÿ‘‹
Have you ever wanted to chat with your favorite books like you're texting a friend? ๐Ÿ“–๐Ÿ’ฌ
Well, you're in the right place! In this blog post, Iโ€™ll walk you through how I built BookChatBot, an AI-powered chatbot that can answer questions about a book, using:

  • ๐Ÿง  LangChain (for LLM logic)
  • ๐ŸŒฒ Pinecone (for vector search)
  • ๐Ÿงพ PDF loading and splitting
  • โšก Google Gemini (for answering questions)
  • ๐Ÿงช Flask (as the web framework)

You can find the full code on GitHub:
๐Ÿ‘‰ GitHub Repo


๐Ÿ’ก What are we building?

We're building a chatbot web app that can read PDFs (like a book ๐Ÿ“˜), store them in Pineconeโ€™s vector database, and allow users to ask questions about the content!
The AI will retrieve the most relevant chunks and generate human-like answers using Google's Gemini model.

๐Ÿ’ธ Note: Pineconeโ€™s free tier only allows one index. So for now, you can't dynamically upload new books โ€” but once set up, it's super efficient for Q&A!


๐Ÿ—‚๏ธ Project Structure

bookchatbot-/
โ”œโ”€โ”€ app.py               # Flask app and RAG chain
โ”œโ”€โ”€ helper.py            # PDF loading, chunking, and embeddings
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ prompt.py        # System prompt for LLM
โ”œโ”€โ”€ data/                # Folder with your PDF files
โ”œโ”€โ”€ templates/
โ”‚   โ””โ”€โ”€ chat.html        # Simple frontend
โ”œโ”€โ”€ .env                 # API keys (not shared!)
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  How does it work?

This is a RAG (Retrieval-Augmented Generation) pipeline:

  1. Load and split PDFs into chunks
  2. Convert chunks into vector embeddings
  3. Store in Pinecone (vector DB)
  4. Accept user question
  5. Find the top relevant chunks (via Pinecone)
  6. Use Gemini to answer based on retrieved content

๐Ÿงพ helper.py โ€“ Preprocessing the PDFs

def load_pdf(data):
    loader = DirectoryLoader(data, glob="*.pdf", loader_cls=PyMuPDFLoader)
    return loader.load()
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฅ We load all PDFs from the data/ folder.

def text_splitter(extraced_date):
    text_split = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
    return text_split.split_documents(extraced_date)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“š We split documents into manageable 500-token chunks to help with better retrieval.

def load_geneni_embeddings():
    embeddings = GoogleGenerativeAIEmbeddings(
        model="models/embedding-001",
        google_api_key=os.getenv("GOOGLE_API_KEY")
    )
    return embeddings
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” We use Google's embedding model to turn text chunks into vectors!


๐Ÿš€ app.py โ€“ The Flask App + AI Brain

We start by setting up Pinecone:

pc = Pinecone(api_key= PINECONE_API_KEY)
docsearch = PineconeVectorStore.from_existing_index(index_name="bookchat", embedding=embeddings)
retriver = docsearch.as_retriever(search_type="similarity", search_kwargs={"k": 3})
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  This allows us to retrieve 3 most similar chunks from our stored book.

Then we build a prompt + Gemini LLM:

llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")

prompt = ChatPromptTemplate.from_messages([
    ("system", system_prompt),
    ("human", "{input}"),
])
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ฌ system_prompt defines how the AI should behave (e.g., polite, detailed).

Create the RAG chain:

question_answer_chain = create_stuff_documents_chain(llm, prompt)
rag_chain = create_retrieval_chain(retriver, question_answer_chain)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก This is the brain of the chatbot โ€” retrieval + generation.

Finally, the Flask endpoints:

@app.route("/")
def index():
    return render_template('chat.html')

@app.route("/get", methods=["GET", "POST"])
def chat():
    msg = request.form["msg"]
    response = rag_chain.invoke({"input": msg})
    return str(response["answer"])
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ก The front end sends a message โ†’ gets a smart reply from the AI!


๐Ÿงช Testing it Out

Just run:

python app.py
Enter fullscreen mode Exit fullscreen mode

Then open http://localhost:8080 and start chatting with your book! ๐Ÿ—จ๏ธ๐Ÿ“•


โš ๏ธ Limitations

  • Pineconeโ€™s free tier = only one index. So, you can't upload new books at runtime unless you upgrade or manage your own embedding storage.
  • Static loading: you must re-run the app if you want to embed a different book.
  • Basic HTML frontend โ€“ could be upgraded with React, Tailwind, or Chat UI kits.

๐Ÿ› ๏ธ Ideas for Improvements

  • Add file upload (if using a paid Pinecone plan or local vector store like FAISS)
  • Use streaming responses for a more chat-like feel
  • Add authentication and user-specific history
  • Display source chunk(s) below each answer for transparency

๐ŸŒ Conclusion

Building an AI chatbot like this is easier than ever thanks to:

  • ๐Ÿง  LangChain for chaining LLM workflows
  • ๐ŸŒฒ Pinecone for fast vector search
  • โšก Google Gemini for intelligent responses
  • ๐Ÿงช Flask for quick APIs

If you liked this post, donโ€™t forget to โญ the GitHub repo and follow me here on Dev.to!

Got questions or ideas? Drop them below! ๐Ÿ’ฌ๐Ÿ‘‡


Top comments (0)