{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "d:\\ankelodon_multiagent_system\\.venv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "from agent import build_workflow\n", "from config import config\n", "from tools.code_interpreter import safe_code_run" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "graph = build_workflow()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "💡 ════════════════════\n", "💡 USER QUERY \n", "💡 ════════════════════\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" ] } ], "source": [ "print(result[\"final_answer\"])" ] }, { "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" } ], "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": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }