Skip to content

Commit 81a340e

Browse files
committed
Add linter
1 parent 586eda0 commit 81a340e

File tree

6 files changed

+47
-174
lines changed

6 files changed

+47
-174
lines changed

code_it/__main__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
logger = logging.getLogger(__name__)
1515

1616
code_editor = PythonCodeEditor()
17-
refactored_code_editor = PythonCodeEditor(filename="refactored_persistent_source.py")
18-
1917

2018
model_builder = build_llama_base_llm
2119

2220
config = TaskExecutionConfig()
2321

24-
task_executor = TaskExecutor(code_editor, refactored_code_editor, model_builder, config)
22+
task_executor = TaskExecutor(code_editor, model_builder, config)
2523

2624
with open("task.txt", "r") as fp:
2725
task = fp.read()

code_it/agents/coder.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,36 @@
44
class Coder(BaseAgent):
55
def __init__(self, llm) -> None:
66
super().__init__(llm)
7-
self.stop_string = "Subtask:"
7+
self.stop_string = "Objective:"
88
self.prompt_template = """"You're an expert python programmer AI Agent. You solve problems by using Python code,
99
and you're capable of providing code snippets, debugging and much more, whenever it's asked of you. You are usually given
10-
an existing source code and a small subtask. You should focus on fulfilling only the subtask by providing the required code.
10+
an existing source code that's poorly written and contains many duplications. You should make it better by refactoring and removing errors.
1111
1212
You should fulfill your role in the example below:
1313
14-
Existing Source Code:
14+
Objective: Write a code to print 'hello, world'
15+
Plan: 1. Call print function with the parameter 'hello, world'
16+
Source Code:
17+
import os
18+
import os
1519
import os
16-
17-
Subtask: Write a code to print 'hello, world'
18-
Programmer AI:
1920
print('hello, world')
20-
Subtask:
21+
Thought: The code contains duplication and an unused import. Here's an improved version.
22+
New Code:
23+
print('hello, world')
24+
Objective:
2125
22-
Notice that you once you finish the subtask, you should add the word 'Subtask:' in a new line,
26+
Notice that you once you finish the subtask, you should add the word 'Objective:' in a new line,
2327
like in the example above.
2428
29+
You should ALWAYS output the full code.
30+
2531
Now please help with the subtask below.
2632
27-
Existing Source Code: {source_code}
28-
Subtask: {subtask}
29-
Programmer AI:
33+
Objective: {objective}
34+
Plan: {plan}
35+
Source Code: {source_code}
36+
New Code:
3037
"""
3138

3239
def parse_output(self, result):

code_it/agents/debugger.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

code_it/agents/dependency_tracker.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def __init__(self, llm) -> None:
4747

4848
def parse_output(self, output):
4949
output = output.lower()
50-
print("output ", output)
5150
if self.stop_string in output:
5251
output = output.split(self.stop_string)[0]
53-
print("output ", output)
5452
return [step for step in output.split("\n") if len(step) > 0]

code_it/agents/refactor.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

code_it/task_executor.py

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from code_it.agents.planner import Planner
66
from code_it.agents.coder import Coder
77
from code_it.agents.linter import Linter
8-
from code_it.agents.refactor import Refactor
98
from code_it.agents.dependency_tracker import DependencyTracker
109
from code_it.models import HTTPBaseLLM
1110
from typing import Callable
@@ -29,6 +28,7 @@ def _trim_md(code_editor):
2928
class TaskExecutionConfig:
3029
execute_code = True
3130
install_dependencies = True
31+
apply_linter = True
3232
dependency_samples = 3
3333
max_refactor_attempts = 5
3434
dependency_install_attempts = 5
@@ -42,12 +42,10 @@ class TaskExecutor:
4242
def __init__(
4343
self,
4444
code_editor: PythonCodeEditor,
45-
refactored_code_editor: PythonCodeEditor,
4645
model_builder: Callable[[], HTTPBaseLLM],
4746
config: TaskExecutionConfig,
4847
) -> None:
4948
self.code_editor = code_editor
50-
self.refactored_code_editor = refactored_code_editor
5149
self.config = config
5250

5351
# Planner
@@ -58,14 +56,9 @@ def __init__(
5856
# Coder
5957
coder_llm = model_builder()
6058
coder_llm.set_parameter("temperature", config.coder_temperature)
61-
self.coder = Coder(coder_llm)
62-
63-
# Refactor
64-
refactoring_llm = model_builder()
65-
refactoring_llm.set_parameter("temperature", config.refactor_temperature)
66-
refactoring_llm.set_parameter("max_new_tokens", 1024)
59+
coder_llm.set_parameter("max_new_tokens", 1024)
6760

68-
self.refactor = Refactor(refactoring_llm)
61+
self.coder = Coder(coder_llm)
6962

7063
# Linter
7164
linter_llm = model_builder()
@@ -81,7 +74,6 @@ def __init__(
8174
self.dependency_tracker = DependencyTracker(dependency_tracker_llm)
8275

8376
def execute(self, task: str):
84-
8577
# Generating a coding plan
8678
plan = self.planner.execute_task(task=task)
8779
logger.info(type(plan))
@@ -107,10 +99,10 @@ def execute(self, task: str):
10799

108100
logger.info("Dependency lines: %s", dependencies)
109101
for dependency in dependencies:
110-
self.refactored_code_editor.add_dependency(dependency)
102+
self.code_editor.add_dependency(dependency)
111103

112-
self.refactored_code_editor.create_env()
113-
process = self.refactored_code_editor.install_dependencies()
104+
self.code_editor.create_env()
105+
process = self.code_editor.install_dependencies()
114106
if process.returncode != 0:
115107
logger.error("Dependency install failed for: %s", "\n".join(dependencies))
116108
attempt += 1
@@ -124,55 +116,40 @@ def execute(self, task: str):
124116
logger.info("Installed dependencies successfully!")
125117

126118
# Coding
127-
for step in plan:
128-
logger.info("Coding step: %s", step)
129-
new_code = self.coder.execute_task(
130-
subtask=step, source_code=self.code_editor.display_code()
119+
for i in range(self.config.max_refactor_attempts):
120+
logger.info("Coding, attempt: %s", i)
121+
refactored = self.coder.execute_task(
122+
source_code=self.code_editor.display_code(), objective=task, plan="\n".join(plan)
131123
)
132-
self.code_editor.add_code(new_code)
133-
logger.info("Trimming MD syntax")
124+
self.code_editor.overwrite_code(refactored)
134125
_trim_md(self.code_editor)
135126

136-
logger.info("Applying linter...")
137-
(pylint_stdout, pylint_stderr) = lint.py_run(self.code_editor.filename, return_std=True)
138-
pylint_stdout = pylint_stdout.getvalue()
139-
pylint_stderr = pylint_stderr.getvalue()
140-
logger.info(pylint_stdout)
141-
logger.error(pylint_stderr)
142-
143-
new_code = self.linter.execute_task(
144-
source_code=self.code_editor.display_code(),
145-
stdout=pylint_stdout,
146-
stderr=pylint_stderr
147-
)
148-
149-
self.code_editor.overwrite_code(new_code)
150-
127+
logger.info(self.code_editor.display_code())
151128

152-
logger.info("Finished generating code!")
129+
if self.config.apply_linter:
130+
logger.info("Applying linter...")
131+
(pylint_stdout, _) = lint.py_run(self.code_editor.filename, return_std=True)
132+
pylint_stdout = pylint_stdout.getvalue()
133+
logger.info(pylint_stdout)
153134

154-
logger.info("Current code: %s", self.code_editor.display_code())
155-
156-
157-
# Refactoring
158-
for i in range(self.config.max_refactor_attempts):
159-
logger.info("After refactoring, attempt: %s", i)
160-
refactored = self.refactor.execute_task(
161-
source_code=self.code_editor.display_code(), objective=task, plan="\n".join(plan)
162-
)
163-
self.refactored_code_editor.overwrite_code(refactored)
164-
_trim_md(self.refactored_code_editor)
165-
166-
logger.info(self.refactored_code_editor.display_code())
135+
new_code = self.linter.execute_task(
136+
source_code=self.code_editor.display_code(),
137+
stdout=pylint_stdout,
138+
)
139+
logger.warn("Linted code: %s", new_code)
140+
if new_code:
141+
self.code_editor.overwrite_code(new_code)
167142

168143
if not self.config.execute_code:
169-
return self.refactored_code_editor.display_code()
144+
return self.code_editor.display_code()
170145

171-
result = self.refactored_code_editor.run_code()
146+
result = self.code_editor.run_code()
172147

173148
if "Succeeded" in result:
174149
break
175150

151+
logger.info("Finished generating code!")
152+
176153
if "Succeeded" in result:
177154
logger.info("Source code is functional!")
178155
return "Task Success: " + result

0 commit comments

Comments
 (0)