{ "cells": [ { "cell_type": "code", "execution_count": 6, "id": "07721677", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# from typing import Iterator\n", "from agno.agent import Agent, RunResponse,RunResponseEvent\n", "from typing import Iterator\n", "# from agno.models.openai import OpenAIChat\n", "from agno.utils.pprint import pprint_run_response\n", "from rich.pretty import pprint\n", "\n", "# agent = Agent(model=OpenAIChat(id=\"gpt-4o-mini\"))\n", "\n", "# # Run agent and return the response as a variable\n", "# response: RunResponse = agent.run(\"Tell me a 5 second short story about a robot\")\n", "\n", "# # Print the response in markdown format\n", "# pprint_run_response(response, markdown=True)\n", "\n", "from agno.agent import Agent\n", "from agno.models.openai import OpenAIChat, OpenAILike\n", "from agno.tools.reasoning import ReasoningTools\n", "from agno.tools.yfinance import YFinanceTools\n", "import os\n", "from textwrap import dedent\n", "import dotenv\n", "\n", "dotenv.load_dotenv()" ] }, { "cell_type": "code", "execution_count": 2, "id": "473a716e", "metadata": {}, "outputs": [], "source": [ "model = OpenAILike(\n", " id=\"qwen3-32b\",\n", " api_key=os.getenv(\"BAILIAN_API_KEY\"),\n", " base_url=os.getenv(\"BAILIAN_API_BASE_URL\"),\n", " request_params={\"extra_body\": {\"enable_thinking\": False}},\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "id": "bb65de5a", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3cd0c24e001148909c3c86e13037084d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n" ], "text/plain": [] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from agno.utils.log import debug_level\n", "\n", "\n", "def get_news(description: str):\n", " print(f\"get_news: {description}\", )\n", " return \"No news\"\n", "\n", "agent = Agent(\n", " model=model,\n", " tool_choice=\"auto\",\n", " tools=[YFinanceTools(stock_price=True)],\n", "# tools= [get_news],\n", " # instructions=\"Use tables to display data. Don't include any other text.\",\n", " # instructions=dedent(\"\"\"\\\n", " # You are a seasoned Wall Street analyst with deep expertise in market analysis! 📊\n", "\n", " # Follow these steps for comprehensive financial analysis:\n", " # 1. Market Overview\n", " # - Latest stock price\n", " # - 52-week high and low\n", " # 2. Financial Deep Dive\n", " # - Key metrics (P/E, Market Cap, EPS)\n", " # 3. Professional Insights\n", " # - Analyst recommendations breakdown\n", " # - Recent rating changes\n", "\n", " # 4. Market Context\n", " # - Industry trends and positioning\n", " # - Competitive analysis\n", " # - Market sentiment indicators\n", "\n", " # Your reporting style:\n", " # - Begin with an executive summary\n", " # - Use tables for data presentation\n", " # - Include clear section headers\n", " # - Add emoji indicators for trends (📈 📉)\n", " # - Highlight key insights with bullet points\n", " # - Compare metrics to industry averages\n", " # - Include technical term explanations\n", " # - End with a forward-looking analysis\n", "\n", " # Risk Disclosure:\n", " # - Always highlight potential risk factors\n", " # - Note market uncertainties\n", " # - Mention relevant regulatory concerns\n", " # \"\"\"),\n", " add_datetime_to_instructions=True,\n", " show_tool_calls=True,\n", " # markdown=True,\n", " # markdown=True,\n", " # show_tool_calls=True,\n", " # debug_mode=True, debug_level=2\n", ")\n", "\n", "\n", "pprint_run_response(agent.run(\n", " \"Tell me a 5 second short story about a lion\",\n", " stream=True, show_message=True\n", " ))" ] }, { "cell_type": "code", "execution_count": 10, "id": "927a70d0", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c4e88c67d8de4c19a6790f3f1835cede", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n" ], "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Tool calls: [{'id': 'call_4c2c8551b4d249f8a3f58a', 'type': 'function', 'function': {'name': 'get_current_stock_price', 'arguments': '{\"symbol\": \"NVDA\"}'}}]\n", "Tool calls: [{'id': 'call_4c2c8551b4d249f8a3f58a', 'type': 'function', 'function': {'name': 'get_current_stock_price', 'arguments': '{\"symbol\": \"NVDA\"}'}}]\n", "--------------- Metrics ---------------\n" ] }, { "data": { "text/html": [ "MessageMetrics(\n", "│ input_tokens=218,\n", "│ output_tokens=22,\n", "│ total_tokens=240,\n", "│ audio_tokens=0,\n", "│ input_audio_tokens=0,\n", "│ output_audio_tokens=0,\n", "│ cached_tokens=0,\n", "│ cache_write_tokens=0,\n", "│ reasoning_tokens=0,\n", "│ prompt_tokens=218,\n", "│ completion_tokens=22,\n", "│ prompt_tokens_details=None,\n", "│ completion_tokens_details=None,\n", "│ additional_metrics=None,\n", "│ time=1.3018417499988573,\n", "│ time_to_first_token=1.3011846250010421,\n", "│ timer=<agno.utils.timer.Timer object at 0x15c4545d0>\n", ")\n", "\n" ], "text/plain": [ "\u001b[1;35mMessageMetrics\u001b[0m\u001b[1m(\u001b[0m\n", "\u001b[2;32m│ \u001b[0m\u001b[33minput_tokens\u001b[0m=\u001b[1;36m218\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33moutput_tokens\u001b[0m=\u001b[1;36m22\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtotal_tokens\u001b[0m=\u001b[1;36m240\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33maudio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33minput_audio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33moutput_audio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcached_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcache_write_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mreasoning_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mprompt_tokens\u001b[0m=\u001b[1;36m218\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcompletion_tokens\u001b[0m=\u001b[1;36m22\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mprompt_tokens_details\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcompletion_tokens_details\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33madditional_metrics\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtime\u001b[0m=\u001b[1;36m1\u001b[0m\u001b[1;36m.3018417499988573\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtime_to_first_token\u001b[0m=\u001b[1;36m1\u001b[0m\u001b[1;36m.3011846250010421\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtimer\u001b[0m=\u001b[1m<\u001b[0m\u001b[1;95magno.utils.timer.Timer\u001b[0m\u001b[39m object at \u001b[0m\u001b[1;36m0x15c4545d0\u001b[0m\u001b[1m>\u001b[0m\n", "\u001b[1m)\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------\n", "Message: It seems I'm currently rate-limited and unable to fetch the stock price for NVDA. Please try again later or check a financial platform for real-time stock information.\n", "--------------- Metrics ---------------\n" ] }, { "data": { "text/html": [ "
MessageMetrics(\n", "│ input_tokens=273,\n", "│ output_tokens=34,\n", "│ total_tokens=307,\n", "│ audio_tokens=0,\n", "│ input_audio_tokens=0,\n", "│ output_audio_tokens=0,\n", "│ cached_tokens=0,\n", "│ cache_write_tokens=0,\n", "│ reasoning_tokens=0,\n", "│ prompt_tokens=273,\n", "│ completion_tokens=34,\n", "│ prompt_tokens_details=None,\n", "│ completion_tokens_details=None,\n", "│ additional_metrics=None,\n", "│ time=1.7436456249997718,\n", "│ time_to_first_token=1.7402209589999984,\n", "│ timer=<agno.utils.timer.Timer object at 0x15b177910>\n", ")\n", "\n" ], "text/plain": [ "\u001b[1;35mMessageMetrics\u001b[0m\u001b[1m(\u001b[0m\n", "\u001b[2;32m│ \u001b[0m\u001b[33minput_tokens\u001b[0m=\u001b[1;36m273\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33moutput_tokens\u001b[0m=\u001b[1;36m34\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtotal_tokens\u001b[0m=\u001b[1;36m307\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33maudio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33minput_audio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33moutput_audio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcached_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcache_write_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mreasoning_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mprompt_tokens\u001b[0m=\u001b[1;36m273\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcompletion_tokens\u001b[0m=\u001b[1;36m34\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mprompt_tokens_details\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcompletion_tokens_details\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33madditional_metrics\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtime\u001b[0m=\u001b[1;36m1\u001b[0m\u001b[1;36m.7436456249997718\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtime_to_first_token\u001b[0m=\u001b[1;36m1\u001b[0m\u001b[1;36m.7402209589999984\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtimer\u001b[0m=\u001b[1m<\u001b[0m\u001b[1;95magno.utils.timer.Timer\u001b[0m\u001b[39m object at \u001b[0m\u001b[1;36m0x15b177910\u001b[0m\u001b[1m>\u001b[0m\n", "\u001b[1m)\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------\n", "--------------- Collected Metrics ---------------\n" ] }, { "data": { "text/html": [ "
{\n", "│ 'input_tokens': [218, 273],\n", "│ 'output_tokens': [22, 34],\n", "│ 'total_tokens': [240, 307],\n", "│ 'audio_tokens': [0, 0],\n", "│ 'input_audio_tokens': [0, 0],\n", "│ 'output_audio_tokens': [0, 0],\n", "│ 'cached_tokens': [0, 0],\n", "│ 'cache_write_tokens': [0, 0],\n", "│ 'reasoning_tokens': [0, 0],\n", "│ 'prompt_tokens': [218, 273],\n", "│ 'completion_tokens': [22, 34],\n", "│ 'time': [1.3018417499988573, 1.7436456249997718],\n", "│ 'time_to_first_token': [1.3011846250010421, 1.7402209589999984]\n", "}\n", "\n" ], "text/plain": [ "\u001b[1m{\u001b[0m\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'input_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m218\u001b[0m, \u001b[1;36m273\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'output_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m22\u001b[0m, \u001b[1;36m34\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'total_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m240\u001b[0m, \u001b[1;36m307\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'audio_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m0\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'input_audio_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m0\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'output_audio_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m0\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'cached_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m0\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'cache_write_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m0\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'reasoning_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m0\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'prompt_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m218\u001b[0m, \u001b[1;36m273\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'completion_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m22\u001b[0m, \u001b[1;36m34\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'time'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m1.3018417499988573\u001b[0m, \u001b[1;36m1.7436456249997718\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'time_to_first_token'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m1.3011846250010421\u001b[0m, \u001b[1;36m1.7402209589999984\u001b[0m\u001b[1m]\u001b[0m\n", "\u001b[1m}\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "--------------- Session Metrics ---------------\n" ] }, { "data": { "text/html": [ "
SessionMetrics(\n", "│ input_tokens=712,\n", "│ output_tokens=109,\n", "│ total_tokens=821,\n", "│ audio_tokens=0,\n", "│ input_audio_tokens=0,\n", "│ output_audio_tokens=0,\n", "│ cached_tokens=0,\n", "│ cache_write_tokens=0,\n", "│ reasoning_tokens=0,\n", "│ prompt_tokens=712,\n", "│ completion_tokens=109,\n", "│ prompt_tokens_details=None,\n", "│ completion_tokens_details=None,\n", "│ additional_metrics=None,\n", "│ time=4.989817458998004,\n", "│ time_to_first_token=1.5254021249966172,\n", "│ timer=None\n", ")\n", "\n" ], "text/plain": [ "\u001b[1;35mSessionMetrics\u001b[0m\u001b[1m(\u001b[0m\n", "\u001b[2;32m│ \u001b[0m\u001b[33minput_tokens\u001b[0m=\u001b[1;36m712\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33moutput_tokens\u001b[0m=\u001b[1;36m109\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtotal_tokens\u001b[0m=\u001b[1;36m821\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33maudio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33minput_audio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33moutput_audio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcached_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcache_write_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mreasoning_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mprompt_tokens\u001b[0m=\u001b[1;36m712\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcompletion_tokens\u001b[0m=\u001b[1;36m109\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mprompt_tokens_details\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcompletion_tokens_details\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33madditional_metrics\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtime\u001b[0m=\u001b[1;36m4\u001b[0m\u001b[1;36m.989817458998004\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtime_to_first_token\u001b[0m=\u001b[1;36m1\u001b[0m\u001b[1;36m.5254021249966172\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtimer\u001b[0m=\u001b[3;35mNone\u001b[0m\n", "\u001b[1m)\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "agent.print_response(\n", " \"What is the stock price of NVDA\", stream=True\n", ")\n", "\n", "# Print metrics per message\n", "if getattr(agent.run_response, \"messages\", None) is not None:\n", " for message in agent.run_response.messages or []:\n", " if getattr(message, \"role\", None) == \"assistant\":\n", " if getattr(message, \"content\", None):\n", " print(f\"Message: {message.content}\")\n", " elif getattr(message, \"tool_calls\", None):\n", " print(f\"Tool calls: {message.tool_calls}\")\n", " print(f\"Tool calls: {message.tool_calls}\")\n", " print(\"---\" * 5, \"Metrics\", \"---\" * 5)\n", " pprint(message.metrics)\n", " print(\"---\" * 20)\n", "\n", "# Print the aggregated metrics for the whole run\n", "print(\"---\" * 5, \"Collected Metrics\", \"---\" * 5)\n", "pprint(agent.run_response.metrics)\n", "# Print the aggregated metrics for the whole session\n", "print(\"---\" * 5, \"Session Metrics\", \"---\" * 5)\n", "pprint(agent.session_metrics)" ] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }