-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for thinking LLMs directly in
gr.ChatInterface
(#10305)
* ungroup thoughts from messages * rename messagebox to thought * refactor * * add metadata typing * group thoughts when nested * tweaks * tweak * add changeset * fix expanded rotation * border radius * update thought design * move spinner * prevent circular reference * revert border removal * css tweaks * border tweak * move chevron to the left * tweak nesting logic * thought group spacing * update run.py * icon changes * format * add changeset * add nested thought demo * changes * changes * changes * add demo * docs * guide * refactor styles and clean up logic * revert demo change and and deeper nested thought to demo * add optional duration to message types * add nested thoughts story * format * guide * change dropdown icon button * remove requirement for id's in nested thoughts * support markdown in thought title * get thought content in copied value * add funcs to utils * move is_all_text * remove comment * notebook * change bot padding * changes * changes * changes * panel css fix * changes * changes * changes * changes * tweak thought content opacity * more changes * add changeset * changes * restore * changes * changes * revert everythign * revert everythign * revert * changes * revert * make changes to demo * notebooks * more docs * format * changes * changes * update demo * fix typing issues * chatbot * document chatmessage helper class * add changeset * changes * format * docs --------- Co-authored-by: Hannah <[email protected]> Co-authored-by: gradio-pr-bot <[email protected]> Co-authored-by: aliabd <[email protected]>
- Loading branch information
1 parent
d2691e7
commit be40307
Showing
16 changed files
with
311 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@gradio/chatbot": minor | ||
"gradio": minor | ||
"website": minor | ||
--- | ||
|
||
feat:Add support for thinking LLMs directly in `gr.ChatInterface` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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", " type=\"messages\",\n", " title=\"Nested Thoughts Chat Interface\",\n", " examples=[\"What is the weather in San Francisco right now?\"]\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import gradio as gr | ||
from gradio import ChatMessage | ||
import time | ||
|
||
sleep_time = 0.1 | ||
long_sleep_time = 1 | ||
|
||
def generate_response(message, history): | ||
start_time = time.time() | ||
responses = [ | ||
ChatMessage( | ||
content="In order to find the current weather in San Francisco, I will need to use my weather tool.", | ||
) | ||
] | ||
yield responses | ||
time.sleep(sleep_time) | ||
|
||
main_thought = ChatMessage( | ||
content="", | ||
metadata={"title": "Using Weather Tool", "id": 1, "status": "pending"}, | ||
) | ||
|
||
responses.append(main_thought) | ||
|
||
yield responses | ||
time.sleep(long_sleep_time) | ||
responses[-1].content = "Will check: weather.com and sunny.org" | ||
yield responses | ||
time.sleep(sleep_time) | ||
responses.append( | ||
ChatMessage( | ||
content="Received weather from weather.com.", | ||
metadata={"title": "Checking weather.com", "parent_id": 1, "id": 2, "duration": 0.05}, | ||
) | ||
) | ||
yield responses | ||
|
||
sunny_start_time = time.time() | ||
time.sleep(sleep_time) | ||
sunny_thought = ChatMessage( | ||
content="API Error when connecting to sunny.org 💥", | ||
metadata={"title": "Checking sunny.org", "parent_id": 1, "id": 3, "status": "pending"}, | ||
) | ||
|
||
responses.append(sunny_thought) | ||
yield responses | ||
|
||
time.sleep(sleep_time) | ||
responses.append( | ||
ChatMessage( | ||
content="Failed again", | ||
metadata={"title": "I will try again", "id": 4, "parent_id": 3, "duration": 0.1}, | ||
|
||
) | ||
) | ||
sunny_thought.metadata["status"] = "done" | ||
sunny_thought.metadata["duration"] = time.time() - sunny_start_time | ||
|
||
main_thought.metadata["status"] = "done" | ||
main_thought.metadata["duration"] = time.time() - start_time | ||
|
||
yield responses | ||
|
||
time.sleep(long_sleep_time) | ||
|
||
responses.append( | ||
ChatMessage( | ||
content="Based on the data only from weather.com, the current weather in San Francisco is 60 degrees and sunny.", | ||
) | ||
) | ||
yield responses | ||
|
||
demo = gr.ChatInterface( | ||
generate_response, | ||
type="messages", | ||
title="Nested Thoughts Chat Interface", | ||
examples=["What is the weather in San Francisco right now?"] | ||
) | ||
|
||
if __name__ == "__main__": | ||
demo.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_options"]}, {"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", "import random\n", "\n", "example_code = \"\"\"\n", "Here's an example Python lambda function:\n", "\n", "lambda x: x + {}\n", "\n", "Is this correct?\n", "\"\"\"\n", "\n", "def chat(message, history):\n", " if message == \"Yes, that's correct.\":\n", " return \"Great!\"\n", " else:\n", " return {\n", " \"role\": \"assistant\",\n", " \"content\": example_code.format(random.randint(1, 100)),\n", " \"options\": [\n", " {\"value\": \"Yes, that's correct.\", \"label\": \"Yes\"},\n", " {\"value\": \"No\"}\n", " ]\n", " }\n", "\n", "demo = gr.ChatInterface(\n", " chat,\n", " type=\"messages\",\n", " examples=[\"Write an example Python lambda function.\"]\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} | ||
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_options"]}, {"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", "import random\n", "\n", "example_code = \"\"\"\n", "Here's an example Python lambda function:\n", "\n", "lambda x: x + {}\n", "\n", "Is this correct?\n", "\"\"\"\n", "\n", "def chat(message, history):\n", " if message == \"Yes, that's correct.\":\n", " return \"Great!\"\n", " else:\n", " return gr.ChatMessage(\n", " content=example_code.format(random.randint(1, 100)),\n", " options=[\n", " {\"value\": \"Yes, that's correct.\", \"label\": \"Yes\"},\n", " {\"value\": \"No\"}\n", " ]\n", " )\n", "\n", "demo = gr.ChatInterface(\n", " chat,\n", " type=\"messages\",\n", " examples=[\"Write an example Python lambda function.\"]\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import gradio as gr | ||
from gradio import ChatMessage | ||
import time | ||
|
||
sleep_time = 0.5 | ||
|
||
def simulate_thinking_chat(message, history): | ||
start_time = time.time() | ||
response = ChatMessage( | ||
content="", | ||
metadata={"title": "_Thinking_ step-by-step", "id": 0, "status": "pending"} | ||
) | ||
yield response | ||
|
||
thoughts = [ | ||
"First, I need to understand the core aspects of the query...", | ||
"Now, considering the broader context and implications...", | ||
"Analyzing potential approaches to formulate a comprehensive answer...", | ||
"Finally, structuring the response for clarity and completeness..." | ||
] | ||
|
||
accumulated_thoughts = "" | ||
for thought in thoughts: | ||
time.sleep(sleep_time) | ||
accumulated_thoughts += f"- {thought}\n\n" | ||
response.content = accumulated_thoughts.strip() | ||
yield response | ||
|
||
response.metadata["status"] = "done" | ||
response.metadata["duration"] = time.time() - start_time | ||
yield response | ||
|
||
response = [ | ||
response, | ||
ChatMessage( | ||
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." | ||
) | ||
] | ||
yield response | ||
|
||
|
||
demo = gr.ChatInterface( | ||
simulate_thinking_chat, | ||
title="Thinking LLM Chat Interface 🤔", | ||
type="messages", | ||
) | ||
|
||
if __name__ == "__main__": | ||
demo.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.