File size: 3,254 Bytes
69d6754
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_nested_thoughts"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from gradio import ChatMessage\n", "import time\n", "\n", "sleep_time = 0.1\n", "long_sleep_time = 1\n", "\n", "def generate_response(message, history):\n", "    start_time = time.time()\n", "    responses = [\n", "        ChatMessage(\n", "            content=\"In order to find the current weather in San Francisco, I will need to use my weather tool.\",\n", "        )\n", "    ]\n", "    yield responses\n", "    time.sleep(sleep_time)\n", "\n", "    main_thought = ChatMessage(\n", "            content=\"\",\n", "            metadata={\"title\": \"Using Weather Tool\", \"id\": 1, \"status\": \"pending\"},\n", "        )\n", "\n", "    responses.append(main_thought)\n", "\n", "    yield responses\n", "    time.sleep(long_sleep_time)\n", "    responses[-1].content = \"Will check: weather.com and sunny.org\"\n", "    yield responses\n", "    time.sleep(sleep_time)\n", "    responses.append(\n", "        ChatMessage(\n", "            content=\"Received weather from weather.com.\",\n", "            metadata={\"title\": \"Checking weather.com\", \"parent_id\": 1, \"id\": 2, \"duration\": 0.05},\n", "        )\n", "    )\n", "    yield responses\n", "\n", "    sunny_start_time = time.time()\n", "    time.sleep(sleep_time)\n", "    sunny_thought = ChatMessage(\n", "            content=\"API Error when connecting to sunny.org \ud83d\udca5\",\n", "            metadata={\"title\": \"Checking sunny.org\", \"parent_id\": 1, \"id\": 3, \"status\": \"pending\"},\n", "        )\n", "\n", "    responses.append(sunny_thought)\n", "    yield responses\n", "\n", "    time.sleep(sleep_time)\n", "    responses.append(\n", "        ChatMessage(\n", "            content=\"Failed again\",\n", "            metadata={\"title\": \"I will try again\", \"id\": 4, \"parent_id\": 3, \"duration\": 0.1},\n", "\n", "        )\n", "    )\n", "    sunny_thought.metadata[\"status\"] = \"done\"\n", "    sunny_thought.metadata[\"duration\"] = time.time() - sunny_start_time\n", "\n", "    main_thought.metadata[\"status\"] = \"done\"\n", "    main_thought.metadata[\"duration\"] = time.time() - start_time\n", "\n", "    yield responses\n", "\n", "    time.sleep(long_sleep_time)\n", "\n", "    responses.append(\n", "        ChatMessage(\n", "            content=\"Based on the data only from weather.com, the current weather in San Francisco is 60 degrees and sunny.\",\n", "        )\n", "    )\n", "    yield responses\n", "\n", "demo = gr.ChatInterface(\n", "    generate_response,\n", "    title=\"Nested Thoughts Chat Interface\",\n", "    examples=[\"What is the weather in San Francisco right now?\"],\n", "    api_name=\"chat\"\n", ")\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}