A single-agent architecture built with LangGraph and FastAPI that supports multi-turn conversations with short-term memory.
- Single Agent Architecture: Uses LangChain's
create_agentfor agent creation - Multi-turn Conversations: Maintains conversation history and context using PostgreSQL
- Structured Output: Returns structured responses with Pydantic models
- Modular Design: Clean separation of concerns across different modules
- FastAPI Integration: RESTful API with automatic documentation
- Memory Management: PostgreSQL-based conversation memory
- Error Handling: Centralized error handling with consistent response format
agent_poc/
├── app/
│ ├── agent/ # Agent creation and management
│ ├── api/ # FastAPI endpoints
│ ├── memory/ # Conversation memory
│ ├── models/ # Pydantic models
│ ├── prompts/ # Prompt templates
│ └── tools/ # Agent tools
├── tests/ # Test files
├── main.py # Application entry point
├── requirements.txt # Dependencies
└── README.md # This file
- fetch_account_details - Retrieve account related information
- fetch_facility_details - Retrieve facility related information
- save_notes - Save MOM or notes given by user
- fetch_notes - Retrieve notes based on user_id/date/last 5 notes
The agent returns structured responses with:
conversation_id: Unique conversation identifierfinal_response: Human-friendly natural language responsecard_key: UI card type (account_overview,facility_overview,notes_overview,other)account_overview: Account details arrayfacility_overview: Facility details arraynote_overview: Notes arrayrewards_overview: Rewards informationorder_overview: Order information
-
Install Dependencies:
pip install -r requirements.txt
-
Set up PostgreSQL Database:
# Install PostgreSQL (Ubuntu/Debian) sudo apt-get install postgresql postgresql-contrib # Start PostgreSQL service sudo systemctl start postgresql sudo systemctl enable postgresql # Create database and user sudo -u postgres createdb agent_poc_db sudo -u postgres createuser --interactive # Follow prompts to create a user with password # Grant privileges sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE agent_poc_db TO your_username;"
-
Set Environment Variables:
cp env.example .env # Edit .env with your OpenAI API key and database credentials -
Run the Application:
python main.py
Or with uvicorn directly:
uvicorn app.api.main:app --host 0.0.0.0 --port 8000 --reload
GET /- Root endpointPOST /chat- Chat with the agent (main endpoint)
The application now uses JSON files for mock data storage:
app/data/account_data.json- Account informationapp/data/facility_data.json- Facility informationapp/data/notes_data.json- User notes data
All tools now read from and write to these JSON files instead of using hardcoded data.
The /chat endpoint accepts the following request format:
Account Overview Request:
{
"text": "show account overview",
"user_id": "kaushal.sethia.c@evolus.com",
"title": "sample",
"account_id": "A-011977763"
}Fetch Account Details:
{
"text": "fetch account details",
"user_id": "3867",
"title": "postman_test",
"account_id": "A-011977763"
}Fetch Facility Details:
{
"text": "fetch facility details",
"user_id": "3867",
"title": "postman_test",
"account_id": "A-011977763",
"facility_id": "F-013203268"
}Follow-up Question:
{
"text": "how many points do I need to go to next tier",
"user_id": "kaushal.sethia.c@evolus.com",
"title": "sample",
"account_id": "A-011977763",
"conversation_id": "c625fbc7-cc93-4a7e-841b-180872a9420a"
}For development, install additional dependencies:
pip install -r requirements-dev.txtSet up pre-commit hooks:
pre-commit installA Postman collection is provided (postman_collection.json) with pre-configured requests for easy testing. Import this collection into Postman to test all endpoints.
Once the server is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
The application can be configured through environment variables:
OPENAI_API_KEY: Your OpenAI API key (required)MODEL_NAME: Model to use (default: gpt-4o-mini)DEBUG: Enable debug mode (default: True)
DATABASE_URL: Complete PostgreSQL connection string (optional)DB_HOST: Database host (default: localhost)DB_PORT: Database port (default: 5432)DB_NAME: Database name (default: agent_poc_db)DB_USER: Database username (default: username)DB_PASSWORD: Database password (default: password)
If DATABASE_URL is provided, it will be used instead of the individual database settings.
The codebase follows a modular architecture with SOLID principles:
- Tools: Implemented as LangChain tools with proper schemas
- Prompts: Comprehensive prompts with detailed instructions and examples
- Memory: PostgreSQL-based conversation memory with automatic cleanup
- Models: Pydantic models for request/response validation and type safety
- API: FastAPI with centralized error handling and consistent responses
- Agent Factory: Refactored with helper functions following single responsibility principle
- The application uses mock data for demonstration purposes
- In production, replace mock data with actual API calls
- Configure CORS settings appropriately for your frontend
- Set up proper logging and monitoring for production use