master
py 78 lines 3.22 KB
Raw
1 import os
2 import json
3
4 from dotenv import load_dotenv
5 from google import genai
6
7
8 load_dotenv()
9
10 API_KEY = os.getenv("GOOGLE_API_KEY")
11 client = genai.Client(api_key=API_KEY)
12
13 class AIRequest:
14 def __init__(self):
15 self.genai_client = client
16
17 def ai_request(self,data_string: str) -> dict:
18 """
19 Function to get an AI request for a book recommendation
20 :param data_string:
21 :return: movie_recommendation
22 """
23 response = self.genai_client.models.generate_content(
24 model="gemini-2.0-flash",
25 contents="""Can you recommend a movie for me based on my ratings of the given dataset,
26 with personal ratings form 0 to 10 as float values, please?:
27 "dataset":"""+data_string+"""
28 Please answer in the python dictionary format , so I can load the request into python.
29 Here an example:
30 {"movie":
31 {"title": "some title",
32 "director": "some director",
33 "year": "some year",
34 "poster": "movies's poster" as a string to load as link in html img tag,
35 "imdb": "some imdb id with only numbers please",
36 "country": "the country of the movie,
37 "genre": "the genre of the movie",
38 "plot": "the plot of the movie",},
39 "reasoning": "your reasoning text"}
40 Please only answer with the above format and nothing else.
41 """
42 )
43 json_string = response.text.replace("```python", "")
44 movie_recommendation = json_string.replace("```", "")
45 movie_recommendation = json.loads(movie_recommendation)
46 return movie_recommendation
47
48
49 def ai_excluded_movie_request(self,data_string: str, excluded_movie: str) -> dict:
50 """
51 Function to get an AI request for a movie recommendation excluding a ceratain movie
52 :param data_string:
53 :param excluded_movie:
54 :return: movie_recommendation
55 """
56 response = self.genai_client.models.generate_content(
57 model="gemini-2.0-flash",
58 contents="""Can you recommend a movie for me based on my ratings of the given dataset,
59 with personal ratings form 0 to 10 as float values, please?:
60 "dataset":"""+data_string+""" and excluding the movie: """+excluded_movie+"""
61 Please answer in the python dictionary format , so I can load the request into python.
62 Here an example:
63 {"movie":
64 {"title": "some title",
65 "director": "some director",
66 "year": "some year",
67 "poster": "movies's poster" as a string to load as link in html img tag,
68 "imdb": "some imdb id with only numbers please",
69 "country": "the country of the movie,
70 "genre": "the genre of the movie",
71 "plot": "the plot of the movie",},
72 "reasoning": "your reasoning text"}
73 Please only answer with the above format and nothing else.
74 """
75 )
76 json_string = response.text.replace("```python", "").replace("```", "")
77 movie_recommendation = json.loads(json_string)
78 return movie_recommendation