backend apis updation
This commit is contained in:
@@ -20,22 +20,17 @@ Usage:
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from collections import defaultdict
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from app.services.vector_store import ensure_brand_schema, upsert_brand_products, _connect, _sanitize_name # noqa: E402
|
||||
from app.services.brand_registry import resolve_parent_brand # noqa: E402
|
||||
from app.services.brand_sync import SEED_DIR, load_seed_catalogs, seed_brands # noqa: E402
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SEED_DIR = Path(__file__).resolve().parents[1] / "data" / "seed_catalogs"
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Seed pgvector with bundled sample catalogs")
|
||||
@@ -63,34 +58,11 @@ def main() -> None:
|
||||
logger.error("Seed directory not found: %s", SEED_DIR)
|
||||
sys.exit(1)
|
||||
|
||||
files = sorted(SEED_DIR.glob("*.json"))
|
||||
if args.only:
|
||||
wanted = [w.lower() for w in args.only]
|
||||
files = [f for f in files if any(w in f.name.lower() for w in wanted)]
|
||||
|
||||
if not files:
|
||||
logger.warning("No matching seed files found in %s", SEED_DIR)
|
||||
return
|
||||
|
||||
# Aggregate products by resolved (canonical) brand so that multiple
|
||||
# Products are aggregated by resolved (canonical) brand so that multiple
|
||||
# seed files contributing to the same brand table are upserted as a
|
||||
# single batch. This ensures the stale-product cleanup (cleanup=True)
|
||||
# doesn't orphan products from a sibling file.
|
||||
brand_products: dict[str, list[dict]] = defaultdict(list)
|
||||
file_brand_map: dict[str, str] = {}
|
||||
|
||||
for f in files:
|
||||
data = json.loads(f.read_text(encoding="utf-8-sig"))
|
||||
products = data.get("products", [])
|
||||
brand = data.get("brand") or (products[0].get("brand_name") if products else None)
|
||||
if not brand or not products:
|
||||
logger.warning("Skipping %s - no brand/products found", f.name)
|
||||
continue
|
||||
|
||||
resolved = resolve_parent_brand(brand)
|
||||
brand_products[resolved].extend(products)
|
||||
file_brand_map[f.name] = resolved
|
||||
logger.info("Read %d products from %s -> resolved brand '%s'", len(products), f.name, resolved)
|
||||
brand_products = load_seed_catalogs(only=args.only)
|
||||
|
||||
if not brand_products:
|
||||
logger.warning("No valid seed data found in %s", SEED_DIR)
|
||||
@@ -99,17 +71,7 @@ def main() -> None:
|
||||
# Do not drop existing brand tables to preserve user database state.
|
||||
# Stale table cleanup disabled per environment requirements.
|
||||
|
||||
|
||||
total = 0
|
||||
for resolved_brand, all_products in brand_products.items():
|
||||
logger.info("Seeding %d product(s) for brand '%s'", len(all_products), resolved_brand)
|
||||
table = ensure_brand_schema(resolved_brand)
|
||||
if not table:
|
||||
logger.error("Could not create/verify table for brand '%s' - is pgvector reachable?", resolved_brand)
|
||||
continue
|
||||
upsert_brand_products(resolved_brand, all_products, cleanup=True)
|
||||
logger.info("Seeded %d products for brand '%s' (table=%s)", len(all_products), resolved_brand, table)
|
||||
total += len(all_products)
|
||||
total = seed_brands(brand_products, cleanup=True)
|
||||
|
||||
print(f"\nDone. Seeded {total} products across {len(brand_products)} brand(s) into pgvector.")
|
||||
print("Start the API with `uvicorn app.main:app --reload` and try a search/chat query.")
|
||||
|
||||
Reference in New Issue
Block a user