th'ddlZddlZddlmZmZddlmZddlmZddl m Z ddl m Z eej eZdeded efd Zd ed eefd Zd eeeefd efdZe jeddZe jeddZd efdZedkrejejdedejds eddS eddZeeZedeeddd d!d"d#d!d$d%d!gZeeZ ed&e dS#e!$rZ"ed'e"YdZ"["dSdZ"["wwxYwdS)(N)ListDict) load_dotenv) ReActAgent) FunctionTool) GoogleGenAIfilenamedefault_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 : Texc_info) ospathdirname__file__joinopenreadloggerinfoFileNotFoundErrorwarning Exceptionerror)r r script_dir prompt_pathfpromptes ;/Users/aedelon/Workspace/GAIA_Agent/agents/planner_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  objectivecZtd|dddtjdd}tjd}|stdd gSd |d } t || }td |||}|j d}g}|D]}|}|s|rU|d r;d|ddvr/| ddd} n4| dr|dd} n|} | r| | |st ddgStdt|d|S#t$r/} td| dd| gcYd} ~ Sd} ~ wwxYw)z Generate a list of sub-steps (4-8) from the given objective using an LLM. Args: objective (str): The research or task objective. Returns: List[str]: A list of sub-steps as strings, or an error message list. zGenerating plan for objective: Ndz...PLANNER_TOOL_LLM_MODELmodels/gemini-1.5-proGEMINI_API_KEYz/GEMINI_API_KEY not found for planning tool LLM.z+Error: GEMINI_API_KEY not set for planning.zYou are a research assistant. Given an objective, break it down into a list of 4-8 concise, actionable sub-steps. Ensure the steps are logically ordered. Objective: z$ Sub-steps (one per line, numbered):api_keymodelzUsing planning LLM:  r.z- z-LLM generated no sub-steps for the objective.z$Error: Failed to generate sub-steps.z Generated z sub-steps.z!LLM call failed during planning: TrzError during planning: )rrrgetenvrrcompletetextstripsplitisdigit startswithappendrlenr) r%planner_llm_modelgemini_api_key input_promptllmresponselines sub_stepsliner5r"s r#planrD$st KKF)DSD/FFFGGG ":> .  . . . /.8IJJJ >+<>>???<< -- ##%%++D11  ' 'D::<r?r@r"s r#synthesize_and_respondrQ`s KKHS\\HHHIII 4CDDD33Nw''TT 6::j-!A#--88H&;<<SacSSXSS!SSvSSSSI&=?VWWY/00N > GHHH==L..8MNNN C,ACCDDD<< -- +,,,} ... =!== MMM-!--------.s A#E// F'9#F"F'"F'rQzAggregates results from multiple sub-steps into a final coherent report. Input: results (List[Dict[str, str]]) where each dict has "sub_step" and "answer". Output: A unified report (str) or error message.)fnname descriptiongenerate_substepsuDecomposes a high-level objective into a concise roadmap of 4–8 actionable sub-steps using an LLM. Input: objective (str). Output: List of sub-step strings (List[str]) or error list.ctdtjdd}tjd}|s)tdt d t ||}td|d }td |}||krtd ttg}gd }td d||||}td|S#t$r%}td|dd}~wwxYw)zInitializes the Planner Agent.zInitializing PlannerAgent...PLANNER_AGENT_LLM_MODELr)r*z*GEMINI_API_KEY not found for PlannerAgent.z+GEMINI_API_KEY must be set for PlannerAgentr+zUsing agent LLM: zFYou are PlannerAgent... [Default prompt content - replace with actual]z#../prompts/planner_agent_prompt.txtz6Using default/fallback system prompt for PlannerAgent.) code_agentresearch_agent math_agent role_agentimage_analyzer_agenttext_analyzer_agentverifier_agentreasoning_agent planner_agentzStrategically plans tasks by breaking down objectives into sub-steps using `generate_substeps`. Orchestrates execution by handing off sub-steps to specialized agents. Synthesizes final results using `synthesize_and_respond`.)rSrTtoolsr? system_promptcan_handoff_toz&PlannerAgent initialized successfully.z*Error during PlannerAgent initialization: TrN) rrrr3r ValueErrorrr$rgenerate_substeps_toolsynthesize_toolrr) agent_llm_modelr=r?default_system_promptrbravalid_handoffsagentr"s r#initialize_planner_agentrksv KK.///i 9;RSSOY/00N H ABBBFGGG+.HHH 999:::"j-.SUjkk 1 1 1 ^^T U U U(9     L')      <===  E!EEPT UUU s0B"D E D==E__main__z4%(asctime)s - %(name)s - %(levelname)s - %(message)s)levelformatz0Running planner_agent.py directly for testing...r*zDError: GEMINI_API_KEY environment variable not set. Cannot run test.z Testing plan generation...zCAnalyze the market trends for electric vehicles in Europe for 2024.zGenerated Sub-steps: z Testing synthesis...z(Identify key EV manufacturers in Europe.zTesla, VW, Stellantis, Renault.)rHrIzFind recent sales data.z,EV sales grew 25% year-over-year in Q1 2024.zAnalyze government incentives.z2Germany reduced subsidies, France maintained them.zSynthesized Report: zError during testing: )#rloggingtypingrrdotenvrllama_index.core.agent.workflowrllama_index.core.toolsrllama_index.llms.google_genair getLogger__name__rstrr$rDrQ from_defaultsrfrerk basicConfigINFOrr3printtest_objectivesubsteps test_resultsreportrr"r#rs 666666//////555555   8 $ $C&:/C:/DI:/:/:/:/x-.Dc3h$8-.S-.-.-.-.`-,, ! ; 43  ^ 7*7777t zGgl3ijjjj KKBCCC 29% & &0 TUUUUU 0 E0 1 1 1bNtN++H E5855 6 6 6 E* + + +GStuu6Bpqq=I}~~L ,+L99F E2&22 3 3 3 3 3  0 0 0 E.1.. / / / / / / / / / 0;s-AEE% E  E%