Pavel Duchovny commited on
Commit
26e73e0
1 Parent(s): b54e32e
Files changed (3) hide show
  1. app.py +120 -0
  2. flagged/log.csv +3 -0
  3. requirements.tx +2 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from time import sleep
3
+ from pymongo import MongoClient
4
+ from bson import ObjectId
5
+ from openai import OpenAI
6
+ openai_client = OpenAI()
7
+ import os
8
+
9
+ uri = os.environ.get('MONGODB_ATLAS_URI')
10
+ client = MongoClient(uri)
11
+ db_name = 'whatscooking'
12
+ collection_name = 'restaurants'
13
+ restaurants_collection = client[db_name][collection_name]
14
+ trips_collection = client[db_name]['smart_trips']
15
+
16
+
17
+
18
+ def get_restaurants(search, location, meters):
19
+
20
+ newTrip = pre_aggregate_meters(location, meters)
21
+
22
+ response = openai_client.embeddings.create(
23
+ input=search,
24
+ model="text-embedding-3-small",
25
+ dimensions=256
26
+ )
27
+
28
+ restaurant_docs = list(trips_collection.aggregate([{
29
+ "$vectorSearch": {
30
+ "index" : "vector_index",
31
+ "queryVector": response.data[0].embedding,
32
+ "path" : "embedding",
33
+ "numCandidates": 10,
34
+ "limit": 3,
35
+ "filter": {"searchTrip": newTrip}
36
+ }},
37
+ {"$project": {"_id" : 0, "embedding": 0}}]))
38
+
39
+
40
+ chat_response = openai_client.chat.completions.create(
41
+ model="gpt-3.5-turbo",
42
+ messages=[
43
+ {"role": "system", "content": "You are a helpful restaurant assistant."},
44
+ { "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."}
45
+ ]
46
+ )
47
+
48
+ trips_collection.delete_many({"searchTrip": newTrip})
49
+
50
+ return chat_response.choices[0].message.content
51
+
52
+
53
+ def pre_aggregate_meters(location, meters):
54
+
55
+ tripId = ObjectId()
56
+
57
+ restaurants_collection.aggregate([
58
+ {
59
+ "$geoNear": {
60
+ "near": location,
61
+ "distanceField": "distance",
62
+ "maxDistance": meters,
63
+ "spherical": True,
64
+ },
65
+ },
66
+ {
67
+ "$addFields": {
68
+ "searchTrip" : tripId,
69
+ "date" : tripId.generation_time
70
+ }
71
+ },
72
+ {
73
+ "$merge": {
74
+ "into": "smart_trips"
75
+ }
76
+ }
77
+ ]);
78
+
79
+ sleep(10)
80
+
81
+ return tripId
82
+
83
+
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown(
86
+ """
87
+ # MongoDB's Vector Restaurant planner
88
+ Start typing below to see the results
89
+ """)
90
+ gr.HTML(value='<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&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>')
91
+ #
92
+ gr.Interface(
93
+ get_restaurants,
94
+ [
95
+
96
+ gr.Textbox(placeholder="What type of dinner are you looking for?"),
97
+ gr.Radio([("work",{
98
+ "type": "Point",
99
+ "coordinates": [
100
+ -73.98527039999999,
101
+ 40.7589099
102
+ ]
103
+ }), ("home",{
104
+ "type": "Point",
105
+ "coordinates": [
106
+ 40.701975, -74.013686
107
+ ]
108
+ }), ("park", {
109
+ "type": "Point",
110
+ "coordinates": [40.720777, -74.000468
111
+ ]
112
+ })], label="Location", info="What location you need?"),
113
+ gr.Slider(minimum=500, maximum=10000, randomize=False, step=5, label="Radius in meters")],
114
+ gr.Textbox(label="MongoDB Vector Recommendations", placeholder="Results will be displayed here"),
115
+
116
+ )
117
+ #radio.change(location_searched, loc, out)
118
+ if __name__ == "__main__":
119
+ demo.launch()
120
+
flagged/log.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ search,Location,out,output,flag,username,timestamp
2
+ Movies,work,,I will now search: Movies!,,,2024-02-07 12:04:10.349102
3
+ Movies,work,,I will now search: Movies!,,,2024-02-07 12:04:12.484796
requirements.tx ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai
2
+ pymongo