How trongdung143/langchain works
Project Type and Purpose
• This is a containerized, FastAPI-based, full‐stack question-answering and emotional-response service. It uses LangChain for document processing and vector embeddings (Faiss) to power two API endpoints: one for factual Q&A and one for styled emotional responses.
File Structure Analysis
Top-level:
• Dockerfile, docker-compose.yml, nginx.conf – container orchestration and reverse proxy
• requirements.txt – Python dependencies
• app/ – main application
– api/ (ask.py, rewrite.py, home.py) – HTTP endpoints
– services/ – business logic (prompt creation, document loading, RAG chain orchestration, vector store access, templates)
– models/ – ML model loading (response model, emotional style model)
– config.py, main.py, setup.py – app configuration, entry point, packaging
• data/ – persistence
– courses/ – raw PDFs
– vectorstores/ – Faiss indices + metadata
• scripts/ – utility scripts for conversion and vector DB creation
• tests/ – endpoint test coverage
README Insights
• Project description, features, architecture overview
• Env-setup (.env variables for external API keys and file paths)
• Docker & manual run instructions
• Two API “type” modes (1=Q&A, 2=emotional rewrite)
Main Components
a. Clients
– User via HTTP (curl, UI, other services)
b. Edge / Ingress
– Nginx reverse proxy (docker-compose)
c. Application Server
– Uvicorn ASGI server running FastAPI (app.main:app)
d. API Layer
– Routers in app/api:
• /ask → services.rag_chain & models.load_response_model
• /rewrite → services.rag_chain & models.load_emotional_model
• /home → health check/static
e. Service Layer
– create_prompt, document_loader (PDF parsing), read_vector_db
– rag_chain orchestrates retrieval + LLM calls
– templates fill in prompts
f. Model Layer
– Response model (LLM)
– Emotion model (LLM)
g. Data Layer
– PDF file storage (data/courses)
– Vector DB (Faiss index + pickle metadata in data/vectorstores)
h. External Services
– Together API (LLM inference)
– Google API (if used for embeddings)
Relationships and Data Flow
• Client → Nginx → Uvicorn/FastAPI router
• Router → Service (document loader → vector DB retrieval → chain)
• Chain → External LLM API → process output with templates
• Service → Model layer → construct response
• Response → Router → Client
• Scripts (convert.py, create_vector_db.py) are offline processes updating data/courses & data/vectorstores
Architectural Patterns & Principles
• Layered architecture (Ingress → API → Services → Models → Data)
• Separation of concerns: routing vs business logic vs ML model loading vs persistence
• Stateless API containers with persistent volumes for data/vectorstores
• 12-factor friendly (env config, logs to stdout, backing services as attached resources)