Spaces:
Sleeping
Sleeping
initial deploy
Browse files- .gitignore +7 -0
- Dockerfile +16 -0
- app.py +77 -0
- chunker.py +58 -0
- embedder.py +35 -0
- generator.py +58 -0
- pipeline.py +152 -0
- requirements.txt +13 -0
- retriever.py +22 -0
- router.py +37 -0
- shopify_client.py +161 -0
- shopify_ingest.py +133 -0
.gitignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
.env
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
| 5 |
+
*.pyo
|
| 6 |
+
.DS_Store
|
| 7 |
+
schema_cache.json
|
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from pipeline import answer
|
| 5 |
+
import uvicorn
|
| 6 |
+
import asyncio
|
| 7 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
+
|
| 9 |
+
app = FastAPI(
|
| 10 |
+
title="ReplyFlow RAG API",
|
| 11 |
+
description="Multi-brand comment reply API powered by RAG + Shopify",
|
| 12 |
+
version="2.0.0",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"],
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Thread pool for running sync pipeline code in async context
|
| 23 |
+
executor = ThreadPoolExecutor(max_workers=4)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# ββ Request / Response models βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 27 |
+
|
| 28 |
+
class QueryRequest(BaseModel):
|
| 29 |
+
query: str
|
| 30 |
+
channel: str # 'instagram' | 'facebook' | 'shopify' | 'web'
|
| 31 |
+
channel_identifier: str # page_id | shop_domain
|
| 32 |
+
external_id: str | None = None # original comment ID from Meta
|
| 33 |
+
commenter_handle: str | None = None # @username
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class QueryResponse(BaseModel):
|
| 37 |
+
answer: str
|
| 38 |
+
query: str
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ββ Routes ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 42 |
+
|
| 43 |
+
@app.get("/health")
|
| 44 |
+
async def health():
|
| 45 |
+
return {"status": "ok"}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@app.post("/query", response_model=QueryResponse)
|
| 49 |
+
async def query(req: QueryRequest):
|
| 50 |
+
if not req.query.strip():
|
| 51 |
+
raise HTTPException(status_code=400, detail="Query cannot be empty")
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
# Run blocking pipeline in thread pool so it doesn't block the event loop
|
| 55 |
+
loop = asyncio.get_event_loop()
|
| 56 |
+
reply = await loop.run_in_executor(
|
| 57 |
+
executor,
|
| 58 |
+
lambda: answer(
|
| 59 |
+
query = req.query,
|
| 60 |
+
channel = req.channel,
|
| 61 |
+
channel_identifier = req.channel_identifier,
|
| 62 |
+
external_id = req.external_id,
|
| 63 |
+
commenter_handle = req.commenter_handle,
|
| 64 |
+
)
|
| 65 |
+
)
|
| 66 |
+
except ValueError as e:
|
| 67 |
+
raise HTTPException(status_code=404, detail=str(e))
|
| 68 |
+
except Exception as e:
|
| 69 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 70 |
+
|
| 71 |
+
return QueryResponse(answer=reply, query=req.query)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# ββ Dev runner ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)
|
chunker.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 2 |
+
|
| 3 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 4 |
+
chunk_size=500, # characters not words
|
| 5 |
+
chunk_overlap=50, # overlap between chunks
|
| 6 |
+
separators=["\n\n", "\n", ".", "!", "?", ",", " "],
|
| 7 |
+
length_function=len,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
def prepare_policy_chunks(policy: dict) -> list[dict]:
|
| 11 |
+
chunks = splitter.split_text(policy["content"])
|
| 12 |
+
return [
|
| 13 |
+
{
|
| 14 |
+
"brand_id": policy["brand_id"],
|
| 15 |
+
"source_type": "policy",
|
| 16 |
+
"source_id": policy["id"],
|
| 17 |
+
"chunk_index": i,
|
| 18 |
+
"content": chunk,
|
| 19 |
+
"metadata": {
|
| 20 |
+
"policy_type": policy["type"],
|
| 21 |
+
"policy_title": policy["title"]
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
for i, chunk in enumerate(chunks)
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def prepare_faq_chunks(faq: dict) -> list[dict]:
|
| 29 |
+
content = f"Question: {faq['question']}\nAnswer: {faq['answer']}"
|
| 30 |
+
return [
|
| 31 |
+
{
|
| 32 |
+
"brand_id": faq["brand_id"],
|
| 33 |
+
"source_type": "faq",
|
| 34 |
+
"source_id": faq["id"],
|
| 35 |
+
"chunk_index": 0,
|
| 36 |
+
"content": content,
|
| 37 |
+
"metadata": {"category": faq.get("category", "")}
|
| 38 |
+
}
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def prepare_product_chunks(product: dict) -> list[dict]:
|
| 43 |
+
attrs = product.get("attributes", {})
|
| 44 |
+
attr_str = ", ".join(f"{k}: {v}" for k, v in attrs.items())
|
| 45 |
+
content = f"{product['name']} | {product['category']} | {product['description']} | {attr_str} | Price: ${product['base_price']}"
|
| 46 |
+
return [
|
| 47 |
+
{
|
| 48 |
+
"brand_id": product["brand_id"],
|
| 49 |
+
"source_type": "product",
|
| 50 |
+
"source_id": product["id"],
|
| 51 |
+
"chunk_index": 0,
|
| 52 |
+
"content": content,
|
| 53 |
+
"metadata": {
|
| 54 |
+
"category": product.get("category", ""),
|
| 55 |
+
"name": product["name"]
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
]
|
embedder.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
from supabase import create_client
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 9 |
+
|
| 10 |
+
supabase = create_client(
|
| 11 |
+
os.environ["SUPABASE_URL"],
|
| 12 |
+
os.environ["SUPABASE_SERVICE_KEY"]
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
def get_embedding(text: str) -> list[float]:
|
| 16 |
+
return model.encode(text).tolist()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def store_chunks(chunks: list[dict]):
|
| 20 |
+
for chunk in chunks:
|
| 21 |
+
embedding = get_embedding(chunk["content"])
|
| 22 |
+
supabase.table("embeddings").upsert({
|
| 23 |
+
"brand_id": chunk["brand_id"],
|
| 24 |
+
"source_type": chunk["source_type"],
|
| 25 |
+
"source_id": chunk["source_id"],
|
| 26 |
+
"chunk_index": chunk["chunk_index"],
|
| 27 |
+
"content": chunk["content"],
|
| 28 |
+
"embedding": embedding,
|
| 29 |
+
"metadata": chunk["metadata"]
|
| 30 |
+
}).execute()
|
| 31 |
+
print(f" β {len(chunks)} chunks stored")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def delete_chunks_for_source(source_id: str):
|
| 35 |
+
supabase.table("embeddings").delete().eq("source_id", source_id).execute()
|
generator.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from groq import Groq
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def generate_answer(query: str, chunks: list[dict], brand_name: str) -> str:
|
| 12 |
+
context = "\n\n".join(
|
| 13 |
+
f"[{c['source_type'].upper()}]: {c['content']}"
|
| 14 |
+
for c in chunks
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
system_prompt = f"""
|
| 18 |
+
You are a helpful customer support assistant for {brand_name}.
|
| 19 |
+
Answer the user's question using ONLY the context provided below.
|
| 20 |
+
If the context has no relevant information at all, say "I don't have that information."
|
| 21 |
+
Be concise and friendly.
|
| 22 |
+
|
| 23 |
+
CONTEXT:
|
| 24 |
+
{context}
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
response = client.chat.completions.create(
|
| 28 |
+
model="llama-3.3-70b-versatile",
|
| 29 |
+
messages=[
|
| 30 |
+
{"role": "system", "content": system_prompt},
|
| 31 |
+
{"role": "user", "content": query}
|
| 32 |
+
],
|
| 33 |
+
temperature=0.3
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return response.choices[0].message.content.strip()
|
| 37 |
+
|
| 38 |
+
def generate_combined_answer(query: str, rag_context: str, inventory_context: str, brand_name: str) -> str:
|
| 39 |
+
response = client.chat.completions.create(
|
| 40 |
+
model=os.environ.get("GROQ_MODEL", "llama-3.1-8b-instant"),
|
| 41 |
+
messages=[
|
| 42 |
+
{
|
| 43 |
+
"role": "system",
|
| 44 |
+
"content": f"""You are a helpful customer support assistant for {brand_name}.
|
| 45 |
+
Answer the user's question using the knowledge and inventory data provided below.
|
| 46 |
+
Combine both naturally into one cohesive answer. Be concise and friendly.
|
| 47 |
+
|
| 48 |
+
PRODUCT KNOWLEDGE:
|
| 49 |
+
{rag_context}
|
| 50 |
+
|
| 51 |
+
INVENTORY DATA:
|
| 52 |
+
{inventory_context}"""
|
| 53 |
+
},
|
| 54 |
+
{"role": "user", "content": query}
|
| 55 |
+
],
|
| 56 |
+
temperature=0.3
|
| 57 |
+
)
|
| 58 |
+
return response.choices[0].message.content.strip()
|
pipeline.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from router import route_query
|
| 2 |
+
from retriever import retrieve
|
| 3 |
+
from generator import generate_answer, generate_combined_answer
|
| 4 |
+
from shopify_client import get_inventory
|
| 5 |
+
from supabase import create_client
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
supabase = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_SERVICE_KEY"])
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def get_brand_by_channel(channel: str, channel_identifier: str) -> dict:
|
| 15 |
+
"""
|
| 16 |
+
Look up brand via their Instagram page_id, Facebook page_id, or shop domain.
|
| 17 |
+
This is how we know which brand a comment belongs to.
|
| 18 |
+
"""
|
| 19 |
+
result = (
|
| 20 |
+
supabase.table("brand_channels")
|
| 21 |
+
.select("brand_id, channel_identifier, credentials")
|
| 22 |
+
.eq("channel", channel)
|
| 23 |
+
.eq("channel_identifier", channel_identifier)
|
| 24 |
+
.eq("is_active", True)
|
| 25 |
+
.limit(1)
|
| 26 |
+
.execute()
|
| 27 |
+
.data
|
| 28 |
+
)
|
| 29 |
+
if not result:
|
| 30 |
+
raise ValueError(f"No brand found for {channel}: {channel_identifier}")
|
| 31 |
+
|
| 32 |
+
brand_id = result[0]["brand_id"]
|
| 33 |
+
brand = supabase.table("brands").select("*").eq("id", brand_id).single().execute().data
|
| 34 |
+
return brand, result[0]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def get_shopify_credentials(brand_id: str) -> dict:
|
| 38 |
+
result = (
|
| 39 |
+
supabase.table("brand_channels")
|
| 40 |
+
.select("channel_identifier, credentials")
|
| 41 |
+
.eq("brand_id", brand_id)
|
| 42 |
+
.eq("channel", "shopify")
|
| 43 |
+
.eq("is_active", True)
|
| 44 |
+
.limit(1)
|
| 45 |
+
.execute()
|
| 46 |
+
.data
|
| 47 |
+
)
|
| 48 |
+
if not result:
|
| 49 |
+
raise ValueError(f"No Shopify channel found for brand {brand_id}")
|
| 50 |
+
return result[0]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def log_comment(
|
| 54 |
+
brand_id: str,
|
| 55 |
+
channel: str,
|
| 56 |
+
comment_text: str,
|
| 57 |
+
reply_text: str,
|
| 58 |
+
route: str,
|
| 59 |
+
external_id: str = None,
|
| 60 |
+
commenter_handle: str = None,
|
| 61 |
+
):
|
| 62 |
+
supabase.table("comments").insert({
|
| 63 |
+
"brand_id": brand_id,
|
| 64 |
+
"channel": channel,
|
| 65 |
+
"external_id": external_id,
|
| 66 |
+
"commenter_handle": commenter_handle,
|
| 67 |
+
"comment_text": comment_text,
|
| 68 |
+
"reply_text": reply_text,
|
| 69 |
+
"route": route,
|
| 70 |
+
}).execute()
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def answer(
|
| 74 |
+
query: str,
|
| 75 |
+
channel: str, # 'instagram' | 'facebook' | 'shopify' | 'web'
|
| 76 |
+
channel_identifier: str, # page_id | shop_domain
|
| 77 |
+
external_id: str = None,
|
| 78 |
+
commenter_handle: str = None,
|
| 79 |
+
) -> str:
|
| 80 |
+
brand, channel_row = get_brand_by_channel(channel, channel_identifier)
|
| 81 |
+
brand_id = brand["id"]
|
| 82 |
+
brand_name = brand["name"]
|
| 83 |
+
|
| 84 |
+
# Get Shopify credentials separately (always needed for inventory)
|
| 85 |
+
shopify = (
|
| 86 |
+
supabase.table("brand_channels")
|
| 87 |
+
.select("channel_identifier, credentials")
|
| 88 |
+
.eq("brand_id", brand_id)
|
| 89 |
+
.eq("channel", "shopify")
|
| 90 |
+
.eq("is_active", True)
|
| 91 |
+
.limit(1)
|
| 92 |
+
.execute()
|
| 93 |
+
.data
|
| 94 |
+
)
|
| 95 |
+
if not shopify:
|
| 96 |
+
raise ValueError(f"No Shopify channel linked for brand {brand_id}")
|
| 97 |
+
|
| 98 |
+
shop_domain = shopify[0]["channel_identifier"]
|
| 99 |
+
access_token = shopify[0]["credentials"]["access_token"]
|
| 100 |
+
|
| 101 |
+
decision = route_query(query)
|
| 102 |
+
print(f" β Router decision: {decision}")
|
| 103 |
+
|
| 104 |
+
if decision == "INVENTORY":
|
| 105 |
+
inventory_context = get_inventory(shop_domain, access_token)
|
| 106 |
+
reply = generate_answer(
|
| 107 |
+
query = query,
|
| 108 |
+
chunks = [{"source_type": "inventory", "content": inventory_context}],
|
| 109 |
+
brand_name = brand_name,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
elif decision == "RAG":
|
| 113 |
+
chunks = retrieve(query, brand_id, top_k=5)
|
| 114 |
+
reply = generate_answer(query, chunks, brand_name)
|
| 115 |
+
|
| 116 |
+
elif decision == "BOTH":
|
| 117 |
+
chunks = retrieve(query, brand_id, top_k=3)
|
| 118 |
+
rag_context = "\n".join([c["content"] for c in chunks])
|
| 119 |
+
inv_context = get_inventory(shop_domain, access_token)
|
| 120 |
+
reply = generate_combined_answer(query, rag_context, inv_context, brand_name)
|
| 121 |
+
|
| 122 |
+
else:
|
| 123 |
+
reply = "I'm not sure how to answer that."
|
| 124 |
+
decision = "UNKNOWN"
|
| 125 |
+
|
| 126 |
+
log_comment(
|
| 127 |
+
brand_id = brand_id,
|
| 128 |
+
channel = channel,
|
| 129 |
+
comment_text = query,
|
| 130 |
+
reply_text = reply,
|
| 131 |
+
route = decision,
|
| 132 |
+
external_id = external_id,
|
| 133 |
+
commenter_handle = commenter_handle,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
return reply
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
tests = [
|
| 140 |
+
("What is your return policy?", "facebook", "123"),
|
| 141 |
+
("What snowboard is a good fit for older people?", "facebook", "123"),
|
| 142 |
+
("Do you have any snowboards in stock?", "instagram", "123"),
|
| 143 |
+
("Tell me about the Complete Snowboard and is it in stock?", "instagram", "123"),
|
| 144 |
+
]
|
| 145 |
+
|
| 146 |
+
for query, channel, identifier in tests:
|
| 147 |
+
print(f"\nQ ({identifier}): {query}")
|
| 148 |
+
print(f"A: {answer(query, channel, identifier)}")
|
| 149 |
+
|
| 150 |
+
for query, brand in tests:
|
| 151 |
+
print(f"\nQ ({brand}): {query}")
|
| 152 |
+
print(f"A: {answer(query, brand)}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
supabase
|
| 2 |
+
psycopg2-binary
|
| 3 |
+
sentence-transformers
|
| 4 |
+
groq
|
| 5 |
+
python-dotenv
|
| 6 |
+
numpy
|
| 7 |
+
vanna[pgvector]
|
| 8 |
+
langchain-text-splitters
|
| 9 |
+
fastapi
|
| 10 |
+
uvicorn
|
| 11 |
+
requests
|
| 12 |
+
beautifulsoup4
|
| 13 |
+
pydantic
|
retriever.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from supabase import create_client
|
| 2 |
+
from embedder import get_embedding
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
supabase = create_client(
|
| 9 |
+
os.environ["SUPABASE_URL"],
|
| 10 |
+
os.environ["SUPABASE_SERVICE_KEY"]
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def retrieve(query: str, brand_id: str, top_k: int = 5) -> list[dict]:
|
| 14 |
+
query_embedding = get_embedding(query)
|
| 15 |
+
|
| 16 |
+
result = supabase.rpc("match_embeddings", {
|
| 17 |
+
"query_embedding": query_embedding,
|
| 18 |
+
"p_brand_id": brand_id,
|
| 19 |
+
"match_count": top_k,
|
| 20 |
+
"match_threshold": 0.1
|
| 21 |
+
}).execute()
|
| 22 |
+
return result.data
|
router.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from groq import Groq
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 8 |
+
|
| 9 |
+
ROUTER_PROMPT = """
|
| 10 |
+
You are a query router. Classify the user query into one of three categories:
|
| 11 |
+
|
| 12 |
+
INVENTORY β only asking about stock, price, availability, specific variants
|
| 13 |
+
RAG β only asking about policies, FAQs, product info, recommendations
|
| 14 |
+
BOTH β asking about product knowledge AND availability/price/stock in same question
|
| 15 |
+
|
| 16 |
+
Examples:
|
| 17 |
+
"Is size 10 Air Max available?" β INVENTORY
|
| 18 |
+
"What is your return policy?" β RAG
|
| 19 |
+
"Is Air Max good for running and do you have size 10?" β BOTH
|
| 20 |
+
"Tell me about the Galaxy S24 and how much does it cost?"β BOTH
|
| 21 |
+
"How do I assemble KALLAX?" β RAG
|
| 22 |
+
"Do you have KALLAX in black?" β INVENTORY
|
| 23 |
+
|
| 24 |
+
Respond with ONLY one word: INVENTORY or RAG or BOTH
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
def route_query(query: str) -> str:
|
| 28 |
+
response = client.chat.completions.create(
|
| 29 |
+
model=os.environ.get("GROQ_MODEL", "llama-3.1-8b-instant"),
|
| 30 |
+
messages=[
|
| 31 |
+
{"role": "system", "content": ROUTER_PROMPT},
|
| 32 |
+
{"role": "user", "content": query}
|
| 33 |
+
],
|
| 34 |
+
temperature=0
|
| 35 |
+
)
|
| 36 |
+
decision = response.choices[0].message.content.strip().upper()
|
| 37 |
+
return decision if decision in ["INVENTORY", "RAG", "BOTH"] else "RAG"
|
shopify_client.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# shopify_client.py
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
|
| 5 |
+
SHOPIFY_API_VERSION = "2024-01"
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _headers(access_token: str) -> dict:
|
| 9 |
+
return {"X-Shopify-Access-Token": access_token}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def _base_url(shop_domain: str) -> str:
|
| 13 |
+
return f"https://{shop_domain}/admin/api/{SHOPIFY_API_VERSION}"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _strip_html(html: str) -> str:
|
| 17 |
+
"""Shopify descriptions come as HTML β strip to plain text for embedding."""
|
| 18 |
+
if not html:
|
| 19 |
+
return ""
|
| 20 |
+
return BeautifulSoup(html, "html.parser").get_text(separator=" ").strip()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# ββ Ingest time βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
+
|
| 25 |
+
def get_products(shop_domain: str, access_token: str, brand_id: str) -> list[dict]:
|
| 26 |
+
"""
|
| 27 |
+
Pull all products from Shopify and map them to the shape
|
| 28 |
+
prepare_product_chunks() already expects.
|
| 29 |
+
"""
|
| 30 |
+
url = f"{_base_url(shop_domain)}/products.json"
|
| 31 |
+
params = {"limit": 250}
|
| 32 |
+
products = []
|
| 33 |
+
|
| 34 |
+
# Paginate through all products
|
| 35 |
+
while url:
|
| 36 |
+
res = requests.get(url, headers=_headers(access_token), params=params)
|
| 37 |
+
res.raise_for_status()
|
| 38 |
+
data = res.json()
|
| 39 |
+
products.extend(data.get("products", []))
|
| 40 |
+
|
| 41 |
+
# Shopify pagination via Link header
|
| 42 |
+
link = res.headers.get("Link", "")
|
| 43 |
+
url = None
|
| 44 |
+
params = {}
|
| 45 |
+
if 'rel="next"' in link:
|
| 46 |
+
for part in link.split(","):
|
| 47 |
+
if 'rel="next"' in part:
|
| 48 |
+
url = part.split(";")[0].strip().strip("<>")
|
| 49 |
+
|
| 50 |
+
# Map Shopify shape β your chunker shape
|
| 51 |
+
result = []
|
| 52 |
+
for p in products:
|
| 53 |
+
# Collect all variants for attributes
|
| 54 |
+
sizes = list({v["option1"] for v in p["variants"] if v.get("option1")})
|
| 55 |
+
colors = list({v["option2"] for v in p["variants"] if v.get("option2")})
|
| 56 |
+
|
| 57 |
+
attributes = {}
|
| 58 |
+
if sizes: attributes["sizes"] = sizes
|
| 59 |
+
if colors: attributes["colors"] = colors
|
| 60 |
+
|
| 61 |
+
# Tags as a list
|
| 62 |
+
tags = [t.strip() for t in p.get("tags", "").split(",") if t.strip()]
|
| 63 |
+
if tags: attributes["tags"] = tags
|
| 64 |
+
|
| 65 |
+
result.append({
|
| 66 |
+
"id": str(p["id"]), # Shopify product ID as source_id
|
| 67 |
+
"brand_id": brand_id,
|
| 68 |
+
"name": p["title"],
|
| 69 |
+
"description": _strip_html(p.get("body_html", "")),
|
| 70 |
+
"category": p.get("product_type", ""),
|
| 71 |
+
"base_price": float(p["variants"][0]["price"]) if p["variants"] else 0.0,
|
| 72 |
+
"attributes": attributes,
|
| 73 |
+
})
|
| 74 |
+
|
| 75 |
+
print(f" β Pulled {len(result)} products from Shopify")
|
| 76 |
+
return result
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def get_policies(shop_domain: str, access_token: str, brand_id: str) -> list[dict]:
|
| 80 |
+
"""
|
| 81 |
+
Pull store policies from Shopify and map them to the shape
|
| 82 |
+
prepare_policy_chunks() already expects.
|
| 83 |
+
"""
|
| 84 |
+
url = f"{_base_url(shop_domain)}/policies.json"
|
| 85 |
+
res = requests.get(url, headers=_headers(access_token))
|
| 86 |
+
res.raise_for_status()
|
| 87 |
+
|
| 88 |
+
policies = res.json().get("policies", [])
|
| 89 |
+
result = []
|
| 90 |
+
|
| 91 |
+
for p in policies:
|
| 92 |
+
if not p.get("body"):
|
| 93 |
+
continue
|
| 94 |
+
result.append({
|
| 95 |
+
"id": p["handle"], # e.g. "refund-policy" as source_id
|
| 96 |
+
"brand_id": brand_id,
|
| 97 |
+
"type": p["handle"], # "refund-policy" | "shipping-policy" etc
|
| 98 |
+
"title": p["title"],
|
| 99 |
+
"content": _strip_html(p["body"]),
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
print(f" β Pulled {len(result)} policies from Shopify")
|
| 103 |
+
return result
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# ββ Query time (live, never embedded) βββββββββββββββββββββββββββββββββββββββββ
|
| 107 |
+
|
| 108 |
+
def get_inventory(shop_domain: str, access_token: str, query: str = None) -> str:
|
| 109 |
+
"""
|
| 110 |
+
Hit Shopify live for real-time stock and pricing.
|
| 111 |
+
Returns a readable string passed directly to the LLM as context.
|
| 112 |
+
"""
|
| 113 |
+
url = f"{_base_url(shop_domain)}/products.json"
|
| 114 |
+
params = {"limit": 250}
|
| 115 |
+
|
| 116 |
+
if query:
|
| 117 |
+
params["title"] = query # rough filter β LLM handles semantic matching
|
| 118 |
+
|
| 119 |
+
res = requests.get(url, headers=_headers(access_token), params=params)
|
| 120 |
+
res.raise_for_status()
|
| 121 |
+
products = res.json().get("products", [])
|
| 122 |
+
|
| 123 |
+
if not products:
|
| 124 |
+
return "No matching products found in inventory."
|
| 125 |
+
|
| 126 |
+
lines = []
|
| 127 |
+
for p in products:
|
| 128 |
+
lines.append(f"\nProduct: {p['title']} ({p.get('product_type', '')})")
|
| 129 |
+
for v in p["variants"]:
|
| 130 |
+
status = "In Stock" if v["inventory_quantity"] > 0 else "Out of Stock"
|
| 131 |
+
lines.append(
|
| 132 |
+
f" - {v['title']} | Price: ${v['price']} | "
|
| 133 |
+
f"Stock: {v['inventory_quantity']} | {status}"
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
return "\n".join(lines)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# ββ Quick test βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
import os
|
| 143 |
+
from dotenv import load_dotenv
|
| 144 |
+
load_dotenv()
|
| 145 |
+
|
| 146 |
+
shop_domain = os.environ["SHOPIFY_SHOP_DOMAIN"]
|
| 147 |
+
access_token = os.environ["SHOPIFY_ACCESS_TOKEN"]
|
| 148 |
+
brand_id = "test-brand-id"
|
| 149 |
+
|
| 150 |
+
print("=== PRODUCTS ===")
|
| 151 |
+
products = get_products(shop_domain, access_token, brand_id)
|
| 152 |
+
for p in products[:2]:
|
| 153 |
+
print(p)
|
| 154 |
+
|
| 155 |
+
print("\n=== POLICIES ===")
|
| 156 |
+
policies = get_policies(shop_domain, access_token, brand_id)
|
| 157 |
+
for p in policies:
|
| 158 |
+
print(p)
|
| 159 |
+
|
| 160 |
+
print("\n=== LIVE INVENTORY ===")
|
| 161 |
+
print(get_inventory(shop_domain, access_token))
|
shopify_ingest.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# shopify_ingest.py
|
| 2 |
+
from supabase import create_client
|
| 3 |
+
from chunker import prepare_policy_chunks, prepare_faq_chunks, prepare_product_chunks
|
| 4 |
+
from embedder import store_chunks, delete_chunks_for_source
|
| 5 |
+
from shopify_client import get_products, get_policies
|
| 6 |
+
from datetime import datetime, timezone
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
supabase = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_SERVICE_KEY"])
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def get_brand(brand_slug: str) -> dict:
|
| 16 |
+
return (
|
| 17 |
+
supabase.table("brands")
|
| 18 |
+
.select("*")
|
| 19 |
+
.eq("slug", brand_slug)
|
| 20 |
+
.single()
|
| 21 |
+
.execute()
|
| 22 |
+
.data
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_shopify_credentials(brand_id: str) -> dict:
|
| 27 |
+
return (
|
| 28 |
+
supabase.table("brand_channels")
|
| 29 |
+
.select("channel_identifier, credentials")
|
| 30 |
+
.eq("brand_id", brand_id)
|
| 31 |
+
.eq("channel", "shopify")
|
| 32 |
+
.eq("is_active", True)
|
| 33 |
+
.single()
|
| 34 |
+
.execute()
|
| 35 |
+
.data
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def ingest_brand(brand_slug: str):
|
| 40 |
+
# Get brand + shopify credentials
|
| 41 |
+
brand = get_brand(brand_slug)
|
| 42 |
+
brand_id = brand["id"]
|
| 43 |
+
channel = get_shopify_credentials(brand_id)
|
| 44 |
+
shop_domain = channel["channel_identifier"]
|
| 45 |
+
access_token = channel["credentials"]["access_token"]
|
| 46 |
+
|
| 47 |
+
print(f"\nIngesting {brand['name']} from Shopify...")
|
| 48 |
+
|
| 49 |
+
# policies β pull from Shopify, chunk, embed
|
| 50 |
+
policies = get_policies(shop_domain, access_token, brand_id)
|
| 51 |
+
for policy in policies:
|
| 52 |
+
delete_chunks_for_source(policy["id"])
|
| 53 |
+
chunks = prepare_policy_chunks(policy)
|
| 54 |
+
store_chunks(chunks)
|
| 55 |
+
print(f" β Policy: {policy['title']}")
|
| 56 |
+
|
| 57 |
+
# faqs β still from your DB, unchanged
|
| 58 |
+
faqs = (
|
| 59 |
+
supabase.table("faqs")
|
| 60 |
+
.select("*")
|
| 61 |
+
.eq("brand_id", brand_id)
|
| 62 |
+
.execute()
|
| 63 |
+
.data
|
| 64 |
+
)
|
| 65 |
+
for faq in faqs:
|
| 66 |
+
delete_chunks_for_source(faq["id"])
|
| 67 |
+
chunks = prepare_faq_chunks(faq)
|
| 68 |
+
store_chunks(chunks)
|
| 69 |
+
print(f" β FAQ: {faq['question'][:50]}")
|
| 70 |
+
|
| 71 |
+
# products β pull from Shopify, chunk, embed
|
| 72 |
+
products = get_products(shop_domain, access_token, brand_id)
|
| 73 |
+
for product in products:
|
| 74 |
+
delete_chunks_for_source(product["id"])
|
| 75 |
+
chunks = prepare_product_chunks(product)
|
| 76 |
+
store_chunks(chunks)
|
| 77 |
+
print(f" β Product: {product['name']}")
|
| 78 |
+
|
| 79 |
+
# mark as indexed
|
| 80 |
+
supabase.table("brands").update({
|
| 81 |
+
"indexed_at": datetime.now(timezone.utc).isoformat()
|
| 82 |
+
}).eq("id", brand_id).execute()
|
| 83 |
+
print(f" β Marked as indexed")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def sync_new_brands():
|
| 87 |
+
unindexed = (
|
| 88 |
+
supabase.table("brands")
|
| 89 |
+
.select("slug, name")
|
| 90 |
+
.filter("indexed_at", "is", "null")
|
| 91 |
+
.execute()
|
| 92 |
+
.data
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
if not unindexed:
|
| 96 |
+
print("β
No new brands to index.")
|
| 97 |
+
return
|
| 98 |
+
|
| 99 |
+
print(f"Found {len(unindexed)} unindexed brand(s)...")
|
| 100 |
+
for brand in unindexed:
|
| 101 |
+
ingest_brand(brand["slug"])
|
| 102 |
+
print("\nβ
Sync complete!")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def reindex_brand(brand_slug: str):
|
| 106 |
+
print(f"Force re-indexing {brand_slug}...")
|
| 107 |
+
ingest_brand(brand_slug)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def reindex_all():
|
| 111 |
+
brands = supabase.table("brands").select("slug").execute().data
|
| 112 |
+
for brand in brands:
|
| 113 |
+
ingest_brand(brand["slug"])
|
| 114 |
+
print("\nβ
Full re-index complete!")
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
import sys
|
| 119 |
+
|
| 120 |
+
if len(sys.argv) == 1:
|
| 121 |
+
sync_new_brands()
|
| 122 |
+
|
| 123 |
+
elif sys.argv[1] == "--reindex" and len(sys.argv) == 3:
|
| 124 |
+
reindex_brand(sys.argv[2])
|
| 125 |
+
|
| 126 |
+
elif sys.argv[1] == "--reindex-all":
|
| 127 |
+
reindex_all()
|
| 128 |
+
|
| 129 |
+
else:
|
| 130 |
+
print("Usage:")
|
| 131 |
+
print(" python shopify_ingest.py # sync new brands only")
|
| 132 |
+
print(" python shopify_ingest.py --reindex <slug> # force re-index one brand")
|
| 133 |
+
print(" python shopify_ingest.py --reindex-all # re-index everything")
|