SaileshSivan commited on
Commit
8027f5b
·
1 Parent(s): 8f277d4

Updated prompt template.

Browse files

Updated prompt template to a Doctor AI.

Files changed (1) hide show
  1. app.py +43 -24
app.py CHANGED
@@ -1,40 +1,59 @@
1
- ## Doctor AI
2
  import streamlit as st
3
-
4
- from langchain.schema import HumanMessage,SystemMessage,AIMessage
5
  from langchain.chat_models import ChatOpenAI
6
 
7
- ## Streamlit UI
8
- st.set_page_config(page_title="Conversational Q&A Chatbot")
9
- st.header("Hey, Let's Chat")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  from dotenv import load_dotenv
12
  load_dotenv()
13
  import os
14
 
15
- chat=ChatOpenAI(temperature=0.5)
 
16
 
17
  if 'flowmessages' not in st.session_state:
18
  st.session_state['flowmessages'] = [
19
  SystemMessage(content="Your are an AI Doctor assistant. A user will give an input of what he is suffering from or what health problem he has, you should suggest the user with correct medicine and tell the user how to recover fastly from it. Gve a short and sharp answer. If the input is different from a body or health issue or any other medical issues, tell the user who you are and ask the user to provide the appropriate input.")
20
  ]
21
 
22
- ## Function to load OpenAI model and get respones
23
-
24
- def get_chatmodel_response(question):
25
-
26
- st.session_state['flowmessages'].append(HumanMessage(content=question))
27
- answer=chat(st.session_state['flowmessages'])
28
- st.session_state['flowmessages'].append(AIMessage(content=answer.content))
29
- return answer.content
30
-
31
- input=st.text_input("Input: ",key="input")
32
- response=get_chatmodel_response(input)
33
-
34
- submit=st.button("Ask the question")
35
-
36
- ## If ask button is clicked
37
 
 
38
  if submit:
39
- st.subheader("The Response is")
40
- st.write(response)
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import time
3
+ from langchain.schema import HumanMessage, SystemMessage, AIMessage
4
  from langchain.chat_models import ChatOpenAI
5
 
6
+
7
+ def get_chatmodel_response(question):
8
+ # Retry logic
9
+ max_retries = 3
10
+ retries = 0
11
+
12
+ while retries < max_retries:
13
+ try:
14
+ st.session_state['flowmessages'].append(HumanMessage(content=question))
15
+ answer = chat(st.session_state['flowmessages'])
16
+ st.session_state['flowmessages'].append(AIMessage(content=answer.content))
17
+ return answer.content
18
+ except Exception as e:
19
+ print(f"Error: {e}")
20
+ if "Rate limit" in str(e):
21
+ print(f"Rate limit exceeded. Waiting and retrying...")
22
+ time.sleep(5) # Adjust the waiting time as needed
23
+ retries += 1
24
+ else:
25
+ print("Unhandled exception. Please try again later.")
26
+ break
27
+
28
+ print("Exceeded the maximum number of retries. Please try again later.")
29
+ return None
30
+
31
+ # Streamlit app setup
32
+ st.set_page_config(page_title="Doctor AI")
33
+ st.header("Hello, how can I help you?!")
34
 
35
  from dotenv import load_dotenv
36
  load_dotenv()
37
  import os
38
 
39
+ # ChatOpenAI class
40
+ chat = ChatOpenAI(temperature=0.5)
41
 
42
  if 'flowmessages' not in st.session_state:
43
  st.session_state['flowmessages'] = [
44
  SystemMessage(content="Your are an AI Doctor assistant. A user will give an input of what he is suffering from or what health problem he has, you should suggest the user with correct medicine and tell the user how to recover fastly from it. Gve a short and sharp answer. If the input is different from a body or health issue or any other medical issues, tell the user who you are and ask the user to provide the appropriate input.")
45
  ]
46
 
47
+ # Streamlit UI
48
+ input_question = st.text_input("Type here.", key="input")
49
+ submit = st.button("Submit")
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ # If the "Ask" button is clicked
52
  if submit:
53
+ response = get_chatmodel_response(input_question)
54
+
55
+ if response is not None:
56
+ # st.subheader("Here you go,")
57
+ st.write(response)
58
+ else:
59
+ st.subheader("Error: Unable to get response. Please try again later.")