| 1 | import json |
| 2 | import os |
| 3 | |
| 4 | from dotenv import load_dotenv |
| 5 | from google import genai |
| 6 | |
| 7 | load_dotenv() |
| 8 | |
| 9 | API_KEY = os.getenv("GOOGLE_API_KEY") |
| 10 | client = genai.Client(api_key=API_KEY) |
| 11 | |
| 12 | def ai_request(data_string): |
| 13 | """ |
| 14 | Function to get an AI request for a book recommendation |
| 15 | :param data: |
| 16 | :return: |
| 17 | """ |
| 18 | response = client.models.generate_content( |
| 19 | model="gemini-2.0-flash", |
| 20 | contents="""Can you recommend a book for me based on my ratings of the following dataset, |
| 21 | with personal ratings form 0 to 10 as float vlaues, please?: |
| 22 | "dataset":"""+data_string+""" |
| 23 | Please answer in the json string format , so I can load the request into python. |
| 24 | Here an example: |
| 25 | {"book": |
| 26 | {"title": "some title", |
| 27 | "author": "some author", |
| 28 | "year": "some year", |
| 29 | "isbn": "some isbn with only numbers please", |
| 30 | "birthday": "author's birthday" as python datetime object with format "YYYY-MM-DD", |
| 31 | "died": "author's deathday" as pythondatetime object \ |
| 32 | withformat "YYYY-MM-DD" or |
| 33 | Null if still alive}, |
| 34 | "reasoning": "your reasoning text"} |
| 35 | """ |
| 36 | ) |
| 37 | |
| 38 | json_string = response.text.replace("```json", "") |
| 39 | json_string = json_string.replace("```", "") |
| 40 | |
| 41 | data = json.loads(json_string) |
| 42 | return data |