Skip to content

A production-ready, AI-powered e-commerce support chatbot featuring MCP-style tool execution, Generative UI (GenUI), Universal Commerce Protocol (UCP), and RAG-based vector search.

Notifications You must be signed in to change notification settings

Aparnap2/smart_commerce_agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Smart Commerce Agent

A production-ready, AI-powered e-commerce support chatbot featuring MCP-style tool execution, Generative UI (GenUI), Universal Commerce Protocol (UCP), and RAG-based vector search.

⚠️ Important: This is NOT Vercel AI SDK

This project uses OpenAI SDK directly with Ollama's OpenAI-compatible API for local LLM inference. The repository name is historical - it no longer uses Vercel AI SDK.


✨ Features

  • Conversational AI: Natural support for orders, products, support tickets, and refunds
  • MCP-Style Tools: Modular tool execution with db_query, web_search (RAG), and semantic_search (vector)
  • GenUI (Generative UI): Dynamic product cards, tool call displays, and data visualizations
  • UCP Protocol: Universal Commerce Protocol for standardized e-commerce interactions
  • RAG + Vector Search: PostgreSQL with pgvector for semantic product search
  • Multi-Agent Orchestration: LangGraph-based supervisor agent for complex workflows
  • Streaming Responses: Real-time SSE streaming for instant UX
  • Comprehensive Testing: E2E with Playwright, LLM evaluation with Ollama

πŸ› οΈ Tech Stack

Layer Technology
Frontend Next.js 15, React 19, Tailwind CSS, framer-motion, sonner
Backend OpenAI SDK + Ollama (local) or Google Gemini
Database PostgreSQL + Prisma ORM + pgvector
AI Framework LangGraph (disabled), LangChain
Protocols MCP-style tools, UCP, AP2
State Redis (checkpoints), Zustand
Testing Playwright (E2E), Jest (unit), Ollama (LLM eval)

πŸ“ Project Structure

β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ chat/
β”‚   β”‚   β”‚   β”œβ”€β”€ route.ts              # Main chat API (OpenAI SDK + MCP tools)
β”‚   β”‚   β”‚   └── langgraph/route.ts    # LangGraph API (disabled)
β”‚   β”‚   β”œβ”€β”€ agent/route.ts            # Multi-agent supervisor
β”‚   β”‚   └── refunds/webhook/route.ts  # Stripe refund webhook
β”‚   └── dashboard/                     # Admin dashboard with GenUI
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ agents/                        # LangGraph agents
β”‚   β”‚   β”œβ”€β”€ supervisor.ts              # Agent orchestration
β”‚   β”‚   β”œβ”€β”€ refund.ts                  # Refund processing agent
β”‚   β”‚   β”œβ”€β”€ ui.ts                      # UI generation agent
β”‚   β”‚   └── state.ts                   # Agent state types
β”‚   β”œβ”€β”€ auth/                          # Authentication
β”‚   β”œβ”€β”€ components/                    # React components (GenUI)
β”‚   β”œβ”€β”€ mcp/                           # MCP-style tools
β”‚   β”‚   β”œβ”€β”€ tools.ts                   # Tool definitions
β”‚   β”‚   β”œβ”€β”€ server.ts                  # Tool server
β”‚   β”‚   β”œβ”€β”€ adapter.ts                 # Prisma adapter
β”‚   β”‚   └── rag-tools.ts               # RAG-based tools
β”‚   β”œβ”€β”€ rag/                           # RAG + Vector search
β”‚   β”œβ”€β”€ redis/                         # Redis checkpointer
β”‚   β”œβ”€β”€ schemas/                       # Zod validation
β”‚   β”œβ”€β”€ stores/                        # Zustand stores
β”‚   β”œβ”€β”€ stripe/                        # Payment processing
β”‚   β”œβ”€β”€ ucp/                           # Universal Commerce Protocol
β”‚   └── search/                        # Hybrid search
β”œβ”€β”€ prisma/
β”‚   └── schema.prisma                  # Database schema + pgvector
β”œβ”€β”€ scripts/
β”‚   └── llm_eval.py                    # LLM evaluation with Ollama
└── tests/
    β”œβ”€β”€ e2e/                           # Playwright E2E tests
    └── unit/                          # Jest unit tests

πŸš€ Quick Start

1. Clone and install

git clone https://github.com/Aparnap2/smart_commerce_agent.git
cd vercel-ai-sdk
pnpm install

2. Start PostgreSQL with pgvector

docker run -d --name postgres \
  -e POSTGRES_PASSWORD=postgres \
  -e POSTGRES_DB=smart_commerce \
  -p 5432:5432 \
  pgvector/pgvector:pg17

# Initialize Prisma
npx prisma db push
npx prisma generate

3. Start Redis (optional, for LangGraph)

docker run -d --name redis \
  -p 6379:6379 \
  redis:alpine

4. Configure environment

# .env.local
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/smart_commerce"
REDIS_URL="redis://localhost:6379"
OLLAMA_BASE_URL="http://localhost:11434"
OLLAMA_MODEL="qwen2.5-coder:3b"

5. Start Ollama

ollama pull qwen2.5-coder:3b
ollama serve

6. Run the app

pnpm dev

Open http://localhost:3000 and try:

  • "Show me my orders for john@example.com"
  • "What laptops do you have?"
  • "Recommend a laptop for programming"
  • "What's your return policy?"

🧠 Architecture

Chat Flow (MCP-Style Tools)

User Message
    ↓
Tool Detection (regex-based)
    ↓
Execute MCP Tools (parallel):
  - db_query β†’ Prisma ORM
  - web_search β†’ RAG document search
  - semantic_search β†’ pgvector similarity
    ↓
Build System Prompt + Tool Context
    ↓
Send to LLM (Ollama/OpenAI-compatible)
    ↓
Stream SSE Response + GenUI rendering

MCP Tool Reference

Tool Purpose Backend
db_query Query orders, products, customers, tickets Prisma ORM
web_search Search policies, FAQs (RAG) PostgreSQL full-text
semantic_search Product recommendations pgvector

UCP Protocol (Universal Commerce Protocol)

Standardized commerce interactions:

import { UCPMessage, UCPAction, UCPProduct } from '@/lib/ucp';

// UCP message structure
const ucpMessage: UCPMessage = {
  version: '1.0',
  action: UCPAction.SEARCH_PRODUCTS,
  payload: {
    query: 'laptop',
    filters: { priceRange: [500, 2000] },
  },
  timestamp: Date.now(),
};

GenUI (Generative UI)

Dynamic React components rendered based on LLM responses:

  • ProductCard - Dynamic product displays with pricing
  • ToolCallDisplay - MCP tool execution visualization
  • DataVisualization - Charts and graphs from data

πŸ§ͺ Testing

E2E Tests (Playwright)

# Run all E2E tests
pnpm test:e2e

# Run with UI
pnpm test:e2e --ui

# Run specific test
pnpm test:e2e --project=chromium "GenUI*"

Test Coverage:

  • GenUI component rendering
  • MCP tool execution
  • UCP commerce flows
  • Error handling & edge cases
  • Mobile responsiveness

Unit Tests (Jest)

# Run unit tests
pnpm test:unit

# Run with coverage
pnpm test:unit --coverage

LLM Evaluation (Ollama)

# Run LLM evaluation suite
source .venv/bin/activate
python scripts/llm_eval.py

# Use different model
OLLAMA_MODEL="lfm2.5-thinking:1.2b" python scripts/llm_eval.py

Metrics Evaluated:

  • Tool Correctness: 80%
  • Answer Relevancy: 44%
  • Faithfulness: 38%
  • GEval Correctness: 72%

πŸ“Š Database Schema

// PostgreSQL with pgvector for semantic search

model Customer {
  id             Int     @id @default(autoincrement())
  email          String  @unique
  name           String?
  phone          String?
  orders         Order[]
  supportTickets SupportTicket[]
  preferences    Json?
}

model Order {
  id             Int      @id @default(autoincrement())
  customerId     Int
  customer       Customer @relation(fields: [customerId], references: [id])
  productId      Int
  product        Product  @relation(fields: [productId], references: [id])
  total          Float
  status         String
  paymentStatus  String?
  trackingNumber String?
  orderDate      DateTime @default(now())
}

model Product {
  id          Int     @id @default(autoincrement())
  name        String
  description String?
  price       Float
  stock       Int
  category    String?
  // pgvector embedding for semantic search
  embedding   Unsupported("vector(384)")?
}

model SupportTicket {
  id          Int      @id @default(autoincrement())
  customerId  Int
  customer    Customer @relation(fields: [customerId], references: [id])
  issue       String
  status      String
  priority    String?
  createdAt   DateTime @default(now())
}

πŸ”§ Environment Variables

Variable Required Description
DATABASE_URL Yes PostgreSQL connection string
REDIS_URL No Redis for LangGraph checkpoints
OLLAMA_BASE_URL Dev Ollama server (default: http://localhost:11434)
OLLAMA_MODEL Dev Model name (default: qwen2.5-coder:3b)
OPENAI_API_KEY Prod OpenAI API key
GOOGLE_GENERATIVE_AI_API_KEY Prod Google Gemini API key
STRIPE_SECRET_KEY Prod Stripe secret key

πŸ“ API Reference

POST /api/chat

Request:

{
  "messages": [
    { "role": "user", "content": "Show me my orders for john@example.com" }
  ]
}

Response: Server-Sent Events (SSE)

data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"content":"Here"}}]}
data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"content":" are"}}]}
...
data: [DONE]

POST /api/agent

Multi-agent supervisor for complex workflows.

POST /api/refunds/webhook

Stripe refund webhook handler.


πŸ”’ Security

  • All tool inputs validated with Zod schemas
  • User context enforced on all database queries
  • Environment variables validated at startup
  • Prepared statements via Prisma (no SQL injection)
  • Rate limiting on all API endpoints

πŸ“„ License

MIT


πŸ‘€ Author

Aparnap2 GitHub

About

A production-ready, AI-powered e-commerce support chatbot featuring MCP-style tool execution, Generative UI (GenUI), Universal Commerce Protocol (UCP), and RAG-based vector search.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •