{ "cells": [ { "cell_type": "code", "execution_count": 14, "id": "606a0000", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "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", "\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": 9, "id": "d026bfb6", "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": 18, "id": "885e3313", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c026f81db6784970a50844f45cda0493", "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": null,
   "id": "60497530",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# agent.print_response(\"What is the stock price of Apple?\", stream=True)\n",
    "# agent.print_response(\"What is the stock price of Apple?\", stream=True)\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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f77d6ce0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "
DEBUG ****** Agent ID: 381fc96f-9733-4c24-a9d3-2ffeb63fea9d ******                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ****** Agent ID: \u001b[93m381fc96f-9733-4c24-a9d3-2ffeb63fea9d\u001b[0m ****** \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ***** Session ID: e179c625-5edd-45e5-9ec3-46054fb5f0f8 *****                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ***** Session ID: \u001b[93me179c625-5edd-45e5-9ec3-46054fb5f0f8\u001b[0m ***** \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ** Agent Run Start: bd026372-0439-4bce-8de0-30b76c4980d6 ***                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ** Agent Run Start: \u001b[93mbd026372-0439-4bce-8de0-30b76c4980d6\u001b[0m *** \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG --------------- OpenAI Response Stream Start ---------------                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m --------------- OpenAI Response Stream Start --------------- \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG --------------------- Model: qwen3-32b ---------------------                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m --------------------- Model: qwen3-32b --------------------- \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ========================== system ==========================                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ========================== system ========================== \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG <additional_information>                                                                                     \n",
       "      - The current time is 2025-07-08 16:17:58.971363.                                                            \n",
       "      </additional_information>                                                                                    \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m \u001b[1m<\u001b[0m\u001b[1;95madditional_information\u001b[0m\u001b[39m>\u001b[0m \n", " \u001b[39m- The current time is \u001b[0m\u001b[1;36m2025\u001b[0m\u001b[39m-\u001b[0m\u001b[1;36m07\u001b[0m\u001b[39m-\u001b[0m\u001b[1;36m08\u001b[0m\u001b[39m \u001b[0m\u001b[1;92m16:17:58\u001b[0m\u001b[39m.\u001b[0m\u001b[1;36m971363\u001b[0m\u001b[39m.\u001b[0m \n", " \u001b[39m<\u001b[0m\u001b[35m/\u001b[0m\u001b[95madditional_information\u001b[0m\u001b[1m>\u001b[0m \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG =========================== user ===========================                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m =========================== user =========================== \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG Tell me a 5 second short story about a lion                                                                  \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m Tell me a \u001b[1;36m5\u001b[0m second short story about a lion \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG Calling OpenAI with request parameters: {'tools': [{'type': 'function', 'function': {'name': 'get_news',     \n",
       "      'description': '', 'parameters': {'type': 'object', 'properties': {'description': {'type': 'string'}},       \n",
       "      'required': ['description']}}}], 'tool_choice': 'auto', 'extra_body': {'enable_thinking': False}}            \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m Calling OpenAI with request parameters: \u001b[1m{\u001b[0m\u001b[32m'tools'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1m{\u001b[0m\u001b[32m'type'\u001b[0m: \u001b[32m'function'\u001b[0m, \u001b[32m'function'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'name'\u001b[0m: \u001b[32m'get_news'\u001b[0m, \n", " \u001b[32m'description'\u001b[0m: \u001b[32m''\u001b[0m, \u001b[32m'parameters'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'type'\u001b[0m: \u001b[32m'object'\u001b[0m, \u001b[32m'properties'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'description'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'type'\u001b[0m: \u001b[32m'string'\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m, \n", " \u001b[32m'required'\u001b[0m: \u001b[1m[\u001b[0m\u001b[32m'description'\u001b[0m\u001b[1m]\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m\u001b[1m]\u001b[0m, \u001b[32m'tool_choice'\u001b[0m: \u001b[32m'auto'\u001b[0m, \u001b[32m'extra_body'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'enable_thinking'\u001b[0m: \u001b[3;91mFalse\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "792427e6e9de4ef5ad0ff33767031701", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ======================== assistant =========================                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ======================== assistant ========================= \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG Once upon a time, in the heart of the savannah, there lived a proud lion named Leo. Every day, he roamed the \n",
       "      golden grasslands, his mighty roar echoing through the trees. One sunny afternoon, while chasing shadows, he \n",
       "      met a curious gazelle who challenged him to a race. With a chuckle, Leo agreed, and together they sprinted   \n",
       "      across the plains. Though he was swift, the gazelle outpaced him with grace and agility. Instead of feeling  \n",
       "      defeated, Leo laughed, realizing that joy lies not just in winning, but in the journey itself. From then on, \n",
       "      they became friends, sharing adventures and stories under the vast African sky.                              \n",
       "                                                                                                                   \n",
       "      Would you like me to fetch some news about lions or wildlife?                                                \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m Once upon a time, in the heart of the savannah, there lived a proud lion named Leo. Every day, he roamed the \n", " golden grasslands, his mighty roar echoing through the trees. One sunny afternoon, while chasing shadows, he \n", " met a curious gazelle who challenged him to a race. With a chuckle, Leo agreed, and together they sprinted \n", " across the plains. Though he was swift, the gazelle outpaced him with grace and agility. Instead of feeling \n", " defeated, Leo laughed, realizing that joy lies not just in winning, but in the journey itself. From then on, \n", " they became friends, sharing adventures and stories under the vast African sky. \n", " \n", " Would you like me to fetch some news about lions or wildlife? \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ************************  METRICS  *************************                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ************************ METRICS ************************* \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG * Tokens:                      input=195, output=148, total=343                                              \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m * Tokens: \u001b[33minput\u001b[0m=\u001b[1;36m195\u001b[0m, \u001b[33moutput\u001b[0m=\u001b[1;36m148\u001b[0m, \u001b[33mtotal\u001b[0m=\u001b[1;36m343\u001b[0m \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG * Time:                        5.2285s                                                                       \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m * Time: \u001b[1;36m5.\u001b[0m2285s \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG * Tokens per second:           28.3066 tokens/s                                                              \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m * Tokens per second: \u001b[1;36m28.3066\u001b[0m tokens/s \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG * Time to first token:         1.6973s                                                                       \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m * Time to first token: \u001b[1;36m1.\u001b[0m6973s \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ************************  METRICS  *************************                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ************************ METRICS ************************* \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ---------------- OpenAI Response Stream End ----------------                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ---------------- OpenAI Response Stream End ---------------- \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG Added RunResponse to Memory                                                                                  \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m Added RunResponse to Memory \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG *** Agent Run End: bd026372-0439-4bce-8de0-30b76c4980d6 ****                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m *** Agent Run End: \u001b[93mbd026372-0439-4bce-8de0-30b76c4980d6\u001b[0m **** \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Run agent and return the response as a stream\n",
    "response_stream: Iterator[RunResponseEvent] = agent.run(\n",
    "    \"Tell me a 5 second short story about a lion\",\n",
    "    stream=True\n",
    ")\n",
    "\n",
    "# Print the response stream in markdown format\n",
    "pprint_run_response(response_stream, markdown=True)"
   ]
  }
 ],
 "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
}