added some stile and tested ai api
Lee Roy Stevenson committed
May 20, 2025 at 05:47 UTC
e74ca733367f65e0ab467cf7142ddfdb50779396
1 file changed
+46
ai_request.py
new
+46
@@ -0,0 +1,46 @@
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