How bo2874/chatbot_phone works
Project type and purpose
• This is a full-stack Retrieval-Augmented Generation (RAG) chatbot application (“Chatbot Phone”).
• Frontend: Streamlit UI for user interaction.
• Backend: FastAPI service implementing a modular RAG pipeline.
• Datastores: Milvus (vector DB) + PostgreSQL (relational DB).
• External service: OpenAI LLM API via LangChain.
File structure
• Top-level:
• app/ – backend code (FastAPI application, RAG modules, data preprocessing/indexing, config, logging).
• web/ – Streamlit frontend (web/app.py).
• requirements.txt, .env.example – dependencies and environment variables.
• app/ modules:
• main.py – entry point (creates FastAPI app, mounts router).
• router.py – defines REST endpoints (e.g., POST /query).
• chatbot.py – orchestrates RAG flow.
• rewrite_query.py – rewrites user queries using chat history.
• vector_search.py, embed.py, milvus_connection.py – semantic search pipeline.
• conn_postgres.py – SQL search interface.
• indexer.py, insert_text.py, insert_product_from_text.py, data_preprocessing.py – ETL / indexing utilities.
• logging_config.py – central logging setup.
• web/app.py: Streamlit UI that calls backend’s /query endpoint.
README for architecture insights
• Features: hybrid search (semantic + BM25), SQL filtering, streaming LLM tokens.
• Tech stack: Streamlit, FastAPI, LangChain, Milvus, PostgreSQL, OpenAI API.
• Deployment hints: Docker for Milvus & Postgres, uvicorn for backend, streamlit run for frontend.
Main components
a. Client / Frontend
• Streamlit UI (browser)
b. API Layer (FastAPI)
• Endpoint /query
• Query Router & Rewriter
• RAG Orchestrator (chatbot.py)
c. Retrieval Layer
• Semantic Search:
• embed.py → Milvus SDK → Milvus vector store
• Keyword / SQL Search:
• conn_postgres.py → PostgreSQL
d. LLM Layer
• LangChain orchestration → OpenAI API
• Streaming response back to API Layer
e. Data & Indexing
• ETL / Indexer modules (data_preprocessing, indexer, insert_text, insert_product)
• Config & Logging
f. Databases (external systems)
• Milvus (vector DB)
• PostgreSQL (relational DB)
g. External Service
• OpenAI LLM API
Relationships and data flow
1. User query enters Streamlit UI → HTTP POST /query → FastAPI
2. FastAPI /query endpoint calls rewrite_query → router → selects retriever
3. Two parallel retrieval paths:
• Semantic path: embed user query → Milvus search → top‐k vectors
• SQL path: translate filters → PostgreSQL query
4. Combine retrieval results → LangChain prompt template
5. Call OpenAI API (streaming) → receive tokens → stream back through FastAPI → Streamlit
6. ETL / Indexing modules run separately (offline jobs) to load and preprocess data into Milvus/Postgres
Architectural patterns & design principles
• Modular RAG pipeline: clear separation of query rewriting, retrieval, LLM generation
• Hybrid search design: vector DB + relational DB coexist
• API-driven backend: single FastAPI service exposing REST endpoints
• Streaming architecture for LLM responses (low latency)
• Decoupled frontend (Streamlit) and backend (FastAPI) for flexibility