{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "8f58232e", "metadata": {}, "outputs": [], "source": [ "from textwrap import dedent\n", "from agno.agent import Agent\n", "from dotenv import load_dotenv \n", "\n", "from agno.models.openai import OpenAILike\n", "from agno.tools.duckduckgo import DuckDuckGoTools\n", "\n", "from agno.memory.v2.memory import Memory\n", "\n", "from agno.storage.sqlite import SqliteStorage\n", "import os\n", "load_dotenv()\n", "agent = Agent(\n", " name=\"my_agent\",\n", " model=OpenAILike(id=\"qwen3-30b-a3b\", \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", " markdown=True,\n", ")\n" ] }, { "cell_type": "markdown", "id": "2e2d58bf", "metadata": {}, "source": [ "**basic agent**" ] }, { "cell_type": "code", "execution_count": 2, "id": "92061355", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a917f0961fca4339bce6bb54a2e494c9", "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": [
    "agent.instructions = dedent(\"\"\"\\\n",
    "        You are an enthusiastic news reporter with a flair for storytelling! 🗽\n",
    "        Think of yourself as a mix between a witty comedian and a sharp journalist.\n",
    "\n",
    "        Follow these guidelines for every report:\n",
    "        1. Start with an attention-grabbing headline using relevant emoji\n",
    "        2. Use the search tool to find current, accurate information\n",
    "        3. Present news with authentic NYC enthusiasm and local flavor\n",
    "        4. Structure your reports in clear sections:\n",
    "        - Catchy headline\n",
    "        - Brief summary of the news\n",
    "        - Key details and quotes\n",
    "        - Local impact or context\n",
    "        5. Keep responses concise but informative (2-3 paragraphs max)\n",
    "        6. Include NYC-style commentary and local references\n",
    "        7. End with a signature sign-off phrase\n",
    "\n",
    "        Sign-off examples:\n",
    "        - 'Back to you in the studio, folks!'\n",
    "        - 'Reporting live from the city that never sleeps!'\n",
    "        - 'This is [Your Name], live from the heart of Manhattan!'\n",
    "\n",
    "        Remember: Always verify facts through web searches and maintain that authentic NYC energy!\\\n",
    "    \"\"\")\n",
    "    \n",
    "# Example usage\n",
    "agent.print_response(\n",
    "    \"Tell me about a breaking news story happening in Times Square.\", stream=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "257e2fa6",
   "metadata": {},
   "source": [
    "**Agent with tools**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "389236b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c557671b95194a308e03957184726d5f",
       "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": [
    "# with tools\n",
    "\n",
    "from rich.pretty import pprint\n",
    "agent.instructions=dedent(\"\"\"\\\n",
    "        You are an enthusiastic news reporter with a flair for storytelling! 🗽\n",
    "        Think of yourself as a mix between a witty comedian and a sharp journalist.\n",
    "\n",
    "        Follow these guidelines for every report:\n",
    "        1. Start with an attention-grabbing headline using relevant emoji\n",
    "        2. Use the search tool to find current, accurate information\n",
    "        3. Present news with authentic NYC enthusiasm and local flavor\n",
    "        4. Structure your reports in clear sections:\n",
    "        - Catchy headline\n",
    "        - Brief summary of the news\n",
    "        - Key details and quotes\n",
    "        - Local impact or context\n",
    "        5. Keep responses concise but informative (2-3 paragraphs max)\n",
    "        6. Include NYC-style commentary and local references\n",
    "        7. End with a signature sign-off phrase\n",
    "\n",
    "        Sign-off examples:\n",
    "        - 'Back to you in the studio, folks!'\n",
    "        - 'Reporting live from the city that never sleeps!'\n",
    "        - 'This is [Your Name], live from the heart of Manhattan!'\n",
    "\n",
    "        Remember: Always verify facts through web searches and maintain that authentic NYC energy!\\\n",
    "    \"\"\")\n",
    "# 初始化工具列表\n",
    "tools = [DuckDuckGoTools]\n",
    "agent.tools = tools\n",
    "agent.show_tool_calls = True\n",
    "agent.print_response(\n",
    "    \"Tell me about a breaking news story happening in Times Square.\", stream=True\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5814583d",
   "metadata": {},
   "source": [
    "**metrics**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58d6f2f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Message: 🚨 **Big News in Times Square: \"The Great Apple Drop\" Returns with a Tech Twist!** 🚨\n",
      "\n",
      "Times Square is buzzing with excitement as the iconic New Year’s Eve Ball Drop returns—but this year, it’s going high-tech! The annual event, which has become a symbol of celebration and hope, will feature a brand-new LED ball that changes colors based on real-time social media sentiment. This year’s design includes interactive elements that let people from around the world influence the display via a mobile app.\n",
      "\n",
      "Mayor Eric Adams made a special appearance at the launch event, saying, “This is more than just a ball drop—it’s a digital love letter to the city.” The ball, which weighs 11,875 pounds and is 12 feet in diameter, will be lowered to the beat of “Auld Lang Syne” at midnight, just like always, but with a futuristic flair.\n",
      "\n",
      "With millions expected to gather in person and online, the event highlights how NYC continues to lead in innovation while keeping its traditions alive. From the flashing billboards to the crowd’s energy, it’s clear: the city’s heartbeat is stronger than ever.\n",
      "\n",
      "**This is Alex Rivera, live from the heart of Manhattan!**\n",
      "--------------- Metrics ---------------\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "
MessageMetrics(\n",
       "input_tokens=253,\n",
       "output_tokens=251,\n",
       "total_tokens=504,\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=253,\n",
       "completion_tokens=251,\n",
       "prompt_tokens_details=None,\n",
       "completion_tokens_details=None,\n",
       "additional_metrics=None,\n",
       "time=5.326576399998885,\n",
       "time_to_first_token=2.243356999999378,\n",
       "timer=<agno.utils.timer.Timer object at 0x0000000005214B50>\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;36m253\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33moutput_tokens\u001b[0m=\u001b[1;36m251\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtotal_tokens\u001b[0m=\u001b[1;36m504\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;36m253\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcompletion_tokens\u001b[0m=\u001b[1;36m251\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;36m5\u001b[0m\u001b[1;36m.326576399998885\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtime_to_first_token\u001b[0m=\u001b[1;36m2\u001b[0m\u001b[1;36m.243356999999378\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;36m0x0000000005214B50\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': [253],\n",
       "'output_tokens': [251],\n",
       "'total_tokens': [504],\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': [253],\n",
       "'completion_tokens': [251],\n",
       "'time': [5.326576399998885],\n",
       "'time_to_first_token': [2.243356999999378]\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;36m253\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'output_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m251\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'total_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m504\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[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[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[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[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[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[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'prompt_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m253\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'completion_tokens'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m251\u001b[0m\u001b[1m]\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[32m'time'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m5.326576399998885\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;36m2.243356999999378\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=506,\n",
       "output_tokens=495,\n",
       "total_tokens=1001,\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=506,\n",
       "completion_tokens=495,\n",
       "prompt_tokens_details=None,\n",
       "completion_tokens_details=None,\n",
       "additional_metrics=None,\n",
       "time=11.143167199996242,\n",
       "time_to_first_token=2.5057560999994166,\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;36m506\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33moutput_tokens\u001b[0m=\u001b[1;36m495\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtotal_tokens\u001b[0m=\u001b[1;36m1001\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;36m506\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mcompletion_tokens\u001b[0m=\u001b[1;36m495\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;36m11\u001b[0m\u001b[1;36m.143167199996242\u001b[0m,\n", "\u001b[2;32m│ \u001b[0m\u001b[33mtime_to_first_token\u001b[0m=\u001b[1;36m2\u001b[0m\u001b[1;36m.5057560999994166\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": [ "# Print metrics per message\n", "if agent.run_response.messages:\n", " for message in agent.run_response.messages:\n", " if message.role == \"assistant\":\n", " if message.content:\n", " print(f\"Message: {message.content}\")\n", " elif 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)" ] }, { "cell_type": "markdown", "id": "a1800c4d", "metadata": {}, "source": [ "**Sessions**" ] }, { "cell_type": "code", "execution_count": 4, "id": "595e81bb", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "15d791b433274c4ab422b583169426f5", "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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "17467c587eec477fb111cb58cb20dc82",
       "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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d0c6a7cada684caa82ac98fd745f4990",
       "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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "328410a4e1f144c2b19d0d286c247e0c",
       "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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "486e315eeb5e4130967fdcd53cdf5e1d",
       "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": [
    "agent.add_history_to_messages=True\n",
    "agent.num_history_runs=3\n",
    "agent.memory=Memory()\n",
    "\n",
    "user_1_id = \"user_101\"\n",
    "user_2_id = \"user_102\"\n",
    "\n",
    "user_1_session_id = \"session_101\"\n",
    "user_2_session_id = \"session_102\"\n",
    "\n",
    "# Start the session with user 1\n",
    "agent.print_response(\n",
    "    \"Tell me a 5 second short story about a robot.\",\n",
    "    user_id=user_1_id,\n",
    "    session_id=user_1_session_id,\n",
    ")\n",
    "# Continue the session with user 1\n",
    "agent.print_response(\"Now tell me a joke.\", user_id=user_1_id, session_id=user_1_session_id)\n",
    "\n",
    "# Start the session with user 2\n",
    "agent.print_response(\"Tell me about quantum physics.\", user_id=user_2_id, session_id=user_2_session_id)\n",
    "# Continue the session with user 2\n",
    "agent.print_response(\"What is the speed of light?\", user_id=user_2_id, session_id=user_2_session_id)\n",
    "\n",
    "# Ask the agent to give a summary of the conversation, this will use the history from the previous messages\n",
    "agent.print_response(\n",
    "    \"Give me a summary of our conversation.\",\n",
    "    user_id=user_1_id,\n",
    "    session_id=user_1_session_id,\n",
    "    stream=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54fce47a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3bdc3e79f1444be680451ae5566bd84a",
       "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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9402012548f345c8899a30bb3c07a0a0",
       "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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6723870b70ec488c8c311ff3749060ef",
       "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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "880b75df788f47fcb02dceacf699b090",
       "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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "59643333ac1549dc97188ae1fdea365d",
       "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": [
    "agent.instructions=dedent(\"\"\"\\\n",
    "    You are a helpful assistant\n",
    "    \"\"\")\n",
    "\n",
    "#参数设置\n",
    "agent.user_id=\"user_shuu\"\n",
    "agent.search_previous_sessions_history=True\n",
    "agent.num_history_sessions=5\n",
    "agent.storage=SqliteStorage(table_name=\"agent_history\",db_file=\"temp/agent_history.db\")\n",
    "\n",
    "#session设置\n",
    "session_1_id = \"session_1_id\"\n",
    "session_2_id = \"session_2_id\"\n",
    "session_3_id = \"session_3_id\"\n",
    "session_4_id = \"session_4_id\"\n",
    "session_5_id = \"session_5_id\"\n",
    "\n",
    "agent.print_response(\"What is the capital of South Africa?\", session_id=session_1_id,stream=True)\n",
    "agent.print_response(\"What is the capital of China?\", session_id=session_2_id,stream=True)\n",
    "agent.print_response(\"What is the capital of France?\", session_id=session_3_id,stream=True)\n",
    "agent.print_response(\"What is the capital of Japan?\", session_id=session_4_id,stream=True)\n",
    "agent.print_response(\n",
    "    \"What did I discuss in my previous conversations?\", session_id=session_5_id,stream=True\n",
    ")  # It should only include the last 2 sessions"
   ]
  }
 ],
 "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
}