Skip to content

maneeshkumar52/ecommerce-assistant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ecommerce-assistant

Conversational AI shopping assistant with product search, real-time inventory checking, personalised recommendations, and shopping cart management — all orchestrated via Azure OpenAI function calling.

Project 8, Chapter 20 of "Prompt to Production" by Maneesh Kumar


Overview

The assistant accepts natural language queries such as "Show me blue running shoes under £80 in size 10" and automatically calls the right tools to fulfil the request:

Tool Backend
search_products Azure AI Search (hybrid keyword + vector)
check_inventory Redis cache (with mock fallback)
get_recommendations Purchase-history engine
add_to_cart In-memory cart store

When Azure credentials are not configured the system operates entirely on built-in mock data so you can develop and test locally without any cloud dependencies.


Architecture

                        ┌─────────────────────────────────────────┐
                        │          ShopSmart UK Customer           │
                        └──────────────────┬──────────────────────┘
                                           │ natural language query
                                           ▼
                        ┌─────────────────────────────────────────┐
                        │           FastAPI  (src/main.py)        │
                        │    POST /api/v1/chat                    │
                        └──────────────────┬──────────────────────┘
                                           │
                                           ▼
                        ┌─────────────────────────────────────────┐
                        │         ShoppingAgent (src/agent.py)    │
                        │   Azure OpenAI GPT-4o tool-call loop    │
                        └──┬──────────┬──────────┬──────────┬────┘
                           │          │          │          │
              ┌────────────▼──┐  ┌────▼────┐  ┌─▼──────┐  ┌▼──────────┐
              │ ProductSearcher│  │Inventory│  │Recomm- │  │Shopping   │
              │(product_search │  │Checker  │  │endation│  │Cart       │
              │    .py)        │  │(inventory│  │Engine  │  │(cart.py)  │
              └───────┬────────┘  │  .py)   │  │(recom- │  └─────┬─────┘
                      │           └────┬────┘  │mendations│       │
                      │                │       │  .py)  │        │
                      ▼                ▼       └────────┘        ▼
              ┌──────────────┐  ┌──────────┐            ┌──────────────┐
              │ Azure AI     │  │  Redis   │            │  In-Memory   │
              │ Search       │  │  Cache   │            │  Cart Store  │
              │ (or mock)    │  │(or mock) │            │  (session)   │
              └──────────────┘  └──────────┘            └──────────────┘
                                           │
                                           ▼
                        ┌─────────────────────────────────────────┐
                        │         Azure OpenAI (GPT-4o)           │
                        │     Function calling orchestration      │
                        └─────────────────────────────────────────┘

Prerequisites

  • Python 3.11+
  • Docker and Docker Compose (optional, for containerised deployment)
  • Azure OpenAI resource with gpt-4o deployment (optional — mock mode works without it)
  • Azure AI Search resource (optional — mock mode works without it)
  • Redis (optional — mock fallback is built in)

Local Development Setup

1. Clone and install

cd ecommerce-assistant
python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install -r requirements.txt

2. Configure environment

cp .env.example .env
# Edit .env and fill in your Azure credentials.
# Leave values as "placeholder" to run in mock mode (no Azure required).

3. Start the server

uvicorn src.main:app --reload --port 8000

The API is now available at http://localhost:8000. Interactive docs: http://localhost:8000/docs

4. (Optional) Run with Docker Compose

docker compose -f infra/docker-compose.yml up --build

This starts the FastAPI app and a Redis container with health checks.


Indexing the Product Catalogue (Azure AI Search)

When Azure AI Search credentials are configured, run the indexer once to populate the search index with vector embeddings:

python -m indexer.index_catalogue

Running the Tests

pytest tests/ -v

No Azure credentials are required — all Azure calls are mocked.


API Reference

POST /api/v1/chat

Send a natural language shopping query.

Request body:

{
  "message": "Show me blue running shoes under £100 in size 10",
  "session_id": "my-session-id",
  "customer_id": "customer-001"
}

Response:

{
  "response": "I found 2 blue running shoes under £100 in size 10 ...",
  "session_id": "my-session-id",
  "cart_items": 0,
  "total": 0.0
}

GET /api/v1/cart/{session_id}

Retrieve cart contents for a session.

DELETE /api/v1/cart/{session_id}

Clear all items from a session's cart.

GET /health

Health check endpoint.


Sample Requests

# Search for blue running shoes under £100 in size 10
curl -X POST http://localhost:8000/api/v1/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "Show me blue running shoes under £100 in size 10", "session_id": "test-123"}'

# Ask for boot recommendations
curl -X POST http://localhost:8000/api/v1/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "What boots do you recommend?", "session_id": "test-123", "customer_id": "customer-001"}'

# Add an item to cart
curl -X POST http://localhost:8000/api/v1/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "Add the Nike Air Max in size 9 to my cart", "session_id": "test-123"}'

# View cart
curl http://localhost:8000/api/v1/cart/test-123

# Clear cart
curl -X DELETE http://localhost:8000/api/v1/cart/test-123

Project Structure

ecommerce-assistant/
├── src/
│   ├── __init__.py
│   ├── main.py                  # FastAPI entry point
│   ├── agent.py                 # Shopping agent with tool-calling loop
│   ├── tools.py                 # 4 OpenAI function schemas
│   ├── product_search.py        # Azure AI Search / mock product search
│   ├── inventory.py             # Redis-cached inventory checks
│   ├── recommendations.py       # Purchase history recommendations
│   ├── cart.py                  # Shopping cart CRUD
│   ├── prompts.py               # System prompt
│   ├── config.py                # Settings (pydantic-settings)
│   └── models.py                # Pydantic data models
├── indexer/
│   ├── index_catalogue.py       # Azure AI Search indexing script
│   └── sample_products.json     # 20 sample products
├── tests/
│   ├── __init__.py
│   └── test_agent.py            # pytest-asyncio test suite
├── infra/
│   ├── Dockerfile
│   └── docker-compose.yml       # App + Redis
├── .env.example
├── requirements.txt
└── README.md

Mock Mode

When AZURE_OPENAI_API_KEY or AZURE_SEARCH_API_KEY is set to "placeholder" (the default), the system operates entirely on local mock data:

  • Product search — filters the built-in MOCK_PRODUCTS dict (20 products)
  • Inventory — reads from MOCK_INVENTORY (no Redis required)
  • Recommendations — uses MOCK_PURCHASE_HISTORY
  • Cart — in-memory dict (lost on restart)

This allows full local development and testing without any cloud resources.


Book Reference

This project is Project 8 from Chapter 20 of "Prompt to Production" by Maneesh Kumar Published 2026 — demonstrating production-grade agentic AI patterns with Azure OpenAI function calling.

About

Conversational AI shopping assistant for e-commerce — product discovery, recommendations, and order support powered by Azure OpenAI and Semantic Kernel.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors