Spaces:
Build error
Build error
Upload 7 files
Browse files- app.py +203 -0
- constants.py +3 -0
- create_embeddings.py +9 -0
- dataset.py +102 -0
- llm.py +38 -0
- requirements.txt +79 -0
- vector_database.py +54 -0
app.py
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
import random
|
| 4 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from llm import contruct_prompt,get_llm_response
|
| 7 |
+
import json
|
| 8 |
+
load_dotenv()
|
| 9 |
+
from vector_database import vector_search_v1
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def validate_user_input(system_prompt,question, user_input):
|
| 15 |
+
response = get_llm_response(system_prompt, user_input, [])
|
| 16 |
+
return json.loads(response)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def initialize_session_state():
|
| 20 |
+
if 'state' not in st.session_state:
|
| 21 |
+
st.session_state.state = 'initial'
|
| 22 |
+
if 'conversation_history' not in st.session_state:
|
| 23 |
+
st.session_state.conversation_history = []
|
| 24 |
+
if 'search_result' not in st.session_state:
|
| 25 |
+
st.session_state.search_result = None
|
| 26 |
+
if 'futures' not in st.session_state:
|
| 27 |
+
st.session_state.futures = None
|
| 28 |
+
if 'futuresresult' not in st.session_state:
|
| 29 |
+
st.session_state.futuresresult = False
|
| 30 |
+
if 'search_start_time' not in st.session_state:
|
| 31 |
+
st.session_state.search_start_time = None
|
| 32 |
+
|
| 33 |
+
def main():
|
| 34 |
+
st.title("AI Receptionist")
|
| 35 |
+
|
| 36 |
+
initialize_session_state()
|
| 37 |
+
|
| 38 |
+
if st.session_state.state == 'initial':
|
| 39 |
+
col1, col2 = st.columns(2)
|
| 40 |
+
with col1:
|
| 41 |
+
if st.button("Emergency"):
|
| 42 |
+
st.session_state.state = 'emergency'
|
| 43 |
+
# st.session_state.conversation_history.append({"role": "assistant", "content": "Can you describe your emergency if possible in detail ?"})
|
| 44 |
+
with st.spinner("Processing your emergency..."):
|
| 45 |
+
|
| 46 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": "Can you describe your emergency if possible in detail ?"})
|
| 47 |
+
# system_prompt = "You are an AI receptionist for a Doctor handling an emergency. Ask the patient to describe their emergency in detail."
|
| 48 |
+
# response = get_llm_response(system_prompt, "", st.session_state.conversation_history)
|
| 49 |
+
|
| 50 |
+
st.rerun()
|
| 51 |
+
with col2:
|
| 52 |
+
if st.button("Leave a Message"):
|
| 53 |
+
st.session_state.state = 'message'
|
| 54 |
+
|
| 55 |
+
with st.spinner("Processing your request..."):
|
| 56 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": "Leave a message for the doctor"})
|
| 57 |
+
# system_prompt = "You are an AI receptionist for Doctor. A patient wants to leave a message. Ask them what message they'd like to leave for the Doctor."
|
| 58 |
+
# response = get_llm_response(system_prompt, "I want to leave a message.", st.session_state.conversation_history)
|
| 59 |
+
# st.session_state.conversation_history.append({"role": "assistant", "content": response})
|
| 60 |
+
st.rerun()
|
| 61 |
+
|
| 62 |
+
# Chat input
|
| 63 |
+
if st.session_state.state in ['emergency', 'message', 'location', 'after_location', 'final','message_final']:
|
| 64 |
+
for message in st.session_state.conversation_history:
|
| 65 |
+
with st.chat_message(message["role"]):
|
| 66 |
+
st.write(message["content"])
|
| 67 |
+
user_input = st.chat_input("Type your message here...")
|
| 68 |
+
|
| 69 |
+
if user_input:
|
| 70 |
+
# Add user message to conversation history
|
| 71 |
+
st.session_state.conversation_history.append({"role": "user", "content": user_input})
|
| 72 |
+
|
| 73 |
+
if st.session_state.state == 'emergency':
|
| 74 |
+
with st.spinner("Processing your emergency..."):
|
| 75 |
+
with ThreadPoolExecutor() as executor:
|
| 76 |
+
validation_result_1 = validate_user_input("Just analyse if the user_input contains a valid symptom related to any health issue.Respond with a JSON object where 'response' is your analysis and 'validAnswer' is a boolean indicating if the answer is valid.\n Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.","Which area are you located right now?", user_input)
|
| 77 |
+
if not validation_result_1['validAnswer']:
|
| 78 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": f"I'm sorry, but I don’t understand that.Please just mention the Emergency the patient or anyone is facing.Other responses would be invalid."})
|
| 79 |
+
st.rerun()
|
| 80 |
+
|
| 81 |
+
st.session_state.futures = executor.submit(vector_search_v1, user_input)
|
| 82 |
+
st.session_state.search_start_time = time.time()
|
| 83 |
+
|
| 84 |
+
system_prompt = "I am checking what you should do immediately, meanwhile, can you tell me which area are you located right now?"
|
| 85 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": system_prompt})
|
| 86 |
+
|
| 87 |
+
if st.session_state.futures.done() and not st.session_state.futuresresult:
|
| 88 |
+
st.session_state.futuresresult = True
|
| 89 |
+
st.session_state.search_result = st.session_state.futures.result()
|
| 90 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": f"Emergency instructions: {st.session_state.search_result}"})
|
| 91 |
+
system_prompt = "If you are done handling the emergency , can you tell me which area are you located right now?"
|
| 92 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": system_prompt})
|
| 93 |
+
|
| 94 |
+
st.session_state.state = 'location'
|
| 95 |
+
st.rerun()
|
| 96 |
+
|
| 97 |
+
elif st.session_state.state == 'location':
|
| 98 |
+
with st.spinner("Processing your location..."):
|
| 99 |
+
validation_result = validate_user_input("Just analyse if the user_input contains a location I mean , city , state or country.Respond with a JSON object where 'response' is your analysis and 'validAnswer' is a boolean indicating if the answer is valid.\n Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.","Which area are you located right now?", user_input)
|
| 100 |
+
if not validation_result['validAnswer']:
|
| 101 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": f"I'm sorry, but I didn't understand your location. Could you please provide a clear location?"})
|
| 102 |
+
st.rerun()
|
| 103 |
+
|
| 104 |
+
if st.session_state.futures.done() and not st.session_state.futuresresult:
|
| 105 |
+
st.session_state.futuresresult = True
|
| 106 |
+
st.session_state.search_result = st.session_state.futures.result()
|
| 107 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": f"Emergency instructions: {st.session_state.search_result}"})
|
| 108 |
+
|
| 109 |
+
eta = random.randint(5, 20)
|
| 110 |
+
system_prompt = f"Doctor will be coming to your location immediately. Doctor's estimated arrival will be in {eta} minutes."
|
| 111 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": system_prompt})
|
| 112 |
+
|
| 113 |
+
st.session_state.state = 'after_location'
|
| 114 |
+
st.rerun()
|
| 115 |
+
|
| 116 |
+
elif st.session_state.state == 'after_location':
|
| 117 |
+
print(user_input)
|
| 118 |
+
with st.spinner("Processing your response..."):
|
| 119 |
+
response_0 = get_llm_response(system_prompt="Analyse the input where you will respond with 'late' or 'too long' strictly.You have to analyse that 'If the user says that the arrival will be too late' or any similar user_input but meaning is similar.Otherwise return 'other' strictly." , user_prompt=user_input ,conversation_history=[])
|
| 120 |
+
print(response_0)
|
| 121 |
+
if "late" in response_0.lower() or "too long" in response_0.lower():
|
| 122 |
+
if not st.session_state.futuresresult:
|
| 123 |
+
elapsed_time = time.time() - st.session_state.search_start_time
|
| 124 |
+
if elapsed_time < 15:
|
| 125 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": "Please hold just a sec while I fetch the emergency instructions..."})
|
| 126 |
+
st.rerun()
|
| 127 |
+
else:
|
| 128 |
+
search_result = st.session_state.futures.result()
|
| 129 |
+
st.session_state.futuresresult = True
|
| 130 |
+
else:
|
| 131 |
+
search_result = st.session_state.search_result
|
| 132 |
+
|
| 133 |
+
response = f"I understand that you are worried that Doctor will arrive too late. Meanwhile, we suggest that you follow these steps: {st.session_state.search_result}"
|
| 134 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": response})
|
| 135 |
+
|
| 136 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": f"Thank you for using AI Receptionist. Is there anything else I can help you with? While you wait for the Doctor."})
|
| 137 |
+
st.session_state.state = 'final'
|
| 138 |
+
st.rerun()
|
| 139 |
+
|
| 140 |
+
elif st.session_state.state == 'message':
|
| 141 |
+
print("Inside the message state.")
|
| 142 |
+
with st.spinner("Processing your message..."):
|
| 143 |
+
system_prompt = "You are an AI receptionist for a Doctor. A patient has left a message. Confirm that you've received the message and will pass it on to Doctor.Don't say anything other than that strictly."
|
| 144 |
+
response = get_llm_response(system_prompt, user_input, [])
|
| 145 |
+
print(response)
|
| 146 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": response})
|
| 147 |
+
|
| 148 |
+
st.session_state.state = 'message_final'
|
| 149 |
+
st.rerun()
|
| 150 |
+
|
| 151 |
+
elif st.session_state.state == 'message_final':
|
| 152 |
+
response_4 =get_llm_response(system_prompt="Analyse the input where you will respond with 'emergency' or 'just_message' or 'other_rubbish' strictly.If the user_input conveys a meaning the its a emergency the just return emergency strictly or if user conveys he wants to add a message or wants to leave another message then return 'just_message' strictly.If its neither related to emergency or any message to doctor , then return other_rubbish strictly." , user_prompt=user_input ,conversation_history=[])
|
| 153 |
+
print(response_4)
|
| 154 |
+
if 'emergency' in response_4.lower():
|
| 155 |
+
st.session_state.state = 'emergency'
|
| 156 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": "Can you describe your emergency if possible in detail ?"})
|
| 157 |
+
st.rerun()
|
| 158 |
+
elif 'just_message' in response_4.lower():
|
| 159 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": f"Sure please add whatever you wanted to add it to the previous message. "})
|
| 160 |
+
st.session_state.state = 'message'
|
| 161 |
+
st.rerun()
|
| 162 |
+
elif 'other_rubbish' in response_4.lower():
|
| 163 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": f"I'm sorry, but I don’t understand that ? "})
|
| 164 |
+
st.rerun()
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
elif st.session_state.state == 'final':
|
| 168 |
+
with st.spinner("Processing your message..."):
|
| 169 |
+
response_3 = get_llm_response(system_prompt="Analyse the input where you will respond with 'yes' or 'no' strictly.You have to analyse that 'If the user's input if user is satisfied or not' or any similar user_input but meaning is similar.Otherwise return 'other' strictly." , user_prompt=user_input ,conversation_history=[])
|
| 170 |
+
|
| 171 |
+
if "yes" in response_3.lower():
|
| 172 |
+
st.session_state.state = 'initial'
|
| 173 |
+
st.session_state.conversation_history = []
|
| 174 |
+
st.session_state.search_result = None
|
| 175 |
+
st.session_state.futures = None
|
| 176 |
+
st.session_state.futuresresult = False
|
| 177 |
+
st.session_state.search_start_time = None
|
| 178 |
+
st.rerun()
|
| 179 |
+
elif "no" in response_3.lower():
|
| 180 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": "Goodbye!"})
|
| 181 |
+
st.stop()
|
| 182 |
+
elif "other" in response_3.lower():
|
| 183 |
+
validation_result = validate_user_input("Analyse if user_input contains a feedback or not strictly.There is another case if you find where user_input is specifying that 'doctor arrival will be too late or when will the doctor arrive in that case add one more key 'doctor_arrival':True in the response json.Respond with a JSON object where 'response' is your analysis and 'validAnswer' is a boolean indicating if the answer is valid and 'doctor_arrival' as boolean if the user_input has the meaning containing doctor's arrival or any similar meaning user_input ,If sentence does not contain that then mark it as None. \n Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.","", user_input)
|
| 184 |
+
print(validation_result)
|
| 185 |
+
if validation_result['doctor_arrival']:
|
| 186 |
+
st.session_state.conversation_history.append({"role": "assistant", "content":st.session_state.conversation_history[-2]['content'] })
|
| 187 |
+
st.rerun()
|
| 188 |
+
if not validation_result['validAnswer']:
|
| 189 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": f"I'm sorry, but I didn't understand your response. Could you please provide with a feedback or you can restart the conversation by clicking on Reset conversation."})
|
| 190 |
+
st.rerun()
|
| 191 |
+
|
| 192 |
+
# Reset button
|
| 193 |
+
if st.button("Reset Conversation"):
|
| 194 |
+
st.session_state.state = 'initial'
|
| 195 |
+
st.session_state.conversation_history = []
|
| 196 |
+
st.session_state.search_result = None
|
| 197 |
+
st.session_state.futures = None
|
| 198 |
+
st.session_state.futuresresult = False
|
| 199 |
+
st.session_state.search_start_time = None
|
| 200 |
+
st.rerun()
|
| 201 |
+
|
| 202 |
+
if __name__ == "__main__":
|
| 203 |
+
main()
|
constants.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
LLM_MODEL = "gpt-4o-mini"
|
| 2 |
+
MAX_TOKENS = 2000
|
| 3 |
+
TEMPERATURE = 0.0
|
create_embeddings.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
sentences = "This is an example sentence"
|
| 3 |
+
|
| 4 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 5 |
+
# embeddings = model.encode(sentences)
|
| 6 |
+
# print(embeddings)
|
| 7 |
+
def create_embeddings(sentence:str):
|
| 8 |
+
embeddings = model.encode(sentences=sentence)
|
| 9 |
+
return embeddings
|
dataset.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
dataset_var = [
|
| 2 |
+
{"symptom": "not breathing", "solution": "Perform CPR: Place your hands on the center of the chest and push hard and fast at a rate of 100-120 compressions per minute. After every 30 compressions, give 2 rescue breaths."},
|
| 3 |
+
{"symptom": "bleeding", "solution": "Apply direct pressure to the wound with a clean cloth or sterile bandage. Elevate the injured area above the heart if possible."},
|
| 4 |
+
{"symptom": "chest pain", "solution": "Have the person sit down and rest. If available, give them an aspirin to chew and swallow."},
|
| 5 |
+
{"symptom": "choking", "solution": "Perform the Heimlich maneuver: Stand behind the person and wrap your arms around their waist. Make a fist with one hand and place it just above their navel. Grasp the fist with your other hand and pull inward and upward with quick, forceful thrusts."},
|
| 6 |
+
{"symptom": "unconscious", "solution": "Check for breathing. If not breathing, start CPR. If breathing, place them in the recovery position (on their side with the top leg bent for support)."},
|
| 7 |
+
{"symptom": "burns", "solution": "Cool the burn under running water for at least 10 minutes. Cover the burn with a sterile, non-stick bandage."},
|
| 8 |
+
{"symptom": "fracture", "solution": "Immobilize the injured area using a splint or sling. Avoid moving the person if a neck or spine injury is suspected."},
|
| 9 |
+
{"symptom": "allergic reaction", "solution": "Administer an epinephrine auto-injector if available. Seek emergency medical attention immediately."},
|
| 10 |
+
{"symptom": "seizure", "solution": "Protect the person from injury by removing nearby objects. Do not restrain them or put anything in their mouth. After the seizure, place them in the recovery position."},
|
| 11 |
+
{"symptom": "poisoning", "solution": "Call the Poison Control Center immediately. Do not induce vomiting unless instructed by a healthcare professional."},
|
| 12 |
+
{"symptom": "heat stroke", "solution": "Move the person to a cooler place. Apply cool, wet cloths to their skin or immerse them in cool water. Seek medical attention immediately."},
|
| 13 |
+
{"symptom": "frostbite", "solution": "Gradually warm the affected area with warm (not hot) water. Do not rub the area as it can cause further damage. Seek medical attention."},
|
| 14 |
+
{"symptom": "shock", "solution": "Lay the person down and elevate their legs if possible. Keep them warm and calm. Seek emergency medical attention."},
|
| 15 |
+
{"symptom": "asthma attack", "solution": "Help the person use their inhaler. If symptoms do not improve, seek emergency medical attention."},
|
| 16 |
+
{"symptom": "diabetic emergency", "solution": "If the person is conscious, give them a sugary drink or snack. If unconscious, seek emergency medical attention immediately."},
|
| 17 |
+
{"symptom": "drowning", "solution": "If safe, remove the person from the water. Start CPR if they are not breathing."},
|
| 18 |
+
{"symptom": "hyperventilation", "solution": "Have the person breathe slowly into a paper bag or cupped hands to help them calm down."},
|
| 19 |
+
{"symptom": "panic attack", "solution": "Encourage the person to breathe slowly and deeply. Offer reassurance and a calming environment."},
|
| 20 |
+
{"symptom": "stroke", "solution": "Call emergency services immediately. Note the time symptoms began and keep the person calm and comfortable."},
|
| 21 |
+
{"symptom": "heart attack", "solution": "Call emergency services immediately. Have the person sit down and try to stay calm. If available, give them an aspirin to chew."},
|
| 22 |
+
{"symptom": "anaphylaxis", "solution": "Administer an epinephrine auto-injector if available. Call emergency services immediately."},
|
| 23 |
+
{"symptom": "spinal injury", "solution": "Do not move the person unless absolutely necessary. Keep their head and neck aligned with the spine. Seek emergency medical attention."},
|
| 24 |
+
{"symptom": "head injury", "solution": "Keep the person still and avoid moving their head or neck. Apply ice to any swelling and seek medical attention."},
|
| 25 |
+
{"symptom": "eye injury", "solution": "Do not rub the eye. Flush it with clean water if there is debris. Seek medical attention for severe injuries."},
|
| 26 |
+
{"symptom": "abdominal pain", "solution": "Have the person lie down and rest. Avoid giving them food or drink until the cause is known. Seek medical attention if severe."},
|
| 27 |
+
{"symptom": "nosebleed", "solution": "Pinch the nostrils closed and lean forward slightly to prevent swallowing blood. Hold for 10 minutes or until bleeding stops."},
|
| 28 |
+
{"symptom": "fainting", "solution": "Lay the person down and elevate their legs. Loosen any tight clothing and ensure they are breathing normally."},
|
| 29 |
+
{"symptom": "earache", "solution": "Apply a warm compress to the affected ear. Over-the-counter pain relievers may help. Seek medical attention if symptoms persist."},
|
| 30 |
+
{"symptom": "toothache", "solution": "Rinse the mouth with warm water and apply a cold compress to the outside of the cheek. Seek dental care as soon as possible."},
|
| 31 |
+
{"symptom": "sprain", "solution": "Rest the injured area and apply ice. Compress with an elastic bandage and elevate to reduce swelling."},
|
| 32 |
+
{"symptom": "sunburn", "solution": "Apply aloe vera or a moisturizing lotion to the affected area. Stay out of the sun and drink plenty of fluids."},
|
| 33 |
+
{"symptom": "diarrhea", "solution": "Stay hydrated by drinking clear fluids. Avoid solid foods until symptoms improve. Seek medical attention if symptoms persist."},
|
| 34 |
+
{"symptom": "constipation", "solution": "Increase fiber intake and drink plenty of water. Exercise regularly and avoid holding in bowel movements."},
|
| 35 |
+
{"symptom": "nausea", "solution": "Sip on clear fluids and eat small, bland meals. Avoid strong odors and rest until symptoms subside."},
|
| 36 |
+
{"symptom": "vomiting", "solution": "Stay hydrated by sipping on clear fluids. Rest and avoid eating solid foods until vomiting stops."},
|
| 37 |
+
{"symptom": "fever", "solution": "Stay hydrated and rest. Over-the-counter medications like acetaminophen or ibuprofen can help reduce fever."},
|
| 38 |
+
{"symptom": "cough", "solution": "Drink warm fluids and use a humidifier. Over-the-counter cough suppressants may help. Seek medical attention if the cough persists."},
|
| 39 |
+
{"symptom": "sore throat", "solution": "Gargle with warm salt water and drink warm fluids. Throat lozenges or over-the-counter pain relievers may help."},
|
| 40 |
+
{"symptom": "headache", "solution": "Rest in a quiet, dark room. Over-the-counter pain relievers can help. Stay hydrated and avoid triggers like stress or certain foods."},
|
| 41 |
+
{"symptom": "muscle cramps", "solution": "Stretch and massage the affected muscle. Drink water or a sports drink to replace lost electrolytes."},
|
| 42 |
+
{"symptom": "joint pain", "solution": "Rest the affected joint and apply ice to reduce swelling. Over-the-counter pain relievers can help."},
|
| 43 |
+
{"symptom": "back pain", "solution": "Rest and apply heat or ice to the affected area. Gentle stretching and over-the-counter pain relievers can help."},
|
| 44 |
+
{"symptom": "stomach ache", "solution": "Rest and avoid eating heavy or greasy foods. Drink clear fluids and try a bland diet until symptoms improve."},
|
| 45 |
+
{"symptom": "cold symptoms", "solution": "Stay hydrated and rest. Over-the-counter medications can help relieve symptoms like congestion and sore throat."},
|
| 46 |
+
{"symptom": "flu symptoms", "solution": "Rest and drink plenty of fluids. Over-the-counter medications can help relieve symptoms like fever, aches, and congestion."},
|
| 47 |
+
{"symptom": "ear infection", "solution": "Apply a warm compress to the affected ear and take over-the-counter pain relievers. Seek medical attention if symptoms persist."},
|
| 48 |
+
{"symptom": "urinary tract infection", "solution": "Drink plenty of water and avoid caffeine and alcohol. Seek medical attention for antibiotics if symptoms persist."},
|
| 49 |
+
{"symptom": "skin rash", "solution": "Apply a cool compress and avoid scratching. Over-the-counter antihistamines or hydrocortisone cream may help."},
|
| 50 |
+
{"symptom": "acne", "solution": "Wash the affected area with a gentle cleanser and apply an over-the-counter acne treatment. Avoid picking at pimples."},
|
| 51 |
+
{"symptom": "insomnia", "solution": "Establish a regular sleep routine and avoid caffeine or electronics before bed. Consider relaxation techniques like deep breathing."},
|
| 52 |
+
{"symptom": "anxiety", "solution": "Practice deep breathing exercises and try to identify and manage stress triggers. Seek professional help if anxiety becomes overwhelming."},
|
| 53 |
+
{"symptom": "depression", "solution": "Reach out for support from friends, family, or a mental health professional. Engage in activities that bring you joy."},
|
| 54 |
+
{"symptom": "dizziness", "solution": "Sit or lie down until the dizziness passes. Stay hydrated and avoid sudden movements."},
|
| 55 |
+
{"symptom": "hiccups", "solution": "Hold your breath and swallow several times, or sip cold water slowly. Avoid carbonated drinks and eating too quickly."},
|
| 56 |
+
{"symptom": "indigestion", "solution": "Eat smaller, more frequent meals and avoid fatty or spicy foods. Over-the-counter antacids may help."},
|
| 57 |
+
{"symptom": "heartburn", "solution": "Avoid lying down after eating and avoid trigger foods like spicy or acidic foods. Over-the-counter antacids can help."},
|
| 58 |
+
{"symptom": "gas", "solution": "Avoid foods that cause gas and eat slowly to reduce swallowed air. Over-the-counter gas relief medications may help."},
|
| 59 |
+
{"symptom": "bloating", "solution": "Avoid carbonated drinks and foods that cause bloating. Stay active and drink plenty of water."},
|
| 60 |
+
{"symptom": "acid reflux", "solution": "Avoid large meals and trigger foods like fatty or acidic foods. Over-the-counter medications can help reduce symptoms."},
|
| 61 |
+
{"symptom": "cold sores", "solution": "Apply an over-the-counter antiviral cream to the affected area. Avoid sharing utensils or drinks."},
|
| 62 |
+
{"symptom": "motion sickness", "solution": "Sit in a stable part of the vehicle and focus on the horizon. Over-the-counter motion sickness medications may help."},
|
| 63 |
+
{"symptom": "morning sickness", "solution": "Eat small, frequent meals and avoid strong odors. Ginger or acupressure wristbands may help relieve symptoms."},
|
| 64 |
+
{"symptom": "bruising", "solution": "Apply ice to the affected area to reduce swelling. Elevate the area if possible."},
|
| 65 |
+
{"symptom": "blisters", "solution": "Leave the blister intact if possible. Cover with a bandage to protect from further irritation."},
|
| 66 |
+
{"symptom": "ingrown toenail", "solution": "Soak the affected foot in warm water and gently lift the nail. Apply an antibiotic ointment and bandage."},
|
| 67 |
+
{"symptom": "hangover", "solution": "Stay hydrated and eat a light meal. Rest and avoid alcohol until symptoms subside."},
|
| 68 |
+
{"symptom": "jet lag", "solution": "Adjust your sleep schedule gradually before your trip. Stay hydrated and get sunlight exposure to help reset your internal clock."},
|
| 69 |
+
{"symptom": "menstrual cramps", "solution": "Apply heat to the lower abdomen and take over-the-counter pain relievers. Exercise and relaxation techniques may help."},
|
| 70 |
+
{"symptom": "yeast infection", "solution": "Apply an over-the-counter antifungal cream or suppository. Wear loose, breathable clothing."},
|
| 71 |
+
{"symptom": "athlete's foot", "solution": "Keep feet clean and dry, and apply an over-the-counter antifungal cream. Wear breathable shoes and change socks regularly."},
|
| 72 |
+
{"symptom": "ringworm", "solution": "Apply an over-the-counter antifungal cream to the affected area. Keep the area clean and dry."},
|
| 73 |
+
{"symptom": "warts", "solution": "Apply an over-the-counter wart treatment containing salicylic acid. Avoid picking at the wart."},
|
| 74 |
+
{"symptom": "eczema", "solution": "Moisturize the affected area regularly and avoid harsh soaps. Over-the-counter hydrocortisone cream may help reduce itching."},
|
| 75 |
+
{"symptom": "psoriasis", "solution": "Apply moisturizing creams and over-the-counter topical treatments. Avoid triggers like stress and cold weather."},
|
| 76 |
+
{"symptom": "hives", "solution": "Take an over-the-counter antihistamine and avoid known triggers. Apply a cool compress to relieve itching."},
|
| 77 |
+
{"symptom": "shingles", "solution": "Apply cool, wet compresses to the affected area. Take over-the-counter pain relievers and seek medical attention."},
|
| 78 |
+
{"symptom": "chickenpox", "solution": "Apply calamine lotion to relieve itching and keep the affected area clean. Avoid scratching and stay hydrated."},
|
| 79 |
+
{"symptom": "measles", "solution": "Rest and stay hydrated. Over-the-counter medications can help relieve fever and discomfort. Seek medical attention if symptoms worsen."},
|
| 80 |
+
{"symptom": "mumps", "solution": "Rest and stay hydrated. Apply a warm or cold compress to the affected area to relieve pain. Seek medical attention."},
|
| 81 |
+
{"symptom": "whooping cough", "solution": "Stay hydrated and rest. Over-the-counter cough suppressants may help. Seek medical attention if symptoms persist."},
|
| 82 |
+
{"symptom": "tetanus", "solution": "Seek immediate medical attention. Keep the wound clean and dry until you receive treatment."},
|
| 83 |
+
{"symptom": "rabies", "solution": "Seek immediate medical attention if bitten by a wild animal. Clean the wound with soap and water."},
|
| 84 |
+
{"symptom": "tuberculosis", "solution": "Seek medical attention for diagnosis and treatment. Follow your healthcare provider's instructions for medication."},
|
| 85 |
+
{"symptom": "malaria", "solution": "Seek medical attention immediately if you suspect malaria. Follow your healthcare provider's instructions for treatment."},
|
| 86 |
+
{"symptom": "dengue fever", "solution": "Rest and stay hydrated. Over-the-counter medications can help relieve fever and pain. Seek medical attention if symptoms worsen."},
|
| 87 |
+
{"symptom": "zika virus", "solution": "Rest and stay hydrated. Over-the-counter medications can help relieve symptoms. Seek medical attention if symptoms worsen."},
|
| 88 |
+
{"symptom": "yellow fever", "solution": "Seek immediate medical attention. Follow your healthcare provider's instructions for treatment."},
|
| 89 |
+
{"symptom": "lyme disease", "solution": "Seek medical attention if bitten by a tick. Follow your healthcare provider's instructions for antibiotics."},
|
| 90 |
+
{"symptom": "rocky mountain spotted fever", "solution": "Seek medical attention immediately if bitten by a tick. Follow your healthcare provider's instructions for treatment."},
|
| 91 |
+
{"symptom": "ebola", "solution": "Seek immediate medical attention if exposed to the virus. Follow your healthcare provider's instructions for treatment."},
|
| 92 |
+
{"symptom": "meningitis", "solution": "Seek immediate medical attention. Keep the person calm and avoid bright lights."},
|
| 93 |
+
{"symptom": "hiv", "solution": "Seek medical attention for diagnosis and treatment. Follow your healthcare provider's instructions for medication."},
|
| 94 |
+
{"symptom": "hepatitis", "solution": "Seek medical attention for diagnosis and treatment. Avoid alcohol and follow your healthcare provider's instructions."},
|
| 95 |
+
{"symptom": "pneumonia", "solution": "Seek medical attention for diagnosis and treatment. Rest and stay hydrated."},
|
| 96 |
+
{"symptom": "bronchitis", "solution": "Rest and drink plenty of fluids. Over-the-counter medications can help relieve symptoms. Seek medical attention if symptoms worsen."},
|
| 97 |
+
{"symptom": "asthma", "solution": "Use your inhaler as prescribed. Avoid triggers and seek medical attention if symptoms worsen."},
|
| 98 |
+
{"symptom": "copd", "solution": "Use your inhaler as prescribed. Avoid triggers and seek medical attention if symptoms worsen."},
|
| 99 |
+
{"symptom": "cancer", "solution": "Seek medical attention for diagnosis and treatment. Follow your healthcare provider's instructions for medication and therapy."}
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
print(len(dataset_var))
|
llm.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from constants import MAX_TOKENS , TEMPERATURE
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
def contruct_prompt(system_prompt:str , user_prompt:str , conversation_history)->str:
|
| 6 |
+
final_prompt = f"""{system_prompt} \n \n
|
| 7 |
+
You strictly follow the instructions given above.\n
|
| 8 |
+
You will also be provided a user query below.\n
|
| 9 |
+
{user_prompt}
|
| 10 |
+
Below is the conversation_history.\n
|
| 11 |
+
{conversation_history}
|
| 12 |
+
Based on the instrcutions given to you and the user query and conversation history understand all of them and give accurate responses.
|
| 13 |
+
"""
|
| 14 |
+
return final_prompt
|
| 15 |
+
|
| 16 |
+
def get_llm_response(system_prompt, user_prompt, conversation_history) -> str:
|
| 17 |
+
url = "https://api.openai.com/v1/chat/completions"
|
| 18 |
+
prompt_to_llm = contruct_prompt(system_prompt, user_prompt, conversation_history)
|
| 19 |
+
headers = {
|
| 20 |
+
"Content-Type": "application/json",
|
| 21 |
+
"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}",
|
| 22 |
+
"OpenAI-Organization": os.getenv('ORG_ID')
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
data = {
|
| 26 |
+
"model": "gpt-4o-mini",
|
| 27 |
+
"max_tokens": MAX_TOKENS,
|
| 28 |
+
"messages": [{"role": "user", "content": f"{prompt_to_llm}"}],
|
| 29 |
+
"temperature": TEMPERATURE
|
| 30 |
+
}
|
| 31 |
+
try:
|
| 32 |
+
response = requests.post(url, headers=headers, json=data)
|
| 33 |
+
output = response.json()
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error Occurred {e}")
|
| 36 |
+
return f"Error Occurred {e}"
|
| 37 |
+
|
| 38 |
+
return output['choices'][0]['message']['content']
|
requirements.txt
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair==5.4.1
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anyio==4.4.0
|
| 4 |
+
attrs==24.2.0
|
| 5 |
+
blinker==1.8.2
|
| 6 |
+
cachetools==5.5.0
|
| 7 |
+
certifi==2024.8.30
|
| 8 |
+
charset-normalizer==3.3.2
|
| 9 |
+
click==8.1.7
|
| 10 |
+
distro==1.9.0
|
| 11 |
+
filelock==3.15.4
|
| 12 |
+
fsspec==2024.6.1
|
| 13 |
+
gitdb==4.0.11
|
| 14 |
+
GitPython==3.1.43
|
| 15 |
+
googleapis-common-protos==1.65.0
|
| 16 |
+
grpcio==1.66.1
|
| 17 |
+
h11==0.14.0
|
| 18 |
+
httpcore==1.0.5
|
| 19 |
+
httpx==0.27.2
|
| 20 |
+
huggingface-hub==0.24.6
|
| 21 |
+
idna==3.8
|
| 22 |
+
Jinja2==3.1.4
|
| 23 |
+
jiter==0.5.0
|
| 24 |
+
joblib==1.4.2
|
| 25 |
+
jsonschema==4.23.0
|
| 26 |
+
jsonschema-specifications==2023.12.1
|
| 27 |
+
lz4==4.3.3
|
| 28 |
+
markdown-it-py==3.0.0
|
| 29 |
+
MarkupSafe==2.1.5
|
| 30 |
+
mdurl==0.1.2
|
| 31 |
+
mpmath==1.3.0
|
| 32 |
+
narwhals==1.6.0
|
| 33 |
+
networkx==3.3
|
| 34 |
+
numpy==2.1.0
|
| 35 |
+
openai==1.43.0
|
| 36 |
+
packaging==24.1
|
| 37 |
+
pandas==2.2.2
|
| 38 |
+
pillow==10.4.0
|
| 39 |
+
pinecone==5.1.0
|
| 40 |
+
pinecone-client==5.0.1
|
| 41 |
+
pinecone-plugin-inference==1.0.3
|
| 42 |
+
pinecone-plugin-interface==0.0.7
|
| 43 |
+
protobuf==4.25.4
|
| 44 |
+
protoc-gen-openapiv2==0.0.1
|
| 45 |
+
pyarrow==17.0.0
|
| 46 |
+
pydantic==2.8.2
|
| 47 |
+
pydantic_core==2.20.1
|
| 48 |
+
pydeck==0.9.1
|
| 49 |
+
Pygments==2.18.0
|
| 50 |
+
python-dateutil==2.9.0.post0
|
| 51 |
+
python-dotenv==1.0.1
|
| 52 |
+
pytz==2024.1
|
| 53 |
+
PyYAML==6.0.2
|
| 54 |
+
referencing==0.35.1
|
| 55 |
+
regex==2024.7.24
|
| 56 |
+
requests==2.32.3
|
| 57 |
+
rich==13.8.0
|
| 58 |
+
rpds-py==0.20.0
|
| 59 |
+
safetensors==0.4.4
|
| 60 |
+
scikit-learn==1.5.1
|
| 61 |
+
scipy==1.14.1
|
| 62 |
+
sentence-transformers==3.0.1
|
| 63 |
+
setuptools==74.0.0
|
| 64 |
+
six==1.16.0
|
| 65 |
+
smmap==5.0.1
|
| 66 |
+
sniffio==1.3.1
|
| 67 |
+
streamlit==1.38.0
|
| 68 |
+
sympy==1.13.2
|
| 69 |
+
tenacity==8.5.0
|
| 70 |
+
threadpoolctl==3.5.0
|
| 71 |
+
tokenizers==0.19.1
|
| 72 |
+
toml==0.10.2
|
| 73 |
+
torch==2.4.0
|
| 74 |
+
tornado==6.4.1
|
| 75 |
+
tqdm==4.66.5
|
| 76 |
+
transformers==4.44.2
|
| 77 |
+
typing_extensions==4.12.2
|
| 78 |
+
tzdata==2024.1
|
| 79 |
+
urllib3==2.2.2
|
vector_database.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pinecone import ServerlessSpec
|
| 2 |
+
from pinecone.grpc import PineconeGRPC as Pinecone
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
load_dotenv()
|
| 5 |
+
import os
|
| 6 |
+
from dataset import dataset_var
|
| 7 |
+
from create_embeddings import create_embeddings
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
pc = Pinecone(api_key=os.getenv('PINECONE_API_KEY'))
|
| 11 |
+
|
| 12 |
+
index_name = "ai-receptionist"
|
| 13 |
+
|
| 14 |
+
if index_name not in pc.list_indexes().names():
|
| 15 |
+
pc.create_index(
|
| 16 |
+
name=index_name,
|
| 17 |
+
dimension=384,
|
| 18 |
+
metric="cosine",
|
| 19 |
+
spec=ServerlessSpec(
|
| 20 |
+
cloud='aws',
|
| 21 |
+
region='us-east-1'
|
| 22 |
+
)
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
#Creating a vector index
|
| 26 |
+
index = pc.Index(index_name)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def vector_search_v1(emergency_description):
|
| 30 |
+
emergency_embedding = create_embeddings(emergency_description)
|
| 31 |
+
query_results = index.query(
|
| 32 |
+
namespace="ai-receptionist-namespace-1",
|
| 33 |
+
vector=emergency_embedding,
|
| 34 |
+
top_k=1,
|
| 35 |
+
include_values=True
|
| 36 |
+
)
|
| 37 |
+
answers= ""
|
| 38 |
+
answers += query_results.get('matches','')[0].get('id')
|
| 39 |
+
return answers
|
| 40 |
+
# return "Perform CPR: Place your hands on the center of the chest and push hard and fast at a rate of 100-120 compressions per minute. After every 30 compressions, give 2 rescue breaths."
|
| 41 |
+
|
| 42 |
+
#inserting the data['symptoms'] into the pinecone
|
| 43 |
+
|
| 44 |
+
# for ds in dataset_var:
|
| 45 |
+
# embedding = create_embeddings(ds['symptom'])
|
| 46 |
+
# index.upsert(
|
| 47 |
+
# vectors=[
|
| 48 |
+
# {"id": ds['solution'], "values": embedding},
|
| 49 |
+
# ],
|
| 50 |
+
# namespace="ai-receptionist-namespace-1"
|
| 51 |
+
# )
|
| 52 |
+
# print(ds['symptom'] , ds['solution'])
|
| 53 |
+
|
| 54 |
+
|