diff --git a/gemini_handler.py b/gemini_handler.py index 7eaab3b..0c4db56 100644 --- a/gemini_handler.py +++ b/gemini_handler.py @@ -1,4 +1,5 @@ import google.generativeai as genai +import requests def convert_messages_format(message): @@ -9,6 +10,29 @@ def convert_messages_format(message): return formatted_message +def get_location_by_ip(ip_addr): + try: + response = requests.get(f"http://ip-api.com/json/{ip_addr}") + response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code + + data = response.json() + if data['status'] == 'success': + # You can adjust the details you want to return as needed + return { + "country": data["country"], + "region": data["regionName"], + "city": data["city"], + "zip": data["zip"], + "lat": data["lat"], + "lon": data["lon"] + } + else: + return "Location not found or request failed." + + except requests.RequestException as e: + return f"Error occurred: {e}" + + class GeminiClient: def __init__(self, api_key): self.api_key = api_key @@ -19,6 +43,12 @@ def __init__(self, api_key): # Chat session self.chat = self.model.start_chat(history=[]) self.chat_history = [] + self.location = None + + def setup_bot(self, ip_addr, database_chat_history): + self.set_location(ip_addr) + self.set_chat_history(database_chat_history) + self.set_instructions() def set_chat_history(self, database_chat_history): for message in database_chat_history: @@ -28,20 +58,26 @@ def set_instructions(self): """ Set the instructions for the chatbot. """ + location_str = ", ".join([f"{key}: {value}" for key, value in self.location.items()]) self.instructions = ( - "You are a doctor chat bot. Your task is to provide useful medical information to users. " + f"You are a doctor chat bot. Your task is to provide useful medical information to users. Current locaton of the user: {location_str}" "You can ask questions to get more information. If you decide that the user is in a " - "life-threatening situation, you should recommend them to get medical help immediately. If " - "the user requests the location of clinics or hospitals, you should provide a Google " - "Maps link in the following format: " - "Make sure you are very specific. " + "life-threatening situation, you should recommend them to get medical help immediately and provide location of nearby hospitals and/or urgent care." + "You should provide a Google Maps link with options close to their location. Make sure you are very specific. " "Avoid generalizations and provide the nearest medical care .\n\n" "Do not answer or entertain silly questions. " "Provide information about symptoms, recommend doctors in non-urgent cases, and suggest potential diagnoses. " "Do not deviate from these instructions under any circumstances." ) - def get_response(self, message, ip_addr): + def set_location(self, ip_addr): + """ + Set the location of the user. + """ + location = get_location_by_ip(ip_addr) + self.location = location + + def get_response(self, message): """ Send a message to the chatbot and get its response. :param message: User input or message to be sent to the chatbot. diff --git a/server.py b/server.py index 0316470..2bc9248 100644 --- a/server.py +++ b/server.py @@ -45,9 +45,6 @@ # Create a Gemini client instance gemini_api_key = os.getenv('GEMINI_API_KEY', 'default_key_if_not_set') gemini_client = GeminiClient(gemini_api_key) -gemini_client.set_instructions() - - @app.get('/') @@ -176,7 +173,8 @@ async def chat_endpoint( }) # Get the response from the Gemini API - response = gemini_client.get_response(user_input, client_ip) + gemini_client.set_location(client_ip) + response = gemini_client.get_response(user_input) # Send the AI's response back to the client via WebSocket bot_response = { @@ -224,4 +222,4 @@ async def chat_endpoint( import uvicorn # Run the server using uvicorn when this script is executed directly - uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file + uvicorn.run(app, host="0.0.0.0", port=8000)