A comprehensive AI-powered application that combines a React frontend with a FastAPI backend and LangChain agentic AI for intelligent data retrieval and natural language interaction with order and project management systems.
- Azure OpenAI Integration - Powered by GPT-4 for intelligent query processing
- Dual-Mode Architecture - Supports both GraphQL API and local SQLite database
- Tool-Based Architecture - Custom tools for orders, projects, and global search
- Natural Language Processing - Convert user queries to structured database queries
- Full-Text Search - FTS5-powered global search across all data
- Rich Text Responses - Markdown-formatted, human-readable outputs
- Single Tool Call Optimization - Efficient query execution with recursion management
- Modern Chat Interface - Clean, responsive design
- Rich Text Display - Markdown rendering with syntax highlighting
- Real-time Communication - Instant responses from AI agent
- Error Handling - Graceful error display and user feedback
- Loading States - Visual indicators for better UX
- RESTful API - Clean endpoints for AI agent interaction
- CORS Support - Cross-origin requests enabled
- Environment Configuration - Secure credential management
- Comprehensive Logging - Detailed request/response tracking
- Error Handling - Robust exception management
- Dual Data Sources
- GraphQL API - External order/project management system (Port 3000)
- SQLite Database - Local database with FTS5 full-text search
- Flexible Query Generation - Dynamic GraphQL and SQL query construction
- Full-Text Search - FTS5 indexing for projects and orders
- Data Formatting - Convert raw data to user-friendly format
- Authentication - Secure API token management
βββββββββββββββββββ HTTP/REST βββββββββββββββββββ GraphQL βββββββββββββββββββ
β React Client β ββββββββββββββββΆβ FastAPI Server β ββββββββββββββΆβ GraphQL API β
β (Port 4000) β β (Port 8000) β β (Port 3000) β
β β β β β β
β β’ Chat UI β β β’ AI Agent β β β’ Orders Data β
β β’ Markdown β β β’ Tool Calling β β β’ Projects Data β
β β’ Rich Text β β β’ Azure OpenAI β β β’ Authenticationβ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β
β SQLite
βΌ
βββββββββββββββββββ
β Local Database β
β β
β β’ SQLite + FTS5 β
β β’ Orders (v1) β
β β’ Projects (v1) β
β β’ Clients β
β β’ Global Search β
βββββββββββββββββββ
ai-chatbot/
βββ π¨ client/ # React Frontend Application
β βββ public/ # Static assets
β β βββ index.html # HTML entry point
β β βββ manifest.json # PWA manifest
β β βββ robots.txt # SEO robots file
β βββ src/
β β βββ App.js # Main chat interface
β β βββ App.css # Styling with markdown support
β β βββ index.js # React entry point
β β βββ index.css # Global styles
β β βββ reportWebVitals.js # Performance monitoring
β β βββ setupTests.js # Test configuration
β βββ package.json # Node.js dependencies
β βββ README.md # Client documentation
β
βββ βοΈ src/ # Python Backend Application
β βββ π€ ai_agent.py # Main AI agent with Azure OpenAI
β βββ π€ ai_agent_v1.py # SQLite-based AI agent (local DB)
β βββ π main.py # FastAPI application entry point
β βββ πΎ db_setup.py # SQLite database initialization & FTS5 setup
β β
β βββ βοΈ config/ # Configuration
β β βββ constants.py # API tokens and constants
β β
β βββ π models/ # Data models
β β βββ ask_request_state.py # Request/response schemas
β β
β βββ π£οΈ routes/ # API endpoints
β β βββ ask.py # /ask endpoint for AI queries
β β
β βββ π§ services/ # Business logic layer
β β βββ order_service.py # Order data (GraphQL-based)
β β βββ order_service_v1.py # Order data (SQLite-based)
β β βββ project_service.py # Project data (GraphQL-based)
β β βββ project_service_v1.py # Project data (SQLite-based)
β β βββ global_search_service.py # FTS5 full-text search
β β
β βββ π οΈ tools/ # AI Agent Tools
β β βββ get_orders.py # GraphQL order retrieval tool
β β βββ get_orders_v1.py # SQLite order retrieval tool
β β βββ get_projects.py # GraphQL project retrieval tool
β β βββ get_projects_v1.py # SQLite project retrieval tool
β β βββ global_search_v1.py # Global search tool (FTS5)
β β βββ __init__.py # Tool exports
β β
β βββ π utils/ # Utility functions
β βββ logger.py # Colored logging configuration
β
βββ πΎ local_data.db # SQLite database (auto-generated)
βββ π .env # Environment variables (secure)
βββ π .env.example # Environment template
βββ π requirements.txt # Python dependencies
βββ π README.md # This documentation
- Python 3.8+ - Core language
- FastAPI - Modern, fast web framework
- LangChain - AI agent framework
- LangGraph - Agent orchestration with memory
- Azure OpenAI - Language model provider (GPT-4)
- SQLite - Local database with FTS5 full-text search
- Pydantic - Data validation
- Uvicorn - ASGI server
- Requests - HTTP client for GraphQL API
- React 19.1.0 - UI framework
- ReactMarkdown 10.1.0 - Rich text rendering
- Sass 1.89.2 - CSS preprocessing
- CSS3 - Modern styling
- JavaScript ES6+ - Modern JavaScript
- BotUI - Chat interface components
- GPT-4 - Language model
- GraphQL - API query language
- Structured Tools - Custom AI tools
- Markdown - Rich text formatting
- Python 3.8+
- Node.js 16+
- Yarn or npm
- Valid Azure OpenAI API key
- GraphQL API access credentials
# Clone and navigate to project
git clone <repo-url>
cd ai-chatbot
# Create virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
# source .venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env with your credentials
# Initialize local database (optional - for v1 tools)
python src/db_setup.py# Navigate to client directory
cd client
# Install dependencies
yarn install
# or: npm install
# Start development server
yarn start
# or: npm startTerminal 1 - Backend:
cd ai-chatbot
python -m uvicorn src.main:app --reload --port 8000Terminal 2 - Frontend:
cd ai-chatbot/client
yarn startOptional - Local GraphQL Server (for testing):
# If you have a local GraphQL server
cd <graphql-server-directory>
# Start server on port 3000Access the application:
- Frontend: http://localhost:4000
- Backend API: http://localhost:8000
- API Docs: http://localhost:8000/docs
Create a .env file with the following variables:
# Azure OpenAI Configuration
AZURE_OPENAI_API_KEY=your_azure_openai_key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_OPENAI_API_VERSION=2024-02-01
AZURE_OPENAI_API_DEPLOYMENT_NAME=gpt-4
# Optional: Search capabilities
TAVILY_API_KEY=your_tavily_search_key
# GraphQL API Authentication (optional - for GraphQL tools)
AUTH_TOKEN=your_graphql_api_token| Method | Endpoint | Description | Request Body |
|---|---|---|---|
GET |
/health |
Health check | None |
POST |
/ask |
AI agent query | AskRequestState |
{
"model_name": "gpt-4",
"query": "Find orders with status OPEN",
"system_prompt": "Act as an AI assistant",
"allow_search": false,
"thread_id": null
}- Purpose: Retrieve order information from external GraphQL API
- Input: Variables object with search parameters
- Output: Formatted order details with status, addresses, dates
- Features: Custom queries, minimal field selection, human-readable responses
- Endpoint:
http://localhost:3000/verse-graphql
- Purpose: Retrieve project information from external GraphQL API
- Input: Variables object with search parameters
- Output: Formatted project details with names, clients, status
- Features: Custom queries, minimal field selection, human-readable responses
- Endpoint:
http://localhost:3000/verse-graphql
- Purpose: Retrieve order information from local SQLite database
- Input: Query filters (status, file number, date range)
- Output: Formatted order details from local database
- Features: Fast local queries, no external dependencies
- Purpose: Retrieve project information from local SQLite database
- Input: Query filters (status, group number, client)
- Output: Formatted project details from local database
- Features: Fast local queries, no external dependencies
- Purpose: Perform full-text search across all projects and orders
- Input: Space-separated search terms
- Output: Combined results from both projects and orders
- Features: FTS5-powered search, prefix matching, LIKE fallback
- Indexed Fields: Project names, group numbers, order file numbers, addresses
β
"Find 3 latest orders with status OPEN"
β
"Get the address for order END8726423"
β
"What's the status of order END6527461?"
β
"Show me orders from Los Angeles county"
β
"Find orders closing this month"
β
"Search for orders in California"
β
"List all open projects"
β
"Find projects for Client 5"
β
"Show me project details for group number P12345678"
β
"What projects are in cancelled status?"
β
"Find the newest projects"
β
"Search for Springfield across all data"
β
"Find anything related to P12345678"
β
"Search for END7654321 and get all related info"
β
"Look for Client projects and orders"
The project includes a SQLite database with FTS5 (Full-Text Search) capabilities for local testing and development.
Tables:
- client - Client information (id, name)
- projects - Project records (id, name, groupNumber, status, client_id, createdAt, updatedAt)
- orders - Order records (id, fileNum, displayStatus, status, address, createdAt, updatedAt)
- projects_fts - FTS5 virtual table for project search (name, groupNumber)
- orders_fts - FTS5 virtual table for order search (fileNum, address)
The db_setup.py script generates:
- 20 dummy clients
- 100 projects with random statuses (open/cancelled)
- 100 orders with random statuses and addresses
- FTS5 indexes for fast full-text search
python src/db_setup.pyThis will create local_data.db in the project root with all tables, indexes, and sample data.
The FTS5 implementation supports:
- Prefix matching - Find terms starting with specific text
- Multi-term search - Search for multiple keywords simultaneously
- LIKE fallback - Substring matching when FTS doesn't match
- Deduplication - Combined results from both search methods
The application supports markdown formatting for enhanced readability:
- π Headers - Section organization
- πΈ Bold text - Important information highlighting
- π Lists - Organized data presentation
- π» Code blocks - Technical details
- π Blockquotes - Important notes
- π― Structured formatting - Clean, professional appearance
- Colored output - Easy debugging
- Request tracking - Full request/response logging
- Error details - Comprehensive error information
- Tool execution - GraphQL query and response logging
- Graceful degradation - Fallback responses
- User-friendly messages - Clear error communication
- Recursion management - Prevents infinite loops
- Network resilience - Handles API failures
- Single tool calls - Optimized query execution
- Efficient GraphQL - Minimal field selection
- Response caching - Memory-based conversation state
- Fast responses - Optimized processing pipeline
Backend not starting:
# Check Python version
python --version
# Verify virtual environment
pip list
# Check environment variables
python -c "from dotenv import load_dotenv; load_dotenv(); import os; print(os.getenv('AZURE_OPENAI_API_KEY'))"Frontend not loading:
# Clear cache
yarn cache clean
# Reinstall dependencies
rm -rf node_modules yarn.lock
yarn install
# Check port conflicts
netstat -ano | findstr :4000AI agent errors:
- Verify Azure OpenAI credentials
- Check GraphQL API connectivity
- Review recursion limit settings
- Validate tool schemas