forked from ATheorell/gpte-bench-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_agent.py
86 lines (72 loc) · 2.73 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
import tempfile
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
class BenchmarkAgent:
"""
An agent that uses AI to generate and improve code based on a given prompt.
This agent is capable of initializing a codebase from a prompt and improving an existing
codebase based on user input. It uses an AI model to generate and refine code, and it
interacts with a repository and an execution environment to manage and execute the code.
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()
@classmethod
def with_default_config(
cls, path: str, ai: AI = None, preprompts_holder: PrepromptsHolder = None
):
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:
files_dict = improve(
self.ai, prompt, files_dict, self.memory, self.preprompts_holder
)
return files_dict
def default_config_agent():
"""
Creates an instance of BenchmarkAgent with default configuration.
Returns
-------
SimpleAgent
An instance of SimpleAgent with a temporary directory as its base path.
"""
return BenchmarkAgent.with_default_config(tempfile.mkdtemp())