File size: 2,243 Bytes
8bed8d7
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_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.5\n", "\n", "def simulate_thinking_chat(message, history):\n", "    start_time = time.time()\n", "    response = ChatMessage(\n", "        content=\"\",\n", "        metadata={\"title\": \"_Thinking_ step-by-step\", \"id\": 0, \"status\": \"pending\"}\n", "    )\n", "    yield response\n", "\n", "    thoughts = [\n", "        \"First, I need to understand the core aspects of the query...\",\n", "        \"Now, considering the broader context and implications...\",\n", "        \"Analyzing potential approaches to formulate a comprehensive answer...\",\n", "        \"Finally, structuring the response for clarity and completeness...\"\n", "    ]\n", "\n", "    accumulated_thoughts = \"\"\n", "    for thought in thoughts:\n", "        time.sleep(sleep_time)\n", "        accumulated_thoughts += f\"- {thought}\\n\\n\"\n", "        response.content = accumulated_thoughts.strip()\n", "        yield response\n", "\n", "    response.metadata[\"status\"] = \"done\"\n", "    response.metadata[\"duration\"] = time.time() - start_time\n", "    yield response\n", "\n", "    response = [\n", "        response,\n", "        ChatMessage(\n", "            content=\"Based on my thoughts and analysis above, my response is: This dummy repro shows how thoughts of a thinking LLM can be progressively shown before providing its final answer.\"\n", "        )\n", "    ]\n", "    yield response\n", "\n", "\n", "demo = gr.ChatInterface(\n", "    simulate_thinking_chat,\n", "    title=\"Thinking LLM Chat Interface \ud83e\udd14\",\n", "    type=\"messages\",\n", ")\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}