Skip to content

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

ShopGraph AI

Compatibility-first e-commerce shopping agent powered by LangGraph and Neo4j GraphRAG.

A production-oriented reference implementation for PC component shopping. It includes complete source, a browser storefront, a synthetic catalog, an optional OpenAI structured-intent planner and semantic retrieval, deterministic compatibility validation, a bounded bundle optimizer, an evaluator/retry loop, and Docker deployment. It is not a certified production service or a live merchant integration.

Product experience

  • Discover components using lexical search, optional embeddings, and typed graph expansion.
  • Check existing components against specification evidence, with explicit pass/fail/unknown outcomes.
  • Find compatible additional components given selected SKUs.
  • Assemble eight-part PC configurations under a specified INR budget; the solver optimizes total price, not performance.
  • Inspect catalog facts, graph paths, sources, rule results and request trace IDs.
  • No payment, orders, inventory reservations or customer accounts are implemented.

Architecture

flowchart TD
    UI[Browser storefront] --> API[FastAPI API]
    API --> S[LangGraph supervisor / intent planner]
    S --> R[Search agent subgraph]
    S --> C[Compatibility agent subgraph]
    R --> X[Lexical + vector seed retrieval]
    X --> G[Neo4j graph expansion]
    G --> F[Stock and budget filter]
    C --> P[SKU resolution]
    P --> V[Deterministic rules]
    F --> J[Barrier join / synthesizer]
    V --> J
    J --> B[Bounded bundle solver if build mode]
    B --> E[Evaluator]
    E -->|Broaden search once| S2[Replan]
    S2 --> R
    S2 --> C
    E -->|Verified response| UI
    classDef gateway fill:#e2e8f0,stroke:#64748b,color:#0f172a
    classDef planner fill:#ede9fe,stroke:#7c3aed,color:#312e81
    classDef retrieval fill:#dbeafe,stroke:#2563eb,color:#172554
    classDef validation fill:#dcfce7,stroke:#16a34a,color:#14532d
    classDef evalnode fill:#fef3c7,stroke:#d97706,color:#78350f
    class UI,API gateway
    class S,S2 planner
    class R,X,G,F,J,B retrieval
    class C,P,V validation
    class E evalnode
Loading

The two specialist subgraphs run in parallel with isolated input/output mappings. add_edge(["search_agent", "compatibility_agent"], "synthesizer") is a barrier: both workers finish before synthesis. The evaluator revalidates returned products and bundles; errors fail closed. A no-match search may broaden once with a visible warning. There is no unbounded autonomous loop.

Knowledge graph

flowchart LR
    CPU[Product: CPU-A5] -->|HAS_ATTRIBUTE| AM5[Attribute: socket:AM5]
    MB[Product: MB-AM] -->|HAS_ATTRIBUTE| AM5
    MB -->|HAS_ATTRIBUTE| DDR5[Attribute: memory:DDR5]
    RAM[Product: RAM-5L] -->|HAS_ATTRIBUTE| DDR5
    CPU ---|PAIR_RULES_PASS| MB
    MB ---|PAIR_RULES_PASS| RAM
    CPU -->|IN_CATEGORY| CAT[Category: cpu]
Loading

Nodes: Product, Attribute, Category. Relations: HAS_ATTRIBUTE, IN_CATEGORY, PAIR_RULES_PASS. A pair edge only means all modeled pair rules passed; it does not establish whole-build compatibility. Graph facts are traceable to the synthetic catalog source URI. Full-text and vector ranked lists are fused by reciprocal rank; graph expansion adds shared attribute and pair-rule neighbors. Candidate graph paths are not themselves proof of compatibility. The rule engine validates recommendations independently.

GraphRAG generation is deterministic and evidence-grounded: the optional LLM extracts intent, embeddings provide semantic seeds, and server-side synthesis renders catalog facts and rule results. This intentionally avoids uncontrolled LLM-generated specification claims. No LLM-generated Cypher is executed.

Visual walkthrough

PC compatibility graph

The following conceptual view shows the relationships checked while constructing a PC build. Shared attributes provide retrieval evidence; the rule engine separately verifies socket, memory, clearance, power and other modeled constraints. The PC Build node represents an assembled configuration, not an additional Neo4j schema label.

flowchart LR
    CPU["CPU"] -->|HAS_ATTRIBUTE| S["AM5 socket"]
    MB["Motherboard"] -->|HAS_ATTRIBUTE| S
    MB -->|HAS_ATTRIBUTE| M["DDR5 standard"]
    RAM["DDR5 RAM"] -->|HAS_ATTRIBUTE| M
    CPU ---|Socket fit| COOL["CPU cooler"]
    CPU ---|Socket fit| MB
    MB ---|Memory support| RAM
    COOL ---|Height clearance| CASE["Case"]
    GPU["Graphics card"] ---|Length clearance| CASE
    PSU["Power supply"] ---|Power requirements| GPU
    MB --> BUILD["PC build"]
    CPU --> BUILD
    RAM --> BUILD
    COOL --> BUILD
    GPU --> BUILD
    PSU --> BUILD
    CASE --> BUILD
    SSD["Storage"] --> BUILD
    classDef product fill:#dbeafe,stroke:#2563eb,color:#172554
    classDef attribute fill:#ede9fe,stroke:#7c3aed,color:#312e81
    classDef config fill:#dcfce7,stroke:#16a34a,color:#14532d
    class CPU,MB,RAM,COOL,CASE,GPU,PSU,SSD product
    class S,M attribute
    class BUILD config
Loading

Shopping experience: compatible PC under ₹85,000

Illustrative output from the original bundle solver run on the synthetic catalog. Prices are demo data, not live retail offers.

pie showData
    title Budget allocation (INR)
    "Selected components" : 64400
    "Remaining budget" : 20600
Loading
Component Demo product Price (INR)
Processor Aster A5 14,000
Motherboard Aster B650 Compact 11,500
Memory Orbit 16GB DDR5 4,600
Graphics card Nova 8GB 22,000
Power supply Pulse 450W 3,200
Case Frame Compact 3,400
Cooler Breeze 120 1,900
Storage Flash 1TB SATA 3,800
Total 27 modeled compatibility checks passed in the original example 64,400
Remaining 24.2% of budget 20,600
flowchart LR
    Q["Build a gaming PC under INR 85,000"] --> RET["Retrieve product candidates"]
    RET --> RULE["Check modeled compatibility constraints"]
    RULE --> OPT["Optimize total price"]
    OPT --> VAL["Revalidate complete configuration"]
    VAL --> OUT["INR 64,400 demo build"]
    OUT --> SAVE["INR 20,600 remaining"]
    classDef success fill:#dcfce7,stroke:#16a34a,color:#14532d
    class OUT,SAVE success
Loading

Validation dashboard

Historical generation-time checks, not a claim of current CI status or live deployment certification.

flowchart TB
    V["Generation-time verification"] --> CORE["19 of 19 core tests passed"]
    V --> DATA["27 synthetic catalog products"]
    V --> PY["Python compilation passed"]
    V --> JS["JavaScript syntax check passed"]
    V --> INT["Workflow/API module skipped"]
    INT --> EXT["Live Neo4j, OpenAI, Docker and browser integration unverified"]
    classDef passed fill:#dcfce7,stroke:#16a34a,color:#14532d
    classDef info fill:#dbeafe,stroke:#2563eb,color:#172554
    classDef pending fill:#fef3c7,stroke:#d97706,color:#78350f
    class CORE,PY,JS passed
    class DATA info
    class INT,EXT pending
Loading

Quick start: Neo4j + Docker

Requires Docker Compose v2 and internet access for initial image/dependency downloads.

cp .env.example .env
# Edit .env and replace both password placeholders with strong random values.
# OPENAI_API_KEY is optional; leave empty for lexical + graph retrieval.
docker compose up --build

Open http://localhost:8000 and enter SHOP_API_KEY in the storefront. OpenAPI schema: http://localhost:8000/openapi.json . The interactive Swagger UI is blocked by the strict content-security policy; use the storefront or curl.

The ingestion job creates constraints and indexes, loads products and builds graph relationships before the API starts. Run docker compose run --rm ingest after changing the seed catalog; deploy with maintenance coordination because ingestion is not an atomic versioned catalog swap. Removed SKUs require a deliberate migration; the supplied ingestion upserts rather than deletes products. Set OPENAI_API_KEY before ingestion for semantic indexing. Reingest after changing the embedding model. The default embedding dimensions are 1536.

Local development (no graph database or API key)

python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements-dev.txt
export CATALOG_BACKEND=memory
uvicorn app.main:app --host 127.0.0.1 --port 8000

Memory mode uses an in-process attribute graph and deterministic pair checks, not Neo4j or vector indexing. LangGraph is still required. This mode is suitable for demos and unit tests, not public deployment.

API examples

# Full compatible build; omitted budget is inferred from supported INR syntax.
curl -s http://localhost:8000/api/shop \
  -H 'Content-Type: application/json' -H 'X-API-Key: YOUR_KEY' \
  -d '{"query":"Build a gaming PC under ₹85,000","mode":"build","budget":85000}'

# Incompatible CPU / motherboard
curl -s http://localhost:8000/api/shop \
  -H 'Content-Type: application/json' -H 'X-API-Key: YOUR_KEY' \
  -d '{"query":"Are these compatible?","mode":"check","selected_skus":["CPU-A5","MB-I"]}'

# Find compatible memory for an existing motherboard
curl -s http://localhost:8000/api/shop \
  -H 'Content-Type: application/json' -H 'X-API-Key: YOUR_KEY' \
  -d '{"query":"DDR5 RAM","mode":"search","selected_skus":["MB-A"],"budget":25000}'

Use mode: search explicitly for recommendations anchored to selected SKUs. auto with selected SKUs typically checks them unless intent indicates a build. An explicit budget applies to the total including selected components. Supported inferred budgets include under ₹85,000 and under 85k; arbitrary natural-language currency and constraint parsing is out of scope. GET /api/catalog lists SKUs. GET /health/live, /health/ready and /metrics support operations. API-key header is required when configured.

Repository map

Path Responsibility
app/main.py API, auth, request-size limits, concurrency, health, metrics
app/workflow.py Native LangGraph supervisor, parallel subgraphs, barrier, evaluator
app/ai.py Optional OpenAI structured planner and embeddings
app/catalog.py Memory and Neo4j GraphRAG retrieval, rank fusion
app/ingest.py Graph schema, index and relationship ingestion
app/compatibility.py Deterministic three-valued rules with provenance
app/solver.py Bounded branch-and-bound optimizer
app/models.py Typed validated contracts
app/static/ Responsive browser UI
data/catalog.json 27 synthetic products across eight categories
scripts/generate_catalog.py Reproducible seed generator
tests/ Core and workflow/API tests
docs/ Operational and validation details
compose.yaml, Dockerfile Local graph deployment
.github/workflows/ci.yml Automated dependency install and tests

Verification

python -m pytest -q
python -m compileall -q app scripts tests
node --check app/static/app.js

At generation time: 19 core tests passed, one workflow/API module skipped because LangGraph was unavailable; Python compilation and JavaScript syntax checks passed. The package index in the generation environment did not offer LangGraph/Neo4j dependencies. Live Neo4j, OpenAI, Docker and browser interactions were not verified. CI installs dependencies and runs the full test suite. See docs/VALIDATION.md for reproducible results and integration smoke checks.

Production boundary

The data is synthetic, catalog scale is deliberately small, and compatibility rules are illustrative. Real integrations require manufacturer support lists, BIOS revisions, connector quantities, transient power, clearance interactions, verified pricing/inventory and revisioned catalog feeds. Add authentication/authorization, per-user quotas, distributed admission control, versioned ingestion, TLS, secrets management, audit and privacy controls, dependency locking, performance/load testing, and integration tests before an internet-facing launch. The reference deploys a single Neo4j Community instance, not an HA cluster. There is no persistent user memory or checkout. The app never places orders.

Technical references