{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "fb666050", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "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.duckduckgo import DuckDuckGoTools\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", "import json\n", "import httpx\n", "dotenv.load_dotenv()" ] }, { "cell_type": "code", "execution_count": 2, "id": "1b9e8791", "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": 5, "id": "b97c0629", "metadata": {}, "outputs": [], "source": [ "def get_top_hackernews_stories(num_stories: int = 10) -> str:\n", " \"\"\"Use this function to get top stories from Hacker News.\n", "\n", " Args:\n", " num_stories (int): Number of stories to return. Defaults to 10.\n", "\n", " Returns:\n", " str: JSON string of top stories.\n", " \"\"\"\n", "\n", " # Fetch top story IDs\n", " response = httpx.get('https://hacker-news.firebaseio.com/v0/topstories.json')\n", " story_ids = response.json()\n", "\n", " # Fetch story details\n", " stories = []\n", " for story_id in story_ids[:num_stories]:\n", " story_response = httpx.get(f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json')\n", " story = story_response.json()\n", " if \"text\" in story:\n", " story.pop(\"text\", None)\n", " stories.append(story)\n", " return json.dumps(stories)" ] }, { "cell_type": "code", "execution_count": null, "id": "a4862c47", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "90104001", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
DEBUG ****** Agent ID: 468e8297-e5d0-49cf-bed3-6d42d27ff3b9 ******                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ****** Agent ID: \u001b[93m468e8297-e5d0-49cf-bed3-6d42d27ff3b9\u001b[0m ****** \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ***** Session ID: 263e0b0a-c944-4ac2-898d-26b55d366714 *****                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ***** Session ID: \u001b[93m263e0b0a-c944-4ac2-898d-26b55d366714\u001b[0m ***** \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG Processing tools for model                                                                                   \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m Processing tools for model \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG Added tool get_news                                                                                          \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m Added tool get_news \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG ** Agent Run Start: eb0147d4-ab3d-4cb9-bb6e-df48dabba64d ***                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m ** Agent Run Start: \u001b[93meb0147d4-ab3d-4cb9-bb6e-df48dabba64d\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-09 10:44:38.514495.                                                            \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;36m09\u001b[0m\u001b[39m \u001b[0m\u001b[1;92m10:44:38\u001b[0m\u001b[39m.\u001b[0m\u001b[1;36m514495\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": "99ed8e58fb0c4649be558f325b0417ce", "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 A lion, majestic and proud, roamed the savanna. With a mighty roar, he called his pride, and together they   \n",
       "      vanished into the golden sunset.                                                                             \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m A lion, majestic and proud, roamed the savanna. With a mighty roar, he called his pride, and together they \n", " vanished into the golden sunset. \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=32, total=227                                               \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m * Tokens: \u001b[33minput\u001b[0m=\u001b[1;36m195\u001b[0m, \u001b[33moutput\u001b[0m=\u001b[1;36m32\u001b[0m, \u001b[33mtotal\u001b[0m=\u001b[1;36m227\u001b[0m \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG * Time:                        1.5037s                                                                       \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m * Time: \u001b[1;36m1.\u001b[0m5037s \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG * Tokens per second:           21.2808 tokens/s                                                              \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m * Tokens per second: \u001b[1;36m21.2808\u001b[0m tokens/s \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DEBUG * Time to first token:         1.5024s                                                                       \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m * Time to first token: \u001b[1;36m1.\u001b[0m5024s \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: eb0147d4-ab3d-4cb9-bb6e-df48dabba64d ****                                                 \n",
       "
\n" ], "text/plain": [ "\u001b[32mDEBUG\u001b[0m *** Agent Run End: \u001b[93meb0147d4-ab3d-4cb9-bb6e-df48dabba64d\u001b[0m **** \n" ] }, "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",
    "    tools=[get_top_hackernews_stories],\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",
    "    \"Summarize the top 5 stories on hackernews?\",\n",
    "    stream=True, show_message=True\n",
    "    ))"
   ]
  }
 ],
 "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
}