-
Notifications
You must be signed in to change notification settings - Fork 7
/
workflow.py
153 lines (140 loc) · 7.3 KB
/
workflow.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
import argparse
import json
import re
from tqdm import tqdm
from agents.query_expansion_agent import QueryExpansionAgent
from agents.plot_agent import PlotAgent
from agents.visual_refine_agent import VisualRefineAgent
import logging
import os
import shutil
import glob
import sys
from agents.utils import is_run_code_success, run_code, get_code
parser = argparse.ArgumentParser()
parser.add_argument('--workspace', type=str, default='./workspace')
parser.add_argument('--model_type', type=str, default='gpt-3.5-turbo')
parser.add_argument('--visual_refine', type=bool, default=True)
args = parser.parse_args()
def mainworkflow(expert_instruction, simple_instruction, workspace, max_try=3):
# Query expanding
logging.info('=========Query Expansion AGENT=========')
config = {'workspace': workspace}
query_expansion_agent = QueryExpansionAgent(expert_instruction, simple_instruction,model_type=args.model_type)
expanded_simple_instruction = query_expansion_agent.run('simple')
logging.info('=========Expanded Simple Instruction=========')
logging.info(expanded_simple_instruction)
logging.info('=========Plotting=========')
# GPT-4 Plot Agent
# Initial plotting
action_agent = PlotAgent(config, expanded_simple_instruction)
logging.info('=========Novice 4 Plotting=========')
novice_log, novice_code = action_agent.run_initial(args.model_type, 'novice.png')
logging.info(novice_log)
logging.info('=========Original Code=========')
logging.info(novice_code)
# Code refinement
# code_refine_agent = CodeRefineAgent(expanded_simple_instruction, novice_4_code)
# refined_response = code_refine_agent.run()
# logging.info('=========Refined Code=========')
# logging.info(refined_response)
# Visual refinement
# if True:
# # refined_code = get_code(refined_response)
# # if refined_code == '':
# # refined_code = refined_response.strip('"""')
# # else:
# # pass
# refined_code = 'print(json.load())'
# if check_refined_code_executable(refined_code, 'gpt-4', 'novice', config['workspace']):
# print('Use refined code for visual feedback')
# visual_refine_agent = VisualRefineAgent('novice_4.png', config, refined_code, simple_instruction)
# visual_feedback = visual_refine_agent.run('gpt-4', 'novice', 'novice_4_final.png')
# logging.info('=========Visual Feedback=========')
# logging.info(visual_feedback)
# final_instruction = refined_code + '\n\n' + visual_feedback
# action_agent = PlotAgent(config, final_instruction)
# novice_4_log, novice_4_code = action_agent.run_vis('gpt-4', 'novice_4_final.png')
# logging.info(novice_4_log)
# else:
if args.visual_refine and os.path.exists(f'{workspace}/novice.png'):
print('Use original code for visual feedback')
visual_refine_agent = VisualRefineAgent('novice.png', config, '', simple_instruction)
visual_feedback = visual_refine_agent.run('gpt-4', 'novice', 'novice_final.png')
logging.info('=========Visual Feedback=========')
logging.info(visual_feedback)
final_instruction = '' + '\n\n' + visual_feedback
action_agent = PlotAgent(config, final_instruction)
novice_log, novice_code = action_agent.run_vis(args.model_type, 'novice_final.png')
logging.info(novice_log)
# GPT-3.5-turbo Plot Agent
# Initial plotting
'''action_agent = PlotAgent(config, expanded_simple_instruction)
logging.info('=========Novice 3.5 Plotting=========')
novice_35_log, novice_35_code = action_agent.run_initial('gpt-3.5-turbo', 'novice_35.png')
logging.info(novice_35_log)
logging.info('=========Original Code=========')
logging.info(novice_35_code)
# Code refinement
# code_refine_agent = CodeRefineAgent(expanded_simple_instruction, novice_35_code)
# refined_response = code_refine_agent.run()
# logging.info('=========Refined Code=========')
# logging.info(refined_response)
# Visual refinement
if True:
# refined_code = get_code(refined_response)
# if refined_code == '':
# refined_code = refined_response.strip('"""')
# else:
# pass
refined_code = 'print(json.load())'
if check_refined_code_executable(refined_code, 'gpt-3.5-turbo', 'novice', config['workspace']):
print('Use refined code for visual feedback')
visual_refine_agent = VisualRefineAgent('novice_35.png', config, refined_code, simple_instruction)
visual_feedback = visual_refine_agent.run('gpt-3.5-turbo', 'novice', 'novice_35_final.png')
logging.info('=========Visual Feedback=========')
logging.info(visual_feedback)
final_instruction = refined_code + '\n\n' + visual_feedback
action_agent = PlotAgent(config, final_instruction)
novice_35_log, novice_35_code = action_agent.run_vis('gpt-3.5-turbo', 'novice_35_final.png')
logging.info(novice_35_log)
else:
print('Use original code for visual feedback')
visual_refine_agent = VisualRefineAgent('novice_35.png', config, '', simple_instruction)
visual_feedback = visual_refine_agent.run('gpt-3.5-turbo', 'novice', 'novice_35_final.png')
logging.info('=========Visual Feedback=========')
logging.info(visual_feedback)
final_instruction = '' + '\n\n' + visual_feedback
action_agent = PlotAgent(config, final_instruction)
novice_35_log, novice_35_code = action_agent.run_vis('gpt-3.5-turbo', 'novice_35_final.png')
logging.info(novice_35_log)'''
def check_refined_code_executable(refined_code, model_type, query_type, workspace):
file_name = f'code_action_{model_type}_{query_type}_refined.py'
with open(os.path.join(workspace, file_name), 'w') as f1:
f1.write(refined_code)
log = run_code(workspace, file_name)
return is_run_code_success(log)
if __name__ == "__main__":
workspace_base = args.workspace
data_path = '/home/zhoupeng/project/LLM/agent/plotagent/benchmark/newPlotAgent/plot-agent/benchmark_data/'
# open the json file
data = json.load(open(f'{data_path}/benchmark_instructions.json'))
for item in tqdm(data):
novice_instruction = item['simple_instruction']
expert_instruction = item['expert_instruction']
example_id = item['id']
directory_path = f'{workspace_base}/example_{example_id}'
# Check if the directory already exists
if not os.path.exists(directory_path):
# If it doesn't exist, create the directory
os.mkdir(directory_path)
print(f"Directory '{directory_path}' created successfully.")
input_path = f'{data_path}/data/{example_id}'
if os.path.exists(input_path):
#全部copy到f"Directory '{directory_path}'
os.system(f'cp -r {input_path}/* {directory_path}')
else:
print(f"Directory '{directory_path}' already exists.")
continue
logging.basicConfig(level=logging.INFO, filename=f'{directory_path}/workflow.log', filemode='w', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
mainworkflow(expert_instruction, novice_instruction, workspace=directory_path)