-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie_recs2.py
35 lines (28 loc) · 866 Bytes
/
movie_recs2.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
import pymongo
import openai
import os
from dotenv import load_dotenv
load_dotenv('.env.local')
# Set your OpenAI API key
openai.api_key = os.getenv('API_KEY')
client = pymongo.MongoClient(os.getenv('CONNECTION_STRING'))
db = client.sample_mflix
collection = db.embedded_movies
def generate_embedding(text: str) -> list[float]:
response = openai.Embedding.create(
model="text-embedding-ada-002",
input=text
)
return response['data'][0]['embedding']
query = "imaginary characters from outer space at war"
results = collection.aggregate([
{"$vectorSearch": {
"queryVector": generate_embedding(query),
"path": "plot_embedding",
"numCandidates": 100,
"limit": 4,
"index": "PlotSemanticSearch",
}}
]);
for document in results:
print(f'Movie Name: {document["title"]},\nMovie Plot: {document["plot"]}\n')