Skip to content

Document additional helper classes for gr.Chatbot #10346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jan 14, 2025
6 changes: 6 additions & 0 deletions .changeset/poor-facts-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gradio": minor
"website": minor
---

feat:Document additional helper classes for `gr.Chatbot`
24 changes: 23 additions & 1 deletion gradio/components/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,39 @@
from gradio.exceptions import Error


@document()
class MetadataDict(TypedDict):
"""
A dictionary to represent metadata for a message in the Chatbot component. An
instance of this dictionary is used as the `metadata` field in a ChatMessage when
the chat message should be displayed as a thought.
Parameters:
title: The title of the "thought" message. Only required field.
id: The ID of the message. Only used for nested thoughts. Nested thoughts can be nested by setting the parent_id to the id of the parent thought.
parent_id: The ID of the parent message. Only used for nested thoughts.
duration: The duration of the message. Appears next to the title in the thought bubble in a subdued font.
status: The status of the message. If "pending", the status is displayed as a spinner icon.
"""

title: Union[str, None]
id: NotRequired[int | str]
parent_id: NotRequired[int | str]
duration: NotRequired[float]
status: NotRequired[Literal["pending", "done"]]


@document()
class Option(TypedDict):
label: NotRequired[str]
"""
A dictionary to represent an option in a ChatMessage. An instance of this
dictionary is used as the `options` field in a ChatMessage.
Parameters:
value: The value to return when the option is selected.
label: The text to display in the option, if different from the value.
"""

value: str
label: NotRequired[str]


class FileDataDict(TypedDict):
Expand Down
2 changes: 1 addition & 1 deletion gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ def get_return_types(func: Callable) -> list:
if return_hint in {inspect.Signature.empty, None, NoneType}:
return []

if get_origin(return_hint) == tuple:
if get_origin(return_hint) is tuple:
return list(get_args(return_hint))

return [return_hint]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
delete components_no_dataclasses["eraser"];
delete components_no_dataclasses["waveformoptions"];
delete components_no_dataclasses["chatmessage"];
delete components_no_dataclasses["option"];
delete components_no_dataclasses["metadatadict"];

let events = get_object("events");
let events_matrix = get_object("events_matrix");
Expand Down
77 changes: 77 additions & 0 deletions js/_website/src/lib/templates/gradio/03_components/chatbot.svx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,51 @@

let obj = get_object("chatbot");
let chatmessage_obj = get_object("chatmessage");
let chatmessage_options = get_object("option");
let chatmessage_metadatadict = get_object("metadatadict");


chatmessage_options.parameters = [
{
"name": "value",
"annotation": "str",
"doc": "The value to return when the option is selected."
},
{
"name": "label",
"annotation": "str",
"doc": "The text to display in the option, if different from the value."
}
]

chatmessage_metadatadict.parameters = [
{
"name": "title",
"annotation": "str",
"doc": "The title of the 'thought' message. Only required field."
},
{
"name": "id",
"annotation": "int | str",
"doc": "The ID of the message. Only used for nested thoughts. Nested thoughts can be nested by setting the parent_id to the id of the parent thought."
},
{
"name": "parent_id",
"annotation": "int | str",
"doc": "The ID of the parent message. Only used for nested thoughts."
},
{
"name": "duration",
"annotation": "float",
"doc": "The duration of the message. Appears next to the title in the thought bubble in a subdued font."
},
{
"name": "status",
"annotation": "Literal['pending', 'done']",
"doc": "The status of the message. If 'pending', the status is displayed as a spinner icon."
}
]


let embedded_demo_obj = `[
{"role": "user", "content": "Hello World"},
Expand Down Expand Up @@ -188,6 +233,38 @@ gradio.ChatMessage(···)
#### Initialization
<ParamTable parameters={chatmessage_obj.parameters} />

<!--- Title -->
### MetadataDict

<!--- Usage -->
```python
gradio.MetadataDict(···)
```

<!--- Description -->
#### Description
## {@html style_formatted_text(chatmessage_metadatadict.description)}

<!--- Initialization -->
#### Initialization
<ParamTable parameters={chatmessage_metadatadict.parameters} />

<!--- Title -->
### Option

<!--- Usage -->
```python
gradio.Option(···)
```

<!--- Description -->
#### Description
## {@html style_formatted_text(chatmessage_options.description)}

<!--- Initialization -->
#### Initialization
<ParamTable parameters={chatmessage_options.parameters} />

</div>

{#if obj.guides && obj.guides.length > 0}
Expand Down
Loading