Bangla-first AI commerce intelligence for marketplace sellers.
BazarSync AI helps sellers monitor products, competitors, pricing, inventory, revenue, dead stock, promotions, alerts, and business memory from one dashboard. The backend uses deterministic business services for financial and operational calculations, while the Copilot layer combines seller-scoped current data with RAG memory for grounded business answers.
Production note: this repository is a full-stack application. Deploy the FastAPI backend, worker, PostgreSQL/pgvector database, Redis, and Vite frontend as separate production services unless your hosting platform explicitly supports the full stack.
- Features
- Architecture
- Tech Stack
- Repository Structure
- Local Development
- Environment Variables
- Database and Migrations
- AI Copilot and RAG Memory
- Telegram Alerts
- Testing and Quality Gates
- Deployment
- Security Notes
- Troubleshooting
- Product catalog and marketplace listing management.
- Competitor monitoring, rankings, price events, and market opportunities.
- Inventory intelligence with stockout, reorder, aging, and risk workflows.
- Pricing analytics, MarginGuard checks, dynamic pricing recommendations, and marketplace fee policies.
- Sales import with transaction unit price, line total, status, currency, and sales coverage tracking.
- Revenue and estimated profit analytics with deterministic backend calculations.
- Dead-stock classification, capital recovery estimates, and recovery recommendations.
- Alerts, recommendations, and realtime dashboard activity.
- Seller-scoped chat widget on dashboard pages.
- Dynamic planner for broad business questions.
- Allowlisted tools and allowlisted metric execution only.
- Hybrid answers from current database facts plus seller-scoped RAG memory.
- Evidence, confidence, missing-data, freshness, tool usage, and action proposal metadata.
- Action proposal flow for price changes, promotions, and reorder plans without live marketplace execution.
- Conversation hydration with rich assistant-message metadata.
- Seller-scoped memory documents and chunks.
- Product, sales, inventory, pricing, competitor, recommendation, alert, promotion, and Copilot-history memory sources where implemented.
- pgvector-backed vector search with seller filtering.
- Bounded retrieval with deleted/stale document exclusion by default.
- Local BGE embedding support through SentenceTransformers.
- Telegram connection and alert delivery.
- Product intelligence snapshots.
- Celery worker workflows for scheduled and event-driven intelligence jobs.
- Redis-backed async task queue.
flowchart TD
UI["React + Vite Frontend"] --> API["FastAPI Backend"]
API --> Auth["JWT Seller Auth"]
API --> DB[("PostgreSQL + pgvector")]
API --> Redis[("Redis")]
Redis --> Worker["Celery Worker"]
Worker --> DB
API --> Groq["Groq API<br/>backend only"]
API --> Telegram["Telegram Bot API"]
subgraph Copilot["AI Business Copilot"]
Planner["Question Planner"]
Catalogs["Business + Metric Catalogs"]
Tools["Allowlisted Tools"]
Metrics["MetricExecutionService"]
RAG["Seller-scoped RAG Retrieval"]
Validator["Response Validator"]
end
API --> Planner
Planner --> Catalogs
Catalogs --> Tools
Tools --> Metrics
Tools --> RAG
Metrics --> DB
RAG --> DB
Tools --> Validator
Validator --> API
Copilot safety model:
Seller question
-> planner
-> business data catalog + metric catalog
-> allowlisted tools + MetricExecutionService
-> seller-scoped deterministic analytics
-> seller-scoped RAG memory retrieval
-> hybrid answer engine
-> evidence/confidence/missing-data validation
-> frontend widget
The Copilot must not generate SQL, execute arbitrary SQL, expose the Groq key, directly mutate marketplace state, or bypass seller ownership checks.
- Python 3.14
- FastAPI
- SQLAlchemy
- Alembic
- PostgreSQL with pgvector for RAG memory
- Redis
- Celery
- Socket.IO
- Groq SDK
- SentenceTransformers / BGE embeddings
- Playwright and scraping utilities
- React 19
- Vite 8
- TypeScript 6
- Tailwind CSS
- TanStack Query
- TanStack Table
- React Router
- Recharts
- Socket.IO client
- Vitest and Testing Library
.
├── backend/
│ ├── alembic/ # Database migrations
│ ├── app/
│ │ ├── api/ # FastAPI routes
│ │ ├── core/ # Config, security, Celery, Socket.IO
│ │ ├── db/ # Database session and vector helpers
│ │ ├── models/ # SQLAlchemy models
│ │ ├── repositories/ # Data-access helpers
│ │ ├── schemas/ # Pydantic schemas
│ │ ├── services/ # Business services and Copilot/RAG logic
│ │ ├── tasks/ # Celery tasks
│ │ └── main.py # FastAPI app entrypoint
│ ├── scripts/ # Local operational scripts
│ └── tests/ # Backend test suite
├── frontend/
│ ├── src/
│ │ ├── components/ # UI components
│ │ ├── hooks/ # React Query and UI hooks
│ │ ├── pages/ # Route pages
│ │ ├── routes/ # Router and nav config
│ │ ├── services/ # API clients
│ │ ├── types/ # Frontend types
│ │ └── utils/ # Formatters and helpers
│ └── package.json
├── docs/ # Phase notes and implementation summaries
├── docker-compose.yml
└── README.md
- Python 3.14
- Node.js compatible with the frontend lockfile/environment
- npm
- PostgreSQL
- pgvector extension for RAG features
- Redis
- Optional: Telegram bot token
- Optional: Groq API key
git clone https://github.com/DevNexus09/BazarSync-AI.git
cd BazarSync-AI
cp .env.example .envSet at least these backend values in .env:
APP_ENV=development
DATABASE_URL=postgresql+psycopg2://USER:PASSWORD@localhost:5432/bazarsync_ai
JWT_SECRET_KEY=replace_with_a_long_random_secret
VITE_API_BASE_URL=http://127.0.0.1:8000/api/v1redis-serverOr with Docker:
docker compose up -d rediscd backend
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python scripts/check_local_setup.py
python -m alembic upgrade head
python -m uvicorn app.main:app --reload --host 127.0.0.1 --port 8000Useful backend URLs:
http://127.0.0.1:8000http://127.0.0.1:8000/docshttp://127.0.0.1:8000/api/v1/health
In a second terminal:
cd backend
source venv/bin/activate
celery -A app.core.celery_app:celery_app worker --loglevel=infoIn a third terminal:
cd frontend
npm install
npm run devOpen:
http://127.0.0.1:5173
Use .env.example as the source of truth. Important groups are listed below.
| Variable | Required | Notes |
|---|---|---|
APP_ENV |
Recommended | development, test, or production environment name. |
DATABASE_URL |
Yes | SQLAlchemy database URL. |
JWT_SECRET_KEY |
Yes | Backend-only signing key. Use a long random value. |
JWT_ALGORITHM |
No | Defaults to HS256. |
ACCESS_TOKEN_EXPIRE_MINUTES |
No | Defaults to 60. |
CORS_ORIGINS |
Production | Comma-separated allowed frontend origins. |
DB_BOOTSTRAP_ON_STARTUP |
No | Development helper. Prefer migrations in production. |
| Variable | Required | Notes |
|---|---|---|
VITE_API_BASE_URL |
Yes | Public FastAPI API base URL, usually ending in /api/v1. |
Never define VITE_GROQ_API_KEY, VITE_SUPABASE_KEY, or any frontend variable containing backend credentials.
| Variable | Required | Notes |
|---|---|---|
GROQ_API_KEY |
For Groq features | Backend-only. |
GROQ_MODEL |
No | Default non-RAG Groq model. |
RAG_ENABLED |
No | Enables authenticated RAG endpoint. |
RAG_GROQ_MODEL |
For RAG answers | Model used by grounded RAG answer generation. |
RAG_EMBEDDING_PROVIDER |
No | Defaults to local_bge. |
RAG_EMBEDDING_MODEL |
No | Defaults to BAAI/bge-m3. |
RAG_EMBEDDING_DEVICE |
No | cpu, mps, or supported accelerator. |
RAG_RETRIEVAL_TOP_K |
No | Bounded retrieval default. |
RAG_MIN_SIMILARITY |
No | Retrieval threshold. |
| Variable | Required | Notes |
|---|---|---|
REDIS_URL |
Recommended | Redis connection URL. |
REDIS_BROKER_URL |
Recommended | Celery broker URL; falls back to REDIS_URL. |
SCRAPER_SCHEDULE_ENABLED |
No | Enables scheduled scraper jobs. |
RAG_MEMORY_SCHEDULE_ENABLED |
No | Enables scheduled memory indexing. |
INVENTORY_INTELLIGENCE_SCHEDULE_ENABLED |
No | Enables scheduled inventory jobs. |
DEAD_STOCK_WORKFLOW_SCHEDULE_ENABLED |
No | Enables scheduled dead-stock workflows. |
| Variable | Required | Notes |
|---|---|---|
PASSWORD_RESET_ENABLED |
No | Defaults to enabled. |
PASSWORD_RESET_CODE_SECRET |
Production | Required outside development/test. |
EMAIL_PROVIDER |
Production | Use smtp outside local development. |
SMTP_HOST |
Production | SMTP host. |
SMTP_FROM_EMAIL |
Production | Sender address. |
SMTP_USERNAME / SMTP_PASSWORD |
As needed | SMTP credentials. |
| Variable | Required | Notes |
|---|---|---|
TELEGRAM_BOT_TOKEN |
For Telegram | Bot token from BotFather. |
TELEGRAM_WEBHOOK_URL |
Production webhook | Public HTTPS URL ending in /api/v1/telegram/webhook. |
TELEGRAM_WEBHOOK_SECRET |
Recommended | Telegram secret token validation. |
Run migrations from backend/:
source venv/bin/activate
python -m alembic upgrade headValidate latest migration reversibility when changing schema:
python -m alembic downgrade -1
python -m alembic upgrade headRAG memory requires PostgreSQL pgvector. The migration attempts Supabase's extension schema first:
CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA extensions;If unavailable, local PostgreSQL can use:
CREATE EXTENSION IF NOT EXISTS vector;Use a pgvector-enabled PostgreSQL image or install pgvector for your local PostgreSQL version before running RAG migrations.
The Copilot is designed around production safety rules:
- Seller identity comes from backend authentication context.
- Product-specific actions require product ownership checks.
- Groq never queries the database directly.
- Groq does not generate SQL.
- Arbitrary SQL is not accepted by Copilot.
- Only registered tools and registered metrics can execute.
- Financial calculations are deterministic backend calculations.
- Revenue uses transaction unit price or line total where available.
- Profit is blocked or marked partial when cost or marketplace fee policy is missing.
- Direct current data overrides stale memory.
- RAG memory retrieval is seller-filtered.
- Answers include evidence, confidence, missing data, freshness, tools, and domains when available.
- Marketplace changes are proposals only unless a separate execution layer is explicitly implemented and tested.
Core backend modules:
backend/app/services/copilot/question_planner.pybackend/app/services/copilot/tool_registry.pybackend/app/services/copilot/metric_catalog.pybackend/app/services/copilot/metric_execution_service.pybackend/app/services/copilot/hybrid_answer_engine.pybackend/app/services/copilot/copilot_response_validator.pybackend/app/services/rag_retrieval_service.pybackend/app/services/rag_memory_document_builder.pybackend/app/repositories/rag_memory_repository.py
Main endpoints:
POST /api/v1/copilot/askGET /api/v1/copilot/conversationsGET /api/v1/copilot/conversations/{conversation_id}GET /api/v1/copilot/toolsPOST /api/v1/rag/askGET /api/v1/rag/coverage
Telegram support includes connection management, alert delivery, and product intelligence snapshots.
Product snapshot endpoint:
POST /api/v1/telegram/send-product-snapshot
Example:
curl -X POST http://localhost:8000/api/v1/telegram/send-product-snapshot \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{"product_id":"PRODUCT_UUID"}'If TELEGRAM_WEBHOOK_URL is configured, the backend registers the webhook on startup.
Run from backend/:
source venv/bin/activate
pytest -q
pytest tests/api/test_copilot.py tests/api/test_rag.py -q
pytest tests/test_copilot_*.py -q
pytest tests/unit/rag tests/integration/rag -qOptional quality checks if installed/configured:
ruff check .
ruff format --check .
mypy appRun from frontend/:
npm run lint
npm run typecheck
npm test -- --run
npm run buildCurrent known frontend lint warning: TanStack Table can trigger a React Compiler react-hooks/incompatible-library warning in the dashboard competitor table. It is a warning, not a build failure.
- Frontend static app: Vercel, Netlify, Cloudflare Pages, or equivalent.
- Backend API: container platform, VM, Railway/Fly/Render, ECS, or equivalent.
- PostgreSQL with pgvector.
- Redis.
- Celery worker process.
- Optional Celery beat process if scheduled jobs are enabled.
- Set
APP_ENVto the production environment name. - Set strong
JWT_SECRET_KEYandPASSWORD_RESET_CODE_SECRET. - Set production
DATABASE_URL. - Ensure pgvector is available before RAG migrations.
- Run
alembic upgrade head. - Set
CORS_ORIGINSto the real frontend origin. - Set Redis variables for Celery.
- Configure
GROQ_API_KEYonly on the backend if AI answer generation is enabled. - Configure SMTP if password reset is enabled.
- Configure Telegram webhook variables if Telegram is enabled.
- Run backend tests and migration downgrade/upgrade validation before release.
- Set
VITE_API_BASE_URLto the public backend API base URL. - Do not set backend secrets as
VITE_*variables. - Run
npm run lint,npm run typecheck,npm test -- --run, andnpm run build. - Deploy the generated
frontend/distoutput.
The included docker-compose.yml provides backend API, worker, and Redis services. It does not replace a production PostgreSQL/pgvector plan.
docker compose up --build- Frontend currently stores JWTs in
localStorage. For production, prefer httpOnly secure cookies or another mitigated token strategy, and enforce a strong CSP. - Groq and SMTP credentials must remain backend-only.
- Do not expose Supabase service keys, database URLs, embeddings, raw prompts, or internal stack traces to the frontend.
- Use migrations for schema changes in production. Avoid startup bootstrapping in multi-instance deployments.
- Keep seller isolation enforced in every service and query path.
- Marketplace mutations must stay behind explicit, tested execution layers. Copilot approval currently represents internal proposal approval, not live marketplace execution.
- Validate all uploaded/imported business data before analytics use.
If Celery fails with:
Error 61 connecting to localhost:6379. Connection refused.
Start Redis:
redis-serveror:
docker compose up -d redisInstall pgvector or use a pgvector-enabled PostgreSQL image, then rerun:
cd backend
source venv/bin/activate
python -m alembic upgrade headThe backend should return safe fallback behavior for supported endpoints. Set:
GROQ_API_KEY=...
RAG_GROQ_MODEL=openai/gpt-oss-120bOnly set these on the backend.
Check:
VITE_API_BASE_URL=http://127.0.0.1:8000/api/v1Then restart the Vite dev server.
No license file is currently included in this repository. Add a license before publishing or distributing the project publicly.