yrobel-lima commited on
Commit
3b5c559
·
verified ·
1 Parent(s): 4320b9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -33
app.py CHANGED
@@ -4,12 +4,14 @@ from typing import Optional
4
  import openai
5
  import streamlit as st
6
  from langchain_core.messages import AIMessage, HumanMessage
 
7
 
8
  from rag.runnable_and_memory import get_runnable_and_memory
9
  from utils.error_message_template import ERROR_MESSAGE
10
 
11
  logging.basicConfig(level=logging.ERROR)
12
 
 
13
 
14
  # Streamlit page configuration
15
  st.set_page_config(
@@ -19,7 +21,7 @@ st.set_page_config(
19
  initial_sidebar_state="collapsed",
20
  )
21
 
22
- # CSS
23
  with open("styles/styles.css") as css:
24
  st.markdown(f"<style>{css.read()}</style>", unsafe_allow_html=True)
25
 
@@ -113,38 +115,52 @@ if __name__ == "__main__":
113
 
114
  # Chat interface
115
  if user_input and user_input.strip():
116
- st.session_state["chat_history"].append(HumanMessage(content=user_input))
117
-
118
- # Append the location to the user input (important!)
119
- user_query_location = (
120
- f"{user_input}\nLocation: {st.session_state.selected_location}."
121
  )
122
 
123
- # Render the user input
124
- with st.chat_message("Human", avatar=avatars["Human"]):
125
- st.write(user_input)
126
-
127
- # Render the AI response
128
- with st.chat_message("AI", avatar=avatars["AI"]):
129
- try:
130
- with st.spinner(" "):
131
- response = st.write_stream(
132
- st.session_state["runnable"].stream(
133
- {"user_query": user_query_location}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  )
135
- )
136
- except openai.BadRequestError:
137
- handle_errors()
138
- except Exception:
139
- handle_errors()
140
-
141
- # Add AI response to the message history
142
- st.session_state["chat_history"].append(AIMessage(content=response))
143
-
144
- # Update runnable memory
145
- st.session_state["memory"].chat_memory.add_user_message(
146
- HumanMessage(content=user_query_location)
147
- )
148
- st.session_state["memory"].chat_memory.add_ai_message(
149
- AIMessage(content=response)
150
- )
 
4
  import openai
5
  import streamlit as st
6
  from langchain_core.messages import AIMessage, HumanMessage
7
+ from openai import OpenAI
8
 
9
  from rag.runnable_and_memory import get_runnable_and_memory
10
  from utils.error_message_template import ERROR_MESSAGE
11
 
12
  logging.basicConfig(level=logging.ERROR)
13
 
14
+ openai_client = OpenAI()
15
 
16
  # Streamlit page configuration
17
  st.set_page_config(
 
21
  initial_sidebar_state="collapsed",
22
  )
23
 
24
+ # Streamlit CSS configuration
25
  with open("styles/styles.css") as css:
26
  st.markdown(f"<style>{css.read()}</style>", unsafe_allow_html=True)
27
 
 
115
 
116
  # Chat interface
117
  if user_input and user_input.strip():
118
+ # OPENAI moderator api
119
+ moderator = openai_client.moderations.create(
120
+ model="omni-moderation-latest",
121
+ input=user_input,
 
122
  )
123
 
124
+ if moderator.results[0].flagged:
125
+ response = """Sorry, I can't process your message because it doesn't follow our content guidelines. Please revise it and try again.
126
+ If you need assistance, please contact our clinic directly [here](https://www.talltreehealth.ca/contact-us).
127
+ """
128
+
129
+ with st.chat_message("AI", avatar=avatars["AI"]):
130
+ st.warning(response, icon="⚠️")
131
+
132
+ else:
133
+ st.session_state["chat_history"].append(HumanMessage(content=user_input))
134
+ # Append the location to the user input (important!)
135
+ user_query_location = (
136
+ f"{user_input}\nLocation: {st.session_state.selected_location}."
137
+ )
138
+
139
+ # Render the user input
140
+ with st.chat_message("Human", avatar=avatars["Human"]):
141
+ st.write(user_input)
142
+
143
+ # Render the AI response
144
+ with st.chat_message("AI", avatar=avatars["AI"]):
145
+ try:
146
+ with st.spinner(" "):
147
+ response = st.write_stream(
148
+ st.session_state["runnable"].stream(
149
+ {"user_query": user_query_location}
150
+ )
151
  )
152
+ except openai.BadRequestError:
153
+ handle_errors()
154
+ except Exception:
155
+ handle_errors()
156
+
157
+ # Add AI response to the message history
158
+ st.session_state["chat_history"].append(AIMessage(content=response))
159
+
160
+ # Update runnable memory
161
+ st.session_state["memory"].chat_memory.add_user_message(
162
+ HumanMessage(content=user_query_location)
163
+ )
164
+ st.session_state["memory"].chat_memory.add_ai_message(
165
+ AIMessage(content=response)
166
+ )