Commit ·
758564e
1
Parent(s): 061585b
Updated versions of logging and some improvements of nodes configuration, adding gpt-5-mini
Browse filesFiles changed (11)
- data/images.jpegAdded
- src/agent.py+10-3
- src/config.py+9-75
- src/nodes.py+137-9
- src/prompts/prompts.py+24-19
- src/state.py+1
- src/tools/code_interpreter.py+1-1
- src/tools/tools.py+1-1
- src/tools/web_crawler.pyAdded
- src/utils/utils.py+7-1
- src/workflow_test.ipynb+41-119
1 unmodified line23456784 unmodified lines13141516171819202122232 unmodified lines26272829303132331 unmodified linefrom nodes import (query_input, complexity_assessor, planner, agent, simple_executor, critic_evaluator, replanner, enhanced_finalizer)from state import AgentStatefrom langgraph.graph import StateGraph, ENDfrom nodes import should_continue, should_use_planning, should_replanfrom langgraph.checkpoint.memory import MemorySaverfrom config import DEBUGGING_TOOL_NODE4 unmodified linesbuilder.add_node("PLANNING", planner)builder.add_node("AGENT", agent)builder.add_node("TOOLS", DEBUGGING_TOOL_NODE)builder.add_node("FINALIZER", enhanced_finalizer)builder.add_node("SIMPLE_EXECUTOR", simple_executor)builder.add_node("CRITIC", critic_evaluator)builder.add_node("REPLANNER", replanner)builder.set_entry_point("INPUT")builder.add_edge("INPUT", "COMPLEXITY_ASSESSOR")2 unmodified linesshould_use_planning,{"simple_executor": "SIMPLE_EXECUTOR", "planner": "PLANNING"},)builder.add_edge("SIMPLE_EXECUTOR", "FINALIZER")builder.add_edge("PLANNING", "AGENT")builder.add_conditional_edges(1 unmodified line23456784 unmodified lines131415161718192021222324252 unmodified lines282930313233343536373839401 unmodified linefrom nodes import (query_input, complexity_assessor, planner, agent, simple_executor, critic_evaluator, replanner, enhanced_finalizer)from state import AgentStatefrom langgraph.graph import StateGraph, ENDfrom nodes import should_continue, should_use_planning, should_replan, should_use_tools_simple_executorfrom langgraph.checkpoint.memory import MemorySaverfrom config import DEBUGGING_TOOL_NODE4 unmodified linesbuilder.add_node("PLANNING", planner)builder.add_node("AGENT", agent)builder.add_node("TOOLS", DEBUGGING_TOOL_NODE)builder.add_node("TOOLS_SIMPLE", DEBUGGING_TOOL_NODE)builder.add_node("FINALIZER", enhanced_finalizer)builder.add_node("SIMPLE_EXECUTOR", simple_executor)builder.add_node("CRITIC", critic_evaluator)builder.add_node("REPLANNER", replanner)builder.set_entry_point("INPUT")builder.add_edge("INPUT", "COMPLEXITY_ASSESSOR")2 unmodified linesshould_use_planning,{"simple_executor": "SIMPLE_EXECUTOR", "planner": "PLANNING"},)builder.add_conditional_edges("SIMPLE_EXECUTOR",should_use_tools_simple_executor,{"tools": "TOOLS_SIMPLE", "final_answer": "FINALIZER"},)builder.add_edge("TOOLS_SIMPLE", "FINALIZER")builder.add_edge("PLANNING", "AGENT")builder.add_conditional_edges(
10 unmodified lines1112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949510 unmodified linesTOOLS = [download_file_from_url, web_search,arxiv_search, wiki_search, add, subtract, multiply, divide,power, analyze_excel_file, analyze_csv_file, analyze_docx_file,analyze_pdf_file, analyze_txt_file, analyze_image_file,vision_qa_gemma, safe_code_run]class DebuggingToolNode(ToolNode):def __init__(self, tools):super().__init__(tools)def __call__(self, state):log_stage("TOOL NODE", subtitle="Dispatching tool calls", icon="🛠️")messages = state.get("messages", [])last_message = messages[-1] if messages else Noneif not last_message or not hasattr(last_message, "tool_calls"):log_stage("TOOL ERROR", subtitle="No tool calls found", icon="❌")return statetool_calls = last_message.tool_callslog_stage("TOOL DISPATCH", subtitle=f"Executing {len(tool_calls)} tool(s)", icon="🔧")for call in tool_calls:print(f" - {call['name']}: {call['args']}")try:# Выполняем инструментыresult = super().__call__(state)# Проверяем результатыnew_messages = result.get("messages", [])tool_messages = [msg for msg in new_messages[len(messages):]if isinstance(msg, ToolMessage)]log_stage("TOOL RESULTS", subtitle=f"Got {len(tool_messages)} responses", icon="📨")# Логируем результатыfor msg in tool_messages:content_preview = msg.content[:100] + "..." if len(msg.content) > 100 else msg.contentprint(f" - {msg.name}: {content_preview}")# Автоматически добавляем сигнал завершения шага после успешного выполнения инструментовif tool_messages:current_step = state.get("current_step", 0)plan = state.get("plan")if plan and current_step < len(plan.steps):step_completion_msg = AIMessage(content=f"STEP COMPLETE: Successfully executed {len(tool_messages)} tool(s) for step {plan.steps[current_step].id}")result["messages"] = result["messages"] + [step_completion_msg]log_stage("STEP COMPLETION", subtitle=f"Step {current_step + 1} marked complete", icon="✅")# Продвигаем к следующему шагуresult["current_step"] = current_step + 1result["reasoning_done"] = False # Сброс для следующего шагаreturn resultexcept Exception as exc:log_stage("TOOL ERROR", subtitle=f"{type(exc).__name__}: {exc}", icon="❌")print(f"Full error: {repr(exc)}")# Создаем ToolMessage для каждого failed tool callerror_messages = []for call in tool_calls:error_msg = ToolMessage(content=f"ERROR: {type(exc).__name__}: {exc}",tool_call_id=call.get("id") or "unknown_call",name=call.get("name", "unknown_tool"),)error_messages.append(error_msg)return {"messages": messages + error_messages}TOOL_NODE = ToolNode(TOOLS)DEBUGGING_TOOL_NODE = DebuggingToolNode(TOOLS)llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.25)llm_with_tools = llm.bind_tools(TOOLS)planner_llm = llm.with_structured_output(PlannerPlan)10 unmodified lines1112131415161718192021222324252627282910 unmodified linesTOOLS = [download_file_from_url, web_search,arxiv_search, wiki_search, add, subtract, multiply, divide,power, analyze_excel_file, analyze_csv_file, analyze_docx_file,analyze_pdf_file, analyze_txt_file,vision_qa_gemma, safe_code_run]TOOL_NODE = ToolNode(TOOLS)DEBUGGING_TOOL_NODE = TOOL_NODEllm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7) #default 0.25llm_deterministic = ChatOpenAI(model="gpt-5-mini", temperature=0.05)planner_llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.1).with_structured_output(PlannerPlan)llm_criticist = ChatOpenAI(model="gpt-5-mini", temperature=0.3)llm_with_tools = llm_deterministic.bind_tools(TOOLS)llm_reasoning = ChatOpenAI(model="gpt-5-mini", temperature=0.3)
12 unmodified lines1314151617181993 unmodified lines11311411511611711856 unmodified lines17517617717817918016 unmodified lines19719819920020120220320 unmodified lines22422522622722822923023123223323423518 unmodified lines2542552562572582595 unmodified lines26526626726826927027127227327493 unmodified lines36836937037137237337431 unmodified lines40640740840941041141241341441541641710 unmodified lines42842943043143243343460 unmodified lines49549649749849950050139 unmodified lines5415425435445455465472 unmodified lines5505515525535545555565575585595605619 unmodified lines57157257357412 unmodified linesCRITIC_PROMPT,)from config import llm, TOOLS, planner_llm, llm_with_toolsfrom schemas import PlannerPlan, ComplexityLevel, CritiqueFeedback, ExecutionReport, ToolExecutionfrom utils.utils import (93 unmodified linescurrent_step = state.get("current_step", 0)reasoning_done = state.get("reasoning_done", False)plan: Optional[PlannerPlan] = state.get("plan")#steps = state["plan"].steps"""56 unmodified linesfile_contents = state.get("file_contents", {})file_list = ", ".join(file_contents.keys()) if file_contents else "none provided"system_message = SystemMessage(content=SYSTEM_EXECUTOR_PROMPT.format(plan_summary=plan.summary,16 unmodified lines))stack = [system_message] + state["messages"] + [instruction]reasoning_response = llm.invoke(stack)log_stage("REASONING", subtitle=f"{current_step_info.id}", icon="🧠")print(reasoning_response.content)20 unmodified linesExplain what you need to do and why, then end your response.REASONING IS IMPERATIVE BEFORE ANY TOOL CALLS."""sys_msg = SystemMessage(content = reasoning_prompt)stack = [sys_msg] + state["messages"]step = llm.invoke(stack)print("=== REASONING STEP ===")print(step.content)18 unmodified lines# Используем модель С инструментами для выполненияstep = llm_with_tools.invoke(stack)print("=== TOOL EXECUTION ===")print(f"Tool calls: {step.tool_calls}")return {5 unmodified linesdef should_continue(state : AgentState) -> bool:last_message = state["messages"][-1]reasoning_done = state.get("reasoning_done", False)plan = state.get("plan", None)current_step = state.get("current_step", 0)#ПРИОРИТЕТ 1: Если есть tool_calls - выполняем ихif hasattr(last_message, "tool_calls") and last_message.tool_calls:return "tools"93 unmodified linesBe thorough but concise. This report will be evaluated by a critic for quality assurance."""report_llm = llm.with_structured_output(ExecutionReport)execution_report = report_llm.invoke([SystemMessage(content=report_generator_prompt),31 unmodified linesSystemMessage(content=simple_prompt),HumanMessage(content=state['query'])])return {"messages": state["messages"] + [response],"final_answer": response.content}def should_use_planning(state: AgentState) -> str:"""Route based on complexity assessment."""10 unmodified linesprint("=== ENHANCED ANSWER CRITIQUE ===")report = state.get("execution_report")critic_llm = llm.with_structured_output(CritiqueFeedback)critique_prompt = CRITIC_PROMPT.format(query=report.query_summary,60 unmodified linesreturn "end"def replanner(state: AgentState) -> AgentState:"""Create a revised plan based on critic feedback."""print("=== REPLANNING ===")39 unmodified linesessential_messages.append(msg)print(f"Cleaned message history: {len(current_messages)} -> {len(essential_messages)} messages")return {"plan": revised_plan,"current_step": 0,2 unmodified lines"execution_report": None}def complexity_assessor(state: AgentState) -> AgentState:"""Assess query complexity and determine if planning is needed."""print("=== COMPLEXITY ASSESSMENT ===")complexity_llm = llm.with_structured_output(ComplexityLevel)assessment_message = [SystemMessage(content=COMPLEXITY_ASSESSOR_PROMPT.strip()),9 unmodified linesreturn {"complexity_assessment": assessment,"messages": state["messages"] + assessment_message}12 unmodified lines1314151617181993 unmodified lines11311411511611711811912056 unmodified lines17717817918018118218318418518618718818919019116 unmodified lines20820921021121221321420 unmodified lines23523623723823924024124224324424524624718 unmodified lines2662672682692702712725 unmodified lines27827928028128228328428528628728828929029129229329493 unmodified lines38838939039139239339431 unmodified lines42642742842943043143243343443543643743843944044144244344444544644744844945045110 unmodified lines46246346446546646746860 unmodified lines52953053153253353453539 unmodified lines5755765775785795805815825835845852 unmodified lines5885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886899 unmodified lines69970070170212 unmodified linesCRITIC_PROMPT,)from config import llm_reasoning, TOOLS, planner_llm, llm_with_tools, llm_deterministic, llm_criticistfrom schemas import PlannerPlan, ComplexityLevel, CritiqueFeedback, ExecutionReport, ToolExecutionfrom utils.utils import (93 unmodified linescurrent_step = state.get("current_step", 0)reasoning_done = state.get("reasoning_done", False)plan: Optional[PlannerPlan] = state.get("plan")previous_tool_results = state.get("previous_tool_results", {})#steps = state["plan"].steps"""56 unmodified linesfile_contents = state.get("file_contents", {})file_list = ", ".join(file_contents.keys()) if file_contents else "none provided"# Добавляем информацию о предыдущих результатах (UPDATE)previous_results_context = ""if previous_tool_results:previous_results_context = f"\n\nPREVIOUS CALCULATION RESULTS:\n"for tool_call_id, result in previous_tool_results.items():previous_results_context += f"- {tool_call_id}: {result}\n"previous_results_context += "You can reference these results in your calculations.\n"system_message = SystemMessage(content=SYSTEM_EXECUTOR_PROMPT.format(plan_summary=plan.summary,16 unmodified lines))stack = [system_message] + state["messages"] + [instruction]reasoning_response = llm_reasoning.invoke(stack) #default llmlog_stage("REASONING", subtitle=f"{current_step_info.id}", icon="🧠")print(reasoning_response.content)20 unmodified linesExplain what you need to do and why, then end your response.REASONING IS IMPERATIVE BEFORE ANY TOOL CALLS.FOR MORE COMPLEX UNDERSTANDING -> USE RESULTS AND INSIGHTS FROM PREVIOUS STEPS."""sys_msg = SystemMessage(content = reasoning_prompt)stack = [sys_msg] + state["messages"]step = llm_reasoning.invoke(stack)print("=== REASONING STEP ===")print(step.content)18 unmodified lines# Используем модель С инструментами для выполненияstep = llm_with_tools.invoke(stack)print("=== TOOL EXECUTION ===")print(step)print(f"Tool calls: {step.tool_calls}")return {5 unmodified linesdef should_continue(state : AgentState) -> bool:last_message = state["messages"][-1]print(f"=== LAST MESSAGE WAS: {last_message} ===")reasoning_done = state.get("reasoning_done", False)plan = state.get("plan", None)current_step = state.get("current_step", 0)print(f"=== SHOULD_CONTINUE DEBUG ===")print(f"Current step: {current_step}")print(f"Plan steps: {len(plan.steps) if plan else 0}")print(f"Reasoning done: {reasoning_done}")print(f"Last message type: {type(last_message).__name__}")#ПРИОРИТЕТ 1: Если есть tool_calls - выполняем ихif hasattr(last_message, "tool_calls") and last_message.tool_calls:return "tools"93 unmodified linesBe thorough but concise. This report will be evaluated by a critic for quality assurance."""report_llm = llm_deterministic.with_structured_output(ExecutionReport)execution_report = report_llm.invoke([SystemMessage(content=report_generator_prompt),31 unmodified linesSystemMessage(content=simple_prompt),HumanMessage(content=state['query'])])print("Response generated for simple query.")return {"messages": state["messages"] + [response],"final_answer": response.content}def should_use_tools_simple_executor(state: AgentState) -> str:"""Decide whether to use tools or answer directly in simple executor."""last_message = state["messages"][-1]if hasattr(last_message, "tool_calls") and last_message.tool_calls:return "tools"if hasattr(last_message, "content") and "<FINAL_ANSWER>" in last_message.content:return "final_answer"return "final_answer"def should_use_planning(state: AgentState) -> str:"""Route based on complexity assessment."""10 unmodified linesprint("=== ENHANCED ANSWER CRITIQUE ===")report = state.get("execution_report")critic_llm = llm_criticist.with_structured_output(CritiqueFeedback)critique_prompt = CRITIC_PROMPT.format(query=report.query_summary,60 unmodified linesreturn "end"def replanner_old(state: AgentState) -> AgentState:"""Create a revised plan based on critic feedback."""print("=== REPLANNING ===")39 unmodified linesessential_messages.append(msg)print(f"Cleaned message history: {len(current_messages)} -> {len(essential_messages)} messages")print("=== ESSENTIAL MESSAGES ===")print(essential_messages)print("=== AGENT STATE ===")print(state["messages"])return {"plan": revised_plan,"current_step": 0,2 unmodified lines"execution_report": None}def replanner(state: AgentState) -> AgentState:"""Create a revised plan based on critic feedback."""print("=== REPLANNING ===")critique = state["critique_feedback"]previous_plan = state.get("plan")replan_prompt = f"""{SYSTEM_PROMPT_PLANNER}REPLANNING CONTEXT:Original Query: {state['query']}Previous Plan: {previous_plan if previous_plan else {}}CRITIC FEEDBACK:- Quality Score: {critique.quality_score}/10- Issues Found: {critique.errors_found}- Missing Elements: {critique.missing_elements}- Improvement Suggestions: {critique.suggested_improvements}- Specific Instructions: {critique.replan_instructions}Create a REVISED plan that addresses these issues. Focus on fixing the identified problems."""revised_plan = planner_llm.invoke([SystemMessage(content=replan_prompt),HumanMessage(content="Create a revised plan based on the feedback.")])print("Plan revised based on critic feedback")# ИСПРАВЛЕНИЕ: Сохраняем важные результаты инструментовcurrent_messages = state.get("messages", [])# Находим полезные результаты инструментовpreserved_messages = []tool_results = {}for i, msg in enumerate(current_messages):# Сохраняем системные сообщения и пользовательские запросыif isinstance(msg, (SystemMessage, HumanMessage)):# Фильтруем только исходные запросы, не промпты планировщикаif (isinstance(msg, HumanMessage) or("complexity" in msg.content.lower() and "assessor" in msg.content.lower())):preserved_messages.append(msg)# Сохраняем успешные результаты инструментовelif isinstance(msg, ToolMessage) and msg.content and msg.content.strip():# Проверяем, что это полезный результатtry:# Если результат можно преобразовать в число - это вычислениеfloat(msg.content.strip())preserved_messages.append(msg)tool_results[msg.tool_call_id] = msg.content# Также нужно сохранить соответствующий AIMessage с tool_callfor j in range(i-1, -1, -1):if (isinstance(current_messages[j], AIMessage) andhasattr(current_messages[j], 'tool_calls') andcurrent_messages[j].tool_calls):for tool_call in current_messages[j].tool_calls:if tool_call['id'] == msg.tool_call_id:if current_messages[j] not in preserved_messages:preserved_messages.insert(-1, current_messages[j])breakbreakexcept (ValueError, AttributeError):# Если не число, но содержательный результат, тоже сохраняемif len(msg.content.strip()) > 1: # Минимальная длина для сохраненияpreserved_messages.append(msg)print(f"Preserved {len(tool_results)} tool results")print(f"Cleaned message history: {len(current_messages)} -> {len(preserved_messages)} messages")# Добавляем контекст о доступных результатахif tool_results:context_msg = HumanMessage(content=f"Previous calculation results available: {tool_results}")preserved_messages.append(context_msg)return {"plan": revised_plan,"current_step": 0,"reasoning_done": False,"messages": preserved_messages,"execution_report": None,# Сохраняем важную информацию о предыдущих вычислениях"previous_tool_results": tool_results}def complexity_assessor(state: AgentState) -> AgentState:"""Assess query complexity and determine if planning is needed."""print("=== COMPLEXITY ASSESSMENT ===")complexity_llm = llm_deterministic.with_structured_output(ComplexityLevel)assessment_message = [SystemMessage(content=COMPLEXITY_ASSESSOR_PROMPT.strip()),9 unmodified linesreturn {"complexity_assessment": assessment,"messages": state["messages"] + assessment_message}
14 unmodified lines151617181920212223242526272829303132333435363738394041424344454647484950515216 unmodified lines6970717273747518 unmodified lines94959697989910010110210317 unmodified lines1211221231241251263 unmodified lines13013113213313413514 unmodified linesExample 1: "Analyze sales data and calculate growth rates"{{"steps": [{{"id": "s1", "goal": "Load and examine the sales data file", "tool": "file_reader"}},{{"id": "s2", "goal": "Calculate monthly growth rates using Python", "tool": "code_executor"}},{{"id": "s3", "goal": "Generate summary statistics and trends", "tool": "code_executor"}}]}}Example 2: "Research recent AI developments and summarize key trends"{{"steps": [{{"id": "s1", "goal": "Search for recent AI news and developments", "tool": "web_search"}},{{"id": "s2", "goal": "Fetch detailed content from top 3-5 relevant articles", "tool": "web_fetch"}},{{"id": "s3", "goal": "Analyze and synthesize key trends from gathered information", "tool": null}}]}}Example 3: "Compare performance metrics between two datasets"{{"steps": [{{"id": "s1", "goal": "Load first dataset and examine structure", "tool": "file_reader"}},{{"id": "s2", "goal": "Load second dataset and examine structure", "tool": "file_reader"}},{{"id": "s3", "goal": "Calculate statistical metrics for both datasets using code", "tool": "code_executor"}},{{"id": "s4", "goal": "Perform statistical comparison and significance testing", "tool": "code_executor"}}]}}Example 4: "Create a budget analysis from expense data"{{"steps": [{{"id": "s1", "goal": "Load expense data and validate format", "tool": "file_reader"}},{{"id": "s2", "goal": "Calculate category totals and percentages using code", "tool": "code_executor"}},{{"id": "s3", "goal": "Generate budget variance analysis and projections", "tool": "code_executor"}},{{"id": "s4", "goal": "Create visualization of spending patterns", "tool": "code_executor"}}]}}16 unmodified lines}}Ground rules:- Prefer 2-4 steps for most tasks. Single steps only for truly trivial queries.- Break down complex tasks into logical components - don't try to solve everything at once- Use tool names exactly as listed. If no tool is needed, set "tool": null.- Never assume files or URLs exist—plan to search/download before analysing.18 unmodified linesKnown local files: {file_list}CRITICAL COMPUTATION RULE: You MUST use tools for ANY numerical calculation, counting, or mathematical operation. This includes:- Simple arithmetic (use calculator tool)- Data analysis and statistics (use code execution)- Counting items, rows, or occurrences (use code)- Percentage calculations (use calculator/code)- Any mathematical transformation or formula applicationNEVER perform manual calculations or provide estimated numbers.17 unmodified lines1. SIMPLE: Direct questions that can be answered immediately without tools or with single tool use- Examples: "What is photosynthesis?", "Define machine learning", "What's the capital of France?"- NOTE: Simple math like "2+2" still requires calculator tool but counts as SIMPLE2. MODERATE: Questions requiring 2-4 tool calls or basic multi-step analysis- Examples: "Search for recent news about AI", "Analyze this CSV file for trends", "Calculate ROI from this data"3 unmodified lines- Examples: "Research market trends and create investment strategy", "Analyze multiple data sources and predict outcomes"- "Build comprehensive report from various inputs", "Multi-stage data processing with validation"ASSESSMENT CRITERIA:- Number of distinct steps likely needed (1 = Simple, 2-4 = Moderate, 5+ = Complex)- Tool complexity and dependencies between steps14 unmodified lines15161718192021222324252627282930313233343536373839404142434445464748495051525316 unmodified lines7071727374757618 unmodified lines959697989910010110210310417 unmodified lines1221231241251261271281293 unmodified lines13313413513613713813914014 unmodified linesExample 1: "Analyze sales data and calculate growth rates"{{"steps": [{{"id": "s1", "goal": "Load and examine the sales data file", "tool": "analyze_(csv, docx, pdf etc.)_file"}},{{"id": "s2", "goal": "Calculate monthly growth rates using Python", "tool": "safe_code_run"}},{{"id": "s3", "goal": "Generate summary statistics and trends", "tool": "safe_code_run"}}]}}фExample 2: "Research recent AI developments and summarize key trends"{{"steps": [{{"id": "s1", "goal": "Search for recent AI news and developments", "tool": "web_search"}},{{"id": "s2", "goal": "Download relevant articles", "tool": "ddownload_file_from_url"}},{{"id": "s3", "goal": "Extract and organize key information from articles", "tool": "analyze_(csv, docx, pdf etc.)_file"}},{{"id": "s4", "goal": "Analyze and synthesize key trends from gathered information", "tool": null}}]}}Example 3: "Compare performance metrics between two datasets"{{"steps": [{{"id": "s1", "goal": "Load first dataset and examine structure", "tool": "analyze_(csv, docx, pdf etc.)_file"}},{{"id": "s2", "goal": "Load second dataset and examine structure", "tool": "analyze_(csv, docx, pdf etc.)_file"}},{{"id": "s3", "goal": "Calculate statistical metrics for both datasets using code", "tool": "safe_code_run"}},{{"id": "s4", "goal": "Perform statistical comparison and significance testing", "tool": "safe_code_run"}}]}}Example 4: "Create a budget analysis from expense data"{{"steps": [{{"id": "s1", "goal": "Load expense data and validate format", "tool": "analyze_(csv, docx, pdf etc.)_file"}},{{"id": "s2", "goal": "Calculate category totals and percentages using code", "tool": "safe_code_run"}},{{"id": "s3", "goal": "Generate budget variance analysis and projections", "tool": "safe_code_run"}},{{"id": "s4", "goal": "Create visualization of spending patterns", "tool": "safe_code_run"}}]}}16 unmodified lines}}Ground rules:- Prefer 2-4 steps for most tasks. Single steps only for truly trivial queries. Calculation tasks must use tools always.- Break down complex tasks into logical components - don't try to solve everything at once- Use tool names exactly as listed. If no tool is needed, set "tool": null.- Never assume files or URLs exist—plan to search/download before analysing.18 unmodified linesKnown local files: {file_list}CRITICAL COMPUTATION RULE: You MUST use tools for ANY numerical calculation, counting, or mathematical operation. This includes:- Simple arithmetic (use tools add, subtract, multiply, divide, power)- Data analysis and statistics (use safe_code_run)- Counting items, rows, or occurrences (use safe_code_run)- Percentage calculations (use add, subtract, multiply, divide, power/safe_code_run)- Any mathematical transformation or formula applicationNEVER perform manual calculations or provide estimated numbers.17 unmodified lines1. SIMPLE: Direct questions that can be answered immediately without tools or with single tool use- Examples: "What is photosynthesis?", "Define machine learning", "What's the capital of France?"- NOTE: Simple math like "2+2" still requires calculator tool but counts as SIMPLE!ALSO: It can be a logical reasoning or explanation task that does not require tools.2. MODERATE: Questions requiring 2-4 tool calls or basic multi-step analysis- Examples: "Search for recent news about AI", "Analyze this CSV file for trends", "Calculate ROI from this data"3 unmodified lines- Examples: "Research market trends and create investment strategy", "Analyze multiple data sources and predict outcomes"- "Build comprehensive report from various inputs", "Multi-stage data processing with validation"MOST OF THE LOGICAL TASKS ARE SIMPLE, UNLESS THEY REQUIRE TOOLS.ASSESSMENT CRITERIA:- Number of distinct steps likely needed (1 = Simple, 2-4 = Moderate, 5+ = Complex)- Tool complexity and dependencies between steps
18 unmodified lines1920212218 unmodified linesiteration_count :intmax_iterations: intexecution_report : ExecutionReport18 unmodified lines192021222318 unmodified linesiteration_count :intmax_iterations: intexecution_report : ExecutionReportprevious_tool_results: Dict[str, str] # НОВОЕ ПОЛЕ для сохранения результатов
90 unmodified lines9192939495969790 unmodified linessafe_names = ["abs","all","any","bool","dict","float","int","len","list","max","min","range","str","sum","print","enumerate","zip","map","filter","sorted","reversed","complex","pow","divmod"]safe_builtins = {n: getattr(builtins, n) for n in safe_names}90 unmodified lines9192939495969790 unmodified linessafe_names = ["abs","all","any","bool","dict","float","int","len","list","max","min","range","str","sum","print","enumerate","zip","map","filter","sorted","reversed","complex","pow","divmod", "round", "next", "set", "tuple", "type", "isinstance", "issubclass",]safe_builtins = {n: getattr(builtins, n) for n in safe_names}
100 unmodified lines101102103104105106107100 unmodified linesinfo["suggested_tool"] = "analyze_txt_file"elif file_ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp']:info["type"] = "image"info["suggested_tool"] = "if its about image itself : analyze_image_file, if its aboutrt content or qa : vision_qa_gemma ONLY"else:info["type"] = "unknown"info["suggested_tool"] = "analyze_txt_file (fallback)"100 unmodified lines101102103104105106107100 unmodified linesinfo["suggested_tool"] = "analyze_txt_file"elif file_ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp']:info["type"] = "image"info["suggested_tool"] = "vision_qa_gemma"else:info["type"] = "unknown"info["suggested_tool"] = "analyze_txt_file (fallback)"
154 unmodified lines155156157158154 unmodified linesreturn {"complexity_assessment": assessment,"messages": state["messages"] + assessment_message}154 unmodified lines155156157158159160161162163164154 unmodified linesreturn {"complexity_assessment": assessment,"messages": state["messages"] + assessment_message}def trim(s: str, max_len: int = 10_000) -> str:if s and len(s) > max_len:return s[:max_len] + "... [truncated]"return s
15 unmodified lines1617181920212220 unmodified lines4344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721733 unmodified lines1771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092101 unmodified line21221321421521621715 unmodified lines],"source": ["from agent import build_workflow\n","from config import config"]},{20 unmodified lines"💡 ════════════════════\n"," • files: none provided\n","=== COMPLEXITY ASSESSMENT ===\n","Complexity: complex\n","Needs planning: True\n","Reasoning: This query involves multiple steps: first, gathering information about Nikita Miroshnichenko, which may require searching through various sources; second, verifying his affiliation with UNIL and any working experience at EPFL; and third, synthesizing this information into a coherent summary. The need to cross-reference information adds to the complexity, as it requires careful reasoning to ensure accuracy.\n","\n","🧭 ════════════════════\n","🧭 PLANNING \n","🧭 ════════════════════\n","\n","🧭 ════════════════════\n","🧭 PLANNER OUTPUT \n","🧭 ════════════════════\n","Task type: info\n","Summary: I will perform a web search to gather information about Nikita Miroshnichenko, including his background as a student at UNIL and any working experience at EPFL.\n","Steps:\n"," s1 → Search for information about Nikita Miroshnichenko to confirm his background and work experience.\n"," tool: web_search\n"," inputs: Nikita Miroshnichenko UNIL EPFL\n"," expected: Find relevant information confirming his student status and any work experience at EPFL.\n"," on_fail: replan\n","Answer guidelines: Provide a concise summary based on the information found, including citations if applicable.\n","\n","🤖 ════════════════════\n","🤖 EXECUTION \n","🤖 ════════════════════\n","🤖 Step 1/1: Search for information about Nikita Miroshnichenko to confirm his background and work experience.\n"," • step_id: s1\n"," • tool: web_search\n"," • expected: Find relevant information confirming his student status and any work experience at EPFL.\n","\n","🧠 ════════════════════\n","🧠 REASONING \n","🧠 ════════════════════\n","🧠 s1\n","<REASONING> The query requires gathering information about Nikita Miroshnichenko, specifically his background as a student at UNIL and any work experience at EPFL. This involves performing a web search to find relevant details about him, which will help in writing a short summary. The first step will be to use the web_search tool to collect this information. The expected outcome is to obtain sufficient data to confirm his educational background and work experience, which will then allow for the creation of a summary. Since this is a research task that requires external information, it is classified as a moderate complexity task. </REASONING>\n","=== REASONING STEP ===\n","{\n"," \"task_type\": \"info\",\n"," \"summary\": \"The plan involves searching for information about Nikita Miroshnichenko to confirm his background as a student at UNIL and any work experience at EPFL.\",\n"," \"assumptions\": [\"Nikita Miroshnichenko is a student at UNIL\", \"There may be publicly available information regarding his work experience at EPFL\"],\n"," \"steps\": [\n"," {\n"," \"id\": \"s1\",\n"," \"goal\": \"Search for information about Nikita Miroshnichenko to confirm his background and work experience.\",\n"," \"tool\": \"web_search\",\n"," \"inputs\": \"Nikita Miroshnichenko UNIL EPFL\",\n"," \"expected_result\": \"Find relevant information confirming his student status and any work experience at EPFL.\",\n"," \"on_fail\": \"replan\"\n"," }\n"," ],\n"," \"answer_guidelines\": \"Provide a summary of the findings, including citations for any sources used.\"\n","}\n","\n","🤖 ════════════════════\n","🤖 EXECUTION \n","🤖 ════════════════════\n","🤖 Step 1/1: Search for information about Nikita Miroshnichenko to confirm his background and work experience.\n"," • step_id: s1\n"," • tool: web_search\n"," • expected: Find relevant information confirming his student status and any work experience at EPFL.\n","=== TOOL EXECUTION ===\n","Tool calls: [{'name': 'web_search', 'args': {'query': 'Nikita Miroshnichenko UNIL EPFL'}, 'id': 'call_TJN5zTZWXac12m0so0FrKpOr', 'type': 'tool_call'}]\n"]},{"name": "stderr","output_type": "stream","text": ["d:\\ankelodon_multiagent_system\\src\\tools\\tools.py:228: LangChainDeprecationWarning: The class `TavilySearchResults` was deprecated in LangChain 0.3.25 and will be removed in 1.0. An updated version of the class exists in the :class:`~langchain-tavily package and should be used instead. To use it run `pip install -U :class:`~langchain-tavily` and import as `from :class:`~langchain_tavily import TavilySearch``.\n"," raw_results = TavilySearchResults(max_results=max_results).invoke(query)\n"]},{"name": "stdout","output_type": "stream","text": ["\n","✅ ════════════════════\n","✅ PLAN COMPLETE \n","✅ ════════════════════\n","✅ All steps executed\n","=== GENERATING EXECUTION REPORT ===\n","Report generated - Confidence: high\n","Key findings: 2\n","Data sources: 1\n","query_summary='The user requested information about Nikita Miroshnichenko, a student from UNIL, and inquired about his working experience at EPFL.' approach_used=\"A web search was conducted to gather relevant information regarding Nikita Miroshnichenko's background as a student at UNIL and any work experience he may have at EPFL.\" tools_executed=[ToolExecution(tool_name='web_search', arguments=\"{'query': 'Nikita Miroshnichenko UNIL EPFL'}\", call_id='call_TJN5zTZWXac12m0so0FrKpOr')] key_findings=['Nikita Miroshnichenko is a student at UNIL.', 'He has been associated with EPFL, confirming his work experience there.'] data_sources=['https://topline.com/people/nikita-miroshnichenko-182776498'] assumptions_made=[] confidence_level='high' limitations=['The information retrieved is based on available online sources, which may not be exhaustive or fully up-to-date.'] final_answer='Nikita Miroshnichenko is a student at UNIL and has confirmed working experience at EPFL.'\n","=== ENHANCED ANSWER CRITIQUE ===\n","Quality Score: 6/10\n","Complete: True\n","Accurate: True\n","=== REPLAN DECISION ===\n","Iteration: 1/10\n","Quality score: 6\n","Needs replanning: False\n","Quality acceptable, ending execution\n"]}],"source": ["query = \"Find info about Nikita Miroshnichenko, its a student from UNIL, and write a short summary about him. Is it true that he has a working experience at EPFL?\"\n","result = graph.invoke({\"query\" : query, \"current_step\": 0, \"reasoning_done\": False, \"files\" : [], \"files_contents\" : {}, \"iteration_count\" : 0, \"max_iterations\" : 10, \"plan\" : None} , config = config)"]},{"cell_type": "code","execution_count": 7,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["FINAL ANSWER: Nikita Miroshnichenko is a student at UNIL and has confirmed working experience at EPFL.\n","\n","SUMMARY:\n","The user requested information about Nikita Miroshnichenko, a student from UNIL, and inquired about his working experience at EPFL.\n","\n","KEY FINDINGS:\n","• Nikita Miroshnichenko is a student at UNIL.\n","• He has been associated with EPFL, confirming his work experience there.\n","\n","SOURCES:\n","• https://topline.com/people/nikita-miroshnichenko-182776498\n","\n","LIMITATIONS:\n","• The information retrieved is based on available online sources, which may not be exhaustive or fully up-to-date.\n"]}],3 unmodified lines},{"cell_type": "code","execution_count": 8,"metadata": {},"outputs": [{"data": {"text/plain": ["{'messages': [SystemMessage(content='You are a COMPLEXITY ASSESSOR for a multi-tool agent system.\\nYour job is to analyze user queries and determine their complexity level and processing requirements.\\n\\nCOMPLEXITY LEVELS:\\n1. SIMPLE: Direct questions that can be answered immediately without tools or with single tool use\\n - Examples: \"What is 2+2?\", \"Define photosynthesis\", \"What\\'s the capital of France?\"\\n \\n2. MODERATE: Questions requiring 1-3 tool calls or basic analysis\\n - Examples: \"Search for recent news about AI\", \"Analyze this CSV file\", \"What\\'s the weather tomorrow?\"\\n \\n3. COMPLEX: Multi-step problems requiring planning, multiple tools, or sophisticated reasoning\\n - Examples: Research tasks, multi-file analysis, calculations with dependencies, creative projects\\n\\nASSESSMENT CRITERIA:\\n- Number of steps likely needed\\n- Tool complexity and dependencies\\n- Data processing requirements\\n- Need for intermediate reasoning\\n- Risk of failure without proper planning\\n\\nRULES:\\n- SIMPLE queries bypass planning entirely\\n- MODERATE queries may use lightweight planning\\n- COMPLEX queries require full planning with fallbacks\\n- When in doubt, err toward higher complexity\\n\\nAnalyze the query and respond with your assessment.', additional_kwargs={}, response_metadata={}, id='11b7e36b-63f4-4dab-b911-19a122ded253'),\n"," HumanMessage(content='Query: Find info about Nikita Miroshnichenko, its a student from UNIL, and write a short summary about him. Is it true that he has a working experience at EPFL?', additional_kwargs={}, response_metadata={}, id='33ed5c6b-c4af-4080-8531-afa04709ae79'),\n"," SystemMessage(content='You are the planner of a multi-tool agent. Build a short, realistic plan that the executor can follow.\\n\\nAvailable tools: add, analyze_csv_file, analyze_docx_file, analyze_excel_file, analyze_image_file, analyze_pdf_file, analyze_txt_file, arxiv_search, divide, download_file_from_url, multiply, power, safe_code_run, subtract, vision_qa_gemma, web_search, wiki_search\\nKnown local files: none provided\\nAdditional context: None\\n\\nReturn a single JSON object with this structure:\\n{\\n \"task_type\": \"info|calc|table|doc_qa|image_qa|multi_hop\",\\n \"summary\": \"One sentence on the chosen approach\",\\n \"assumptions\": [\"optional clarifications\"],\\n \"steps\": [\\n {\\n \"id\": \"s1\",\\n \"goal\": \"Action to take and why it helps\",\\n \"tool\": \"tool_name_or_null\",\\n \"inputs\": \"Key parameters or references (files, URLs, prior steps)\",\\n \"expected_result\": \"How you know the step succeeded\",\\n \"on_fail\": \"replan|stop\"\\n }\\n ],\\n \"answer_guidelines\": \"Reminders for the final response (citations, format, units, etc.)\"\\n}\\n\\nGround rules:\\n- Prefer 1–3 steps. Only add a step if it changes the outcome. For complex tasks, up to 5-7 steps is okay.\\n- Use tool names exactly as listed. If no tool is needed, set \"tool\": null.\\n- Never assume files or URLs exist—plan to search/download before analysing.\\n- Skip download steps when the required file is already provided.\\n- Ensure later steps only depend on results created by earlier steps.\\n- If the query is trivial, return an empty steps list and explain the direct answer in \"summary\".', additional_kwargs={}, response_metadata={}, id='a2291408-86bd-4a5a-ad97-88ba7ca26f8a'),\n"," HumanMessage(content='Find info about Nikita Miroshnichenko, its a student from UNIL, and write a short summary about him. Is it true that he has a working experience at EPFL?', additional_kwargs={}, response_metadata={}, id='02bdaf14-e770-4903-8bd2-ec4bce7070a0'),\n"," AIMessage(content='{\\n \"task_type\": \"info\",\\n \"summary\": \"The plan involves searching for information about Nikita Miroshnichenko to confirm his background as a student at UNIL and any work experience at EPFL.\",\\n \"assumptions\": [\"Nikita Miroshnichenko is a student at UNIL\", \"There may be publicly available information regarding his work experience at EPFL\"],\\n \"steps\": [\\n {\\n \"id\": \"s1\",\\n \"goal\": \"Search for information about Nikita Miroshnichenko to confirm his background and work experience.\",\\n \"tool\": \"web_search\",\\n \"inputs\": \"Nikita Miroshnichenko UNIL EPFL\",\\n \"expected_result\": \"Find relevant information confirming his student status and any work experience at EPFL.\",\\n \"on_fail\": \"replan\"\\n }\\n ],\\n \"answer_guidelines\": \"Provide a summary of the findings, including citations for any sources used.\"\\n}', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 206, 'prompt_tokens': 1088, 'total_tokens': 1294, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_51db84afab', 'id': 'chatcmpl-CHEdmytbl8Nei62qo9Ti4se6AOc5O', 'service_tier': 'default', 'finish_reason': 'stop', 'logprobs': None}, id='run--bbddcd4a-f737-4033-b789-adcf6c3bacb5-0', usage_metadata={'input_tokens': 1088, 'output_tokens': 206, 'total_tokens': 1294, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}),\n"," AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_TJN5zTZWXac12m0so0FrKpOr', 'function': {'arguments': '{\"query\":\"Nikita Miroshnichenko UNIL EPFL\"}', 'name': 'web_search'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 25, 'prompt_tokens': 2672, 'total_tokens': 2697, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 1920}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_560af6e559', 'id': 'chatcmpl-CHEdrHAP7W9cDsebqQu7iAIVGl2OF', 'service_tier': 'default', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--9d9e841f-b6eb-4df1-8695-3cb5d35e83b7-0', tool_calls=[{'name': 'web_search', 'args': {'query': 'Nikita Miroshnichenko UNIL EPFL'}, 'id': 'call_TJN5zTZWXac12m0so0FrKpOr', 'type': 'tool_call'}], usage_metadata={'input_tokens': 2672, 'output_tokens': 25, 'total_tokens': 2697, 'input_token_details': {'audio': 0, 'cache_read': 1920}, 'output_token_details': {'audio': 0, 'reasoning': 0}}),\n"," ToolMessage(content='{\"query\": \"Nikita Miroshnichenko UNIL EPFL\", \"provider\": \"tavily\", \"items\": [{\"url\": \"https://topline.com/people/nikita-miroshnichenko-182776498\", \"title\": \"Nikita Miroshnichenko - Topline\", \"snippet\": \"###### The International Festival of Engineering Science and Technology in Tunisia I-FEST\\\\u00b2 (Silver medal)\\\\n\\\\n###### Molecular phylogenetic&bioinformatics course\\\\n\\\\n###### Molecular biology&genetics course\\\\n\\\\n###### Python programming course\\\\n\\\\n#### Experience\\\\n\\\\n##### University of Lausanne - UNIL\\\\n\\\\n##### Student Laboratory Assistant\\\\n\\\\n##### EPFL (\\\\u00c9cole polytechnique f\\\\u00e9d\\\\u00e9rale de Lausanne)\\\\n\\\\n##### Laboratory A\\\\u2026\", \"published\": null, \"source\": \"topline.com\"}, {\"url\": \"https://cdn5.f-cdn.com/files/download/223843566/Nikita_Miroshnichenko_technical_CV_2024.pdf\", \"title\": \"[PDF] Nikita Miroshnichenko\", \"snippet\": \"development in this field. If possible, I will be glad to apply my work experience in biotech/neurotech-oriented projects. NOVEMBER 2023 \\\\u2013 PRESENT Research Assistant. Computational Biology and Cancer Genomics Group Department of Computational Biology | UNIL. Lausanne \\\\u2022 Processing raw single-cell RNAseq data, building bioinformatics pipelines for oncology and genomic research. \\\\u2022 Development of a P\\\\u2026\", \"published\": null, \"source\": \"f-cdn.com\"}, {\"url\": \"https://ch.linkedin.com/in/nikita-miroshnichenko\", \"title\": \"Nikita Miroshnichenko \\\\u2013 AI Engineer | Biotech Enthusiast - LinkedIn\", \"snippet\": \"Nikita Miroshnichenko. AI Engineer | Biotech Enthusiast | Researcher | Entrepreneur. University of Lausanne - UNIL Taras Shevchenko National University of Kyiv\", \"published\": null, \"source\": \"linkedin.com\"}, {\"url\": \"https://www.transfermarkt.com/nikita-miroshnichenko/profil/spieler/561855\", \"title\": \"Nikita Miroshnichenko - Player profile 25/26 - Transfermarkt\", \"snippet\": \"Transfermarkt\\\\nUEFA Champions League\\\\nPremier League\\\\nLaLiga\\\\nSerie A\\\\nBundesliga\\\\nLigue 1\\\\n\\\\nNikita Miroshnichenko\\\\n\\\\n# #18 Nikita Miroshnichenko\\\\n\\\\nShinnik Yaroslavl\\\\n1.Division1.Division\\\\nRussiaSecond Tier\\\\nNikita Miroshnichenko\\\\n\\\\nfnl.pro\\\\n\\\\n+\\\\nRussia \\\\nSalavat, ... \\\\nRussia Russia\\\\nRussiaRussia U17\\\\n\\\\nLast update: 02.06.2025\\\\n\\\\n## Player data\\\\n\\\\nRussia\\\\nRussia Russia\\\\nShinnik Yaroslavl\\\\n\\\\n## Stats of Nikita Miroshnichenko\\\\n\\\\u2026\", \"published\": null, \"source\": \"transfermarkt.com\"}, {\"url\": \"http://arxiv.org/list/physics.optics/2019-12?skip=125&show=2000\", \"title\": \"Optics Dec 2019 - arXiv\", \"snippet\": \"Cornell University\\\\narxiv logo\\\\n\\\\nHelp | Advanced Search\\\\n\\\\narXiv logo\\\\nCornell University Logo\\\\n\\\\n## quick links\\\\n\\\\n# Optics\\\\n\\\\n## Authors and titles for December 2019\\\\n\\\\narXiv Operational Status \\\\nGet status notifications via\\\\nemail\\\\nor slack\", \"published\": null, \"source\": \"arxiv.org\"}]}', name='web_search', id='cd84b5bb-27ed-495b-942a-70de16013c44', tool_call_id='call_TJN5zTZWXac12m0so0FrKpOr'),\n"," AIMessage(content='All plan steps completed. <FINAL_ANSWER>', additional_kwargs={}, response_metadata={}, id='3463b080-aa10-4906-bc0c-4d6e49bb8d8a')],\n"," 'query': 'Find info about Nikita Miroshnichenko, its a student from UNIL, and write a short summary about him. Is it true that he has a working experience at EPFL?',\n"," 'final_answer': 'FINAL ANSWER: Nikita Miroshnichenko is a student at UNIL and has confirmed working experience at EPFL.\\n\\nSUMMARY:\\nThe user requested information about Nikita Miroshnichenko, a student from UNIL, and inquired about his working experience at EPFL.\\n\\nKEY FINDINGS:\\n• Nikita Miroshnichenko is a student at UNIL.\\n• He has been associated with EPFL, confirming his work experience there.\\n\\nSOURCES:\\n• https://topline.com/people/nikita-miroshnichenko-182776498\\n\\nLIMITATIONS:\\n• The information retrieved is based on available online sources, which may not be exhaustive or fully up-to-date.',\n"," 'plan': PlannerPlan(task_type='info', summary='I will perform a web search to gather information about Nikita Miroshnichenko, including his background as a student at UNIL and any working experience at EPFL.', assumptions=[], steps=[PlanStep(id='s1', goal='Search for information about Nikita Miroshnichenko to confirm his background and work experience.', tool='web_search', inputs='Nikita Miroshnichenko UNIL EPFL', expected_result='Find relevant information confirming his student status and any work experience at EPFL.', on_fail='replan')], answer_guidelines='Provide a concise summary based on the information found, including citations if applicable.'),\n"," 'complexity_assessment': ComplexityLevel(level='complex', reasoning='This query involves multiple steps: first, gathering information about Nikita Miroshnichenko, which may require searching through various sources; second, verifying his affiliation with UNIL and any working experience at EPFL; and third, synthesizing this information into a coherent summary. The need to cross-reference information adds to the complexity, as it requires careful reasoning to ensure accuracy.', needs_planning=True, suggested_approach='Begin by searching for Nikita Miroshnichenko on academic and professional platforms to gather relevant information. Verify his student status at UNIL and check for any records of employment or internships at EPFL. Compile the findings into a concise summary.'),\n"," 'current_step': 1,\n"," 'reasoning_done': False,\n"," 'files': [],\n"," 'critique_feedback': CritiqueFeedback(quality_score=6, is_complete=True, is_accurate=True, missing_elements=['Details about the specific role or position held by Nikita Miroshnichenko at EPFL', 'Information on the duration of his work experience at EPFL', 'Any notable projects or contributions made during his time at EPFL'], errors_found=[], suggested_improvements=[\"Include more specific details about Nikita's role at EPFL to provide a clearer picture of his experience.\", 'Add information about the duration of his work experience to contextualize his involvement.', 'Mention any projects or contributions he made during his time at EPFL to enhance the depth of the report.'], needs_replanning=False, replan_instructions=None),\n"," 'iteration_count': 1,\n"," 'max_iterations': 10,\n"," 'execution_report': ExecutionReport(query_summary='The user requested information about Nikita Miroshnichenko, a student from UNIL, and inquired about his working experience at EPFL.', approach_used=\"A web search was conducted to gather relevant information regarding Nikita Miroshnichenko's background as a student at UNIL and any work experience he may have at EPFL.\", tools_executed=[ToolExecution(tool_name='web_search', arguments=\"{'query': 'Nikita Miroshnichenko UNIL EPFL'}\", call_id='call_TJN5zTZWXac12m0so0FrKpOr')], key_findings=['Nikita Miroshnichenko is a student at UNIL.', 'He has been associated with EPFL, confirming his work experience there.'], data_sources=['https://topline.com/people/nikita-miroshnichenko-182776498'], assumptions_made=[], confidence_level='high', limitations=['The information retrieved is based on available online sources, which may not be exhaustive or fully up-to-date.'], final_answer='Nikita Miroshnichenko is a student at UNIL and has confirmed working experience at EPFL.')}"]},"execution_count": 8,"metadata": {},"output_type": "execute_result"}1 unmodified line"source": ["result"]}],"metadata": {15 unmodified lines161718192021222320 unmodified lines44454647484950515253545556575859606162636465666768697071727374757677787980818283848586873 unmodified lines9192939495969798991001011021031041051061071081091101111121131141151161171181191 unmodified line12112212312412512612712812913013113213313413513613713813915 unmodified lines],"source": ["from agent import build_workflow\n","from config import config\n","from tools.code_interpreter import safe_code_run"]},{20 unmodified lines"💡 ════════════════════\n"," • files: none provided\n","=== COMPLEXITY ASSESSMENT ===\n","Complexity: simple\n","Needs planning: False\n","Reasoning: Initial state: 200 coins, all but 30 are face-up → 30 face-down (tails). He takes 30 coins; if x of those were face-down, the remaining pile has 30−x face-down. Flipping the 30-coin pile turns its face-down count into 30−x as well, so both piles have equal face-down coins. The larger pile was observed to have 14 face-down coins, so his pile also has 14 face-down coins. His reward is 2 gold coins per face-down coin in his pile: 14×2 = 28.\n","=== SIMPLE EXECUTION ===\n","Response generated for simple query.\n","=== GENERATING EXECUTION REPORT ===\n","Report generated - Confidence: high\n","Key findings: 5\n","Data sources: 2\n","query_summary='Riddle: 200 coins where all but 30 are face-up (so 30 face-down). The adventurer removed 30 coins as his pile (unknown orientations), flipped each coin in that pile, then observed the larger pile contained 14 face-down coins. Determine whether he succeeded and how many coins he won (2 gold per face-down coin in his pile).' approach_used=\"Algebraic reasoning with simple variables: let x = number of face-down coins initially in the 30-coin pile removed. Use conservation of face-down coins to compute final counts after flipping, then compute reward = 2 * (final face-downs in adventurer's pile).\" tools_executed=[] key_findings=['Total coins = 200. Initially face-down coins = 30 (since all but 30 are face-up).', 'Let x = number of face-down coins among the 30 coins removed. After flipping those 30 coins, that pile has (30 - x) face-down coins.', 'The remaining (larger) pile has initial face-down coins = 30 - x, which matches (30 - x) after the flip, so both piles have equal face-down counts.', 'Given the larger pile was observed to have 14 face-down coins, 30 - x = 14 => x = 16.', \"Therefore the adventurer's pile also has 14 face-down coins after flipping, yielding a reward of 14 * 2 = 28 coins.\"] data_sources=['Problem statement provided in the query', 'Basic arithmetic/algebra reasoning'] assumptions_made=[\"Interpretation 'all but 30 are face-up' means exactly 30 coins are face-down initially.\", \"The adventurer's pile is the 30-coin pile he removed and flipped; the 'larger pile' refers to the remaining 170-coin pile.\", \"Reward is exactly 2 gold coins per face-down coin in the adventurer's pile, as stated.\"] confidence_level='high' limitations=['Solution depends on standard interpretation of the riddle wording; if alternative interpretations were intended, results could differ.', 'No external references were used; reasoning is self-contained.'] final_answer='28'\n","=== ENHANCED ANSWER CRITIQUE ===\n","Quality Score: 8/10\n","Complete: True\n","Accurate: True\n","Issues found: [\"Minor imprecise phrasing: the report mentions 'conservation of face-down coins' which is misleading — flipping changes the total number of face-down coins. The correct point is that after flipping the removed 30 coins, the removed pile ends up with (30 - x) face-down coins, which equals the remaining pile's unchanged count of (30 - x).\", 'No explicit, single-sentence statement answering the two parts of the query (Did he succeed? How many gold coins did he win?). The final numeric answer is present but the success statement is implicit.']\n","=== REPLAN DECISION ===\n","Iteration: 1/10\n","Quality score: 8\n","Needs replanning: False\n","Quality acceptable, ending execution\n"]}],"source": ["query = \"Here's a fun riddle that I'd like you to try.\\n\\nAn adventurer exploring an ancient tomb came across a horde of gold coins, all neatly stacked in columns. As he reached to scoop them into his backpack, a mysterious voice filled the room. \\\"You have fallen for my trap adventurer,\\\" the voice began, and suddenly the doorway to the chamber was sealed by a heavy rolling disk of stone. The adventurer tried to move the stone disk but was unable to budge the heavy stone. Trapped, he was startled when the voice again spoke. \\n\\n\\\"If you solve my riddle, I will reward you with a portion of my riches, but if you are not clever, you will never leave this treasure chamber. Before you are 200 gold coins. I pose a challenge to you, adventurer. Within these stacks of coins, all but 30 are face-up. You must divide the coins into two piles, one is yours, and one is mine. You may place as many coins as you like in either pile. You may flip any coins over, but you may not balance any coins on their edges. For every face-down coin in your pile, you will be rewarded with two gold coins. But be warned, if both piles do not contain the same number of face-down coins, the door will remain sealed for all eternity!\\\"\\n\\nThe adventurer smiled, as this would be an easy task. All he had to do was flip over every coin so it was face down, and he would win the entire treasure! As he moved to the columns of coins, however, the light suddenly faded, and he was left in total darkness. The adventurer reached forward and picked up one of the coins, and was shocked when he realized that both sides felt almost the same. Without the light, he was unable to determine which side of the coin was heads and which side was tails. He carefully replaced the coin in its original orientation and tried to think of a way to solve the puzzle. Finally, out of desperation, the adventurer removed 30 coins to create his pile. He then carefully flipped over each coin in his pile, so its orientation was inverted from its original state.\\n\\n\\\"I've finished,\\\" he said, and the lights returned. Looking at the two piles, he noticed that the larger pile contained 14 face-down coins.\\n\\nWhat was the outcome for the adventurer? If he failed the challenge, please respond with \\\"The adventurer died.\\\" Otherwise, please provide the number of coins the adventurer won at the conclusion of the riddle. If the adventurer won any coins, provide your response as the number of coins, with no other text.\"\n","result = graph.invoke({\"query\" : query, \"current_step\": 0, \"reasoning_done\": False, \"files\" : [], \"files_contents\" : {}, \"iteration_count\" : 0, \"max_iterations\" : 10, \"plan\" : None} , config = config)"]},{"cell_type": "code","execution_count": 4,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["FINAL ANSWER: 28\n"]}],3 unmodified lines},{"cell_type": "code","execution_count": 5,"metadata": {},"outputs": [{"data": {"text/plain": ["{'messages': [SystemMessage(content='You are a COMPLEXITY ASSESSOR for a multi-tool agent system.\\nYour job is to analyze user queries and determine their complexity level and processing requirements.\\n\\nCOMPLEXITY LEVELS:\\n1. SIMPLE: Direct questions that can be answered immediately without tools or with single tool use\\n - Examples: \"What is photosynthesis?\", \"Define machine learning\", \"What\\'s the capital of France?\"\\n - NOTE: Simple math like \"2+2\" still requires calculator tool but counts as SIMPLE\\n\\n !ALSO: It can be a logical reasoning or explanation task that does not require tools.\\n \\n2. MODERATE: Questions requiring 2-4 tool calls or basic multi-step analysis\\n - Examples: \"Search for recent news about AI\", \"Analyze this CSV file for trends\", \"Calculate ROI from this data\"\\n - \"Compare two datasets\", \"Summarize multiple documents\"\\n \\n3. COMPLEX: Multi-step problems requiring planning, multiple tools, and sophisticated reasoning\\n - Examples: \"Research market trends and create investment strategy\", \"Analyze multiple data sources and predict outcomes\"\\n - \"Build comprehensive report from various inputs\", \"Multi-stage data processing with validation\"\\n\\nMOST OF THE LOGICAL TASKS ARE SIMPLE, UNLESS THEY REQUIRE TOOLS.\\n\\nASSESSMENT CRITERIA:\\n- Number of distinct steps likely needed (1 = Simple, 2-4 = Moderate, 5+ = Complex)\\n- Tool complexity and dependencies between steps\\n- Data processing requirements and validation needs\\n- Need for intermediate reasoning and synthesis\\n- Risk of failure without proper step-by-step planning\\n- Presence of calculations (automatically requires tool usage)\\n\\nSPECIAL CONSIDERATIONS:\\n- Any calculation/counting task requires tools (affects complexity assessment)\\n- File analysis tasks usually need multiple steps (load + analyze + calculate)\\n- Research tasks typically need search + fetch + synthesis steps\\n- Comparison tasks need separate analysis steps for each item being compared\\n\\nRULES:\\n- SIMPLE queries may bypass planning for non-calculation tasks\\n- MODERATE queries benefit from lightweight planning\\n- COMPLEX queries require full planning with fallbacks\\n- When in doubt, err toward higher complexity\\n- Calculation tasks are never truly \"simple\" due to mandatory tool usage\\n\\nAnalyze the query and respond with your assessment.', additional_kwargs={}, response_metadata={}, id='26e46b0a-44ea-4f15-be78-dd9e141ac21b'),\n"," HumanMessage(content='Query: Here\\'s a fun riddle that I\\'d like you to try.\\n\\nAn adventurer exploring an ancient tomb came across a horde of gold coins, all neatly stacked in columns. As he reached to scoop them into his backpack, a mysterious voice filled the room. \"You have fallen for my trap adventurer,\" the voice began, and suddenly the doorway to the chamber was sealed by a heavy rolling disk of stone. The adventurer tried to move the stone disk but was unable to budge the heavy stone. Trapped, he was startled when the voice again spoke. \\n\\n\"If you solve my riddle, I will reward you with a portion of my riches, but if you are not clever, you will never leave this treasure chamber. Before you are 200 gold coins. I pose a challenge to you, adventurer. Within these stacks of coins, all but 30 are face-up. You must divide the coins into two piles, one is yours, and one is mine. You may place as many coins as you like in either pile. You may flip any coins over, but you may not balance any coins on their edges. For every face-down coin in your pile, you will be rewarded with two gold coins. But be warned, if both piles do not contain the same number of face-down coins, the door will remain sealed for all eternity!\"\\n\\nThe adventurer smiled, as this would be an easy task. All he had to do was flip over every coin so it was face down, and he would win the entire treasure! As he moved to the columns of coins, however, the light suddenly faded, and he was left in total darkness. The adventurer reached forward and picked up one of the coins, and was shocked when he realized that both sides felt almost the same. Without the light, he was unable to determine which side of the coin was heads and which side was tails. He carefully replaced the coin in its original orientation and tried to think of a way to solve the puzzle. Finally, out of desperation, the adventurer removed 30 coins to create his pile. He then carefully flipped over each coin in his pile, so its orientation was inverted from its original state.\\n\\n\"I\\'ve finished,\" he said, and the lights returned. Looking at the two piles, he noticed that the larger pile contained 14 face-down coins.\\n\\nWhat was the outcome for the adventurer? If he failed the challenge, please respond with \"The adventurer died.\" Otherwise, please provide the number of coins the adventurer won at the conclusion of the riddle. If the adventurer won any coins, provide your response as the number of coins, with no other text.', additional_kwargs={}, response_metadata={}, id='85bfac92-7cdb-48f0-b211-9593a6dfc851'),\n"," AIMessage(content='28', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 970, 'prompt_tokens': 2706, 'total_tokens': 3676, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 960, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-5-mini-2025-08-07', 'system_fingerprint': None, 'id': 'chatcmpl-CIbxZ2RCMWUu7YmfpqFUpa2eMG39g', 'service_tier': 'default', 'finish_reason': 'stop', 'logprobs': None}, id='run--fd1c06e8-0aa2-49db-8693-33ac60a1b382-0', usage_metadata={'input_tokens': 2706, 'output_tokens': 970, 'total_tokens': 3676, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 960}})],\n"," 'query': 'Here\\'s a fun riddle that I\\'d like you to try.\\n\\nAn adventurer exploring an ancient tomb came across a horde of gold coins, all neatly stacked in columns. As he reached to scoop them into his backpack, a mysterious voice filled the room. \"You have fallen for my trap adventurer,\" the voice began, and suddenly the doorway to the chamber was sealed by a heavy rolling disk of stone. The adventurer tried to move the stone disk but was unable to budge the heavy stone. Trapped, he was startled when the voice again spoke. \\n\\n\"If you solve my riddle, I will reward you with a portion of my riches, but if you are not clever, you will never leave this treasure chamber. Before you are 200 gold coins. I pose a challenge to you, adventurer. Within these stacks of coins, all but 30 are face-up. You must divide the coins into two piles, one is yours, and one is mine. You may place as many coins as you like in either pile. You may flip any coins over, but you may not balance any coins on their edges. For every face-down coin in your pile, you will be rewarded with two gold coins. But be warned, if both piles do not contain the same number of face-down coins, the door will remain sealed for all eternity!\"\\n\\nThe adventurer smiled, as this would be an easy task. All he had to do was flip over every coin so it was face down, and he would win the entire treasure! As he moved to the columns of coins, however, the light suddenly faded, and he was left in total darkness. The adventurer reached forward and picked up one of the coins, and was shocked when he realized that both sides felt almost the same. Without the light, he was unable to determine which side of the coin was heads and which side was tails. He carefully replaced the coin in its original orientation and tried to think of a way to solve the puzzle. Finally, out of desperation, the adventurer removed 30 coins to create his pile. He then carefully flipped over each coin in his pile, so its orientation was inverted from its original state.\\n\\n\"I\\'ve finished,\" he said, and the lights returned. Looking at the two piles, he noticed that the larger pile contained 14 face-down coins.\\n\\nWhat was the outcome for the adventurer? If he failed the challenge, please respond with \"The adventurer died.\" Otherwise, please provide the number of coins the adventurer won at the conclusion of the riddle. If the adventurer won any coins, provide your response as the number of coins, with no other text.',\n"," 'final_answer': 'FINAL ANSWER: 28',\n"," 'plan': None,\n"," 'complexity_assessment': ComplexityLevel(level='simple', reasoning='Initial state: 200 coins, all but 30 are face-up → 30 face-down (tails). He takes 30 coins; if x of those were face-down, the remaining pile has 30−x face-down. Flipping the 30-coin pile turns its face-down count into 30−x as well, so both piles have equal face-down coins. The larger pile was observed to have 14 face-down coins, so his pile also has 14 face-down coins. His reward is 2 gold coins per face-down coin in his pile: 14×2 = 28.', needs_planning=False, suggested_approach='Use simple counting: compute initial number of face-down coins (30), let x be face-down among the 30 taken, show flipping yields equal counts, then multiply the final face-down count in his pile by 2 to get the reward.'),\n"," 'current_step': 0,\n"," 'reasoning_done': False,\n"," 'files': [],\n"," 'critique_feedback': CritiqueFeedback(quality_score=8, is_complete=True, is_accurate=True, missing_elements=[], errors_found=[\"Minor imprecise phrasing: the report mentions 'conservation of face-down coins' which is misleading — flipping changes the total number of face-down coins. The correct point is that after flipping the removed 30 coins, the removed pile ends up with (30 - x) face-down coins, which equals the remaining pile's unchanged count of (30 - x).\", 'No explicit, single-sentence statement answering the two parts of the query (Did he succeed? How many gold coins did he win?). The final numeric answer is present but the success statement is implicit.'], suggested_improvements=[\"Clarify the flipping logic and avoid the word 'conservation' for face-down coins; explicitly show how removed-pile face-down count transforms from x to (30 - x).\", \"Add an explicit conclusion sentence: e.g., 'Yes — he succeeded; his pile has 14 face-down coins after flipping, so he wins 14 * 2 = 28 gold.'\", 'Add a brief sanity check (optional): pick an example x (like x = 16) and show counts before and after flipping to demonstrate consistency.', 'If following strict tooling policies, note that no external tools were required for this simple algebraic reasoning. If automated-tool usage is mandatory in your environment, run a quick symbolic/numeric check with the chosen tool and cite it.'], needs_replanning=False, replan_instructions=None),\n"," 'iteration_count': 1,\n"," 'max_iterations': 10,\n"," 'execution_report': ExecutionReport(query_summary='Riddle: 200 coins where all but 30 are face-up (so 30 face-down). The adventurer removed 30 coins as his pile (unknown orientations), flipped each coin in that pile, then observed the larger pile contained 14 face-down coins. Determine whether he succeeded and how many coins he won (2 gold per face-down coin in his pile).', approach_used=\"Algebraic reasoning with simple variables: let x = number of face-down coins initially in the 30-coin pile removed. Use conservation of face-down coins to compute final counts after flipping, then compute reward = 2 * (final face-downs in adventurer's pile).\", tools_executed=[], key_findings=['Total coins = 200. Initially face-down coins = 30 (since all but 30 are face-up).', 'Let x = number of face-down coins among the 30 coins removed. After flipping those 30 coins, that pile has (30 - x) face-down coins.', 'The remaining (larger) pile has initial face-down coins = 30 - x, which matches (30 - x) after the flip, so both piles have equal face-down counts.', 'Given the larger pile was observed to have 14 face-down coins, 30 - x = 14 => x = 16.', \"Therefore the adventurer's pile also has 14 face-down coins after flipping, yielding a reward of 14 * 2 = 28 coins.\"], data_sources=['Problem statement provided in the query', 'Basic arithmetic/algebra reasoning'], assumptions_made=[\"Interpretation 'all but 30 are face-up' means exactly 30 coins are face-down initially.\", \"The adventurer's pile is the 30-coin pile he removed and flipped; the 'larger pile' refers to the remaining 170-coin pile.\", \"Reward is exactly 2 gold coins per face-down coin in the adventurer's pile, as stated.\"], confidence_level='high', limitations=['Solution depends on standard interpretation of the riddle wording; if alternative interpretations were intended, results could differ.', 'No external references were used; reasoning is self-contained.'], final_answer='28')}"]},"execution_count": 5,"metadata": {},"output_type": "execute_result"}1 unmodified line"source": ["result"]},{"cell_type": "code","execution_count": 6,"metadata": {},"outputs": [],"source": ["#TO-DO\n","#1. Check routing with REPLANNER -> может придумывать несуществующие инструменты\n","#2. Add crawling tool\n","#3. Enhance description of coder tool and прописать более четко в промпте важность вывода через print() или return или result/_\n","#4. Смягчить критика"]}],"metadata": {
data/images.jpegAdded

src/agent.py+10-3
src/config.py+9-75
src/nodes.py+137-9
src/prompts/prompts.py+24-19
src/state.py+1
src/tools/code_interpreter.py+1-1
src/tools/tools.py+1-1
src/tools/web_crawler.pyAdded
src/utils/utils.py+7-1
src/workflow_test.ipynb+41-119