How vinaymaurya14/langchain_fastapi_agent works
Project Type & Purpose
• This is a small full-stack AI application:
• A FastAPI-based backend (“app/main.py”) exposing REST endpoints for email classification and response generation.
• A Gradio-based frontend (“app.py”) providing a user-friendly web UI.
• A core “agent” module (“app/agent.py”) that uses LangChain and the OpenAI API to do the actual NLP work.
Main Components
a. Gradio Frontend (app.py)
• Web UI listening on port 7860.
• Takes raw email text as input, displays classification + generated response.
• Sends HTTP requests to the backend’s /classify-email/ endpoint.
b. FastAPI Backend (app/main.py)
• Uvicorn server on port 8000.
• Defines /classify-email/ endpoint.
• Uses Pydantic models (app/models.py) to validate request/response schemas.
c. Core Agent (app/agent.py)
• Encapsulates business logic: constructs LangChain prompts, calls OpenAI via openai-python SDK.
• Returns a structure containing “category” + “response_text.”
d. Configuration (.env)
• Holds OPENAI_API_KEY, loaded at startup.
e. External Service
• OpenAI’s REST API for GPT models.
Relationships & Data Flow
• User → Gradio UI (HTTP/websocket) →
• Gradio UI → FastAPI Backend (POST /classify-email/ with email text) →
• FastAPI Controller → Pydantic Model Validation → Agent Service →
• LangChain Wrapper → OpenAI API →
• OpenAI Response → Agent Formats Output → FastAPI Serializes via Pydantic →
• FastAPI → Gradio UI → User
Architectural Patterns & Design Principles
• Separation of concerns:
• Presentation layer (Gradio)
• API layer (FastAPI controllers + Pydantic models)
• Service layer (Agent + LangChain)
• Configuration layer (.env)
• Dependency injection of API key into Agent at startup.
• Stateless microservice: no database; each request stands alone.