File size: 6,050 Bytes
e7c3249
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""
This module provides functionality to run benchmarks on different folders within
the 'benchmark' directory, wait for their completion, and generate a report.
"""

# list all folders in benchmark folder
# for each folder, run the benchmark
import contextlib
import json
import os
import subprocess

from datetime import datetime
from itertools import islice
from pathlib import Path
from typing import Iterable, Union

from tabulate import tabulate
from typer import run


def main(
    n_benchmarks: Union[int, None] = None,
):
    """
    Main function that runs benchmarks on folders within the 'benchmark' directory.

    Parameters
    ----------
    n_benchmarks : Union[int, None], optional
        The number of benchmarks to run. If None, all benchmarks are run.

    """

    path = Path("benchmark")

    folders: Iterable[Path] = path.iterdir()

    if n_benchmarks:
        folders = islice(folders, n_benchmarks)

    benchmarks = []
    results = []
    for bench_folder in folders:
        if os.path.isdir(bench_folder):
            print(f"Running benchmark for {bench_folder}")

            log_path = bench_folder / "log.txt"
            log_file = open(log_path, "w")
            process = subprocess.Popen(
                [
                    "python",
                    "-u",  # Unbuffered output
                    "-m",
                    "gpt_engineer.cli.main",
                    bench_folder,
                    "--steps",
                    "benchmark",
                ],
                stdout=log_file,
                stderr=log_file,
                bufsize=0,
            )
            benchmarks.append(bench_folder)
            results.append((process, log_file))

            print("You can stream the log file by running:")
            print(f"tail -f {log_path}")
            print()

    for bench_folder, (process, file) in zip(benchmarks, results):
        process.wait()
        file.close()

        print("process", bench_folder.name, "finished with code", process.returncode)
        print("Running it. Original benchmark prompt:")
        print()
        with open(bench_folder / "prompt") as f:
            print(f.read())
        print()

        with contextlib.suppress(KeyboardInterrupt):
            subprocess.run(
                [
                    "python",
                    "-m",
                    "gpt_engineer.cli.main",
                    bench_folder,
                    "--steps",
                    "evaluate",
                ],
            )

    generate_report(benchmarks, path)


def generate_report(benchmarks, benchmark_path):
    """
    Generates a report of the benchmark results and optionally appends it to a markdown file.

    Parameters
    ----------
    benchmarks : list
        A list of benchmark folder paths that have been processed.
    benchmark_path : Path
        The path to the benchmark directory.

    """

    headers = ["Benchmark", "Ran", "Works", "Perfect", "Notes"]
    rows = []
    for bench_folder in benchmarks:
        memory = bench_folder / ".gpteng" / "memory"
        with open(memory / "review") as f:
            review = json.loads(f.read())
            rows.append(
                [
                    bench_folder.name,
                    to_emoji(review.get("ran", None)),
                    to_emoji(review.get("works", None)),
                    to_emoji(review.get("perfect", None)),
                    review.get("comments", None),
                ]
            )
    table: str = tabulate(rows, headers, tablefmt="pipe")
    print("\nBenchmark report:\n")
    print(table)
    print()
    append_to_results = ask_yes_no("Append report to the results file?")
    if append_to_results:
        results_path = benchmark_path / "RESULTS.md"
        current_date = datetime.now().strftime("%Y-%m-%d")
        insert_markdown_section(results_path, current_date, table, 2)


def to_emoji(value: bool) -> str:
    """
    Converts a boolean value to its corresponding emoji representation.

    Parameters
    ----------
    value : bool
        The boolean value to convert.

    Returns
    -------
    str
        An emoji string representing the boolean value.

    """

    return "\U00002705" if value else "\U0000274C"


def insert_markdown_section(file_path, section_title, section_text, level):
    """
    Inserts a new section into a markdown file at the specified level.

    Parameters
    ----------
    file_path : Path
        The path to the markdown file.
    section_title : str
        The title of the section to insert.
    section_text : str
        The text content of the section to insert.
    level : int
        The header level of the section.

    """

    with open(file_path, "r") as file:
        lines = file.readlines()

    header_prefix = "#" * level
    new_section = f"{header_prefix} {section_title}\n\n{section_text}\n\n"

    # Find the first section with the specified level
    line_number = -1
    for i, line in enumerate(lines):
        if line.startswith(header_prefix):
            line_number = i
            break

    if line_number != -1:
        lines.insert(line_number, new_section)
    else:
        print(
            f"Markdown file was of unexpected format. No section of level {level} found. "
            "Did not write results."
        )
        return

    # Write the file
    with open(file_path, "w") as file:
        file.writelines(lines)


def ask_yes_no(question: str) -> bool:
    """
    Asks a yes/no question and returns the response as a boolean value.

    Parameters
    ----------
    question : str
        The yes/no question to ask.

    Returns
    -------
    bool
        True if the answer is 'yes', False if 'no'.

    """

    while True:
        response = input(question + " (y/n): ").lower().strip()
        if response == "y":
            return True
        elif response == "n":
            return False
        else:
            print("Please enter either 'y' or 'n'.")


if __name__ == "__main__":
    run(main)