Spaces:
Running
Running
File size: 15,238 Bytes
2e77168 e08e97a 2e77168 e08e97a 2e77168 f5c4c58 2e77168 e08e97a 2e77168 e08e97a 2e77168 f5c4c58 2e77168 f5c4c58 2e77168 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# Chatbot
基于 [Ant Design X](https://x.ant.design) 封装的对话机器人组件。
## 示例
### 基础使用
支持传入:
- `user`,`assistant`两种 role。
- `text`,`file`,`tool`,`suggestion`四种消息类型。
<demo name="basic" position="bottom" collapsible="true"></demo>
### Chatbot 配置
支持传入配置项:
- `markdown_config`:Chatbot 中 Markdown 文本的渲染配置。
- `welcome_config`:当 Chatbot 中内容为空时显示的欢迎界面配置。
- `bot_config`:Chatbot 中 bot 信息的展示配置。
- `user_config`:Chatbot 中 user 信息的展示配置。
<demo name="chatbot_config" position="bottom" collapsible="true"></demo>
### Message 配置
`message`对象包含了`bot_config`和`user_config`的所有配置,这意味着用户可以自定义每条消息的显示方式。
<demo name="message_config" position="bottom" collapsible="true"></demo>
### 多模态示例
<demo name="multimodal" position="bottom" collapsible="true"></demo>
### Thinking 示例
<demo name="thinking" position="bottom" collapsible="true"></demo>
## API
### 属性
| 属性 | 类型 | 默认值 | 描述 |
| ------------------------------ | ----------------------------------------------------------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------- |
| value | `list[dict \| ChatbotDataMessage] \| ChatbotDataMessages \| None` | None | 聊天机器人中显示的默认消息列表,每条消息的格式为`ChatbotDataMessage`。 |
| welcome_config | `ChatbotWelcomeConfig \| dict \| None` | None | 欢迎界面配置。若`value`为空则会显示欢迎界面。 |
| markdown_config | `ChatbotMarkdownConfig \| dict \| None` | None | 所有消息的 Markdown 配置。 |
| user_config | `ChatbotUserConfig \| dict \| None` | None | 用户配置,当 message 的 role 为 'user'时生效。 |
| bot_config | `ChatbotBotConfig \| dict \| None` | None | 机器人配置,当 message 的 role 为 'assistant'时生效。 |
| auto_scroll | `bool` | True | 如果为 `True`,则当值改变时将自动滚动到文本框底部,除非用户向上滚动。如果为 `False`,则当值改变时不会滚动到文本框底部。 |
| show_scroll_to_bottom_button | `bool` | True | 如果为 `True`,则会在滚动条向上一定距离时显示滚动到底部按钮。 |
| scroll_to_bottom_button_offset | `int \| float` | 200 | 滚动到底部按钮出现时距离底部的偏移量。 |
| height | `int \| float \| str` | 400 | 组件的高度,如果值为数字,则以像素为单位指定,如果传递的是字符串,则以 CSS 单位指定。如果 messages 超出了组件高度,则组件将可滚动。 |
| max_height | `int \| float \| str \| None` | None | 组件的最大高度,如果传递的是数字,则以像素为单位指定,如果传递的是字符串,则以 CSS 单位指定。 |
| min_height | `int \| float \| str \| None` | None | 组件的最小高度,如果传递的是数字,则以像素为单位指定,如果传递的是字符串,则以 CSS 单位指定。 |
### 事件
| 事件 | 描述 |
| -------------------------------------------- | ------------------------------------ |
| `pro.Chatbot.change(fn, ···)` | 当 Chatbot 的值发生变化时触发。 |
| `pro.Chatbot.copy(fn, ···)` | 当点击复制按钮时触发。 |
| `pro.Chatbot.edit(fn, ···)` | 当用户编辑消息时触发。 |
| `pro.Chatbot.delete(fn, ···)` | 当用户删除消息时触发。 |
| `pro.Chatbot.like(fn, ···)` | 当点击喜欢/不喜欢按钮时触发。 |
| `pro.Chatbot.retry(fn, ···)` | 当点击重试按钮时触发。 |
| `pro.Chatbot.suggestion_select(fn, ···)` | 当选择 suggestion 消息的选项时触发。 |
| `pro.Chatbot.welcome_prompt_select(fn, ···)` | 当选择欢迎提示词的选项时触发。 |
### 插槽
```python
SLOTS=["roles"]
```
另外,如果已有的 role 样式不符合预期,也可以和`Ant Design X`的`Bubble`组件一样使用`ms.Slot("roles")`进行自定义。
### 类型
```python
# Ant Design X prompt props: https://x.ant.design/components/prompts#promptprops
class ChatbotPromptConfig(GradioModel):
disabled: Optional[bool] = None
description: Optional[str] = None
label: Optional[str] = None
icon: Optional[str] = None
key: Optional[str] = None
children: Optional[List[Union[
dict,
]]] = None
# Ant Design X prompts props: https://x.ant.design/components/prompts
class ChatbotPromptsConfig(GradioModel):
title: Optional[str] = None
vertical: Optional[bool] = False
wrap: Optional[bool] = False
styles: Optional[dict] = None
class_names: Optional[dict] = None
elem_style: Optional[dict] = None
elem_classes: Optional[Union[str, List[str]]] = None
items: Optional[List[Union[ChatbotPromptConfig, dict]]] = None
# Ant Design X welcome props: https://x.ant.design/components/welcome
class ChatbotWelcomeConfig(GradioModel):
variant: Optional[Literal['borderless', 'filled']] = 'borderless'
icon: Optional[Union[str, Path]] = None
title: Optional[str] = None
description: Optional[str] = None
extra: Optional[str] = None
elem_style: Optional[dict] = None
elem_classes: Optional[Union[str, List[str]]] = None
styles: Optional[dict] = None
class_names: Optional[dict] = None
prompts: Optional[Union[ChatbotPromptsConfig, dict]] = None
class ChatbotMarkdownConfig(GradioModel):
render_markdown: Optional[bool] = True
latex_delimiters: Optional[list[dict[str, str | bool]]] = field(
default_factory=lambda: [
{
"left": "$$",
"right": "$$",
"display": True
},
{
"left": "$",
"right": "$",
"display": False
},
{
"left": "\\(",
"right": "\\)",
"display": False
},
{
"left": "\\begin{equation}",
"right": "\\end{equation}",
"display": True
},
{
"left": "\\begin{align}",
"right": "\\end{align}",
"display": True
},
{
"left": "\\begin{alignat}",
"right": "\\end{alignat}",
"display": True
},
{
"left": "\\begin{gather}",
"right": "\\end{gather}",
"display": True
},
{
"left": "\\begin{CD}",
"right": "\\end{CD}",
"display": True
},
{
"left": "\\[",
"right": "\\]",
"display": True
},
])
sanitize_html: Optional[bool] = True
line_breaks: Optional[bool] = True
rtl: Optional[bool] = False
allow_tags: Optional[Union[List[str], bool]] = False
class ChatbotActionConfig(GradioModel):
action: Literal['copy', 'like', 'dislike', 'retry', 'edit', 'delete']
disabled: Optional[bool] = None
# Ant Design tooltip: https://ant.design/components/tooltip
tooltip: Optional[Union[str, dict]] = None
# Ant Design popconfirm props: https://ant.design/components/popconfirm
popconfirm: Optional[Union[str, dict]] = None
class ChatbotUserConfig(GradioModel):
# Action buttons for user message
actions: Optional[List[Union[Literal[
'copy',
'edit',
'delete',
], ChatbotActionConfig, dict]]] = field(default_factory=lambda: [
"copy"
# 'edit',
# ChatbotActionConfig(
# action='delete',
# popconfirm=dict(title="Delete the message",
# description="Are you sure to delete this message?",
# okButtonProps=dict(danger=True)))
])
disabled_actions: Optional[List[Union[Literal[
'copy',
'edit',
'delete',
]]]] = None
header: Optional[str] = None
footer: Optional[str] = None
# Ant Design avatar props: https://ant.design/components/avatar
avatar: Optional[Union[str, Path, dict, None]] = None
variant: Optional[Literal['filled', 'borderless', 'outlined',
'shadow']] = None
shape: Optional[Literal['round', 'corner']] = None
placement: Optional[Literal['start', 'end']] = 'end'
loading: Optional[bool] = None
typing: Optional[Union[bool, dict]] = None
elem_style: Optional[dict] = None
elem_classes: Optional[Union[str, List[str]]] = None
styles: Optional[dict] = None
class_names: Optional[dict] = None
class ChatbotBotConfig(ChatbotUserConfig):
# Action buttons for bot message
actions: Optional[List[Union[Literal[
'copy',
'like',
'dislike',
'retry',
'edit',
'delete',
], ChatbotActionConfig, dict]]] = field(default_factory=lambda: [
'copy',
# 'like', 'dislike', 'retry', 'edit',
# ChatbotActionConfig(
# action='delete',
# popconfirm=dict(title="Delete the message",
# description="Are you sure to delete this message?",
# okButtonProps=dict(danger=True)))
])
disabled_actions: Optional[List[Union[Literal[
'copy',
'like',
'dislike',
'retry',
'edit',
'delete',
]]]] = None
placement: Optional[Literal['start', 'end']] = 'start'
class ChatbotDataTextContentOptions(ChatbotMarkdownConfig):
pass
class ChatbotDataToolContentOptions(ChatbotDataTextContentOptions):
"""
title: tool message title.
status: tool message status, if status is 'done', the message will be collapsed.
"""
title: Optional[str] = None
status: Optional[Literal['pending', 'done']] = None
# Ant Design flex props: https://ant.design/components/flex
class ChatbotDataFileContentOptions(GradioModel):
vertical: Union[bool] = False
wrap: Optional[Union[Literal['nowrap', 'wrap', 'wrap-reverse'],
bool]] = True
justify: Optional[Literal['normal', 'start', 'end', 'flex-start',
'flex-end', 'center', 'left', 'right',
'space-between', 'space-around', 'space-evenly',
'stretch', 'safe', 'unsafe']] = "normal"
align: Optional[Literal['normal', 'start', 'end', 'flex-start', 'flex-end',
'center', 'self-start', 'self-end', 'baseline',
'unsafe', 'stretch']] = "normal"
flex: Optional[str] = "normal"
gap: Optional[Union[Literal["small", "middle", "large"], str, int,
float]] = "small"
image_props: Optional[dict] = None
# Ant Design X prompts props: https://x.ant.design/components/prompts
class ChatbotDataSuggestionContentOptions(ChatbotPromptsConfig):
pass
# Ant Design X prompt props: https://x.ant.design/components/prompts#promptprops
class ChatbotDataSuggestionContentItem(ChatbotPromptConfig):
pass
class ChatbotDataMeta(GradioModel):
feedback: Optional[Literal['like', 'dislike', None]] = None
class ChatbotDataMessageContent(GradioModel):
"""
type: Content type, support 'text', 'tool', 'file', 'suggestion'.
content: Content value.
options: Content options, each content type has different options.
copyable: Whether the content can be copied via the 'copy' button.
editable: Whether the content can be edited via the 'edit' button. Only available for content type 'text' and 'tool'.
"""
type: Optional[Literal['text', 'tool', 'file', 'suggestion']] = 'text'
copyable: Optional[bool] = True
editable: Optional[bool] = True
content: Optional[Union[str, List[Union[FileData,
ChatbotDataSuggestionContentItem,
dict, str]]]] = None
options: Optional[Union[dict, ChatbotDataTextContentOptions,
ChatbotDataFileContentOptions,
ChatbotDataToolContentOptions,
ChatbotDataSuggestionContentOptions]] = None
class ChatbotDataMessage(ChatbotBotConfig):
role: Union[Literal['user', 'assistant', 'system'], str] = None
key: Optional[Union[str, int, float]] = None
# If status is 'pending', the message will not render the footer area (including 'actions' and 'footer').
status: Optional[Literal['pending', 'done']] = None
content: Optional[Union[str, ChatbotDataMessageContent, dict,
List[ChatbotDataMessageContent],
List[dict]]] = None
placement: Optional[Literal['start', 'end']] = None
actions: Optional[List[Union[Literal[
'copy',
'like',
'dislike',
'retry',
'edit',
'delete',
], ChatbotActionConfig, dict]]] = None
disabled_actions: Optional[List[Union[Literal[
'copy',
'like',
'dislike',
'retry',
'edit',
'delete',
]]]] = None
meta: Optional[Union[ChatbotDataMeta, dict]] = None
class ChatbotDataMessages(GradioRootModel):
root: List[ChatbotDataMessage]
```
|