A complete pet shop ordering and support system built with Chainlit, Azure OpenAI, structured outputs, and MongoDB. Features an AI-powered chat assistant that helps customers browse pets, place orders, and track their purchases.
- π€ AI Chat Assistant: Intelligent Chainlit-based chat interface powered by Azure OpenAI
- π οΈ Structured Tool Calling: Type-safe tool calling with Pydantic validation
- πΎ Pet Inventory: Browse dogs, cats, birds, fish, rabbits, and hamsters
- π Order Management: Complete order placement and tracking system
- π¦ REST API: FastAPI-based backend with MongoDB database
- β Structured Outputs: Azure OpenAI structured outputs with strict validation
- π OpenAPI Documentation: Interactive API docs at
/docsand OpenAPI spec at/openapi.json - π Observability: OpenTelemetry tracing, Prometheus metrics, Jaeger UI
- Chainlit Chat App (
app.py): User-facing chat interface - REST API (
api.py): FastAPI service for pet shop operations - Tool Calling (
tools.py): AI agent tools for browsing, ordering, and tracking - Data Models (
models.py): Pydantic models for structured validation - Database (
database.py): MongoDB connection and data management
- Frontend: Chainlit
- AI: Azure OpenAI (GPT-4) with structured outputs
- Backend: FastAPI
- Database: MongoDB
- Validation: Pydantic v2
- Language: Python 3.8+
- Python 3.8 or higher
- MongoDB (local or cloud instance)
- Azure OpenAI account with API access
- Azure OpenAI deployment (GPT-4 recommended)
git clone https://github.com/ianlintner/structured_output_tool_callin.git
cd structured_output_tool_callin
pip install -r requirements.txtCreate a .env file from the example:
cp .env.example .envEdit .env with your credentials:
# Azure OpenAI Configuration
AZURE_OPENAI_API_KEY=your_azure_openai_api_key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_OPENAI_DEPLOYMENT=gpt-4
AZURE_OPENAI_API_VERSION=2024-02-15-preview
# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27017
MONGODB_DATABASE=petshop
# API Configuration
API_HOST=0.0.0.0
API_PORT=8000Local MongoDB:
# Using Docker
docker run -d -p 27017:27017 --name mongodb mongo:latest
# Or start your local MongoDB service
mongodMongoDB Atlas:
- Update
MONGODB_URIin.envwith your Atlas connection string
In one terminal:
python api.pyThe API will be available at http://localhost:8000
- API Docs: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
In another terminal:
chainlit run app.pyThe chat interface will open at http://localhost:8001
User: "Show me all available dogs"
User: "What cats do you have under $700?"
User: "Show me young pets (under 6 months)"
User: "I'd like to order the Golden Retriever"
Assistant: [Collects customer information]
User: [Provides name, email, phone, address]
Assistant: [Places order and provides order ID]
User: "What's the status of my order ORD-12345678?"
Assistant: [Shows order status and details]
The API provides comprehensive OpenAPI documentation:
- Interactive Docs (Swagger UI): http://localhost:8000/docs
- ReDoc Documentation: http://localhost:8000/redoc
- OpenAPI JSON Spec: http://localhost:8000/openapi.json
Browse available pets with optional filters.
Query Parameters:
pet_type: Filter by type (dog, cat, bird, fish, rabbit, hamster)max_price: Maximum pricemin_age_months: Minimum age in monthsmax_age_months: Maximum age in monthsavailable_only: Show only available pets (default: true)
Example:
curl "http://localhost:8000/pets?pet_type=dog&max_price=1000"Create a new order.
Request Body:
{
"customer_name": "John Doe",
"customer_email": "john@example.com",
"customer_phone": "555-0123",
"delivery_address": "123 Main St, City, ST 12345",
"pet_ids": ["pet001", "pet002"]
}Example:
curl -X POST http://localhost:8000/orders \
-H "Content-Type: application/json" \
-d '{"customer_name":"John Doe","customer_email":"john@example.com","customer_phone":"555-0123","delivery_address":"123 Main St","pet_ids":["pet001"]}'Check order status.
Example:
curl http://localhost:8000/orders/ORD-12345678/statusThe chat assistant uses three main tools:
Browse available pets with filters.
Parameters:
pet_type(optional): Type of petmax_price(optional): Maximum pricemin_age_months(optional): Minimum agemax_age_months(optional): Maximum age
Place an order for pets.
Parameters:
customer_name: Customer's full namecustomer_email: Email addresscustomer_phone: Phone numberdelivery_address: Full delivery addresspet_ids: List of pet IDs to order
Check the status of an order.
Parameters:
order_id: Order ID to check
{
"id": "pet001",
"name": "Golden Retriever Puppy",
"type": "dog",
"description": "Friendly and energetic...",
"price": 1200.00,
"age_months": 3,
"available": true,
"image_url": "https://..."
}{
"id": "ORD-12345678",
"customer": {
"name": "John Doe",
"email": "john@example.com",
"phone": "555-0123",
"address": "123 Main St..."
},
"items": [...],
"total_amount": 1200.00,
"status": "pending",
"created_at": "2024-01-01T00:00:00Z"
}# Health check
curl http://localhost:8000/health
# Get all pets
curl http://localhost:8000/pets
# Get specific pet
curl http://localhost:8000/pets/pet001- Open http://localhost:8001
- Try these conversations:
- "Show me all available pets"
- "I want to see dogs under $1000"
- "I'd like to order pet001"
- "Check order status ORD-XXXXXXXX"
- Never commit your
.envfile - Rotate API keys regularly
- Use environment variables for all secrets
- Enable authentication in production
- Use HTTPS in production
- Validate all user inputs
The system initializes with 10 sample pets:
- 3 Dogs (Golden Retriever, Beagle, German Shepherd)
- 2 Cats (British Shorthair, Siamese)
- 2 Birds (Cockatiel, Parakeets)
- 1 Fish (Betta)
- 1 Rabbit (Holland Lop)
- 1 Hamster (Syrian)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
- Ensure MongoDB is running:
mongod --version - Check connection string in
.env - For Atlas, verify IP whitelist and credentials
- Verify API key and endpoint in
.env - Check deployment name matches your Azure OpenAI deployment
- Ensure API version is compatible
- Change
API_PORTin.envfor the API - Chainlit uses port 8001 by default (configurable)
For issues and questions:
- Open an issue on GitHub
- Check the documentation at
/docsendpoint - Review API documentation at
http://localhost:8000/docs