from app.services.repl import repl_service from app.services.llm import llm_service from typing import Dict, Any class RLMService: """ Reason Synergizing Service. Uses the REPL to solve complex logical problems through code generation and execution. """ async def reason(self, problem: str) -> str: """ Takes a logical problem, generates Python code to solve it, and returns the result. """ system_prompt = """ You are the ORA Reasoning Engine. Your task is to write a Python script to solve the user's logical problem or calculation. - Only output the Python code, nothing else. - Use print() to output final results. - You have access to: math, datetime, json. - Do not use dangerous modules (os, subprocess, etc). - Keep the script efficient. """ # 1. Generate the code prompt = f"Problem: {problem}\n\nWrite a Python script to solve this. Only output code." llm_res = await llm_service.generate_response(prompt, system_prompt=system_prompt) code = llm_res.get("content", "").strip() # Clean up markdown if LLM adds it if code.startswith("```python"): code = code.split("```python")[1].split("```")[0].strip() elif code.startswith("```"): code = code.split("```")[1].split("```")[0].strip() if not code: return "Error: Could not generate reasoning code." # 2. Execute in REPL print(f"RLM: Executing reasoning code...") result = repl_service.execute(code) if not result["success"]: return f"Reasoning Fallacy (Code Error): {result['error']}" return result["output"] rlm_service = RLMService()