A backend service for a Walmart-like shopping assistant, built with FastAPI and OpenAI Agents. This project provides intelligent product search, cart management, and user interaction capabilities using advanced AI models and a vector database.
- AI Shopping Assistant: Uses OpenAI GPT-4.1 to help users search, recommend, and manage products.
- Cart Management: Add, remove, and view items in the user's cart with JWT-based authentication.
- Product Search: Supports search by category, product ID, and fuzzy queries.
- Vector Store Retrieval: Retrieves relevant products using vector embeddings and MongoDB.
- Structured Responses: Optionally structures product responses with product IDs in XML tags.
- Logging: In-memory and file-based logging for API requests and errors.
- Docker Support: Easily build and run the backend in a containerized environment.
main.py # FastAPI application entrypoint
wrapper.py # Main agent orchestration and response handling
cartAgent.py # Cart management agent and tools
cartTools.py # Cart-related API tool functions
ragAgent.py # Vector store retrieval agent
searchTools.py # Product search tool functions
utils.py # Shared utilities and user model
test.py # Example/test agent usage
Dockerfile # Docker build instructions
pyproject.toml # Python dependencies and project metadata
fastapi.logs # Log file (runtime generated)
- Recommendation: Use uv instead of pip. (uv is an extremely fast Python package and project manager, written in Rust). checkout more details uv
- Python 3.13+
- uv (for dependency management)
- Docker (optional, for containerized deployment)
- MongoDB instance (for product vector search)
- OpenAI API key and organization/project (for GPT-4.1 and embeddings)
-
Clone the repository:
git clone https://github.com/timetooth/sparky.git cd sparky_backend -
Install dependencies:
uv sync
-
Set environment variables:
OPENAI_API_KEY: Your OpenAI API keyOPENAI_ORG_KEY: Your OpenAI organization keyOPENAI_PROJECT_ID: Your OpenAI project IDNODE_BASE_URI: Base URI for the Node.js backendMONGO_URI: MongoDB connection string- (Optional)
ALLOWED_ORIGINS: Comma-separated list of allowed CORS origins or * for dev environment, example ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5500
You can use a .env file in the project root:
OPENAI_API_KEY=sk-... OPENAI_ORG_KEY=... OPENAI_PROJECT_ID=... NODE_BASE_URI= MONGO_URI= ALLOWED_ORIGINS= -
Run the server:
.venv/bin/fastapi run main.py
Or use Docker:
docker build -t sparky . docker run -p 8000:10000 sparkyOr use uv: (recommended for development)
uv run fastapi dev
GET /— Health checkPOST /agent_response— Get AI agent response (see below for payload)GET /logs— Get recent logsGET /logs/download— Download log fileDELETE /logs/delete— Clear logsGET /cors— Get allowed CORS origins
{
"user_name": "Alice",
"user_age": 28,
"user_input": "Add 2 red t-shirts to my cart",
"user_jwt": "<JWT_TOKEN>",
"last_response_id": null,
"use_structuring": true
}{
"new_message_id": "...",
"user_input": "Add 2 red t-shirts to my cart",
"final_output": "...response..."
}- Cart Manager: Handles cart operations (add, remove, view, clear)
- Search Tools: Search by category, ID, or fuzzy query
- RAG Agent: Retrieves products using vector search
- User Info Tool: Returns user details
- Run test.py for agent and tool usage examples.
- Use the
/logsendpoint to monitor API activity.