Spaces:
Sleeping
Sleeping
jjz5463
commited on
Commit
·
6f2d9ff
1
Parent(s):
46757c3
click(number) instead of button name
Browse files- chatbot_simulator.py +11 -11
chatbot_simulator.py
CHANGED
|
@@ -180,7 +180,7 @@ class ChatbotSimulation:
|
|
| 180 |
return greeting + gpt_instruction
|
| 181 |
|
| 182 |
def _extract_buttons(self):
|
| 183 |
-
"""Extract
|
| 184 |
# Get the last message
|
| 185 |
last_message = self.conversation[-1]
|
| 186 |
|
|
@@ -191,7 +191,7 @@ class ChatbotSimulation:
|
|
| 191 |
# Extract the content of the last message
|
| 192 |
message_content = last_message.get("content", "")
|
| 193 |
|
| 194 |
-
#
|
| 195 |
options_split = re.split(r"you have the following options:", message_content, flags=re.IGNORECASE)
|
| 196 |
|
| 197 |
# If the split doesn't produce at least two parts, return an empty dictionary
|
|
@@ -200,11 +200,11 @@ class ChatbotSimulation:
|
|
| 200 |
|
| 201 |
# Extract button definitions from the second part of the split content
|
| 202 |
button_section = options_split[1]
|
| 203 |
-
pattern = r"\d
|
| 204 |
buttons = re.findall(pattern, button_section)
|
| 205 |
|
| 206 |
-
# Construct the dictionary with button
|
| 207 |
-
return {
|
| 208 |
|
| 209 |
def _is_valid_input(self, user_input):
|
| 210 |
"""Validate user input format."""
|
|
@@ -214,27 +214,27 @@ class ChatbotSimulation:
|
|
| 214 |
return [True, "Enter Anything is empty"]
|
| 215 |
|
| 216 |
# Validate input format
|
| 217 |
-
pattern = r"^(?P<action_type>\w+)\((?P<
|
| 218 |
match = re.match(pattern, user_input)
|
| 219 |
|
| 220 |
if not match:
|
| 221 |
-
return [False, "Your input doesn't match the format: action_type(button
|
| 222 |
|
| 223 |
# Extract parsed components
|
| 224 |
action_type = match.group("action_type").lower()
|
| 225 |
-
button_name = match.group("
|
| 226 |
query = match.group("query") # Optional query for `type`
|
| 227 |
|
| 228 |
-
# Validate button
|
| 229 |
if button_name not in valid_buttons:
|
| 230 |
return [False,
|
| 231 |
-
"Invalid Button
|
| 232 |
if action_type != valid_buttons[button_name]:
|
| 233 |
return [False,
|
| 234 |
"Invalid action type! Recall: Each button is in the format: `number. button name: action_type`"] # Action type must match the button's specified type
|
| 235 |
if action_type == "type" and query is None:
|
| 236 |
return [False,
|
| 237 |
-
"Missing Query for action type 'type'! Recall: use the format: `type(button
|
| 238 |
if action_type != "type" and query is not None:
|
| 239 |
return [False,
|
| 240 |
"Non-`type` action_type cannot take query!"] # Non-`type` actions must not have a query
|
|
|
|
| 180 |
return greeting + gpt_instruction
|
| 181 |
|
| 182 |
def _extract_buttons(self):
|
| 183 |
+
"""Extract button numbers and their action types from the latest conversation if role is 'assistant'."""
|
| 184 |
# Get the last message
|
| 185 |
last_message = self.conversation[-1]
|
| 186 |
|
|
|
|
| 191 |
# Extract the content of the last message
|
| 192 |
message_content = last_message.get("content", "")
|
| 193 |
|
| 194 |
+
# Split the message content to isolate the button section
|
| 195 |
options_split = re.split(r"you have the following options:", message_content, flags=re.IGNORECASE)
|
| 196 |
|
| 197 |
# If the split doesn't produce at least two parts, return an empty dictionary
|
|
|
|
| 200 |
|
| 201 |
# Extract button definitions from the second part of the split content
|
| 202 |
button_section = options_split[1]
|
| 203 |
+
pattern = r"(\d+)\.\s+(.*?):\s+([a-zA-Z_]+)" # Capture the number, button name, and action type
|
| 204 |
buttons = re.findall(pattern, button_section)
|
| 205 |
|
| 206 |
+
# Construct the dictionary with button numbers as keys and action types as values
|
| 207 |
+
return {number: action_type.strip().lower() for number, _, action_type in buttons}
|
| 208 |
|
| 209 |
def _is_valid_input(self, user_input):
|
| 210 |
"""Validate user input format."""
|
|
|
|
| 214 |
return [True, "Enter Anything is empty"]
|
| 215 |
|
| 216 |
# Validate input format
|
| 217 |
+
pattern = r"^(?P<action_type>\w+)\((?P<button_number>[^,]+)(?:,\s*(?P<query>.+))?\)$"
|
| 218 |
match = re.match(pattern, user_input)
|
| 219 |
|
| 220 |
if not match:
|
| 221 |
+
return [False, "Your input doesn't match the format: action_type(button number), OR if type, use type(button number, query)"]
|
| 222 |
|
| 223 |
# Extract parsed components
|
| 224 |
action_type = match.group("action_type").lower()
|
| 225 |
+
button_name = match.group("button_number").strip().lower()
|
| 226 |
query = match.group("query") # Optional query for `type`
|
| 227 |
|
| 228 |
+
# Validate button number and action type
|
| 229 |
if button_name not in valid_buttons:
|
| 230 |
return [False,
|
| 231 |
+
"Invalid Button number! Recall: Each button is in the format: `number. button name: action_type`"] # Button number must match exactly (case insensitive)
|
| 232 |
if action_type != valid_buttons[button_name]:
|
| 233 |
return [False,
|
| 234 |
"Invalid action type! Recall: Each button is in the format: `number. button name: action_type`"] # Action type must match the button's specified type
|
| 235 |
if action_type == "type" and query is None:
|
| 236 |
return [False,
|
| 237 |
+
"Missing Query for action type 'type'! Recall: use the format: `type(button number, query)`"] # `type` action requires a query
|
| 238 |
if action_type != "type" and query is not None:
|
| 239 |
return [False,
|
| 240 |
"Non-`type` action_type cannot take query!"] # Non-`type` actions must not have a query
|