-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrag.py
More file actions
74 lines (57 loc) · 2.52 KB
/
Copy pathrag.py
File metadata and controls
74 lines (57 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""RAG example: semantic retrieval with the native vector API, generation via OpenRouter."""
from __future__ import annotations
import json
import os
import urllib.request
from sqlalchemy import select
from sqlalchemy.orm import Session
from paradedb.sqlalchemy import search, vector
from setup import Product, engine_from_env, setup_database
MODEL = os.environ.get("RAG_MODEL", "anthropic/claude-3-haiku")
# Stand-in for a real embedding model: the demo products embed into a tiny
# 3-dimensional space (footwear, audio, home decor).
QUERY = "What running shoes do you have?"
QUERY_EMBEDDING = [1.0, 0.0, 0.0]
def retrieve(session: Session, query_embedding: list[float], limit: int = 3):
stmt = (
select(Product.description, Product.category, Product.rating)
.where(search.all(Product.id))
.order_by(vector.l2_distance(Product.embedding, query_embedding))
.limit(limit)
)
return session.execute(stmt).all()
def generate(query: str, context: str) -> str:
api_key = os.environ.get("OPENROUTER_API_KEY")
if not api_key:
return "(Set OPENROUTER_API_KEY to enable generation.)"
prompt = (
"You are a helpful product assistant. Answer the customer's question based only on "
f"the product information provided below.\n\nProduct Catalog:\n{context}\n\n"
f"Customer Question: {query}\n\n"
"Provide a helpful, concise answer. If the products don't match what the customer "
"is looking for, say so."
)
request = urllib.request.Request(
"https://openrouter.ai/api/v1/chat/completions",
data=json.dumps({"model": MODEL, "messages": [{"role": "user", "content": prompt}]}).encode(),
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
)
try:
with urllib.request.urlopen(request, timeout=60) as response:
body = json.load(response)
return body["choices"][0]["message"]["content"]
except (OSError, KeyError, IndexError, ValueError) as exc:
return f"(OpenRouter error: {exc}. Check your API key.)"
def main() -> None:
engine = engine_from_env()
setup_database(engine)
with Session(engine) as session:
rows = retrieve(session, QUERY_EMBEDDING)
context = "\n".join(f"- {row.description} | Category: {row.category} | Rating: {row.rating}/5" for row in rows)
print(f"Question: {QUERY}")
print("Retrieved products:")
print(context)
print("Answer:")
print(generate(QUERY, context))
if __name__ == "__main__":
main()