{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# FastAPI Multi-Agent System Testing\n", "\n", "This notebook demonstrates how to test and interact with the FastAPI endpoints.\n", "\n", "## Prerequisites\n", "\n", "Make sure the FastAPI server is running:\n", "```bash\n", "python api.py\n", "# OR\n", "uvicorn api:app --host 0.0.0.0 --port 8000 --reload\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup and Imports" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "āœ… Imports successful\n", "🌐 API Base URL: http://localhost:8000\n" ] } ], "source": [ "import requests\n", "import json\n", "import asyncio\n", "import websockets\n", "from pathlib import Path\n", "import time\n", "from IPython.display import display, Markdown, HTML\n", "\n", "# API Base URL\n", "BASE_URL = \"http://localhost:8000\"\n", "\n", "print(\"āœ… Imports successful\")\n", "print(f\"🌐 API Base URL: {BASE_URL}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Health Check\n", "\n", "Test if the API is running and all agents are initialized." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ„ Health Check Response:\n", " Status: healthy\n", " Initialized: True\n", "\n", " Agent Status:\n", " āœ… crypto: True\n", " āœ… rag: True\n", " āœ… stock: True\n", " āœ… search: True\n", " āœ… finance_tracker: True\n" ] } ], "source": [ "def test_health_check():\n", " \"\"\"Test the health check endpoint.\"\"\"\n", " try:\n", " response = requests.get(f\"{BASE_URL}/health\")\n", " response.raise_for_status()\n", " data = response.json()\n", " \n", " print(\"šŸ„ Health Check Response:\")\n", " print(f\" Status: {data['status']}\")\n", " print(f\" Initialized: {data['initialized']}\")\n", " print(\"\\n Agent Status:\")\n", " for agent, status in data['agents'].items():\n", " emoji = \"āœ…\" if status else \"āŒ\"\n", " print(f\" {emoji} {agent}: {status}\")\n", " \n", " return data\n", " except Exception as e:\n", " print(f\"āŒ Error: {e}\")\n", " return None\n", "\n", "health_data = test_health_check()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Root Endpoint\n", "\n", "Get API information and available endpoints." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ“‹ API Information:\n", "{\n", " \"message\": \"Multi-Agent Assistant API\",\n", " \"version\": \"1.0.0\",\n", " \"docs\": \"/docs\",\n", " \"health\": \"/health\"\n", "}\n" ] } ], "source": [ "def test_root_endpoint():\n", " \"\"\"Test the root endpoint.\"\"\"\n", " try:\n", " response = requests.get(f\"{BASE_URL}/\")\n", " response.raise_for_status()\n", " data = response.json()\n", " \n", " print(\"šŸ“‹ API Information:\")\n", " print(json.dumps(data, indent=2))\n", " \n", " return data\n", " except Exception as e:\n", " print(f\"āŒ Error: {e}\")\n", " return None\n", "\n", "api_info = test_root_endpoint()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Streaming Chat with Server-Sent Events (SSE)\n", "\n", "Test the streaming chat endpoint that returns real-time updates." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ’¬ Query: What is the current price of Bitcoin?\n", "\n", "šŸ“” Streaming Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking for the current price of Bitcoin. The `CALL_CRYPTO` agent is designed to get cryptocurrency market data and prices. This is the most direct and appropriate tool to answer the query.\n", " Action: CRYPTO\n", " Justification: This action will directly retrieve the current price of Bitcoin, which is precisely what the user is asking for.\n", "\n", "šŸ”§ Calling Crypto Agent...\n", "\n", "šŸ“Š Crypto Agent Results:\n", " The current price of Bitcoin is $95,748.\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: I have already called the `CALL_CRYPTO` agent, which provided the current price of Bitcoin as $95,748. This directly answers the user's query. I have sufficient information to provide a final answer.\n", " Action: FINISH\n", " Justification: The `CALL_CRYPTO` agent has successfully retrieved the current price of Bitcoin, which was the user's original query. No further information is needed.\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "The current price of Bitcoin is $95,748.\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n" ] } ], "source": [ "def test_streaming_chat(message, history=None):\n", " \"\"\"Test streaming chat with SSE.\"\"\"\n", " if history is None:\n", " history = []\n", " \n", " payload = {\n", " \"message\": message,\n", " \"history\": history\n", " }\n", " \n", " print(f\"šŸ’¬ Query: {message}\")\n", " print(\"\\nšŸ“” Streaming Response:\")\n", " print(\"=\" * 80)\n", " \n", " try:\n", " response = requests.post(\n", " f\"{BASE_URL}/api/v1/chat/stream\",\n", " json=payload,\n", " headers={\"Accept\": \"text/event-stream\"},\n", " stream=True\n", " )\n", " response.raise_for_status()\n", " \n", " final_answer = \"\"\n", " \n", " for line in response.iter_lines():\n", " if line:\n", " line_str = line.decode('utf-8')\n", " if line_str.startswith('data: '):\n", " data_str = line_str[6:] # Remove 'data: ' prefix\n", " try:\n", " event = json.loads(data_str)\n", " event_type = event.get('type', 'unknown')\n", " \n", " if event_type == 'thinking':\n", " print(f\"\\nšŸ’­ Step {event.get('step', '?')}: Reasoning\")\n", " print(f\" Thought: {event.get('thought', 'N/A')}\")\n", " print(f\" Action: {event.get('action', 'N/A').upper()}\")\n", " print(f\" Justification: {event.get('justification', 'N/A')}\")\n", " \n", " elif event_type == 'action':\n", " agent = event.get('agent', 'unknown')\n", " print(f\"\\nšŸ”§ Calling {agent.title()} Agent...\")\n", " \n", " elif event_type == 'observation':\n", " agent = event.get('agent', 'unknown')\n", " summary = event.get('summary', '')\n", " print(f\"\\nšŸ“Š {agent.title()} Agent Results:\")\n", " print(f\" {summary[:200]}...\" if len(summary) > 200 else f\" {summary}\")\n", " \n", " elif event_type == 'final_start':\n", " print(\"\\n✨ Final Answer:\")\n", " print(\"-\" * 80)\n", " \n", " elif event_type == 'final_token':\n", " final_answer = event.get('accumulated', '')\n", " \n", " elif event_type == 'final_complete':\n", " print(final_answer)\n", " print(\"-\" * 80)\n", " \n", " elif event_type == 'error':\n", " print(f\"\\nāŒ Error: {event.get('error', 'Unknown error')}\")\n", " \n", " except json.JSONDecodeError:\n", " continue\n", " \n", " print(\"\\n\" + \"=\" * 80)\n", " return final_answer\n", " \n", " except Exception as e:\n", " print(f\"\\nāŒ Error: {e}\")\n", " return None\n", "\n", "# Test with a simple crypto query\n", "result = test_streaming_chat(\"What is the current price of Bitcoin?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test with Different Queries" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ’¬ Query: What are the top 5 cryptocurrencies by market cap?\n", "\n", "šŸ“” Streaming Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking for a list of the top 5 cryptocurrencies based on their market capitalization. This is a specific query about cryptocurrency market data. I have an action called `CALL_CRYPTO` which is designed to get cryptocurrency market data, prices, and trends. This is the most direct and specialized tool for the job.\n", " Action: CRYPTO\n", " Justification: The `CALL_CRYPTO` action is the most appropriate tool to get real-time market capitalization data for cryptocurrencies, which is exactly what is needed to answer the user's query.\n", "\n", "šŸ”§ Calling Crypto Agent...\n", "\n", "šŸ“Š Crypto Agent Results:\n", " The top 5 cryptocurrencies by market cap are:\n", "\n", "1. Bitcoin (BTC) - Market Cap: $2,025,171,098,463 - Price: $101,536\n", "2. Ethereum (ETH) - Market Cap: $411,695,018,306 - Price: $3,410.68\n", "3. Tether (USDT) ...\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: The CRYPTO agent has provided a list of the top 5 cryptocurrencies by market cap, which directly answers the user's query. I have all the necessary information to provide the final answer.\n", " Action: FINISH\n", " Justification: The information gathered from the CRYPTO agent is sufficient and complete to answer the user's question. No further actions are required.\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "Based on current market data, here are the top 5 cryptocurrencies ranked by market capitalization:\n", "\n", "**1. Bitcoin (BTC)**\n", "* **Market Cap:** $2,025,171,098,463\n", "* **Price:** $101,536.00\n", "\n", "**2. Ethereum (ETH)**\n", "* **Market Cap:** $411,695,018,306\n", "* **Price:** $3,410.68\n", "\n", "**3. Tether (USDT)**\n", "* **Market Cap:** $184,005,021,987\n", "* **Price:** $0.999813\n", "\n", "**4. XRP (XRP)**\n", "* **Market Cap:** $143,300,134,340* **Price:** $2.38\n", "\n", "**5. BNB (BNB)**\n", "* **Market Cap:** $131,225,068,810\n", "* **Price:** $952.81\n", "\n", "**Key Insight:**\n", "\n", "Market capitalization is a key metric used to determine the relative size and value of a cryptocurrency. It is calculated by multiplying the current market price of a single coin by the total number of coins in circulation. As shown above, Bitcoin holds a dominant position with a market cap significantly larger than the others combined.\n", "\n", "*It is important to note that the cryptocurrency market is highly volatile, and these rankings, market caps, and prices are subject to change rapidly.*\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n" ] }, { "data": { "text/plain": [ "'Based on current market data, here are the top 5 cryptocurrencies ranked by market capitalization:\\n\\n**1. Bitcoin (BTC)**\\n* **Market Cap:** $2,025,171,098,463\\n* **Price:** $101,536.00\\n\\n**2. Ethereum (ETH)**\\n* **Market Cap:** $411,695,018,306\\n* **Price:** $3,410.68\\n\\n**3. Tether (USDT)**\\n* **Market Cap:** $184,005,021,987\\n* **Price:** $0.999813\\n\\n**4. XRP (XRP)**\\n* **Market Cap:** $143,300,134,340* **Price:** $2.38\\n\\n**5. BNB (BNB)**\\n* **Market Cap:** $131,225,068,810\\n* **Price:** $952.81\\n\\n**Key Insight:**\\n\\nMarket capitalization is a key metric used to determine the relative size and value of a cryptocurrency. It is calculated by multiplying the current market price of a single coin by the total number of coins in circulation. As shown above, Bitcoin holds a dominant position with a market cap significantly larger than the others combined.\\n\\n*It is important to note that the cryptocurrency market is highly volatile, and these rankings, market caps, and prices are subject to change rapidly.*'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Test crypto query\n", "test_streaming_chat(\"What are the top 5 cryptocurrencies by market cap?\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ’¬ Query: What is the current stock price of Apple (AAPL)?\n", "\n", "šŸ“” Streaming Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking for the current stock price of Apple (AAPL). The `CALL_STOCK` action is the most appropriate tool for this query as it is designed to get stock market data. I will use this action to retrieve the price for the ticker \"AAPL\".\n", " Action: STOCK\n", " Justification: This action is specifically designed to retrieve stock market data, which is exactly what is needed to answer the user's query about Apple's stock price.\n", "\n", "šŸ”§ Calling Stock Agent...\n", "\n", "šŸ“Š Stock Agent Results:\n", " The current stock price of Apple (AAPL) is $273.47.\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: I have successfully retrieved the current stock price of Apple (AAPL) from the STOCK agent. This information directly answers the user's query. Therefore, I have sufficient information and can provide the final answer.\n", " Action: FINISH\n", " Justification: The STOCK agent has provided the exact information requested by the user. No further actions are necessary.\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "Based on the real-time data retrieved, the current stock price of Apple Inc. (AAPL) is **$273.47**.\n", "\n", "Please be aware that this price reflects the market value at the time the query was made and is subject to constant fluctuation based on market activity.\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n" ] }, { "data": { "text/plain": [ "'Based on the real-time data retrieved, the current stock price of Apple Inc. (AAPL) is **$273.47**.\\n\\nPlease be aware that this price reflects the market value at the time the query was made and is subject to constant fluctuation based on market activity.'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Test stock query\n", "test_streaming_chat(\"What is the current stock price of Apple (AAPL)?\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ’¬ Query: Search for the latest AI developments in 2025\n", "\n", "šŸ“” Streaming Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking about future AI developments in 2025. This is a general knowledge and predictive query. I have no information gathered yet. The best tool to find information about current events, news, and future trends is the web search tool. The other tools (CRYPTO, STOCK, FINANCE_TRACKER, RAG) are not relevant to this topic. Therefore, I should use CALL_SEARCH to find the requested information.\n", " Action: SEARCH\n", " Justification: This action is the most appropriate for finding information on future technological trends and developments like those anticipated for AI in 2025.\n", "\n", "šŸ”§ Calling Search Agent...\n", "\n", "šŸ“Š Search Agent Results:\n", " I couldn't find any results for \"latest AI developments in 2025.\" Since 2025 is in the future, there are no \"developments\" yet. I can search for predictions or trends for AI in 2025 if you'd like.\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: The initial search for \"latest AI developments in 2025\" failed because 2025 is in the future. The SEARCH agent correctly pointed this out and suggested a better query: searching for \"predictions or trends for AI in 2025\". To answer the user's underlying question, I need to follow this suggestion and perform a new search with the revised query. This will gather the predictive information the user is likely looking for.\n", " Action: SEARCH\n", " Justification: The first search failed because the query was flawed. I will now use the refined query \"AI predictions and trends for 2025\" as suggested by the search agent to gather relevant information about what experts expect to see in the field of AI in the near future.\n", "\n", "šŸ”§ Calling Search Agent...\n", "\n", "šŸ“Š Search Agent Results:\n", " I couldn't find any results for \"AI predictions and trends for 2025.\" This might be due to a temporary issue with the search engine or the way the query was phrased. I will try a more general search f...\n", "\n", "šŸ’­ Step 3: Reasoning\n", " Thought: I have attempted two searches so far, and both have failed to return results. The first search was too specific about \"developments\" in the future. The second, more reasonable search for \"predictions and trends for AI in 2025\" also failed, which the agent noted might be a temporary issue. The agent suggested a broader search for \"future of AI\". This is a logical next step to gather some relevant information, as it's a broader topic that is more likely to yield results which can then be analyzed for trends relevant to 2025. I still have no information to answer the user's query, so another search is necessary.\n", " Action: SEARCH\n", " Justification: My previous two search attempts failed. The search agent suggested a broader query, \"future of AI,\" which is a logical next step to gather relevant information to answer the user's question about AI in 2025. I need to execute this search to obtain the necessary information.\n", "\n", "šŸ”§ Calling Search Agent...\n", "\n", "šŸ“Š Search Agent Results:\n", " I am sorry, but I was unable to find any search results for \"latest AI developments in 2025\" or related queries like \"AI predictions and trends for 2025\" and \"future of AI.\" It seems there might be an...\n", "\n", "šŸ’­ Step 4: Reasoning\n", " Thought: I have attempted to answer the user's query by using the `CALL_SEARCH` agent three times with progressively broader queries (\"latest AI developments in 2025\", \"AI predictions and trends for 2025\", and \"future of AI\"). Each attempt has failed, and the agent has explicitly stated that there might be an issue with the search tool itself. Since the only available tool capable of answering this query is not functioning, I cannot gather the necessary information. Further attempts to use the search tool are unlikely to succeed. Therefore, I must conclude the process and inform the user about the inability to retrieve the information due to a technical issue.\n", " Action: FINISH\n", " Justification: The `CALL_SEARCH` agent, which is the only relevant tool for this query, has failed multiple times and indicated a potential technical problem. I have exhausted all reasonable attempts to gather the information and cannot proceed further. The best course of action is to finish and communicate the issue to the user.\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "I was unable to find information on the latest AI developments for 2025.\n", "\n", "My attempt to answer your query involved a multi-step search process. Initially, I searched for \"latest AI developments in 2025,\" but since 2025 is in the future, no \"developments\" have occurred yet. I then broadened my search to look for \"AI predictions and trends for 2025\" and subsequently the \"future of AI.\"\n", "\n", "Unfortunately, all of these searches failed to return any results. This indicates a potential temporary issue with the search tool or that information specifically categorized for 2025 is not widely available at this moment.\n", "\n", "I recommend trying your query again later, as the issue with the search tool may be resolved.\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n" ] }, { "data": { "text/plain": [ "'I was unable to find information on the latest AI developments for 2025.\\n\\nMy attempt to answer your query involved a multi-step search process. Initially, I searched for \"latest AI developments in 2025,\" but since 2025 is in the future, no \"developments\" have occurred yet. I then broadened my search to look for \"AI predictions and trends for 2025\" and subsequently the \"future of AI.\"\\n\\nUnfortunately, all of these searches failed to return any results. This indicates a potential temporary issue with the search tool or that information specifically categorized for 2025 is not widely available at this moment.\\n\\nI recommend trying your query again later, as the issue with the search tool may be resolved.'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Test web search query\n", "test_streaming_chat(\"Search for the latest AI developments in 2025\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Test finance tracker - adding a transaction\n", "test_streaming_chat(\"Add 10 shares of AAPL that I bought at $150\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ’¬ Query: What's Bitcoin's price and what's the latest news about cryptocurrency?\n", "\n", "šŸ“” Streaming Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking for two pieces of information: the current price of Bitcoin and the latest news about cryptocurrency. I need to address both parts of the query. The `CALL_CRYPTO` action is the most direct way to get the price of Bitcoin. I will start with that.\n", " Action: CRYPTO\n", " Justification: This action is specifically designed to retrieve cryptocurrency market data and prices, which directly answers the first part of the user's query about Bitcoin's price.\n", "\n", "šŸ”§ Calling Crypto Agent...\n", "\n", "šŸ“Š Crypto Agent Results:\n", " I can only fetch the price of Bitcoin at the moment. Bitcoin's current price is $101,568. I cannot get the latest news about cryptocurrency.\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: I have successfully obtained the price of Bitcoin from the CRYPTO agent. However, the user's query also asks for the latest news about cryptocurrency, which the CRYPTO agent was unable to provide. To fulfill the second part of the request, I need to use an agent that can find current news. The SEARCH agent is the most suitable tool for this purpose.\n", " Action: SEARCH\n", " Justification: This action will allow me to find the latest news about cryptocurrency, which is the remaining piece of information needed to fully answer the user's query.\n", "\n", "šŸ”§ Calling Search Agent...\n", "\n", "šŸ“Š Search Agent Results:\n", " Bitcoin's current price is $101,568.\n", "\n", "For the latest cryptocurrency news, you can check out these sources:\n", "\n", "* **Crypto News:** https://cryptonews.com/\n", "* **Forbes Digital Assets:** https://www.forb...\n", "\n", "šŸ’­ Step 3: Reasoning\n", " Thought: I have successfully gathered the necessary information to answer the user's query. The CRYPTO and SEARCH agents both provided Bitcoin's current price, which is consistent. The SEARCH agent also provided a list of reliable sources for the latest cryptocurrency news. I do not need to call any more agents as I have a complete answer. I can now synthesize this information and provide the final response.\n", " Action: FINISH\n", " Justification: I have the price of Bitcoin and a list of sources for the latest cryptocurrency news. This is sufficient to fully answer the user's query.\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "The current price of Bitcoin is **$101,568**.\n", "\n", "For the latest news and in-depth coverage of the cryptocurrency market, you can refer to these reputable sources:\n", "\n", "* **CoinDesk:** [https://www.coindesk.com/](https://www.coindesk.com/)\n", "* **Cointelegraph:** [https://cointelegraph.com/](https://cointelegraph.com/)\n", "* **Crypto News:** [https://cryptonews.com/](https://cryptonews.com/)\n", "* **Forbes Digital Assets:** [https://www.forbes.com/digital-assets/news/](https://www.forbes.com/digital-assets/news/)\n", "* **Reuters Cryptocurrency:** [https://www.reuters.com/markets/cryptocurrency/](https://www.reuters.com/markets/cryptocurrency/)\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n" ] }, { "data": { "text/plain": [ "'The current price of Bitcoin is **$101,568**.\\n\\nFor the latest news and in-depth coverage of the cryptocurrency market, you can refer to these reputable sources:\\n\\n* **CoinDesk:** [https://www.coindesk.com/](https://www.coindesk.com/)\\n* **Cointelegraph:** [https://cointelegraph.com/](https://cointelegraph.com/)\\n* **Crypto News:** [https://cryptonews.com/](https://cryptonews.com/)\\n* **Forbes Digital Assets:** [https://www.forbes.com/digital-assets/news/](https://www.forbes.com/digital-assets/news/)\\n* **Reuters Cryptocurrency:** [https://www.reuters.com/markets/cryptocurrency/](https://www.reuters.com/markets/cryptocurrency/)'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Test multi-agent query (requires multiple agents)\n", "test_streaming_chat(\"What's Bitcoin's price and what's the latest news about cryptocurrency?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. WebSocket Chat\n", "\n", "Test the WebSocket endpoint for real-time bidirectional communication." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ’¬ WebSocket Query: What is Ethereum's current price?\n", "\n", "šŸ“” WebSocket Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking for the current price of Ethereum. Ethereum is a cryptocurrency. The `CALL_CRYPTO` action is specifically designed to get cryptocurrency market data and prices. Therefore, this is the most direct and appropriate action to take.\n", " Action: CRYPTO\n", "\n", "šŸ”§ Calling Crypto Agent...\n", "\n", "šŸ“Š Crypto Agent Results\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: The user asked for the current price of Ethereum. The CRYPTO agent has already provided this information: \"The current price of Ethereum is $3423.75.\" I have a direct and complete answer to the user's query. Therefore, I can now finish.\n", " Action: FINISH\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "Based on the latest market data, the current price of Ethereum (ETH) is **$3,423.75**.\n", "\n", "It is important to note that cryptocurrency prices are highly volatile and can change rapidly. This price reflects a snapshot at the time the data was retrieved.\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n" ] } ], "source": [ "async def test_websocket_chat(message, history=None):\n", " \"\"\"Test WebSocket chat endpoint.\"\"\"\n", " if history is None:\n", " history = []\n", " \n", " ws_url = BASE_URL.replace('http://', 'ws://') + '/ws/v1/chat'\n", " \n", " print(f\"šŸ’¬ WebSocket Query: {message}\")\n", " print(\"\\nšŸ“” WebSocket Response:\")\n", " print(\"=\" * 80)\n", " \n", " try:\n", " async with websockets.connect(ws_url) as websocket:\n", " # Send message\n", " await websocket.send(json.dumps({\n", " \"message\": message,\n", " \"history\": history\n", " }))\n", " \n", " final_answer = \"\"\n", " \n", " # Receive streaming updates\n", " while True:\n", " try:\n", " response = await asyncio.wait_for(websocket.recv(), timeout=60.0)\n", " event = json.loads(response)\n", " event_type = event.get('type', 'unknown')\n", " \n", " if event_type == 'thinking':\n", " print(f\"\\nšŸ’­ Step {event.get('step', '?')}: Reasoning\")\n", " print(f\" Thought: {event.get('thought', 'N/A')}\")\n", " print(f\" Action: {event.get('action', 'N/A').upper()}\")\n", " \n", " elif event_type == 'action':\n", " agent = event.get('agent', 'unknown')\n", " print(f\"\\nšŸ”§ Calling {agent.title()} Agent...\")\n", " \n", " elif event_type == 'observation':\n", " agent = event.get('agent', 'unknown')\n", " summary = event.get('summary', '')\n", " print(f\"\\nšŸ“Š {agent.title()} Agent Results\")\n", " \n", " elif event_type == 'final_start':\n", " print(\"\\n✨ Final Answer:\")\n", " print(\"-\" * 80)\n", " \n", " elif event_type == 'final_token':\n", " final_answer = event.get('accumulated', '')\n", " \n", " elif event_type == 'final_complete':\n", " print(final_answer)\n", " print(\"-\" * 80)\n", " break\n", " \n", " elif event_type == 'error':\n", " print(f\"\\nāŒ Error: {event.get('error', 'Unknown error')}\")\n", " break\n", " \n", " except asyncio.TimeoutError:\n", " print(\"\\nā±ļø Timeout waiting for response\")\n", " break\n", " \n", " print(\"\\n\" + \"=\" * 80)\n", " return final_answer\n", " \n", " except Exception as e:\n", " print(f\"\\nāŒ Error: {e}\")\n", " return None\n", "\n", "# Test WebSocket\n", "ws_result = await test_websocket_chat(\"What is Ethereum's current price?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Document Upload\n", "\n", "Test uploading a document to the RAG agent." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def test_document_upload(file_path):\n", " \"\"\"Test document upload endpoint.\"\"\"\n", " try:\n", " if not Path(file_path).exists():\n", " print(f\"āŒ File not found: {file_path}\")\n", " return None\n", " \n", " print(f\"šŸ“¤ Uploading: {file_path}\")\n", " \n", " with open(file_path, 'rb') as f:\n", " files = {'file': (Path(file_path).name, f)}\n", " response = requests.post(\n", " f\"{BASE_URL}/api/v1/documents/upload\",\n", " files=files\n", " )\n", " \n", " response.raise_for_status()\n", " data = response.json()\n", " \n", " print(\"\\nāœ… Upload Response:\")\n", " print(f\" Success: {data['success']}\")\n", " print(f\" Message: {data['message']}\")\n", " \n", " if data.get('details'):\n", " print(\"\\n Details:\")\n", " for key, value in data['details'].items():\n", " print(f\" {key}: {value}\")\n", " \n", " return data\n", " \n", " except requests.exceptions.HTTPError as e:\n", " print(f\"\\nāŒ HTTP Error: {e}\")\n", " print(f\" Response: {e.response.text}\")\n", " return None\n", " except Exception as e:\n", " print(f\"\\nāŒ Error: {e}\")\n", " return None\n", "\n", "# Example: Upload a test file (update path to your actual file)\n", "test_document_upload(\"PATH_TO_YOUR_DOCUEMNT\")\n", "\n", "print(\"āš ļø To test document upload, uncomment the line above and provide a valid file path\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "āœ… Created test file: test_document.txt\n", "šŸ“¤ Uploading: test_document.txt\n", "\n", "āœ… Upload Response:\n", " Success: True\n", " Message: Document uploaded successfully\n", "\n", " Details:\n", " filename: tmphdduvt25.txt\n", " file_type: .txt\n", " chunks_added: 1\n", " total_documents: 229\n" ] } ], "source": [ "# Create a test text file and upload it\n", "test_file_path = \"test_document.txt\"\n", "\n", "with open(test_file_path, 'w') as f:\n", " f.write(\"\"\"\n", " Test Document for RAG Agent\n", " \n", " This is a sample document for testing the RAG agent's document upload functionality.\n", " \n", " Key Information:\n", " - The multi-agent system uses Google Gemini 2.5 Pro\n", " - It implements a ReAct (Reasoning + Acting) pattern\n", " - Available agents: Crypto, Stock, RAG, Search, Finance Tracker\n", " - Each agent uses specialized MCP servers for their domain\n", " \n", " The system can handle co plex queries that require multiple agents working together.\n", " \"\"\")\n", "\n", "print(f\"āœ… Created test file: {test_file_path}\")\n", "\n", "# Upload the test file\n", "upload_result = test_document_upload(test_file_path)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ’¬ Query: What applications of AI agents are described in my collection?\n", "\n", "šŸ“” Streaming Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking about applications of AI agents described in \"my collection\". This indicates that the information should be retrieved from the user's uploaded documents. The `CALL_RAG` tool is designed for this purpose.\n", " Action: RAG\n", " Justification: The query specifically asks for information from \"my collection,\" which implies searching the user's uploaded documents. `CALL_RAG` is the appropriate tool to retrieve information from these documents.\n", "\n", "šŸ”§ Calling Rag Agent...\n", "\n", "šŸ“Š Rag Agent Results:\n", " Based on the provided document excerpts, there is no information available regarding the current status of any project. The documents discuss applications of AI agents, challenges in their integration...\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: The user asked for \"What applications of AI agents are described in my collection?\". I previously called `CALL_RAG`, and the observation stated: \"The documents discuss applications of AI agents, challenges in their integration, and various machine learning models and their use cases.\" This confirms that the information about AI agent applications *is* present in the documents. However, the previous `RAG` call did not actually list these applications. I need to call `CALL_RAG` again, but this time with a more specific prompt to extract and list the applications of AI agents mentioned in the documents.\n", " Action: RAG\n", " Justification: The previous `CALL_RAG` observation confirmed that the documents contain information about \"applications of AI agents\". I need to call `CALL_RAG` again to specifically extract and list these applications, as the previous call did not provide them.\n", "\n", "šŸ”§ Calling Rag Agent...\n", "\n", "šŸ“Š Rag Agent Results:\n", " Based on the provided document excerpts, there is no information available regarding the benefits of the Mediterranean diet for brain health. The documents discuss applications of AI agents, challenge...\n", "\n", "šŸ’­ Step 3: Reasoning\n", " Thought: The user is asking for a list of AI agent applications from their collection. I have called `CALL_RAG` twice. The first call indicated that the documents discuss \"applications of AI agents\". The second call was problematic, mentioning \"Mediterranean diet\" while still stating that \"The documents discuss applications of AI agents\". This indicates that the RAG tool is acknowledging the presence of the information but failing to extract and list the specific applications. I need to call `CALL_RAG` again, but this time, I must ensure the prompt is very explicit about *listing* the applications to overcome the previous failures.\n", " Action: RAG\n", " Justification: The `CALL_RAG` tool is the only way to access information from the user's uploaded documents (\"my collection\"). Despite previous attempts, the tool has not yet provided the actual list of AI agent applications, even though it confirms the information is present. I need to try again with a more precise prompt to extract and list these applications.\n", "\n", "šŸ”§ Calling Rag Agent...\n", "\n", "šŸ“Š Rag Agent Results:\n", " Based on the provided document excerpts, the following applications of AI agents are described:\n", "\n", "For **Marketing Professionals**:\n", "* Content generation agents\n", "* Market research agents\n", "* Performan...\n", "\n", "šŸ’­ Step 4: Reasoning\n", " Thought: I have successfully retrieved the applications of AI agents described in the user's collection using `CALL_RAG`. The information gathered directly answers the user's query.\n", " Action: FINISH\n", " Justification: I have sufficient information to answer the user's query about the applications of AI agents described in their collection.\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "Based on the documents in your collection, AI agents are described with specific applications for both marketing and sales professionals:\n", "\n", "**For Marketing Professionals:**\n", "* **Content generation agents:** To assist in creating various forms of marketing content.\n", "* **Market research agents:** For conducting research to understand market trends and opportunities.\n", "* **Performance analytics agents:** To analyze the effectiveness of marketing campaigns and strategies.\n", "* **Audience insight agents:** For gathering and interpreting data about target audiences.\n", "\n", "**For Sales Professionals:**\n", "* **Prospect research agents:** To identify and gather information on potential clients.\n", "* **Outreach customization agents:** For tailoring communication to individual prospects.\n", "* **Meeting preparation agents:** To assist in preparing for sales meetings.\n", "* **Follow-up management agents:** For organizing and executing post-meeting follow-up tasks.\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n" ] } ], "source": [ "# Now query the uploaded document\n", "if upload_result and upload_result.get('success'):\n", " test_streaming_chat(\"What applications of AI agents are described in my collection?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Conversation History\n", "\n", "Test chat with conversation history." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "====================================================================================================\n", "MESSAGE 1\n", "====================================================================================================\n", "šŸ’¬ Query: What is Bitcoin?\n", "\n", "šŸ“” Streaming Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking for a definition of \"Bitcoin\". This is a general knowledge question. I do not have any information yet. The best tool for answering general knowledge questions is `CALL_SEARCH`. The other tools are more specialized: `CALL_CRYPTO` is for market data, not definitions; `CALL_STOCK` and `CALL_FINANCE_TRACKER` are for traditional finance; and `CALL_RAG` requires pre-existing documents. Therefore, I should use `CALL_SEARCH` to find out what Bitcoin is.\n", " Action: SEARCH\n", " Justification: The user's query \"What is Bitcoin?\" is a general knowledge question. The `CALL_SEARCH` action is the most appropriate tool to find a definition and explanation from the web.\n", "\n", "šŸ”§ Calling Search Agent...\n", "\n", "šŸ“Š Search Agent Results:\n", " Bitcoin is a decentralized digital currency that allows for direct transactions between users without the need for an intermediary like a bank. It was invented in 2008 by an unknown entity or group us...\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: I have successfully used the `CALL_SEARCH` agent to get a clear and comprehensive definition of Bitcoin. The information gathered explains what it is, who created it, and the technology behind it. This is sufficient to answer the user's query. I do not need to call any other agents. I can now provide the final answer.\n", " Action: FINISH\n", " Justification: I have a complete answer to the user's question \"What is Bitcoin?\" from the previous `CALL_SEARCH` action. No further information is required.\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "Based on the information gathered, here is a comprehensive answer to your query.\n", "\n", "### What is Bitcoin?\n", "\n", "Bitcoin is a decentralized digital currency that enables direct, peer-to-peer transactions between users without the need for an intermediary like a bank or financial institution. It was the first cryptocurrency to be created and remains the most well-known.\n", "\n", "#### Key Features:* **Decentralized:** Unlike traditional currencies issued by governments (like the U.S. Dollar), Bitcoin is not controlled by any single entity. This means it is resistant to censorship and manipulation by a central authority.\n", "* **Digital Nature:** Bitcoin exists only in the digital realm. It is not a physical coin or bill but a balance associated with a digital address.\n", "* **Peer-to-Peer Technology:** Transactions are sent directly from one user to another across a global network of computers.\n", "* **Blockchain Technology:** All transactions are recorded on a public, distributed ledger called the blockchain. This technology ensures transparency and security, as every transaction is permanent and visible to everyone on the network.\n", "* **Cryptography:** Advanced cryptographic techniques are used to secure the network, verify transactions, and control the creation of new bitcoins.\n", "\n", "#### Origins:\n", "\n", "Bitcoin was invented in 2008 by an unknown person or group of people using the pseudonym **Satoshi Nakamoto**. The network went live in 2009, marking the beginning of its use as a currency.\n", "\n", "***\n", "*Sources: Forbes Advisor, Investopedia, Bitcoin.org, Analytics Insight, Wikipedia, Britannica Money, Crypto.com, Cointelegraph*\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n", "\n", "====================================================================================================\n", "MESSAGE 2 (with context from previous message)\n", "====================================================================================================\n", "šŸ’¬ Query: What is its current price?\n", "\n", "šŸ“” Streaming Response:\n", "================================================================================\n", "\n", "šŸ’­ Step 1: Reasoning\n", " Thought: The user is asking for the current price of \"it\". Based on the previous turn in the conversation history, \"it\" refers to Bitcoin. To answer this question, I need to get the current market price of Bitcoin. The `CALL_CRYPTO` tool is specifically designed to retrieve cryptocurrency prices.\n", " Action: CRYPTO\n", " Justification: This action will provide the real-time price of Bitcoin, which is the specific information needed to answer the user's query.\n", "\n", "šŸ”§ Calling Crypto Agent...\n", "\n", "šŸ“Š Crypto Agent Results:\n", " The current price of Bitcoin is $98,857. Over the last 24 hours, its price has changed by -2.74%, and its 24-hour trading volume is $98,820,417,484.34. The market cap for Bitcoin is $1,970,383,895,456...\n", "\n", "šŸ’­ Step 2: Reasoning\n", " Thought: I have successfully retrieved the current price of Bitcoin using the `CALL_CRYPTO` agent. The information gathered directly answers the user's query. Therefore, I have sufficient information and should now provide the final answer.\n", " Action: FINISH\n", " Justification: The `CALL_CRYPTO` agent provided the current price of Bitcoin, which is exactly what the user asked for. No further information is needed.\n", "\n", "✨ Final Answer:\n", "--------------------------------------------------------------------------------\n", "Based on real-time market data, the current price of Bitcoin is **$98,857**.\n", "\n", "Here is some additional market information:\n", "\n", "* **24-Hour Price Change:** -2.74%\n", "* **Market Cap:** $1,970,383,895,456.65* **24-Hour Trading Volume:** $98,820,417,484.34\n", "\n", "***\n", "*Please note: Cryptocurrency prices are highly volatile and can change rapidly. This information reflects the market at the time this query was made.*\n", "--------------------------------------------------------------------------------\n", "\n", "================================================================================\n" ] } ], "source": [ "# Start a conversation\n", "history = []\n", "\n", "# First message\n", "print(\"\\n\" + \"=\"*100)\n", "print(\"MESSAGE 1\")\n", "print(\"=\"*100)\n", "result1 = test_streaming_chat(\"What is Bitcoin?\", history=history)\n", "history.append({\"role\": \"user\", \"content\": \"What is Bitcoin?\"})\n", "history.append({\"role\": \"assistant\", \"content\": result1})\n", "\n", "time.sleep(2)\n", "\n", "# Follow-up question (with context)\n", "print(\"\\n\" + \"=\"*100)\n", "print(\"MESSAGE 2 (with context from previous message)\")\n", "print(\"=\"*100)\n", "result2 = test_streaming_chat(\"What is its current price?\", history=history)\n", "history.append({\"role\": \"user\", \"content\": \"What is its current price?\"})\n", "history.append({\"role\": \"assistant\", \"content\": result2})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. Performance Testing\n", "\n", "Test response times and measure performance." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "ā±ļø Performance Metrics for: 'What is Bitcoin price?'\n", " Time to first token: 14.65s\n", " Total response time: 36.27s\n" ] }, { "data": { "text/plain": [ "{'first_token': 14.646224975585938, 'total': 36.274803161621094}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "\n", "def measure_response_time(query):\n", " \"\"\"Measure response time for a query.\"\"\"\n", " payload = {\"message\": query, \"history\": []}\n", " \n", " start_time = time.time()\n", " first_token_time = None\n", " \n", " try:\n", " response = requests.post(\n", " f\"{BASE_URL}/api/v1/chat/stream\",\n", " json=payload,\n", " headers={\"Accept\": \"text/event-stream\"},\n", " stream=True\n", " )\n", " \n", " for i, line in enumerate(response.iter_lines()):\n", " if i == 0 and first_token_time is None:\n", " first_token_time = time.time() - start_time\n", " if line and b'final_complete' in line:\n", " break\n", " \n", " total_time = time.time() - start_time\n", " \n", " print(f\"\\nā±ļø Performance Metrics for: '{query}'\")\n", " print(f\" Time to first token: {first_token_time:.2f}s\")\n", " print(f\" Total response time: {total_time:.2f}s\")\n", " \n", " return {\"first_token\": first_token_time, \"total\": total_time}\n", " \n", " except Exception as e:\n", " print(f\"āŒ Error: {e}\")\n", " return None\n", "\n", "# Test with simple query\n", "measure_response_time(\"What is Bitcoin price?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8. Error Handling Tests\n", "\n", "Test how the API handles various error conditions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Test empty message\n", "print(\"Testing empty message:\")\n", "test_streaming_chat(\"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Test invalid file upload\n", "print(\"Testing invalid file type:\")\n", "\n", "invalid_file_path = \"/tmp/test.exe\"\n", "with open(invalid_file_path, 'w') as f:\n", " f.write(\"invalid content\")\n", "\n", "test_document_upload(invalid_file_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9. API Documentation\n", "\n", "The FastAPI server provides automatic interactive documentation.\n", "\n", "**Access the documentation at:**\n", "- Swagger UI: http://localhost:8000/docs\n", "- ReDoc: http://localhost:8000/redoc\n", "\n", "You can test all endpoints directly from the browser using the Swagger UI!" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ“š FastAPI Interactive Documentation\n", "\n", "šŸ”— Swagger UI: http://localhost:8000/docs\n", "šŸ”— ReDoc: http://localhost:8000/redoc\n" ] } ], "source": [ "from IPython.display import IFrame\n", "\n", "# Display Swagger UI in notebook (if server is running)\n", "print(\"šŸ“š FastAPI Interactive Documentation\")\n", "print(f\"\\nšŸ”— Swagger UI: {BASE_URL}/docs\")\n", "print(f\"šŸ”— ReDoc: {BASE_URL}/redoc\")\n", "\n", "# Uncomment to embed in notebook\n", "# IFrame(f\"{BASE_URL}/docs\", width=1000, height=600)" ] } ], "metadata": { "kernelspec": { "display_name": "venv (3.11.5)", "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": 4 }