Skip to content

Commit 3f9d868

Browse files
committed
Pull first attempt agent into its own class
1 parent 63370bd commit 3f9d868

File tree

3 files changed

+62
-33
lines changed

3 files changed

+62
-33
lines changed

TestRunner/GenericTestRunner.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import pytest
21
import subprocess
32
from lib.utils import CodeGenSandbox
43
from abc import ABCMeta, abstractmethod
5-
from pytest_plugins import ResultsCollector, SessionStartPlugin
64

75

86
class GenericTestRunner(metaclass=ABCMeta):

lib/agents.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
from langroid import ChatAgent, ChatAgentConfig
4+
5+
from lib.utils import CodeGenSandbox
6+
7+
8+
class GenericAgent(metaclass=ABCMeta):
9+
@abstractmethod
10+
def respond(self, prompt: str, *args, **kwargs) -> str: pass
11+
12+
13+
class FirstAttemptAgent(GenericAgent):
14+
15+
def __init__(self, sandbox: CodeGenSandbox, config: ChatAgentConfig):
16+
self.sandbox = sandbox
17+
18+
with open(self.sandbox.get_sandboxed_class_path(), "r") as f:
19+
self.class_skeleton = f.read()
20+
21+
self.agent = ChatAgent(config)
22+
23+
def respond(self, prompt: str, *args, **kwargs) -> str:
24+
response = self.agent.llm_response()
25+
with open(self.sandbox.get_sandboxed_class_path(), "w+") as _out:
26+
_out.write(response.content)
27+
28+
return response.content

main.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import os
2+
3+
from langroid import ChatAgentConfig
4+
25
from lib.utils import CodeGenSandbox
6+
from lib.agents import FirstAttemptAgent
37
import typer
48

59
import langroid as lr
@@ -12,36 +16,10 @@
1216
setup_colored_logging()
1317

1418

15-
def generate_first_attempt(sandbox: CodeGenSandbox) -> str:
16-
with open(sandbox.get_sandboxed_class_path(), "r") as f:
17-
class_skeleton = f.read()
18-
19-
cfg = lr.ChatAgentConfig(
20-
llm=lr.language_models.OpenAIGPTConfig(
21-
chat_model="ollama/llama3:latest",
22-
),
23-
vecdb=None
24-
)
25-
main_agent = lr.ChatAgent(cfg)
26-
response = main_agent.llm_response(f"You are an expert at writing Python code."
27-
f"Fill in the following class skeleton."
28-
f"Do NOT add any other methods or commentary."
29-
f"Your response should be ONLY the python code."
30-
f"Do not say 'here is the python code'"
31-
f"Do not surround your response with quotes or backticks."
32-
f"DO NOT EVER USE ``` in your output."
33-
f"Your output MUST be valid, runnable python code and NOTHING else."
34-
f"{class_skeleton}")
35-
with open(sandbox.get_sandboxed_class_path(), "w+") as _out:
36-
_out.write(response.content)
37-
38-
return response.content
39-
40-
4119
def generate_next_attempt(sandbox: CodeGenSandbox, test_results: str, test_results_insights: str) -> str:
4220
cfg = lr.ChatAgentConfig(
4321
llm=lr.language_models.OpenAIGPTConfig(
44-
chat_model="ollama/llama3:latest",
22+
chat_model="ollama/llama3.1:latest",
4523
),
4624
vecdb=None
4725
)
@@ -78,7 +56,7 @@ def generate_next_attempt(sandbox: CodeGenSandbox, test_results: str, test_resul
7856
def interpret_test_results(results: str, code: str) -> str:
7957
cfg = lr.ChatAgentConfig(
8058
llm=lr.language_models.OpenAIGPTConfig(
81-
chat_model="ollama/llama3:latest",
59+
chat_model="ollama/llama3.1:latest",
8260
),
8361
vecdb=None
8462
)
@@ -108,8 +86,25 @@ def teardown() -> None:
10886
generated_file.truncate(0)
10987

11088

111-
def chat(sandbox: CodeGenSandbox, test_runner: GenericTestRunner, max_epochs: int=5) -> None:
112-
code_attempt = generate_first_attempt(sandbox)
89+
def chat(
90+
sandbox: CodeGenSandbox,
91+
first_attempt_agent: FirstAttemptAgent,
92+
test_runner: GenericTestRunner,
93+
max_epochs: int = 5
94+
) -> None:
95+
code_attempt = first_attempt_agent.respond(
96+
prompt=f"""
97+
You are an expert at writing Python code.
98+
Fill in the following class skeleton.
99+
Do NOT add any other methods or commentary.
100+
Your response should be ONLY the python code.
101+
Do not say 'here is the python code'
102+
Do not surround your response with quotes or backticks.
103+
DO NOT EVER USE ``` in your output.
104+
Your output MUST be valid, runnable python code and NOTHING else.
105+
{first_attempt_agent.class_skeleton}
106+
"""
107+
)
113108
solved = False
114109
for _ in range(max_epochs):
115110
# test_exit_code, test_result(s = get_test_results()
@@ -168,10 +163,18 @@ def main(
168163
)
169164
)
170165

166+
llama3 = ChatAgentConfig(
167+
llm=lr.language_models.OpenAIGPTConfig(
168+
chat_model="ollama/llama3:latest",
169+
),
170+
vecdb=None
171+
)
172+
171173
sandbox = CodeGenSandbox(project_dir, class_skeleton_path, test_path, sandbox_path)
172174
sandbox.init_sandbox()
175+
fa: FirstAttemptAgent = FirstAttemptAgent(sandbox, llama3)
173176
tr: GenericTestRunner = SubProcessTestRunner(sandbox)
174-
chat(sandbox, tr, max_epochs=max_epochs)
177+
chat(sandbox, fa, tr, max_epochs=max_epochs)
175178

176179

177180
if __name__ == "__main__":

0 commit comments

Comments
 (0)