-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_generator.py
More file actions
148 lines (121 loc) · 4.39 KB
/
Copy pathcode_generator.py
File metadata and controls
148 lines (121 loc) · 4.39 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
from __future__ import annotations
from ...llm import llm_with_message
from ...memory.shared_memory import SharedMemory
from ...util.config import Config
from ...prompt.stepwise.instruction_task import (
gqa_task_title,
refcoco_task_title,
gqa_task_desc,
refcoco_task_desc,
)
from ...prompt.stepwise.code_example import (
gqa_code_example,
refcoco_code_example,
)
from ...prompt.code_api import gqa_code_api, refcoco_code_api
class CodeGenerator:
def __init__(self, model_name: str, dataset: str):
self.model_name = model_name
self.dataset = dataset
self.reset()
def reset(self):
self.conversation = [
{"role": "system", "content": "You are a helpful assistant specializing in visual reasoning tasks. Your goal is to generate Python code that solves a visual reasoning query using the provided code API and examples."}
]
async def __call__(self, query: str, instruction: str, current_step_index: int,
shared_memory: SharedMemory
):
prompt = self.build_prompt(
query, instruction, current_step_index, shared_memory)
if Config.debug:
with open("stepwise_code_generator.txt", "w") as f:
f.write(prompt)
# Add prompt to conversation
self.conversation.append({"role": "user", "content": prompt})
# Generate code
response = await llm_with_message(self.model_name, self.conversation)
if response is not None:
self.conversation.append({"role": "assistant", "content": response})
return response
async def feedback(self, feedback: str) -> str | None:
self.conversation.append({"role": "user", "content": feedback})
response = await llm_with_message(self.model_name, self.conversation)
if response:
self.conversation.append({"role": "assistant", "content": response})
return response
def build_prompt(self, query: str, instruction: str, current_step_index: int, shared_memory: SharedMemory) -> str:
match self.dataset:
case "gqa":
task_title = gqa_task_title
task_description = gqa_task_desc
code_example = gqa_code_example
code_api = gqa_code_api
case "refcoco":
task_title = refcoco_task_title
task_description = refcoco_task_desc
code_example = refcoco_code_example
code_api = refcoco_code_api
case _:
raise ValueError(f"Dataset {self.dataset} is not supported")
# Extract state information from memory bank
previous_instructions = shared_memory.instructions_prompt.strip("\n")
previous_code = shared_memory.codes_prompt.strip("\n")
execution_results = shared_memory.feedbacks_prompt.strip("\n")
variables_info = shared_memory.variables_prompt.strip("\n")
# Format the prompt with clear sections
prompt = f"""API Specification
-----------------
Use the following code API to guide your solution:
<CodeAPI>
{code_api}
</CodeAPI>
Task Description
----------------
Review the task description below to understand the problem context:
<TaskDescription>
{task_title}
{task_description}</TaskDescription>
Example Code
-----------
Here is an example that illustrates the expected format and approach:
<Examples>
{code_example}
</Examples>
User Query
----------
This is the query you need to solve:
<Query>{query}</Query>
Current Step
------------
<Step>{current_step_index}</Step>
Previous Instructions
---------------------
<PreviousInstructions>
{previous_instructions}
</PreviousInstructions>
Current Instruction
-------------------
<Instruction>{instruction}</Instruction>
Previously Executed Code
-----------------------
<ExecutedCode>
{previous_code}
</ExecutedCode>
Execution Results
----------------
<ExecutionResults>
{execution_results}
</ExecutionResults>
Available Variables
-------------------
<Variables>
{variables_info}
</Variables>
-------------------
Generate Python code that solves the query based on the current instruction.
Your code should build upon previous steps and use the available variables.
Use the code API as shown in the example.
Enclose your code in <PythonCode></PythonCode> tags.
If your code provides a final answer, assign it to a variable named 'final_answer'.
"""
return prompt