Spaces:
Runtime error
Runtime error
better handle agent handoff
Browse files- app.py +34 -26
- 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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
if
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
| 189 |
-
return "
|
|
|
|
| 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"}
|