Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gemini integration #42

Merged
merged 3 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions gemini_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import google.generativeai as genai
import requests


def convert_messages_format(message):
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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.
Expand Down
8 changes: 3 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
uvicorn.run(app, host="0.0.0.0", port=8000)
Loading