pocketflow/cookbook/pocketflow-rag
zachary62 89008a04a1 add web hil 2025-04-11 13:12:20 -04:00
..
README.md Update README.md 2025-03-31 13:00:45 -04:00
flow.py update rag tutorial 2025-03-30 21:12:13 -04:00
main.py update rag tutorial 2025-03-30 21:12:13 -04:00
nodes.py update rag tutorial 2025-03-30 21:12:13 -04:00
requirements.txt add web hil 2025-04-11 13:12:20 -04:00
utils.py remove simple embedding 2025-03-31 10:00:52 -04:00

README.md

Retrieval Augmented Generation (RAG)

This project demonstrates a simplified RAG system that retrieves relevant documents based on user queries and generates answers using an LLM. This implementation is based directly on the tutorial: Retrieval Augmented Generation (RAG) from Scratch — Tutorial For Dummies.

Features

  • Document chunking for processing long texts
  • FAISS-powered vector-based document retrieval
  • LLM-powered answer generation

How to Run

  1. Set your API key:

    export OPENAI_API_KEY="your-api-key-here"
    

    Or update it directly in utils.py

  2. Install and run with the default query:

    pip install -r requirements.txt
    python main.py
    
  3. Run the application with a sample query:

    python main.py --"How does the Q-Mesh protocol achieve high transaction speeds?"
    

How It Works

The magic happens through a two-phase pipeline implemented with PocketFlow:

graph TD
    subgraph OfflineFlow[Offline Document Indexing]
        ChunkDocs[ChunkDocumentsNode] --> EmbedDocs[EmbedDocumentsNode] --> CreateIndex[CreateIndexNode]
    end
    
    subgraph OnlineFlow[Online Processing]
        EmbedQuery[EmbedQueryNode] --> RetrieveDoc[RetrieveDocumentNode] --> GenerateAnswer[GenerateAnswerNode]
    end

Here's what each part does:

  1. ChunkDocumentsNode: Breaks documents into smaller chunks for better retrieval
  2. EmbedDocumentsNode: Converts document chunks into vector representations
  3. CreateIndexNode: Creates a searchable FAISS index from embeddings
  4. EmbedQueryNode: Converts user query into the same vector space
  5. RetrieveDocumentNode: Finds the most similar document using vector search
  6. GenerateAnswerNode: Uses an LLM to generate an answer based on the retrieved content

Example Output

✅ Created 5 chunks from 5 documents
✅ Created 5 document embeddings
🔍 Creating search index...
✅ Index created with 5 vectors
🔍 Embedding query: How to install PocketFlow?
🔎 Searching for relevant documents...
📄 Retrieved document (index: 0, distance: 0.3427)
📄 Most relevant text: "Pocket Flow is a 100-line minimalist LLM framework
        Lightweight: Just 100 lines. Zero bloat, zero dependencies, zero vendor lock-in.
        Expressive: Everything you love—(Multi-)Agents, Workflow, RAG, and more.
        Agentic Coding: Let AI Agents (e.g., Cursor AI) build Agents—10x productivity boost!
        To install, pip install pocketflow or just copy the source code (only 100 lines)."

🤖 Generated Answer:
To install PocketFlow, use the command `pip install pocketflow` or simply copy its 100 lines of source code.