colin4554 commited on
Commit
fdea788
·
1 Parent(s): 12f769d

better handle agent handoff

Browse files
Files changed (2) hide show
  1. app.py +34 -26
  2. functions.py +2 -2
app.py CHANGED
@@ -83,7 +83,11 @@ if "openai_model" not in st.session_state:
83
 
84
  if "messages" not in st.session_state:
85
  st.session_state.messages = [{"role": "system", "content": "You are a helpful customer support agent for The Home "
86
- "Depot. Your goal is to answer as many questions as possible without escalating to a human agent."},]
 
 
 
 
87
 
88
  for message in st.session_state.messages:
89
  if message["role"] == "assistant" or message["role"] == "user":
@@ -133,31 +137,35 @@ if prompt := st.chat_input("How can we help you today?"):
133
  print(function_response)
134
  st.session_state.messages.append(function_response)
135
 
136
- message_placeholder = st.empty()
137
- full_message = ""
138
-
139
- for response in client.chat.completions.create(
140
- model=st.session_state["openai_model"],
141
- messages=[
142
- {"role": m["role"], "content": m["content"], "name": m["name"]} if "name" in m else
143
- {"role": m["role"], "content": m["content"]}
144
- for m in st.session_state.messages
145
- ],
146
- functions=functions,
147
- function_call="auto",
148
- stream=True,
149
- ):
150
- if len(response.choices) > 0:
151
- delta = response.choices[0].delta
152
-
153
- full_message += (delta.content or "")
154
- if delta.function_call is not None:
155
- if delta.function_call.name is not None:
156
- func_call["name"] = delta.function_call.name
157
- if delta.function_call.arguments is not None:
158
- func_call["arguments"] += delta.function_call.arguments
159
-
160
- message_placeholder.markdown(full_message + "")
 
 
 
 
161
 
162
  message_placeholder.markdown(full_message)
163
 
 
83
 
84
  if "messages" not in st.session_state:
85
  st.session_state.messages = [{"role": "system", "content": "You are a helpful customer support agent for The Home "
86
+ "Depot. Your goal is to answer as many questions as "
87
+ "possible without escalating to a human agent. "
88
+ "However, if necessary, you can refer the customer to "
89
+ "a human agent if you do not know the answer to their "
90
+ "question. For example, you can help users track their orders, but you **cannot** help with returns."},]
91
 
92
  for message in st.session_state.messages:
93
  if message["role"] == "assistant" or message["role"] == "user":
 
137
  print(function_response)
138
  st.session_state.messages.append(function_response)
139
 
140
+ if function_response["name"] is not None and function_response["name"] == "refer_to_human_agent":
141
+ print("connect to human agent")
142
+ st.info('You will be connected with an agent shortly', icon="ℹ️")
143
+ else:
144
+ message_placeholder = st.empty()
145
+ full_message = ""
146
+
147
+ for response in client.chat.completions.create(
148
+ model=st.session_state["openai_model"],
149
+ messages=[
150
+ {"role": m["role"], "content": m["content"], "name": m["name"]} if "name" in m else
151
+ {"role": m["role"], "content": m["content"]}
152
+ for m in st.session_state.messages
153
+ ],
154
+ functions=functions,
155
+ function_call="auto",
156
+ stream=True,
157
+ ):
158
+ if len(response.choices) > 0:
159
+ delta = response.choices[0].delta
160
+
161
+ full_message += (delta.content or "")
162
+ if delta.function_call is not None:
163
+ if delta.function_call.name is not None:
164
+ func_call["name"] = delta.function_call.name
165
+ if delta.function_call.arguments is not None:
166
+ func_call["arguments"] += delta.function_call.arguments
167
+
168
+ message_placeholder.markdown(full_message + "")
169
 
170
  message_placeholder.markdown(full_message)
171
 
functions.py CHANGED
@@ -185,5 +185,5 @@ def get_product_listing(sku: str):
185
  return response.text
186
 
187
 
188
- def refer_to_human_agent(summary: str) -> str:
189
- return "I'm sorry, I don't know how to help you with that. I'll refer you to a human agent."
 
185
  return response.text
186
 
187
 
188
+ def refer_to_human_agent(summary: str):
189
+ return {"role": "function", "content": summary, "name": "refer_to_human_agent"}