C"h(VddlZddlZddlmZddlmZddlmZddlm Z ej e Z de de de fd Zd e de fd Zd e de fd ZejeddZejeddZdefdZe dkrejejde dddgZdeDZer$eddeddS eddZeeZed edS#e$rZed!eYdZ[dSdZ[wwxYwdS)"N) 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 contextc6tdt|tjdd}tjd}|stddSd|d} t ||d d d }td |||}td|jS#t$r.}td|dd|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-miniOPENAI_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. highg)\(?i@)modelapi_keyreasoning_effort temperature max_tokenszUsing 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_promptllmresponsers r reasoning_tool_fnr5 sX KKOWOOPPP)$9=IIY/00N W PQQQVV ".%"#      A,?AABBB<< 011 :;;;} ... AaAAD QQQ-!--------.s9A&C D*#D DDquestionc(td|ddtjd}|stddStjdd}d |d } t |dd }td |||}td|jS#t$r.}td|dd|cYd}~Sd}~wwxYw)a Answer any question by following this strict format: 1. Include your chain of thought (your reasoning steps). 2. End your reply with the exact template: FINAL ANSWER: [YOUR FINAL ANSWER] YOUR FINAL ANSWER must be: - A number, or - As few words as possible, or - A comma-separated list of numbers and/or strings. Formatting rules: * If asked for a number, do not use commas or units (e.g., $, %), unless explicitly requested. * If asked for a string, do not include articles or abbreviations (e.g., city names), and write digits in plain text. * If asked for a comma-separated list, apply the above rules to each element. This tool should be invoked immediately after completing the final planning sub-step. zAnswering question: NdGEMINI_API_KEYz0GEMINI_API_KEY not set for answer_question tool.zError: GEMINI_API_KEY not set.ANSWER_TOOL_LLM_MODELgemini-2.5-pro-preview-03-25aBYou are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use commas for thousands or any units like $ or % unless specified. If you are asked for a string, omit articles and abbreviations, and write digits in plain text. If you are asked for a comma separated list, apply these rules to each element. Question: z Answer:皙?r(r'r*zUsing answer LLM: zAnswer generated successfully.z*LLM call failed during answer generation: Tr z Error during answer generation: ) rrrr-rrr.r/r)r6gemini_api_key model_nameassistant_promptr3r4rs r answer_questionrAWsH  KK7x~77888Y/00N 0 GHHH//24RSSJ    6.8Vdhiii 555666<< 011 4555} 666 E!EEPT UUU5!555555556s4A$C D##D D Dreasoning_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 descriptionrAzUse this tool to answer any question, reporting your reasoning steps and ending with 'FINAL ANSWER: ...'. Invoke this tool immediately after the final sub-step of planning is complete.c Dtdtjdd}tjd}|s)tdt d t |dd}td |d }td |}||krtd td dtg||gd}|S#t$r%}td|dd}~wwxYw)z Initializes the Reasoning Agent.zInitializing ReasoningAgent...REASONING_AGENT_LLM_MODELr;r9z,GEMINI_API_KEY not found for ReasoningAgent.z-GEMINI_API_KEY must be set for ReasoningAgentr<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_agentaAn autonomous reasoning specialist that applies `reasoning_tool` to perform in-depth chain-of-thought analysis on incoming queries or contexts, then seamlessly delegates the synthesized insights to `planner_agent` or `long_context_management_agent` for subsequent task orchestration.) code_agentresearch_agent math_agent role_agentimage_analyzer_agenttext_analyzer_agent planner_agentlong_context_management_agentadvanced_validation_agentvideo_analyzer_agent)rDrEtoolsr3 system_promptcan_handoff_toz,Error during ReasoningAgent initialization: Tr N) rrrr-r ValueErrorrr!rrrBr)agent_llm_modelr>r3default_system_promptrTagentrs r initialize_reasoning_agentrZsW KK0111i ;=[\\OY/00N J CDDDHIII(.8Vdhiii 999:::"l-.UWlmm 1 1 1 ^^V W W W"X ""'      2  GAGGRV WWW s0A?C00 D: DD__main__z4%(asctime)s - %(name)s - %(levelname)s - %(message)s)levelformatz2Running reasoning_agent.py directly for testing...r9ALPAFLOW_OPENAI_API_KEYc:g|]}tj||S)rr-).0keys r rcs%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: )rloggingllama_index.core.agent.workflowrllama_index.core.toolsrllama_index.llms.google_genairllama_index.llms.openair getLogger__name__rstrr!r5rA from_defaultsrBrZ basicConfigINFOr required_keys missing_keysprintr test_contextreasoning_outputrrr`rdr rus 666666//////555555******  8 $ $C&4.s4.s4.4.4.4.n/6c/6c/6/6/6/6f,+  _ -,,  Y 4J4444n zGgl3ijjjj KKDEEE&'@AMGG=GGGL0 m$))LBYBYmmmnnnnn 0 E2 3 3 3NL00>>  E?-=?? @ @ @ @ @ 0 0 0 E.1.. / / / / / / / / / 01s &DD& D!!D&