locodellm.bench#
Benchmarking utilities for generated code.
- class locodellm.bench.BenchPromptTest(tests: list[PromptTest], description: str = '', max_length: int = 200)#
Benchmarks a model by running prompt tests and comparing results.
- tests#
The list of
PromptTestto evaluate.
- description#
A short description of the benchmark.
- max_length#
Maximum token length for generation.
- run(session: Any, verbose: int = 0, json_output: str | None = None, **search_options: Any) BenchResult#
Runs all prompt tests against the given session.
For each
PromptTest, the session is restarted, the prompt is submitted, the generated code is extracted and executed with the specified arguments, and the results are compared to expected values.- Parameters:
session – A
SessionStateinstance.verbose – Verbosity level. When >= 1, displays a progress bar.
json_output – If set, writes results incrementally to this JSON file path after each prompt test completes.
**search_options – Extra options forwarded to
generate().
- Returns:
A
BenchResultwith outcomes for all tests.
- class locodellm.bench.BenchResult(results: list[PromptTestResult] = <factory>)#
Aggregated results from a
BenchPromptTestrun.- results#
Individual results for each prompt test.
- to_dataframe() DataFrame#
Exports the results as a pandas DataFrame.
Each row represents one expected result assertion. Columns are:
prompt: the prompt textduration: time in seconds spent generating the answercompiled: whether the generated code compiledran: whether the generated code ran without errorinput_index: index of the input setpassed: whether expected matched actual
- Returns:
A
pandas.DataFramewith one row per assertion.
- class locodellm.bench.ExpectedResult(args: tuple[Any, ...], expected: Any)#
A single expected input/output pair for a generated function.
- expected#
The expected return value.
- Type:
Any
- classmethod from_dict(data: dict[str, Any]) ExpectedResult#
Creates an instance from a dictionary.
- class locodellm.bench.PromptTest(prompt: str, expected: list[ExpectedResult] = <factory>)#
A test case pairing a prompt with expected input/output pairs.
- expected#
A list of
ExpectedResultinstances describing how the generated function should behave.
- classmethod from_json(text: str) PromptTest#
Deserializes a
PromptTestfrom a JSON string.
- class locodellm.bench.PromptTestResult(prompt_test: PromptTest, generated_code: str, run_status: RunStatus, results: list[tuple[~locodellm.bench.prompt_test.ExpectedResult, ~typing.Any, bool]]=<factory>, duration: float = 0.0, token_count: int = 0)#
Result of running a single
PromptTest.- prompt_test#
The original prompt test that was evaluated.
- results#
A list of tuples
(expected, actual, passed)for eachExpectedResultentry.- Type:
list[tuple[locodellm.bench.prompt_test.ExpectedResult, Any, bool]]
- class locodellm.bench.RunStatus(compiled: bool = False, compile_error: BaseException | None = None, ran: bool = False, run_error: BaseException | None = None, result: Any = None, _function: Any = None)#
Result of compiling and running a Python function string.
- compile_error#
The exception raised during compilation, if any.
- Type:
BaseException | None
- run_error#
The exception raised during execution, if any.
- Type:
BaseException | None
- result#
The return value of the function, or None if it did not run or raised.
- Type:
Any
- locodellm.bench.dump_prompt_tests(tests: list[PromptTest], path: str) None#
Writes a list of
PromptTestto a JSON Lines file.Each line in the output file is a self-contained JSON object representing one
PromptTest.- Parameters:
tests – The list of prompt tests to serialize.
path – File path to write to.
- locodellm.bench.get_available_benchmarks() dict[str, str]#
Returns a dictionary of available benchmark names and descriptions.
- Returns:
A mapping from benchmark name to a short description.
- locodellm.bench.load_prompt_tests(path: str) list[PromptTest]#
Reads a list of
PromptTestfrom a JSON Lines file.- Parameters:
path – File path to read from.
- Returns:
The deserialized list of prompt tests.
- locodellm.bench.run_function(source: str) RunStatus#
Compiles and executes a Python function defined in source.
The function is called with
UNDEFINEDfor each of its positional parameters and as the default for keyword arguments.- Parameters:
source – Python source code defining exactly one function.
- Returns:
A
RunStatusdescribing the outcome.