import google.generativeai as genai import re import json import os GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") genai.configure(api_key=GEMINI_API_KEY) # model = genai.GenerativeModel('gemini-1.5-pro') # Or 'gemini-pro' model = genai.GenerativeModel('gemini-2.0-flash') def get_travel_info(user_input): """Uses Gemini AI to extract travel intent and location from user input.""" prompt = f""" You are a travel assistant. Your job is to extract the travel intent, location, and departure date from a user's query. The intent can be anything from planning a trip to finding a hotel to suggesting a restaurant. The location is the destination of the travel. If the location is not specified, return None. If the intent is not related to travel, return the intent and location in JSON format ONLY, like this: If either the intent, the location, the departure_date, duration, budget, or num_people is not found, return null for that field. ```json {{ "intent": "the extracted intent", "location": "the extracted location", "departure_date": "the extracted departure date", "duration": "the extracted duration", "budget": "the extracted budget", "num_people": "the extracted number of people" }} ``` User Query: I want to plan a trip to Paris for 5 days with a budget of $2000 for 2 people, leaving on March 15th. ```json {{ "intent": "plan a trip", "location": "Paris", "departure_date": "March 15th", "duration": "5 days", "budget": "$2000", "num_people": "2" }} ``` User Query: Find a hotel in New York City for 3 nights with a budget of $1500 for 1 person, leaving on April 10th. ```json {{ "intent": "Find a hotel", "location": "New York City", "departure_date": "April 10th", "duration": "3 nights", "budget": "$1500", "num_people": "1" }} ``` User Query: What is the weather like today? ```json {{ "intent": null, "location": null, "departure_date": null, "duration": null, "budget": null, "num_people": null }} ``` User Query: {user_input} """ response = model.generate_content(prompt) response_text = response.text # print(f"Raw Gemini Response: {response_text}") # Debugging try: # Use regular expression to find the JSON string within the response match = re.search(r"\{.*?\}", response_text, re.DOTALL) if match: json_string = match.group(0).strip() data = json.loads(json_string) intent = data.get("intent") location = data.get("location") departure_date = data.get("departure_date") duration = data.get("duration") budget = data.get("budget") num_people = data.get("num_people") return { "intent": intent, "location": location, "departure_date": departure_date, "duration": duration, "budget": budget, "num_people": num_people } else: print("Error: No JSON found in Gemini response.") return None except json.JSONDecodeError: print("Error: Could not decode JSON after extraction.") return None except Exception as e: print(f"An unexpected error occurred: {e}") return None def get_travel_recommendations(intent, location, departure_date, duration, budget, num_people): """Uses Gemini AI to provide travel recommendations based on intent and location.""" prompt = f""" You are a travel assistant. Based on the given intent, location, duration, budget, and number of people, provide helpful travel recommendations in Markdown format. Attractions should provide at least 10 recommendations. Accommodations should provide at least 5 recommendations. Activities should provide at least 10 recommendations. Food should provide at least 10 recommendations. Keep in mind the budget and the number of people when suggesting accommodations and activities. Intent: {intent} Location: {location} Departure Date: {departure_date} Duration: {duration} Budget: {budget} Number of people: {num_people} Your response should look like this: # 🌍 Travel Guide: {location} ## 🌤️ Weather: - Weather 1 ## ✈️ Flight: - Flight 1 - Flight 2 ## 🏨 Accommodations: - Hotel 1 - Hotel 2 ## 💳 Mobile Payment: - Mobile Payment 1 - Mobile Payment 2 ## 🚗 Local Transportations: - Local Transportations 1 - Local Transportations 2 ## 🏰 Attractions: - Attraction 1 - Attraction 2 ## 🚀 Activities: - Activity 1 - Activity 2 ## 🍽️ Foods: - Foods 1 - Foods 2 ## 💡 Tips for Planning: - Tips for Planning 1 - Tips for Planning 2 """ response = model.generate_content(prompt) return response.text