Spaces:
Sleeping
Sleeping
prompt update
Browse files- chatbot_simulator.py +17 -25
- task_specific_data_population.py +8 -5
chatbot_simulator.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from openai import OpenAI
|
2 |
import json_repair
|
|
|
3 |
|
4 |
|
5 |
class ChatbotSimulation:
|
@@ -12,7 +13,6 @@ class ChatbotSimulation:
|
|
12 |
self.user_state['current_page'] = 'Home' # Initialize current page
|
13 |
self.user_state['last_page'] = 'Home'
|
14 |
self.user_state['task_completed'] = 0
|
15 |
-
self.user_last_action = ''
|
16 |
self.task = task
|
17 |
self.app_name = app_name
|
18 |
self.log_location = log_location
|
@@ -26,6 +26,7 @@ class ChatbotSimulation:
|
|
26 |
self.prompt_count = 0
|
27 |
self.client = OpenAI(api_key=openai_api_key)
|
28 |
self.actions = []
|
|
|
29 |
|
30 |
def _get_page_uid(self, page_name):
|
31 |
"""Retrieve the UID of the given page from the sitemap."""
|
@@ -45,7 +46,7 @@ class ChatbotSimulation:
|
|
45 |
return f"""
|
46 |
You are a text-based simulator of {self.app_name} app.
|
47 |
You are interacting with a user. User's task is: {self.task}.
|
48 |
-
User's last page was {last_page} and the user
|
49 |
After action, user is currently on the {current_page} page.
|
50 |
Current user state: {self.user_state}.
|
51 |
|
@@ -59,7 +60,7 @@ Provide instructions or request input from the user. If the user provides an inv
|
|
59 |
"Invalid action. Please select a valid option."
|
60 |
|
61 |
### Instruction Format:
|
62 |
-
You are at the {current_page} page. You have the following options:
|
63 |
1. Feature 1
|
64 |
2. Feature 2
|
65 |
3. Feature 3
|
@@ -70,7 +71,7 @@ Please enter your choice as 'Number. Description'. If you have a query, enter as
|
|
70 |
Rules:
|
71 |
- Be sure to display all options that is available in features.
|
72 |
- Be robotic and emotionless. Avoid offering any advice to the user.
|
73 |
-
- If user
|
74 |
"""
|
75 |
|
76 |
def _get_openai_response(self, prompt):
|
@@ -80,25 +81,29 @@ Rules:
|
|
80 |
model="gpt-4",
|
81 |
messages=prompt,
|
82 |
max_tokens=self.buffer_tokens, # Adjusted max_tokens if needed
|
83 |
-
temperature=0,
|
84 |
)
|
85 |
return response.choices[0].message.content
|
86 |
|
87 |
def _calculate_token_count(self, conversation):
|
88 |
-
"""
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
def _trim_conversation(self):
|
92 |
"""Trim the conversation to keep it within the token limit."""
|
93 |
-
while self._calculate_token_count(self.conversation) >= (self.max_tokens - self.buffer_tokens *
|
94 |
self.conversation.pop(0)
|
95 |
|
96 |
def one_conversation_round(self, user_input):
|
97 |
"""Conduct one round of conversation between the user and the assistant."""
|
98 |
# User provides input
|
99 |
-
self.
|
100 |
self.conversation.append({"role": "user", "content": user_input})
|
101 |
-
self.actions.append(user_input)
|
102 |
|
103 |
# Update user state using GPT's response
|
104 |
update_prompt = f"""
|
@@ -131,19 +136,6 @@ Example Output Format:
|
|
131 |
# Parse and update the user state
|
132 |
updated_state = json_repair.loads(updated_state)
|
133 |
|
134 |
-
# if isinstance(updated_state, list):
|
135 |
-
# reformat_prompt = f'''
|
136 |
-
# Given the {updated_state}, reformat it into a proper JSON.
|
137 |
-
# Make sure follow the format:
|
138 |
-
# {{
|
139 |
-
# 'current_page': 'Home',
|
140 |
-
# 'task_completed': 0,
|
141 |
-
# }}
|
142 |
-
# '''
|
143 |
-
# self.conversation.append({"role": "assistant", "content": reformat_prompt})
|
144 |
-
# reformat_state = self._get_openai_response(self.conversation)
|
145 |
-
# updated_state = json_repair.loads(reformat_state)
|
146 |
-
|
147 |
try:
|
148 |
updated_state['task_completed'] = int(updated_state['task_completed'])
|
149 |
if updated_state['task_completed']:
|
@@ -153,7 +145,7 @@ Example Output Format:
|
|
153 |
|
154 |
self.user_state = updated_state
|
155 |
|
156 |
-
self.conversation.clear()
|
157 |
system_prompt = self._generate_system_prompt()
|
158 |
|
159 |
# GPT generates the page instructions
|
@@ -183,7 +175,7 @@ Then generate an "Action" which is the immediate next step you can take.
|
|
183 |
agent_response = self.client.chat.completions.create(
|
184 |
model="gpt-4",
|
185 |
messages=messages,
|
186 |
-
temperature=
|
187 |
)
|
188 |
print(f"LLM-Agent: {agent_response.choices[0].message.content}")
|
189 |
return agent_response.choices[0].message.content
|
|
|
1 |
from openai import OpenAI
|
2 |
import json_repair
|
3 |
+
from transformers import AutoTokenizer
|
4 |
|
5 |
|
6 |
class ChatbotSimulation:
|
|
|
13 |
self.user_state['current_page'] = 'Home' # Initialize current page
|
14 |
self.user_state['last_page'] = 'Home'
|
15 |
self.user_state['task_completed'] = 0
|
|
|
16 |
self.task = task
|
17 |
self.app_name = app_name
|
18 |
self.log_location = log_location
|
|
|
26 |
self.prompt_count = 0
|
27 |
self.client = OpenAI(api_key=openai_api_key)
|
28 |
self.actions = []
|
29 |
+
self.tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
30 |
|
31 |
def _get_page_uid(self, page_name):
|
32 |
"""Retrieve the UID of the given page from the sitemap."""
|
|
|
46 |
return f"""
|
47 |
You are a text-based simulator of {self.app_name} app.
|
48 |
You are interacting with a user. User's task is: {self.task}.
|
49 |
+
User's last page was {last_page} and the user have taken actions: {self.actions}.
|
50 |
After action, user is currently on the {current_page} page.
|
51 |
Current user state: {self.user_state}.
|
52 |
|
|
|
60 |
"Invalid action. Please select a valid option."
|
61 |
|
62 |
### Instruction Format:
|
63 |
+
<if actions is non-empty: You have successfully done {self.actions[-1]}> You are at the {current_page} page. You have the following options:
|
64 |
1. Feature 1
|
65 |
2. Feature 2
|
66 |
3. Feature 3
|
|
|
71 |
Rules:
|
72 |
- Be sure to display all options that is available in features.
|
73 |
- Be robotic and emotionless. Avoid offering any advice to the user.
|
74 |
+
- **If the user requests information you do not possess** (such as a list of restaurants, menus, or similar details), you are permitted to create plausible and relevant information to fulfill the request. Present this fabricated information convincingly as if it were real data.
|
75 |
"""
|
76 |
|
77 |
def _get_openai_response(self, prompt):
|
|
|
81 |
model="gpt-4",
|
82 |
messages=prompt,
|
83 |
max_tokens=self.buffer_tokens, # Adjusted max_tokens if needed
|
84 |
+
temperature=0.7,
|
85 |
)
|
86 |
return response.choices[0].message.content
|
87 |
|
88 |
def _calculate_token_count(self, conversation):
|
89 |
+
"""Accurately calculate the token count in the conversation using a tokenizer."""
|
90 |
+
total_tokens = 0
|
91 |
+
for entry in conversation:
|
92 |
+
# Tokenize each entry content and count tokens
|
93 |
+
tokens = self.tokenizer.encode(entry['content'], truncation=False, add_special_tokens=False)
|
94 |
+
total_tokens += len(tokens)
|
95 |
+
return total_tokens
|
96 |
|
97 |
def _trim_conversation(self):
|
98 |
"""Trim the conversation to keep it within the token limit."""
|
99 |
+
while self._calculate_token_count(self.conversation) >= (self.max_tokens - self.buffer_tokens * 2):
|
100 |
self.conversation.pop(0)
|
101 |
|
102 |
def one_conversation_round(self, user_input):
|
103 |
"""Conduct one round of conversation between the user and the assistant."""
|
104 |
# User provides input
|
105 |
+
self.actions.append(user_input + f'on {self.user_state["current_page"]} page')
|
106 |
self.conversation.append({"role": "user", "content": user_input})
|
|
|
107 |
|
108 |
# Update user state using GPT's response
|
109 |
update_prompt = f"""
|
|
|
136 |
# Parse and update the user state
|
137 |
updated_state = json_repair.loads(updated_state)
|
138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
try:
|
140 |
updated_state['task_completed'] = int(updated_state['task_completed'])
|
141 |
if updated_state['task_completed']:
|
|
|
145 |
|
146 |
self.user_state = updated_state
|
147 |
|
148 |
+
#self.conversation.clear()
|
149 |
system_prompt = self._generate_system_prompt()
|
150 |
|
151 |
# GPT generates the page instructions
|
|
|
175 |
agent_response = self.client.chat.completions.create(
|
176 |
model="gpt-4",
|
177 |
messages=messages,
|
178 |
+
temperature=0.7,
|
179 |
)
|
180 |
print(f"LLM-Agent: {agent_response.choices[0].message.content}")
|
181 |
return agent_response.choices[0].message.content
|
task_specific_data_population.py
CHANGED
@@ -28,7 +28,7 @@ class DataPopulation:
|
|
28 |
model="gpt-4",
|
29 |
messages=conversation,
|
30 |
max_tokens=1000, # Adjusted max_tokens if needed
|
31 |
-
temperature=0,
|
32 |
)
|
33 |
return response.choices[0].message.content.strip()
|
34 |
|
@@ -38,21 +38,23 @@ class DataPopulation:
|
|
38 |
"role": "user",
|
39 |
"content": (
|
40 |
f"Given the task: '{task}' and the sitemap:\n{sitemap}\n\n"
|
41 |
-
"
|
|
|
42 |
"Return the page names exactly as they appear in the sitemap, in JSON format. "
|
43 |
"For each relevant page, provide a brief explanation of its relevance. "
|
44 |
-
"Example response:\
|
45 |
)
|
46 |
})
|
47 |
response_content = self.gpt4_chat(self.conversation)
|
48 |
return response_content
|
49 |
|
50 |
-
def _update_user_data(self, task, relevant_page_details):
|
51 |
"""Populate the relevant user data for the task."""
|
52 |
self.conversation.append({
|
53 |
"role": "user",
|
54 |
"content": (
|
55 |
f"Given the task: '{task}' and the following task-relevant page details:\n{relevant_page_details}\n\n"
|
|
|
56 |
f"Update each page's 'user_data' value with essential information for task-completion."
|
57 |
f"For example, if a task ask us to retrieve previous order, then we will need to populate synthetic order history in user_data."
|
58 |
"Ensure output maintain the exact format and structure as input page details."
|
@@ -96,6 +98,7 @@ class DataPopulation:
|
|
96 |
|
97 |
# Step 1: Identify relevant pages
|
98 |
relevant_pages = self.ask_for_relevant_pages(task, sitemap)
|
|
|
99 |
self.conversation.append({"role": "assistant", "content": relevant_pages})
|
100 |
relevant_pages = json_repair.loads(relevant_pages)
|
101 |
target_page_names = relevant_pages.keys()
|
@@ -109,7 +112,7 @@ class DataPopulation:
|
|
109 |
}
|
110 |
|
111 |
# Step 4: Populate user data for the task (only for relevant pages)
|
112 |
-
updated_user_data = self._update_user_data(task, relevant_page_details)
|
113 |
self.conversation.append({"role": "assistant", "content": updated_user_data})
|
114 |
updated_user_data = json_repair.loads(updated_user_data)
|
115 |
for uid, page_data in updated_user_data.items():
|
|
|
28 |
model="gpt-4",
|
29 |
messages=conversation,
|
30 |
max_tokens=1000, # Adjusted max_tokens if needed
|
31 |
+
temperature=0.7,
|
32 |
)
|
33 |
return response.choices[0].message.content.strip()
|
34 |
|
|
|
38 |
"role": "user",
|
39 |
"content": (
|
40 |
f"Given the task: '{task}' and the sitemap:\n{sitemap}\n\n"
|
41 |
+
f"Respond first with a brief 'Plan' which suggests what data we have to pre-populate the sitemap"
|
42 |
+
f"to make task accomplishable. Then identify the page(s) these data going to be stored on. "
|
43 |
"Return the page names exactly as they appear in the sitemap, in JSON format. "
|
44 |
"For each relevant page, provide a brief explanation of its relevance. "
|
45 |
+
"Example response:\nPlanning sentences. PAGES: {{\n 'Ride History': 'Displays previous ride data needed for the task.'\n}}"
|
46 |
)
|
47 |
})
|
48 |
response_content = self.gpt4_chat(self.conversation)
|
49 |
return response_content
|
50 |
|
51 |
+
def _update_user_data(self, task, relevant_page_details, relevant_pages):
|
52 |
"""Populate the relevant user data for the task."""
|
53 |
self.conversation.append({
|
54 |
"role": "user",
|
55 |
"content": (
|
56 |
f"Given the task: '{task}' and the following task-relevant page details:\n{relevant_page_details}\n\n"
|
57 |
+
f"Here is reason behind each relevant page: {relevant_pages}."
|
58 |
f"Update each page's 'user_data' value with essential information for task-completion."
|
59 |
f"For example, if a task ask us to retrieve previous order, then we will need to populate synthetic order history in user_data."
|
60 |
"Ensure output maintain the exact format and structure as input page details."
|
|
|
98 |
|
99 |
# Step 1: Identify relevant pages
|
100 |
relevant_pages = self.ask_for_relevant_pages(task, sitemap)
|
101 |
+
relevant_pages = relevant_pages.split("PAGES:", 1)[1].strip()
|
102 |
self.conversation.append({"role": "assistant", "content": relevant_pages})
|
103 |
relevant_pages = json_repair.loads(relevant_pages)
|
104 |
target_page_names = relevant_pages.keys()
|
|
|
112 |
}
|
113 |
|
114 |
# Step 4: Populate user data for the task (only for relevant pages)
|
115 |
+
updated_user_data = self._update_user_data(task, relevant_page_details, relevant_pages)
|
116 |
self.conversation.append({"role": "assistant", "content": updated_user_data})
|
117 |
updated_user_data = json_repair.loads(updated_user_data)
|
118 |
for uid, page_data in updated_user_data.items():
|