-
Notifications
You must be signed in to change notification settings - Fork 650
Expand file tree
/
Copy path1_planning_llm.py
More file actions
280 lines (225 loc) · 11.8 KB
/
1_planning_llm.py
File metadata and controls
280 lines (225 loc) · 11.8 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import json
import argparse
import os
import sys
from utils import print_response
from load_dotenv import load_env_config
from litellm_utils import get_llm_client, run_inference
# Load environment variables
load_env_config()
parser = argparse.ArgumentParser()
parser.add_argument('--paper_name',type=str)
parser.add_argument('--model_name',type=str, default="deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct")
parser.add_argument('--tp_size',type=int, default=2)
parser.add_argument('--temperature',type=float, default=1.0)
parser.add_argument('--max_model_len',type=int, default=128000)
parser.add_argument('--paper_format',type=str, default="JSON", choices=["JSON", "LaTeX"])
parser.add_argument('--pdf_json_path', type=str) # json format
parser.add_argument('--pdf_latex_path', type=str) # latex format
parser.add_argument('--output_dir',type=str, default="")
args = parser.parse_args()
paper_name = args.paper_name
paper_format = args.paper_format
pdf_json_path = args.pdf_json_path
pdf_latex_path = args.pdf_latex_path
output_dir = args.output_dir
model_name = args.model_name
tp_size = args.tp_size
max_model_len = args.max_model_len
temperature = args.temperature
if paper_format == "JSON":
with open(f'{pdf_json_path}') as f:
paper_content = json.load(f)
elif paper_format == "LaTeX":
with open(f'{pdf_latex_path}') as f:
paper_content = f.read()
else:
print(f"[ERROR] Invalid paper format. Please select either 'JSON' or 'LaTeX.")
sys.exit(0)
plan_msg = [
{'role': "system", "content": f"""You are an expert researcher and strategic planner with a deep understanding of experimental design and reproducibility in scientific research.
You will receive a research paper in {paper_format} format.
Your task is to create a detailed and efficient plan to reproduce the experiments and methodologies described in the paper.
This plan should align precisely with the paper's methodology, experimental setup, and evaluation metrics.
Instructions:
1. Align with the Paper: Your plan must strictly follow the methods, datasets, model configurations, hyperparameters, and experimental setups described in the paper.
2. Be Clear and Structured: Present the plan in a well-organized and easy-to-follow format, breaking it down into actionable steps.
3. Prioritize Efficiency: Optimize the plan for clarity and practical implementation while ensuring fidelity to the original experiments."""},
{"role": "user",
"content" : f"""## Paper
{paper_content}
## Task
1. We want to reproduce the method described in the attached paper.
2. The authors did not release any official code, so we have to plan our own implementation.
3. Before writing any Python code, please outline a comprehensive plan that covers:
- Key details from the paper's **Methodology**.
- Important aspects of **Experiments**, including dataset requirements, experimental settings, hyperparameters, or evaluation metrics.
4. The plan should be as **detailed and informative** as possible to help us write the final code later.
## Requirements
- You don't need to provide the actual code yet; focus on a **thorough, clear strategy**.
- If something is unclear from the paper, mention it explicitly.
## Instruction
The response should give us a strong roadmap, making it easier to write the code later."""}]
file_list_msg = [
{"role": "user", "content": """Your goal is to create a concise, usable, and complete software system design for reproducing the paper's method. Use appropriate open-source libraries and keep the overall architecture simple.
Based on the plan for reproducing the paper’s main method, please design a concise, usable, and complete software system.
Keep the architecture simple and make effective use of open-source libraries.
-----
## Format Example
[CONTENT]
{
"Implementation approach": "We will ... ,
"File list": [
"main.py",
"dataset_loader.py",
"model.py",
"trainer.py",
"evaluation.py"
],
"Data structures and interfaces": "\nclassDiagram\n class Main {\n +__init__()\n +run_experiment()\n }\n class DatasetLoader {\n +__init__(config: dict)\n +load_data() -> Any\n }\n class Model {\n +__init__(params: dict)\n +forward(x: Tensor) -> Tensor\n }\n class Trainer {\n +__init__(model: Model, data: Any)\n +train() -> None\n }\n class Evaluation {\n +__init__(model: Model, data: Any)\n +evaluate() -> dict\n }\n Main --> DatasetLoader\n Main --> Trainer\n Main --> Evaluation\n Trainer --> Model\n",
"Program call flow": "\nsequenceDiagram\n participant M as Main\n participant DL as DatasetLoader\n participant MD as Model\n participant TR as Trainer\n participant EV as Evaluation\n M->>DL: load_data()\n DL-->>M: return dataset\n M->>MD: initialize model()\n M->>TR: train(model, dataset)\n TR->>MD: forward(x)\n MD-->>TR: predictions\n TR-->>M: training complete\n M->>EV: evaluate(model, dataset)\n EV->>MD: forward(x)\n MD-->>EV: predictions\n EV-->>M: metrics\n",
"Anything UNCLEAR": "Need clarification on the exact dataset format and any specialized hyperparameters."
}
[/CONTENT]
## Nodes: "<node>: <type> # <instruction>"
- Implementation approach: <class 'str'> # Summarize the chosen solution strategy.
- File list: typing.List[str] # Only need relative paths. ALWAYS write a main.py or app.py here.
- Data structures and interfaces: typing.Optional[str] # Use mermaid classDiagram code syntax, including classes, method(__init__ etc.) and functions with type annotations, CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design.
- Program call flow: typing.Optional[str] # Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT.
- Anything UNCLEAR: <class 'str'> # Mention ambiguities and ask for clarifications.
## Constraint
Format: output wrapped inside [CONTENT][/CONTENT] like the format example, nothing else.
## Action
Follow the instructions for the nodes, generate the output, and ensure it follows the format example."""}
]
task_list_msg = [
{'role': 'user', 'content': """Your goal is break down tasks according to PRD/technical design, generate a task list, and analyze task dependencies.
You will break down tasks, analyze dependencies.
You outline a clear PRD/technical design for reproducing the paper’s method and experiments.
Now, let's break down tasks according to PRD/technical design, generate a task list, and analyze task dependencies.
The Logic Analysis should not only consider the dependencies between files but also provide detailed descriptions to assist in writing the code needed to reproduce the paper.
-----
## Format Example
[CONTENT]
{
"Required packages": [
"numpy==1.21.0",
"torch==1.9.0"
],
"Required Other language third-party packages": [
"No third-party dependencies required"
],
"Logic Analysis": [
[
"data_preprocessing.py",
"DataPreprocessing class ........"
],
[
"trainer.py",
"Trainer ....... "
],
[
"dataset_loader.py",
"Handles loading and ........"
],
[
"model.py",
"Defines the model ......."
],
[
"evaluation.py",
"Evaluation class ........ "
],
[
"main.py",
"Entry point ......."
]
],
"Task list": [
"dataset_loader.py",
"model.py",
"trainer.py",
"evaluation.py",
"main.py"
],
"Full API spec": "openapi: 3.0.0 ...",
"Shared Knowledge": "Both data_preprocessing.py and trainer.py share ........",
"Anything UNCLEAR": "Clarification needed on recommended hardware configuration for large-scale experiments."
}
[/CONTENT]
## Nodes: "<node>: <type> # <instruction>"
- Required packages: typing.Optional[typing.List[str]] # Provide required third-party packages in requirements.txt format.(e.g., 'numpy==1.21.0').
- Required Other language third-party packages: typing.List[str] # List down packages required for non-Python languages. If none, specify "No third-party dependencies required".
- Logic Analysis: typing.List[typing.List[str]] # Provide a list of files with the classes/methods/functions to be implemented, including dependency analysis and imports. Include as much detailed description as possible.
- Task list: typing.List[str] # Break down the tasks into a list of filenames, prioritized based on dependency order. The task list must include the previously generated file list.
- Full API spec: <class 'str'> # Describe all APIs using OpenAPI 3.0 spec that may be used by both frontend and backend. If front-end and back-end communication is not required, leave it blank.
- Shared Knowledge: <class 'str'> # Detail any shared knowledge, like common utility functions or configuration variables.
- Anything UNCLEAR: <class 'str'> # Mention any unresolved questions or clarifications needed from the paper or project scope.
## Constraint
Format: output wrapped inside [CONTENT][/CONTENT] like the format example, nothing else.
## Action
Follow the node instructions above, generate your output accordingly, and ensure it follows the given format example."""}]
# config
config_msg = [
{'role': 'user', 'content': """You write elegant, modular, and maintainable code. Adhere to Google-style guidelines.
Based on the paper, plan, design specified previously, follow the "Format Example" and generate the code.
Extract the training details from the above paper (e.g., learning rate, batch size, epochs, etc.), follow the "Format example" and generate the code.
DO NOT FABRICATE DETAILS — only use what the paper provides.
You must write `config.yaml`.
ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Your output format must follow the example below exactly.
-----
# Format Example
## Code: config.yaml
```yaml
## config.yaml
training:
learning_rate: ...
batch_size: ...
epochs: ...
...
```
-----
## Code: config.yaml
"""
}]
# Initialize LLM client (either LiteLLM from environment or vLLM from args)
client, tokenizer, is_litellm, sampling_params = get_llm_client(
model_name=model_name,
tp_size=tp_size,
max_model_len=max_model_len,
temperature=temperature
)
def run_llm(msg):
"""Run inference using either LiteLLM or vLLM based on configuration"""
return run_inference(client, tokenizer, is_litellm, sampling_params, msg)
responses = []
trajectories = []
total_accumulated_cost = 0
for idx, instruction_msg in enumerate([plan_msg, file_list_msg, task_list_msg, config_msg]):
current_stage = ""
if idx == 0 :
current_stage = f"[Planning] Overall plan"
elif idx == 1:
current_stage = f"[Planning] Architecture design"
elif idx == 2:
current_stage = f"[Planning] Logic design"
elif idx == 3:
current_stage = f"[Planning] Configuration file generation"
print(current_stage)
trajectories.extend(instruction_msg)
completion = run_llm(trajectories)
# response
completion_json = {
'text': completion
}
# print and logging
print_response(completion_json, is_llm=True)
responses.append(completion_json)
# trajectories
trajectories.append({'role': 'assistant', 'content': completion})
# save
os.makedirs(output_dir, exist_ok=True)
with open(f'{output_dir}/planning_response.json', 'w') as f:
json.dump(responses, f)
with open(f'{output_dir}/planning_trajectories.json', 'w') as f:
json.dump(trajectories, f)