File size: 1,811 Bytes
5e0532d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()