Optimizer that improves agent prompts using the Reflection method.
| 32 | reasoning: str = Field(description="The reasoning for the decision") |
| 33 | |
| 34 | class ReflectionOptimizer(Optimizer): |
| 35 | """Optimizer that improves agent prompts using the Reflection method.""" |
| 36 | model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow") |
| 37 | |
| 38 | prompt_name: str = Field(default="reflection_optimizer", description="The name of the prompt") |
| 39 | model_name: str = Field(default="openrouter/gemini-3-flash-preview", description="The name of the model") |
| 40 | memory_name: Optional[str] = Field(default=None, description="Name of the optimizer memory system for recording optimization history") |
| 41 | batchsize: int = Field(default=10, description="Batch size for aggregating historical reflections") |
| 42 | |
| 43 | def __init__(self, |
| 44 | workdir: str, |
| 45 | prompt_name: str = "reflection_optimizer", |
| 46 | model_name: str = "openrouter/gemini-3-flash-preview", |
| 47 | memory_name: Optional[str] = "optimizer_memory_system", |
| 48 | optimize_trainable_variables: bool = True, |
| 49 | optimize_solution: bool = True, |
| 50 | batchsize: int = 10, |
| 51 | max_steps: int = 5, |
| 52 | **kwargs |
| 53 | ): |
| 54 | """ |
| 55 | Initialize the optimizer. |
| 56 | |
| 57 | Args: |
| 58 | workdir: Working directory for the optimizer |
| 59 | prompt_name: Name of the prompt used for optimization |
| 60 | model_name: Model name for optimization. |
| 61 | memory_name: Optional name of the optimizer memory system for recording optimization history. |
| 62 | optimize_trainable_variables: Whether to optimize trainable variables (prompt/tool) in phase 1 |
| 63 | optimize_solution: Whether to optimize solution in phase 2 |
| 64 | """ |
| 65 | super().__init__( |
| 66 | workdir=workdir, |
| 67 | prompt_name=prompt_name, |
| 68 | model_name=model_name, |
| 69 | memory_name=memory_name, |
| 70 | **kwargs) |
| 71 | self.workdir = workdir |
| 72 | if model_name: |
| 73 | self.model_name = model_name |
| 74 | if prompt_name: |
| 75 | self.prompt_name = prompt_name |
| 76 | self.memory_name = memory_name |
| 77 | self.batchsize = batchsize |
| 78 | |
| 79 | self.max_steps = max_steps |
| 80 | |
| 81 | self.optimize_trainable_variables = optimize_trainable_variables |
| 82 | self.optimize_solution = optimize_solution |
| 83 | |
| 84 | async def _read_historical_reflections(self, results_file_path: str) -> List[str]: |
| 85 | """ |
| 86 | Read historical phase 1 reflections from results file. |
| 87 | |
| 88 | Args: |
| 89 | results_file_path: Path to the results JSON file |
| 90 | |
| 91 | Returns: |
no outgoing calls
no test coverage detected