diff --git a/notebook/agentchat_websockets.ipynb b/notebook/agentchat_websockets.ipynb index 7bf03f786..bdf98d76d 100644 --- a/notebook/agentchat_websockets.ipynb +++ b/notebook/agentchat_websockets.ipynb @@ -62,6 +62,7 @@ } ], "source": [ + "from datetime import datetime\n", "from tempfile import TemporaryDirectory\n", "\n", "from websockets.sync.client import connect as ws_connect\n", @@ -145,20 +146,17 @@ " @user_proxy.register_for_execution()\n", " @agent.register_for_llm(description=\"Weather forecats for a city\")\n", " def weather_forecast(city: str) -> str:\n", - " return f\"The weather forecast for {city} is sunny.\"\n", + " return f\"The weather forecast for {city} at {datetime.now()} is sunny.\"\n", "\n", " # we will use a temporary directory as the cache path root to ensure fresh completion each time\n", - " with TemporaryDirectory() as cache_path_root:\n", - " with Cache.disk(cache_path_root=cache_path_root) as cache:\n", - " print(\n", - " f\" - on_connect(): Initiating chat with agent {agent} using message '{initial_msg}'\",\n", - " flush=True,\n", - " )\n", - " user_proxy.initiate_chat( # noqa: F704\n", - " agent,\n", - " message=initial_msg,\n", - " cache=cache,\n", - " )" + " print(\n", + " f\" - on_connect(): Initiating chat with agent {agent} using message '{initial_msg}'\",\n", + " flush=True,\n", + " )\n", + " user_proxy.initiate_chat( # noqa: F704\n", + " agent,\n", + " message=initial_msg,\n", + " )" ] }, { @@ -203,7 +201,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "4fbe004d", "metadata": {}, "outputs": [ @@ -212,11 +210,11 @@ "output_type": "stream", "text": [ " - test_setup() with websocket server running on ws://127.0.0.1:8765.\n", - " - on_connect(): Connected to client using IOWebsockets \n", + " - on_connect(): Connected to client using IOWebsockets \n", " - on_connect(): Receiving message from client.\n", " - Connected to server on ws://127.0.0.1:8765\n", " - Sending message to server.\n", - " - on_connect(): Initiating chat with agent using message 'Check out the weather in Paris and write a poem about it.'\n", + " - on_connect(): Initiating chat with agent using message 'Check out the weather in Paris and write a poem about it.'\n", "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", "\n", "Check out the weather in Paris and write a poem about it.\n", @@ -224,12 +222,10 @@ "--------------------------------------------------------------------------------\n", "\u001b[31m\n", ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", - "\u001b[32m\u001b[32m\u001b[0m\n", - "\n", "\u001b[33mchatbot\u001b[0m (to user_proxy):\n", "\n", "\n", - "\u001b[32m***** Suggested tool Call (call_U5VR0hck9KhDFWPdvmo1Eoke): weather_forecast *****\u001b[0m\n", + "\u001b[32m***** Suggested tool call (call_pKFE5KQZOQTe4gC8mQRv6bZX): weather_forecast *****\u001b[0m\n", "Arguments: \n", "{\n", " \"city\": \"Paris\"\n", @@ -243,36 +239,36 @@ "\n", "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", "\n", - "\u001b[32m***** Response from calling tool \"call_U5VR0hck9KhDFWPdvmo1Eoke\" *****\u001b[0m\n", - "The weather forecast for Paris is sunny.\n", + "\u001b[32m***** Response from calling tool (call_pKFE5KQZOQTe4gC8mQRv6bZX) *****\u001b[0m\n", + "The weather forecast for Paris at 2024-03-31 20:17:22.413225 is sunny.\n", "\u001b[32m**********************************************************************\u001b[0m\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[31m\n", ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", - "\u001b[32m\u001b[32mIn the city of love, shines the sun above,\n", - "Paris basks in golden rays, a beautiful day to praise.\n", - "Strolling down the Champs Elysées, the warm light leads the way,\n", - "In the glow, silhouettes dance, a perfect setting for romance.\n", + "\u001b[32m\u001b[32mIn Paris, beneath the golden sun, so bright,\n", + "Where cobblestones glisten with pure delight,\n", + "The weather is sunny, a beautiful sight,\n", + "Oh Paris, you're dazzling in the sunlight.\n", "\n", - "In the sunlight, the Seine sparkles bright, reflecting the City of Light,\n", - "Not a cloud in the crystal-clear blue sky, as the doves sail high.\n", - "Sunny Paris so profound, beauty all around,\n", - "Alive under the radiant crown, she wears her sunlight like a gown.\n", + "The bistros beckon with fragrant delight,\n", + "In gardens, flowers bloom with all their might.\n", + "Paris, your charm shines so bright,\n", + "Under the blanket of the sunny daylight.\n", "\n", "TERMINATE\u001b[0m\n", "\n", "\u001b[33mchatbot\u001b[0m (to user_proxy):\n", "\n", - "In the city of love, shines the sun above,\n", - "Paris basks in golden rays, a beautiful day to praise.\n", - "Strolling down the Champs Elysées, the warm light leads the way,\n", - "In the glow, silhouettes dance, a perfect setting for romance.\n", + "In Paris, beneath the golden sun, so bright,\n", + "Where cobblestones glisten with pure delight,\n", + "The weather is sunny, a beautiful sight,\n", + "Oh Paris, you're dazzling in the sunlight.\n", "\n", - "In the sunlight, the Seine sparkles bright, reflecting the City of Light,\n", - "Not a cloud in the crystal-clear blue sky, as the doves sail high.\n", - "Sunny Paris so profound, beauty all around,\n", - "Alive under the radiant crown, she wears her sunlight like a gown.\n", + "The bistros beckon with fragrant delight,\n", + "In gardens, flowers bloom with all their might.\n", + "Paris, your charm shines so bright,\n", + "Under the blanket of the sunny daylight.\n", "\n", "TERMINATE\n", "\n", @@ -330,19 +326,7 @@ "execution_count": 4, "id": "5e55dc06", "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'fastapi'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[4], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mcontextlib\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m asynccontextmanager \u001b[38;5;66;03m# noqa: E402\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpathlib\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Path \u001b[38;5;66;03m# noqa: E402\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mfastapi\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m FastAPI \u001b[38;5;66;03m# noqa: E402\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mfastapi\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mresponses\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m HTMLResponse \u001b[38;5;66;03m# noqa: E402\u001b[39;00m\n\u001b[1;32m 7\u001b[0m PORT \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m8000\u001b[39m\n", - "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'fastapi'" - ] - } - ], + "outputs": [], "source": [ "from contextlib import asynccontextmanager # noqa: E402\n", "from pathlib import Path # noqa: E402\n", @@ -405,10 +389,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "d92e50b5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO: Started server process [264152]\n", + "INFO: Waiting for application startup.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Websocket server started at ws://127.0.0.1:8080.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO: Application startup complete.\n", + "INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "INFO: 127.0.0.1:46378 - \"GET / HTTP/1.1\" 200 OK\n", + " - on_connect(): Connected to client using IOWebsockets \n", + " - on_connect(): Receiving message from client.\n", + " - on_connect(): Initiating chat with agent using message 'Check out the weather in Paris and write a poem about it.'\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO: Shutting down\n", + "INFO: Waiting for application shutdown.\n", + "INFO: Application shutdown complete.\n", + "INFO: Finished server process [264152]\n" + ] + } + ], "source": [ "import uvicorn # noqa: E402\n", "\n", @@ -463,10 +491,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "708a98de", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Websocket server started at ws://127.0.0.1:8080.\n", + "HTTP server started at http://localhost:8000\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "127.0.0.1 - - [31/Mar/2024 20:20:07] \"GET / HTTP/1.1\" 200 -\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " - on_connect(): Connected to client using IOWebsockets \n", + " - on_connect(): Receiving message from client.\n", + " - on_connect(): Initiating chat with agent using message 'Check out the weather in Paris and write a poem about it.'\n", + " - HTTP server stopped.\n" + ] + } + ], "source": [ "from http.server import HTTPServer, SimpleHTTPRequestHandler # noqa: E402\n", "\n", @@ -529,7 +583,10 @@ "\n", " with HTTPServer((\"\", PORT), handler) as httpd:\n", " print(\"HTTP server started at http://localhost:\" + str(PORT))\n", - " httpd.serve_forever()" + " try:\n", + " httpd.serve_forever()\n", + " except KeyboardInterrupt:\n", + " print(\" - HTTP server stopped.\", flush=True)" ] }, {