-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark_agent.py
99 lines (88 loc) · 3.18 KB
/
benchmark_agent.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
import tempfile
import os
from typing import Optional
from gpt_engineer.core.ai import AI
from gpt_engineer.core.base_execution_env import BaseExecutionEnv
from gpt_engineer.core.base_memory import BaseMemory
from gpt_engineer.core.default.disk_execution_env import DiskExecutionEnv
from gpt_engineer.core.default.disk_memory import DiskMemory
from gpt_engineer.core.default.paths import (
PREPROMPTS_PATH,
memory_path,
)
from gpt_engineer.core.prompt import Prompt
from gpt_engineer.core.default.steps import improve_fn as improve
from gpt_engineer.core.files_dict import FilesDict
from gpt_engineer.core.preprompts_holder import PrepromptsHolder
# THIS METHOD IS USED BY "bench" TO SET UP THE AGENT AND MUST BE IMPLEMENTED.
# THE ONLY OTHER REQUIREMENT IS THAT THE AGENT IMPLEMENTS "improve".
def default_config_agent():
"""
Creates an instance of BenchmarkAgent with default configuration.
Returns
-------
BenchmarkAgent
"""
return BenchmarkAgent.with_default_config(tempfile.mkdtemp())
class BenchmarkAgent:
"""
A template agent for BenchmarkAgents. The only method that MUST BE IMPLEMENTED to run "bench" is "improve".
The below implementation uses the standard improve in gpt-engineer.
Attributes
----------
memory : BaseMemory
The memory interface where the code and related data are stored.
execution_env : BaseExecutionEnv
The execution environment in which the code is executed.
ai : AI
The AI model used for generating and improving code.
preprompts_holder : PrepromptsHolder
The holder for preprompt messages that guide the AI model.
"""
def __init__(
self,
memory: BaseMemory,
execution_env: BaseExecutionEnv,
ai: AI = None,
preprompts_holder: PrepromptsHolder = None,
):
self.preprompts_holder = preprompts_holder or PrepromptsHolder(PREPROMPTS_PATH)
self.memory = memory
self.execution_env = execution_env
self.ai = ai or AI(
model_name=os.environ.get("MODEL_NAME", "gpt-4-turbo"),
)
@classmethod
def with_default_config(
cls, path: str, ai: AI = None, preprompts_holder: PrepromptsHolder = None
):
"""
Convenience method to create a BenchmarkAgent with default configuration.
:param path:
:param ai:
:param preprompts_holder:
:return: BenchmarkAgent
"""
return cls(
memory=DiskMemory(memory_path(path)),
execution_env=DiskExecutionEnv(),
ai=ai,
preprompts_holder=preprompts_holder or PrepromptsHolder(PREPROMPTS_PATH),
)
def improve(
self,
files_dict: FilesDict,
prompt: Prompt,
execution_command: Optional[str] = None,
) -> FilesDict:
"""
A required function that existing code, a prompt and optionally a command for executing the code (used for self-heal).
:param files_dict:
:param prompt:
:param execution_command:
:return: FilesDict
"""
files_dict = improve(
self.ai, prompt, files_dict, self.memory, self.preprompts_holder
)
return files_dict