Refine a strategy based on its application to a specific problem. Args: strategy: The strategy to refine problem: The problem that was solved response: The LLM's final response to the problem thinking: The LLM's reasoning process (if any) client:
(strategy: Strategy, problem: str, response: str, thinking: Optional[str], client, model: str)
| 175 | return results |
| 176 | |
| 177 | def refine_strategy(strategy: Strategy, problem: str, response: str, thinking: Optional[str], client, model: str) -> Strategy: |
| 178 | """ |
| 179 | Refine a strategy based on its application to a specific problem. |
| 180 | |
| 181 | Args: |
| 182 | strategy: The strategy to refine |
| 183 | problem: The problem that was solved |
| 184 | response: The LLM's final response to the problem |
| 185 | thinking: The LLM's reasoning process (if any) |
| 186 | client: LLM client for making API calls |
| 187 | model: Model identifier |
| 188 | |
| 189 | Returns: |
| 190 | Strategy: The refined strategy |
| 191 | """ |
| 192 | try: |
| 193 | # Include thinking in refinement if available |
| 194 | full_response = thinking + "\n\n" + response if thinking else response |
| 195 | |
| 196 | messages = [ |
| 197 | { |
| 198 | "role": "system", |
| 199 | "content": STRATEGY_REFINEMENT_PROMPT |
| 200 | }, |
| 201 | { |
| 202 | "role": "user", |
| 203 | "content": ( |
| 204 | f"Original strategy for {strategy.problem_type} problems:\n{strategy.strategy_text}\n\n" |
| 205 | f"New problem:\n{problem}\n\n" |
| 206 | f"Solution process (including reasoning):\n{full_response}\n\n" |
| 207 | f"Provide a refined version of the original strategy that incorporates " |
| 208 | f"any insights from this new example." |
| 209 | ) |
| 210 | } |
| 211 | ] |
| 212 | |
| 213 | refine_response = client.chat.completions.create( |
| 214 | model=model, |
| 215 | messages=messages, |
| 216 | temperature=0.5, |
| 217 | max_tokens=DEFAULT_MAX_TOKENS # Increased token limit for reasoning LLMs |
| 218 | ) |
| 219 | |
| 220 | response_text = refine_response.choices[0].message.content |
| 221 | |
| 222 | # Extract refined strategy and thinking |
| 223 | refined_text, refinement_thinking = extract_thinking(response_text) |
| 224 | if not refined_text.strip(): |
| 225 | refined_text = response_text # Use full response if extraction failed |
| 226 | |
| 227 | logger.debug(f"Strategy refinement - raw response: '{response_text}'") |
| 228 | logger.debug(f"Strategy refinement - final text after removing thinking: '{refined_text}'") |
| 229 | |
| 230 | # Create a copy of the strategy with the refined text |
| 231 | refined_strategy = Strategy( |
| 232 | strategy_id=strategy.strategy_id, |
| 233 | problem_type=strategy.problem_type, |
| 234 | strategy_text=refined_text.strip(), |
no test coverage detected