uh}@ddlZddlZddlmZddlmZddlmZddlm Z ddl m Z eej e Zdeded efd Zd ed efd Zejed dZd efdZe dkrejejdedddgZdeDZer$eddeddS eddZeeZededS#e$rZedeYdZ[dSdZ[wwxYwdS)N) load_dotenv) ReActAgent) FunctionTool) GoogleGenAI)OpenAIfilenamedefault_promptreturnc tjt}tj||}t |d5}|}td||cdddS#1swxYwYdS#t$r&t d|d|d|cYSt$r.}t d|d|d |cYd}~Sd}~wwxYw) z Loads a prompt from a text file.rz Successfully loaded prompt from Nz Prompt file z not found at z. Using default.zError loading prompt file z: Texc_info) ospathdirname__file__joinopenreadloggerinfoFileNotFoundErrorwarning Exceptionerror)rr script_dir prompt_pathfpromptes =/Users/aedelon/Workspace/GAIA_Agent/agents/reasoning_agent.pyload_prompt_from_filer"sl W__X.. gll:x88 +s # # qVVXXF KKH;HH I I I                   [h[[k[[[\\\  A(AAaAAD QQQsHAB#3B B#BB#BB##-D  D #D>D D contextc0tdt|tjdd}tjd}|stddSd|d} t || }td |||}td |jS#t$r.}td |d d|cYd}~Sd}~wwxYw)a Perform chain-of-thought reasoning over the provided context using a dedicated LLM. Args: context (str): The conversation/workflow history and current problem statement. Returns: str: A structured reasoning trace and conclusion, or an error message. z.Executing reasoning tool with context length: REASONING_LLM_MODELz gpt-4o-miniALPAFLOW_OPENAI_API_KEYz9ALPAFLOW_OPENAI_API_KEY not found for reasoning tool LLM.zEError: ALPAFLOW_OPENAI_API_KEY must be set to use the reasoning tool.zYou are an expert reasoning engine. Analyze the following workflow context and problem statement: --- CONTEXT START --- as --- CONTEXT END --- Perform the following steps: 1. **Comprehension**: Identify the core question/problem and key constraints from the context. 2. **Decomposition**: Break the problem into logical sub-steps. 3. **Chain-of-Thought**: Reason through each sub-step, stating assumptions and deriving implications. 4. **Verification**: Check conclusions against constraints. 5. **Synthesis**: Integrate results into a cohesive answer/recommendation. 6. **Clarity**: Use precise language. Respond with your numbered reasoning steps followed by a concise final conclusion or recommendation. )modelapi_keyzUsing reasoning LLM: z$Reasoning tool execution successful.z&Error during reasoning tool LLM call: Tr zError during reasoning: N) rrlenrgetenvrrcompletetextr)r#reasoning_llm_modelopenai_api_keyreasoning_promptllmresponser s r!reasoning_tool_fnr2$sO KKOWOOPPP)$9=IIY899N W PQQQVV ".%"     A,?AABBB<< 011 :;;;} ... AaAAD QQQ-!--------.s9A#C D'#D DDreasoning_toolzApplies detailed chain-of-thought reasoning to the provided workflow context using a dedicated LLM. Input: context (str). Output: Reasoning steps and conclusion (str) or error message.)fnname descriptioncttdtjdd}tjd}|s)tdt d t ||}td|d }td |}||krtd td d tg||dg}td|S#t$r%}td|dd}~wwxYw)z Initializes the Reasoning Agent.zInitializing ReasoningAgent...REASONING_AGENT_LLM_MODELzmodels/gemini-1.5-proGEMINI_API_KEYz,GEMINI_API_KEY not found for ReasoningAgent.z-GEMINI_API_KEY must be set for ReasoningAgent)r(r'zUsing agent LLM: zHYou are ReasoningAgent... [Default prompt content - replace with actual]z%../prompts/reasoning_agent_prompt.txtz8Using default/fallback system prompt for ReasoningAgent.reasoning_agentzA pure reasoning agent that uses the `reasoning_tool` for detailed chain-of-thought analysis on the provided context, then hands off the result to the `planner_agent`. planner_agent)r5r6toolsr0 system_promptcan_handoff_toz(ReasoningAgent initialized successfully.z,Error during ReasoningAgent initialization: Tr N) rrrr*r ValueErrorrr"rrr3r)agent_llm_modelgemini_api_keyr0default_system_promptr=agentr s r!initialize_reasoning_agentrDcs] KK0111i ;=TUUOY/00N J CDDDHIII.HHH 999:::"l-.UWlmm 1 1 1 ^^V W W W"]""'+,      >???  GAGGRV WWW s0BD D7 D22D7__main__z4%(asctime)s - %(name)s - %(levelname)s - %(message)s)levelformatz2Running reasoning_agent.py directly for testing...r9r&c:g|]}tj||S)rr*).0keys r! rLs%GGGC #GCGGGz1Error: Required environment variable(s) not set: z, z. Cannot run test.z Testing reasoning_tool_fn...zpUser asked: What is the capital of France? ResearchAgent found: Paris. VerifierAgent confirmed: High confidence.zReasoning Tool Output: zError during testing: ) rloggingdotenvrllama_index.core.agent.workflowrllama_index.core.toolsrllama_index.llms.google_genairllama_index.llms.openair getLogger__name__rstrr"r2 from_defaultsr3rD basicConfigINFOr required_keys missing_keysprintr test_contextreasoning_outputrr rIrMr!r_sU 666666//////555555******   8 $ $C&2.s2.s2.2.2.2.j,+  _ 'J''''T zGgl3ijjjj KKDEEE&'@AMGG=GGGL0 m$))LBYBYmmmnnnnn 0 E2 3 3 3NL00>>  E?-=?? @ @ @ @ @ 0 0 0 E.1.. / / / / / / / / / 01s&C==DDD