-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vectorizer.py
53 lines (41 loc) · 1.8 KB
/
Vectorizer.py
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
import pymongo
import openai
#Set up openai key
openai.api_key = "OPENAI API KEY"
# Connect to MongoDB
client = pymongo.MongoClient('MONGODB CONNECTION STRING')
db = client['sample_airbnb']
collection = db['listingsAndReviews']
# Retrieve documents from the collection
documents = collection.find()
# Iterate over the documents
index = 0
for document in documents:
# print(document)
#append various data fields in the database entry, assuming they are not null, to the input string used to generate the embedding
embedding_input_string = ""
if document['name']!=None:
embedding_input_string+=document["name"]+". "
if document['summary']!=None:
embedding_input_string+=document['summary']+". "
if document['space']!=None:
embedding_input_string+=document['space']+". "
if document['description']!=None:
embedding_input_string+=document['description']+". "
if document['transit']!=None:
embedding_input_string+=document['transit']+". "
if document['price']!=None:
embedding_input_string+="Price per night: "+str(document['price'])+'. '
#print the current input string used to generate the embedding
print(embedding_input_string)
#generate openai embedding based on input string
embedding = openai.Embedding.create(input = [embedding_input_string], model="text-embedding-ada-002")['data'][0]['embedding']
#Set corresponding openai_embedding into each document in the database
document['openai_embedding'] = embedding
#keep track of index to display progress in terminal
print("Current index: {}".format(index)) #to keep track of progress
index+=1
# Update the document in the collection
collection.update_one({'_id': document['_id']}, {'$set': document})
# Close the MongoDB connection
client.close()