-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
89 lines (67 loc) · 2.48 KB
/
Copy pathvector.py
File metadata and controls
89 lines (67 loc) · 2.48 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from langchain_ollama import OllamaEmbeddings
from langchain_chroma import Chroma
DB_DIR = "yelp_chroma_db"
embeddings = OllamaEmbeddings(model="mxbai-embed-large")
vector_store = Chroma(
collection_name="yelp_reviews",
persist_directory=DB_DIR,
embedding_function=embeddings,
)
from skills.cuisine_constants import VALID_CUISINES
def get_filtered_reviews(query, restaurant=None, sentiment=None, k=20):
filters = {}
if restaurant:
filters["restaurant"] = restaurant
if sentiment:
filters["sentiment"] = sentiment
return vector_store.similarity_search(
query=query,
k=k,
filter=filters or None
)
def get_restaurant_names(cuisine=None):
"""
Returns a sorted list of unique restaurant names.
If `cuisine` is provided, filters by that cuisine type from metadata.
"""
# 1. Get raw metadatas
metas = vector_store._collection.get(include=["metadatas"], limit=1000000)["metadatas"]
unique_names = set()
for m in metas:
if "restaurant" not in m:
continue
# Filter if a specific cuisine is requested
if cuisine and cuisine != "All":
# Split the restaurant's cuisine string into a list of cleaned categories
db_cuisines = [c.strip() for c in m.get("cuisine", "").split(",")]
if cuisine not in db_cuisines:
continue
unique_names.add(m["restaurant"])
return sorted(unique_names)
def get_all_cuisines():
"""
Returns a sorted list of all unique cuisines found in metadata.
"""
metas = vector_store._collection.get(include=["metadatas"], limit=1000000)["metadatas"]
cuisines = set()
for m in metas:
if "cuisine" in m and m["cuisine"]:
# Split by comma and strip whitespace to get individual cuisines
parts = m["cuisine"].split(",")
for part in parts:
cleaned = part.strip()
if cleaned in VALID_CUISINES:
cuisines.add(cleaned)
return sorted(list(cuisines))
def get_restaurant_info(restaurant_name):
"""
Retrieves metadata for a specific restaurant.
"""
results = vector_store.similarity_search(
query="",
k=1,
filter={"restaurant": restaurant_name}
)
if results:
return results[0].metadata
return {}