Pavel Duchovny commited on
Commit
df3f326
·
1 Parent(s): 73bb5ee

Stability fixes

Browse files
Files changed (1) hide show
  1. app.py +50 -46
app.py CHANGED
@@ -28,56 +28,60 @@ def get_restaurants(search, location, meters):
28
 
29
  # Pre aggregate restaurants collection based on chosen location and radius, the output is stored into
30
  # trips_collection
31
- newTrip, pre_agg = pre_aggregate_meters(restaurants_collection, location, meters)
32
-
33
- ## Get openai embeddings
34
- response = openai_client.embeddings.create(
35
- input=search,
36
- model="text-embedding-3-small",
37
- dimensions=256
38
- )
39
-
40
- ## prepare the similarity search on current trip
41
- vectorQuery = {
42
- "$vectorSearch": {
43
- "index" : "vector_index",
44
- "queryVector": response.data[0].embedding,
45
- "path" : "embedding",
46
- "numCandidates": 10,
47
- "limit": 3,
48
- "filter": {"searchTrip": newTrip}
49
- }}
50
-
51
- ## Run the retrieved documents through a RAG system.
52
- restaurant_docs = list(trips_collection.aggregate([vectorQuery,
53
- {"$project": {"_id" : 0, "embedding": 0}}]))
 
54
 
55
-
56
- chat_response = openai_client.chat.completions.create(
57
- model="gpt-3.5-turbo-0125",
58
- messages=[
59
- {"role": "system", "content": "You are a helpful restaurant assistant. You will get a context if the context is not relevat to the user query please address that and not provide by default the restaurants as is."},
60
- { "role": "user", "content": f"Find me the 2 best restaurant and why based on {search} and {restaurant_docs}. explain trades offs and why I should go to each one. You can mention the third option as a possible alternative."}
61
- ]
62
- )
 
 
 
 
 
 
 
63
 
64
- ## Removed the temporary documents
65
- trips_collection.delete_many({"searchTrip": newTrip})
 
 
 
66
 
67
 
68
- if len(restaurant_docs) == 0:
69
- return "No restaurants found", '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':\'\'}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>', str(pre_agg), str(vectorQuery)
70
-
71
- ## Build the map filter
72
- first_restaurant = restaurant_docs[0]['restaurant_id']
73
- second_restaurant = restaurant_docs[1]['restaurant_id']
74
- third_restaurant = restaurant_docs[2]['restaurant_id']
75
- restaurant_string = f"'{first_restaurant}', '{second_restaurant}', '{third_restaurant}'"
76
-
77
-
78
- iframe = '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':{$in:[' + restaurant_string + ']}}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>'
79
- client.close()
80
- return chat_response.choices[0].message.content, iframe,str(pre_agg), str(vectorQuery)
81
 
82
 
83
  def pre_aggregate_meters(restaurants_collection, location, meters):
 
28
 
29
  # Pre aggregate restaurants collection based on chosen location and radius, the output is stored into
30
  # trips_collection
31
+ try:
32
+ newTrip, pre_agg = pre_aggregate_meters(restaurants_collection, location, meters)
33
+
34
+ ## Get openai embeddings
35
+ response = openai_client.embeddings.create(
36
+ input=search,
37
+ model="text-embedding-3-small",
38
+ dimensions=256
39
+ )
40
+
41
+ ## prepare the similarity search on current trip
42
+ vectorQuery = {
43
+ "$vectorSearch": {
44
+ "index" : "vector_index",
45
+ "queryVector": response.data[0].embedding,
46
+ "path" : "embedding",
47
+ "numCandidates": 10,
48
+ "limit": 3,
49
+ "filter": {"searchTrip": newTrip}
50
+ }}
51
+
52
+ ## Run the retrieved documents through a RAG system.
53
+ restaurant_docs = list(trips_collection.aggregate([vectorQuery,
54
+ {"$project": {"_id" : 0, "embedding": 0}}]))
55
 
56
+
57
+ chat_response = openai_client.chat.completions.create(
58
+ model="gpt-3.5-turbo-0125",
59
+ messages=[
60
+ {"role": "system", "content": "You are a helpful restaurant assistant. You will get a context if the context is not relevat to the user query please address that and not provide by default the restaurants as is."},
61
+ { "role": "user", "content": f"Find me the 2 best restaurant and why based on {search} and {restaurant_docs}. explain trades offs and why I should go to each one. You can mention the third option as a possible alternative."}
62
+ ]
63
+ )
64
+
65
+ ## Removed the temporary documents
66
+ trips_collection.delete_many({"searchTrip": newTrip})
67
+
68
+
69
+ if len(restaurant_docs) == 0:
70
+ return "No restaurants found", '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':\'\'}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>', str(pre_agg), str(vectorQuery)
71
 
72
+ ## Build the map filter
73
+ first_restaurant = restaurant_docs[0]['restaurant_id']
74
+ second_restaurant = restaurant_docs[1]['restaurant_id']
75
+ third_restaurant = restaurant_docs[2]['restaurant_id']
76
+ restaurant_string = f"'{first_restaurant}', '{second_restaurant}', '{third_restaurant}'"
77
 
78
 
79
+ iframe = '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':{$in:[' + restaurant_string + ']}}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>'
80
+ client.close()
81
+ return chat_response.choices[0].message.content, iframe,str(pre_agg), str(vectorQuery)
82
+ except Exception as e:
83
+ print(e)
84
+ return "Your query caused an error, please retry with allowed input only ...", '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':\'\'}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>', str(pre_agg), str(vectorQuery)
 
 
 
 
 
 
 
85
 
86
 
87
  def pre_aggregate_meters(restaurants_collection, location, meters):