Ask LLM to simulate code execution.
(code: str, error: str, client, model: str)
| 256 | return None, response.usage.completion_tokens |
| 257 | |
| 258 | def simulate_execution(code: str, error: str, client, model: str) -> Tuple[Any, int]: |
| 259 | """Ask LLM to simulate code execution.""" |
| 260 | logger.info("Attempting code simulation with LLM") |
| 261 | |
| 262 | response = client.chat.completions.create( |
| 263 | model=model, |
| 264 | messages=[ |
| 265 | {"role": "system", "content": SIMULATION_PROMPT.format( |
| 266 | code=code, error=error)}, |
| 267 | {"role": "user", "content": "Simulate this code and return the final answer value."} |
| 268 | ], |
| 269 | temperature=0.2 |
| 270 | ) |
| 271 | |
| 272 | try: |
| 273 | result = response.choices[0].message.content.strip() |
| 274 | # Try to convert to appropriate type |
| 275 | try: |
| 276 | answer = ast.literal_eval(result) |
| 277 | except: |
| 278 | answer = result |
| 279 | logger.info(f"Simulation successful. Result: {answer}") |
| 280 | return answer, response.usage.completion_tokens |
| 281 | except Exception as e: |
| 282 | logger.error(f"Failed to parse simulation result: {str(e)}") |
| 283 | return None, response.usage.completion_tokens |
| 284 | |
| 285 | def run(system_prompt: str, initial_query: str, client, model: str) -> Tuple[str, int]: |
| 286 | """Main Chain of Code execution function.""" |