forked from tsinghua-fib-lab/AgentSocietyChallenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecAgent_baseline.py
187 lines (158 loc) · 7.93 KB
/
RecAgent_baseline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import json
from websocietysimulator import Simulator
from websocietysimulator.agent import RecommendationAgent
import tiktoken
from websocietysimulator.llm import LLMBase, InfinigenceLLM
from websocietysimulator.agent.modules.planning_modules import PlanningBase
from websocietysimulator.agent.modules.reasoning_modules import ReasoningBase
import re
import logging
import time
logging.basicConfig(level=logging.INFO)
def num_tokens_from_string(string: str) -> int:
encoding = tiktoken.get_encoding("cl100k_base")
try:
a = len(encoding.encode(string))
except:
print(encoding.encode(string))
return a
class RecPlanning(PlanningBase):
"""Inherits from PlanningBase"""
def __init__(self, llm):
"""Initialize the planning module"""
super().__init__(llm=llm)
def create_prompt(self, task_type, task_description, feedback, few_shot):
"""Override the parent class's create_prompt method"""
if feedback == '':
prompt = '''You are a planner who divides a {task_type} task into several subtasks. You also need to give the reasoning instructions for each subtask. Your output format should follow the example below.
The following are some examples:
Task: I need to find some information to complete a recommendation task.
sub-task 1: {{"description": "First I need to find user information", "reasoning instruction": "None"}}
sub-task 2: {{"description": "Next, I need to find item information", "reasoning instruction": "None"}}
sub-task 3: {{"description": "Next, I need to find review information", "reasoning instruction": "None"}}
Task: {task_description}
'''
prompt = prompt.format(task_description=task_description, task_type=task_type)
else:
prompt = '''You are a planner who divides a {task_type} task into several subtasks. You also need to give the reasoning instructions for each subtask. Your output format should follow the example below.
The following are some examples:
Task: I need to find some information to complete a recommendation task.
sub-task 1: {{"description": "First I need to find user information", "reasoning instruction": "None"}}
sub-task 2: {{"description": "Next, I need to find item information", "reasoning instruction": "None"}}
sub-task 3: {{"description": "Next, I need to find review information", "reasoning instruction": "None"}}
end
--------------------
Reflexion:{feedback}
Task:{task_description}
'''
prompt = prompt.format(example=few_shot, task_description=task_description, task_type=task_type, feedback=feedback)
return prompt
class RecReasoning(ReasoningBase):
"""Inherits from ReasoningBase"""
def __init__(self, profile_type_prompt, llm):
"""Initialize the reasoning module"""
super().__init__(profile_type_prompt=profile_type_prompt, memory=None, llm=llm)
def __call__(self, task_description: str):
"""Override the parent class's __call__ method"""
prompt = '''
{task_description}
'''
prompt = prompt.format(task_description=task_description)
messages = [{"role": "user", "content": prompt}]
reasoning_result = self.llm(
messages=messages,
temperature=0.1,
max_tokens=1000
)
return reasoning_result
class MyRecommendationAgent(RecommendationAgent):
"""
Participant's implementation of SimulationAgent
"""
def __init__(self, llm:LLMBase):
super().__init__(llm=llm)
self.planning = RecPlanning(llm=self.llm)
self.reasoning = RecReasoning(profile_type_prompt='', llm=self.llm)
def workflow(self):
"""
Simulate user behavior
Returns:
list: Sorted list of item IDs
"""
# plan = self.planning(task_type='Recommendation Task',
# task_description="Please make a plan to query user information, you can choose to query user, item, and review information",
# feedback='',
# few_shot='')
# print(f"The plan is :{plan}")
plan = [
{'description': 'First I need to find user information'},
{'description': 'Next, I need to find item information'},
{'description': 'Next, I need to find review information'}
]
user = ''
item_list = []
history_review = ''
for sub_task in plan:
if 'user' in sub_task['description']:
user = str(self.interaction_tool.get_user(user_id=self.task['user_id']))
input_tokens = num_tokens_from_string(user)
if input_tokens > 12000:
encoding = tiktoken.get_encoding("cl100k_base")
user = encoding.decode(encoding.encode(user)[:12000])
elif 'item' in sub_task['description']:
for n_bus in range(len(self.task['candidate_list'])):
item = self.interaction_tool.get_item(item_id=self.task['candidate_list'][n_bus])
keys_to_extract = ['item_id', 'name','stars','review_count','attributes','title', 'average_rating', 'rating_number','description','ratings_count','title_without_series']
filtered_item = {key: item[key] for key in keys_to_extract if key in item}
item_list.append(filtered_item)
# print(item)
elif 'review' in sub_task['description']:
history_review = str(self.interaction_tool.get_reviews(user_id=self.task['user_id']))
input_tokens = num_tokens_from_string(history_review)
if input_tokens > 12000:
encoding = tiktoken.get_encoding("cl100k_base")
history_review = encoding.decode(encoding.encode(history_review)[:12000])
else:
pass
task_description = f'''
You are a real user on an online platform. Your historical item review text and stars are as follows: {history_review}.
Now you need to rank the following 20 items: {self.task['candidate_list']} according to their match degree to your preference.
Please rank the more interested items more front in your rank list.
The information of the above 20 candidate items is as follows: {item_list}.
Your final output should be ONLY a ranked item list of {self.task['candidate_list']} with the following format, DO NOT introduce any other item ids!
DO NOT output your analysis process!
The correct output format:
['item id1', 'item id2', 'item id3', ...]
'''
result = self.reasoning(task_description)
try:
# print('Meta Output:',result)
match = re.search(r"\[.*\]", result, re.DOTALL)
if match:
result = match.group()
else:
print("No list found.")
print('Processed Output:',eval(result))
# time.sleep(4)
return eval(result)
except:
print('format error')
return ['']
if __name__ == "__main__":
task_set = "amazon" # "goodreads" or "yelp"
# Initialize Simulator
simulator = Simulator(data_dir="your data_dir", device="auto", cache=True)
# Load scenarios
simulator.set_task_and_groundtruth(task_dir=f"./track2/{task_set}/tasks", groundtruth_dir=f"./track2/{task_set}/groundtruth")
# Set your custom agent
simulator.set_agent(MyRecommendationAgent)
# Set LLM client
simulator.set_llm(InfinigenceLLM(api_key="your api_key"))
# Run evaluation
# If you don't set the number of tasks, the simulator will run all tasks.
agent_outputs = simulator.run_simulation(number_of_tasks=None, enable_threading=True, max_workers=10)
# Evaluate the agent
evaluation_results = simulator.evaluate()
with open(f'./evaluation_results_track2_{task_set}.json', 'w') as f:
json.dump(evaluation_results, f, indent=4)
print(f"The evaluation_results is :{evaluation_results}")