← Lesson
Metadata Filters and Tenants
Joeven
Run
Reset
Python loads on first run
CHUNKS = [ {"id": "acme-refund", "tenant": "acme", "text": "Acme refunds take 5-7 days."}, {"id": "globex-refund", "tenant": "globex", "text": "Globex refunds are instant cash."}, {"id": "acme-oom", "tenant": "acme", "text": "Acme OOM: raise worker memory."}, ] def retrieve(query, tenant, k=2): q = set(query.lower().split()) pool = [c for c in CHUNKS if c["tenant"] == tenant] ranked = [] for c in pool: score = len(q & set(c["text"].lower().split())) ranked.append((score, c["id"], c)) ranked.sort(reverse=True) return [c for score, _id, c in ranked[:k] if score > 0] print("acme", [c["id"] for c in retrieve("refund days", "acme")]) print("globex", [c["id"] for c in retrieve("refund days", "globex")]) print("no mix", all(c["tenant"] == "acme" for c in retrieve("refund OOM", "acme")))
Run to execute this in your browser. Nothing is sent to a server.