ProgramPromptBuilder

Helpers & Utilities · AiDotNet.Evolution.Prompts

Turns a parent program and its surroundings into the bounded, redacted messages sent to a model.

For Beginners

This assembles the actual message your search sends to the AI: the program to improve, how it scored, what was tried before, a few example programs, whatever the program printed when it ran, and the instruction to propose a change. You create one builder with your settings and reuse it for every proposal. Because it is completely deterministic, running the same search twice with the same seed sends the AI exactly the same words both times — which is what lets you compare two experiments honestly.

How It Works

The builder is the only place prompt text is assembled, and it is a pure function of three things: the options it was constructed with, the ProgramPromptContext it is given, and the StableRandom stream it draws from. No clock, no culture, no process-global generator, no file system at build time. That is what makes a run reproducible: replay the same seed and every request body is byte-identical, which is the property a benchmark needs and the reference OpenEvolve sampler cannot offer, because it samples its diverse programs and its template variations from Python’s unseeded module-level generator.

Validation happens when the builder is constructed, not when a prompt is rendered. Every template it will ever use is checked so that each of its placeholders is resolvable — either supplied by the builder, declared as a custom variable, or declared as a template variation — and the offending names are listed if not. Upstream discovers the same class of mistake as a KeyError from inside a formatting call, part-way through a run that has already spent money.

Everything that reaches a prompt is bounded and scrubbed. Program text is truncated to a per-program ceiling, execution output is redacted by PromptTextRedactor and capped in bytes, diagnostics are limited in number, and the finished message is held under a total character ceiling by dropping optional sections in a fixed order — inspirations first, then history, then artifacts — before any hard truncation is considered. Whether anything was dropped is reported through WasTruncated.

Constructors

Constructor Summary
ProgramPromptBuilder(ProgramEvolutionPromptOptions promptOptions, ProgramEvolutionOptions programOptions, ProgramPromptTemplateSet templateSet) Initializes a prompt builder and validates every template it will render.

Properties

Property Summary
ProgramPromptTemplateSet Templates { get; } Gets the validated template set this builder renders from.
string VersionHash { get; } Gets a hash over the templates and the settings that shape every prompt this builder renders.

Methods

Method Summary
IReadOnlyList<ChatMessage> BuildEvaluationMessages(ProgramGenome program, IReadOnlyList<string> criteria, string responseSchema) Builds the messages that ask a model to score a program against named criteria.
ProgramEvolutionOptions GetProgramOptions() Gets a copy of the program settings this builder was constructed with.
ProgramEvolutionPromptOptions GetPromptOptions() Gets a copy of the prompt settings this builder was constructed with.
ProgramPromptResult Build(ProgramPromptContext context, StableRandom random) Builds the messages that ask a model to improve the parent program.
string ToCriterionFieldName(string criterion) Converts a human-readable criterion into the JSON field name the evaluation prompt asks for.