littlebird13 commited on
Commit
7f72bb4
·
verified ·
1 Parent(s): 6ddabd2

Update web_ui.py

Browse files
Files changed (1) hide show
  1. web_ui.py +366 -366
web_ui.py CHANGED
@@ -1,366 +1,366 @@
1
- import os
2
- import pprint
3
- import re
4
- from typing import List, Optional, Union
5
-
6
- from qwen_agent import Agent, MultiAgentHub
7
- from qwen_agent.agents.user_agent import PENDING_USER_INPUT
8
- from qwen_agent.gui.gradio_utils import format_cover_html
9
- from qwen_agent.gui.utils import convert_fncall_to_text, convert_history_to_chatbot, get_avatar_image
10
- from qwen_agent.llm.schema import CONTENT, FILE, IMAGE, NAME, ROLE, USER, Message
11
- from qwen_agent.log import logger
12
- from qwen_agent.utils.utils import print_traceback
13
-
14
- class WebUI:
15
- """A Common chatbot application for agent."""
16
-
17
- def __init__(self, agent: Union[Agent, MultiAgentHub, List[Agent]], chatbot_config: Optional[dict] = None):
18
- """
19
- Initialization the chatbot.
20
-
21
- Args:
22
- agent: The agent or a list of agents,
23
- supports various types of agents such as Assistant, GroupChat, Router, etc.
24
- chatbot_config: The chatbot configuration.
25
- Set the configuration as {'user.name': '', 'user.avatar': '', 'agent.avatar': '', 'input.placeholder': '', 'prompt.suggestions': []}.
26
- """
27
- chatbot_config = chatbot_config or {}
28
-
29
- if isinstance(agent, MultiAgentHub):
30
- self.agent_list = [agent for agent in agent.nonuser_agents]
31
- self.agent_hub = agent
32
- elif isinstance(agent, list):
33
- self.agent_list = agent
34
- self.agent_hub = None
35
- else:
36
- self.agent_list = [agent]
37
- self.agent_hub = None
38
-
39
- user_name = chatbot_config.get('user.name', 'user')
40
- self.user_config = {
41
- 'name': user_name,
42
- 'avatar': chatbot_config.get(
43
- 'user.avatar',
44
- get_avatar_image(user_name),
45
- ),
46
- }
47
-
48
- self.agent_config_list = [{
49
- 'name': agent.name,
50
- 'avatar': chatbot_config.get(
51
- 'agent.avatar',
52
- get_avatar_image(agent.name),
53
- ),
54
- 'description': agent.description or "I'm a helpful assistant.",
55
- } for agent in self.agent_list]
56
-
57
- self.input_placeholder = chatbot_config.get('input.placeholder', '跟我聊聊吧~')
58
- self.prompt_suggestions = chatbot_config.get('prompt.suggestions', [])
59
- self.verbose = chatbot_config.get('verbose', False)
60
-
61
- """
62
- Run the chatbot.
63
-
64
- Args:
65
- messages: The chat history.
66
- """
67
-
68
- def run(self,
69
- messages: List[Message] = None,
70
- share: bool = False,
71
- server_name: str = None,
72
- server_port: int = None,
73
- concurrency_limit: int = 10,
74
- enable_mention: bool = False,
75
- **kwargs):
76
- self.run_kwargs = kwargs
77
-
78
- from qwen_agent.gui.gradio import gr, mgr
79
-
80
- customTheme = gr.themes.Default(
81
- primary_hue=gr.themes.utils.colors.blue,
82
- radius_size=gr.themes.utils.sizes.radius_none,
83
- )
84
-
85
- with gr.Blocks(
86
- css=os.path.join(os.path.dirname(__file__), 'assets/appBot.css'),
87
- theme=customTheme,
88
- ) as demo:
89
- history = gr.State([])
90
-
91
- with gr.Row(elem_classes='container'):
92
- with gr.Column(scale=4):
93
- chatbot = mgr.Chatbot(value=convert_history_to_chatbot(messages=messages),
94
- avatar_images=[
95
- self.user_config,
96
- self.agent_config_list,
97
- ],
98
- height=650,
99
- avatar_image_width=80,
100
- flushing=False,
101
- show_copy_button=False,
102
- latex_delimiters=[{
103
- 'left': '\\(',
104
- 'right': '\\)',
105
- 'display': True
106
- }, {
107
- 'left': '\\begin{equation}',
108
- 'right': '\\end{equation}',
109
- 'display': True
110
- }, {
111
- 'left': '\\begin{align}',
112
- 'right': '\\end{align}',
113
- 'display': True
114
- }, {
115
- 'left': '\\begin{alignat}',
116
- 'right': '\\end{alignat}',
117
- 'display': True
118
- }, {
119
- 'left': '\\begin{gather}',
120
- 'right': '\\end{gather}',
121
- 'display': True
122
- }, {
123
- 'left': '\\begin{CD}',
124
- 'right': '\\end{CD}',
125
- 'display': True
126
- }, {
127
- 'left': '\\[',
128
- 'right': '\\]',
129
- 'display': True
130
- }])
131
-
132
- input = mgr.MultimodalInput(placeholder=self.input_placeholder,)
133
-
134
- with gr.Column(scale=1):
135
- if len(self.agent_list) > 1:
136
- agent_selector = gr.Dropdown(
137
- [(agent.name, i) for i, agent in enumerate(self.agent_list)],
138
- label='Agents',
139
- info='选择一个Agent',
140
- value=0,
141
- interactive=True,
142
- )
143
-
144
- agent_info_block = self._create_agent_info_block()
145
-
146
- # agent_plugins_block = self._create_agent_plugins_block()
147
-
148
- if self.prompt_suggestions:
149
- gr.Examples(
150
- label='推荐对话',
151
- examples=self.prompt_suggestions,
152
- inputs=[input],
153
- )
154
-
155
- if len(self.agent_list) > 1:
156
- agent_selector.change(
157
- fn=self.change_agent,
158
- inputs=[agent_selector],
159
- outputs=[agent_selector, agent_info_block, agent_plugins_block],
160
- queue=False,
161
- )
162
-
163
- input_promise = input.submit(
164
- fn=self.add_text,
165
- inputs=[input, chatbot, history],
166
- outputs=[input, chatbot, history],
167
- queue=False,
168
- )
169
-
170
- if len(self.agent_list) > 1 and enable_mention:
171
- input_promise = input_promise.then(
172
- self.add_mention,
173
- [chatbot, agent_selector],
174
- [chatbot, agent_selector],
175
- ).then(
176
- self.agent_run,
177
- [chatbot, history, agent_selector],
178
- [chatbot, history, agent_selector],
179
- )
180
- else:
181
- input_promise = input_promise.then(
182
- self.agent_run,
183
- [chatbot, history],
184
- [chatbot, history],
185
- )
186
-
187
- input_promise.then(self.flushed, None, [input])
188
-
189
- demo.load(None)
190
-
191
- demo.queue(default_concurrency_limit=concurrency_limit).launch(share=share,
192
- server_name=server_name,
193
- server_port=server_port)
194
-
195
- def change_agent(self, agent_selector):
196
- yield agent_selector, self._create_agent_info_block(agent_selector), self._create_agent_plugins_block(
197
- agent_selector)
198
-
199
- def add_text(self, _input, _chatbot, _history):
200
- from qwen_agent.gui.gradio import gr
201
- if _input.text == "/clear":
202
- _chatbot = []
203
- _history.clear()
204
- yield gr.update(interactive=False, value=""), _chatbot, _history
205
- return
206
-
207
- _history.append({
208
- ROLE: USER,
209
- CONTENT: [{
210
- 'text': _input.text
211
- }],
212
- })
213
-
214
- if self.user_config[NAME]:
215
- _history[-1][NAME] = self.user_config[NAME]
216
-
217
- if _input.files:
218
- for file in _input.files:
219
- if file.mime_type.startswith('image/'):
220
- _history[-1][CONTENT].append({IMAGE: 'file://' + file.path})
221
- else:
222
- _history[-1][CONTENT].append({FILE: file.path})
223
-
224
- _chatbot.append([_input, None])
225
-
226
- yield gr.update(interactive=False, value=None), _chatbot, _history
227
-
228
- def add_mention(self, _chatbot, _agent_selector):
229
- if len(self.agent_list) == 1:
230
- yield _chatbot, _agent_selector
231
-
232
- query = _chatbot[-1][0].text
233
- match = re.search(r'@\w+\b', query)
234
- if match:
235
- _agent_selector = self._get_agent_index_by_name(match.group()[1:])
236
-
237
- agent_name = self.agent_list[_agent_selector].name
238
-
239
- if ('@' + agent_name) not in query and self.agent_hub is None:
240
- _chatbot[-1][0].text = '@' + agent_name + ' ' + query
241
-
242
- yield _chatbot, _agent_selector
243
-
244
- def agent_run(self, _chatbot, _history, _agent_selector=None):
245
- if not _history:
246
- if _agent_selector is not None:
247
- yield _chatbot, _history, _agent_selector
248
- else:
249
- yield _chatbot, _history
250
- return
251
-
252
-
253
- if self.verbose:
254
- logger.info('agent_run input:\n' + pprint.pformat(_history, indent=2))
255
-
256
- num_input_bubbles = len(_chatbot) - 1
257
- num_output_bubbles = 1
258
- _chatbot[-1][1] = [None for _ in range(len(self.agent_list))]
259
-
260
- agent_runner = self.agent_list[_agent_selector or 0]
261
- if self.agent_hub:
262
- agent_runner = self.agent_hub
263
- responses = []
264
- for responses in agent_runner.run(_history, **self.run_kwargs):
265
- # usage = responses.usage
266
- # responses = [Message(ASSISTANT, responses.output.choices[0].message.content)]
267
-
268
- if not responses:
269
- continue
270
- if responses[-1][CONTENT] == PENDING_USER_INPUT:
271
- logger.info('Interrupted. Waiting for user input!')
272
- break
273
-
274
- display_responses = convert_fncall_to_text(responses)
275
- # display_responses[-1][CONTENT] += "\n<summary>" + repr({"usage": usage}) + "</summary>"
276
- if not display_responses:
277
- continue
278
- if display_responses[-1][CONTENT] is None:
279
- continue
280
-
281
- while len(display_responses) > num_output_bubbles:
282
- # Create a new chat bubble
283
- _chatbot.append([None, None])
284
- _chatbot[-1][1] = [None for _ in range(len(self.agent_list))]
285
- num_output_bubbles += 1
286
-
287
- assert num_output_bubbles == len(display_responses)
288
- assert num_input_bubbles + num_output_bubbles == len(_chatbot)
289
-
290
- for i, rsp in enumerate(display_responses):
291
- agent_index = self._get_agent_index_by_name(rsp[NAME])
292
- _chatbot[num_input_bubbles + i][1][agent_index] = rsp[CONTENT]
293
-
294
- if len(self.agent_list) > 1:
295
- _agent_selector = agent_index
296
-
297
- if _agent_selector is not None:
298
- yield _chatbot, _history, _agent_selector
299
- else:
300
- yield _chatbot, _history
301
-
302
- if responses:
303
- for res in responses:
304
- res['content'] = re.sub(r"\n<summary>input tokens.*</summary>", "", res['content'])
305
- _history.extend([res for res in responses if res[CONTENT] != PENDING_USER_INPUT])
306
-
307
- if _agent_selector is not None:
308
- yield _chatbot, _history, _agent_selector
309
- else:
310
- yield _chatbot, _history
311
-
312
- if self.verbose:
313
- logger.info('agent_run response:\n' + pprint.pformat(responses, indent=2))
314
-
315
- def flushed(self):
316
- from qwen_agent.gui.gradio import gr
317
-
318
- return gr.update(interactive=True)
319
-
320
- def _get_agent_index_by_name(self, agent_name):
321
- if agent_name is None:
322
- return 0
323
-
324
- try:
325
- agent_name = agent_name.strip()
326
- for i, agent in enumerate(self.agent_list):
327
- if agent.name == agent_name:
328
- return i
329
- return 0
330
- except Exception:
331
- print_traceback()
332
- return 0
333
-
334
- def _create_agent_info_block(self, agent_index=0):
335
- from qwen_agent.gui.gradio import gr
336
-
337
- agent_config_interactive = self.agent_config_list[agent_index]
338
-
339
- return gr.HTML(
340
- format_cover_html(
341
- bot_name=agent_config_interactive['name'],
342
- bot_description=agent_config_interactive['description'],
343
- bot_avatar=agent_config_interactive['avatar'],
344
- ))
345
-
346
- def _create_agent_plugins_block(self, agent_index=0):
347
- from qwen_agent.gui.gradio import gr
348
-
349
- agent_interactive = self.agent_list[agent_index]
350
-
351
- if agent_interactive.function_map:
352
- capabilities = [key for key in agent_interactive.function_map.keys()]
353
- return gr.CheckboxGroup(
354
- label='插件',
355
- value=capabilities,
356
- choices=capabilities,
357
- interactive=False,
358
- )
359
-
360
- else:
361
- return gr.CheckboxGroup(
362
- label='插件',
363
- value=[],
364
- choices=[],
365
- interactive=False,
366
- )
 
1
+ import os
2
+ import pprint
3
+ import re
4
+ from typing import List, Optional, Union
5
+
6
+ from qwen_agent import Agent, MultiAgentHub
7
+ from qwen_agent.agents.user_agent import PENDING_USER_INPUT
8
+ from qwen_agent.gui.gradio_utils import format_cover_html
9
+ from qwen_agent.gui.utils import convert_fncall_to_text, convert_history_to_chatbot, get_avatar_image
10
+ from qwen_agent.llm.schema import CONTENT, FILE, IMAGE, NAME, ROLE, USER, Message
11
+ from qwen_agent.log import logger
12
+ from qwen_agent.utils.utils import print_traceback
13
+
14
+ class WebUI:
15
+ """A Common chatbot application for agent."""
16
+
17
+ def __init__(self, agent: Union[Agent, MultiAgentHub, List[Agent]], chatbot_config: Optional[dict] = None):
18
+ """
19
+ Initialization the chatbot.
20
+
21
+ Args:
22
+ agent: The agent or a list of agents,
23
+ supports various types of agents such as Assistant, GroupChat, Router, etc.
24
+ chatbot_config: The chatbot configuration.
25
+ Set the configuration as {'user.name': '', 'user.avatar': '', 'agent.avatar': '', 'input.placeholder': '', 'prompt.suggestions': []}.
26
+ """
27
+ chatbot_config = chatbot_config or {}
28
+
29
+ if isinstance(agent, MultiAgentHub):
30
+ self.agent_list = [agent for agent in agent.nonuser_agents]
31
+ self.agent_hub = agent
32
+ elif isinstance(agent, list):
33
+ self.agent_list = agent
34
+ self.agent_hub = None
35
+ else:
36
+ self.agent_list = [agent]
37
+ self.agent_hub = None
38
+
39
+ user_name = chatbot_config.get('user.name', 'user')
40
+ self.user_config = {
41
+ 'name': user_name,
42
+ 'avatar': chatbot_config.get(
43
+ 'user.avatar',
44
+ get_avatar_image(user_name),
45
+ ),
46
+ }
47
+
48
+ self.agent_config_list = [{
49
+ 'name': agent.name,
50
+ 'avatar': chatbot_config.get(
51
+ 'agent.avatar',
52
+ get_avatar_image(agent.name),
53
+ ),
54
+ 'description': agent.description or "I'm a helpful assistant.",
55
+ } for agent in self.agent_list]
56
+
57
+ self.input_placeholder = chatbot_config.get('input.placeholder', '跟我聊聊吧~')
58
+ self.prompt_suggestions = chatbot_config.get('prompt.suggestions', [])
59
+ self.verbose = chatbot_config.get('verbose', False)
60
+
61
+ """
62
+ Run the chatbot.
63
+
64
+ Args:
65
+ messages: The chat history.
66
+ """
67
+
68
+ def run(self,
69
+ messages: List[Message] = None,
70
+ share: bool = False,
71
+ server_name: str = None,
72
+ server_port: int = None,
73
+ concurrency_limit: int = 10,
74
+ enable_mention: bool = False,
75
+ **kwargs):
76
+ self.run_kwargs = kwargs
77
+
78
+ from qwen_agent.gui.gradio import gr, mgr
79
+
80
+ customTheme = gr.themes.Default(
81
+ primary_hue=gr.themes.utils.colors.blue,
82
+ radius_size=gr.themes.utils.sizes.radius_none,
83
+ )
84
+
85
+ with gr.Blocks(
86
+ css=os.path.join(os.path.dirname(__file__), 'assets/appBot.css'),
87
+ theme=customTheme,
88
+ ) as demo:
89
+ history = gr.State([])
90
+
91
+ with gr.Row(elem_classes='container'):
92
+ with gr.Column(scale=4):
93
+ chatbot = mgr.Chatbot(value=convert_history_to_chatbot(messages=messages),
94
+ avatar_images=[
95
+ self.user_config,
96
+ self.agent_config_list,
97
+ ],
98
+ height=650,
99
+ avatar_image_width=80,
100
+ flushing=False,
101
+ show_copy_button=False,
102
+ latex_delimiters=[{
103
+ 'left': '\\(',
104
+ 'right': '\\)',
105
+ 'display': True
106
+ }, {
107
+ 'left': '\\begin{equation}',
108
+ 'right': '\\end{equation}',
109
+ 'display': True
110
+ }, {
111
+ 'left': '\\begin{align}',
112
+ 'right': '\\end{align}',
113
+ 'display': True
114
+ }, {
115
+ 'left': '\\begin{alignat}',
116
+ 'right': '\\end{alignat}',
117
+ 'display': True
118
+ }, {
119
+ 'left': '\\begin{gather}',
120
+ 'right': '\\end{gather}',
121
+ 'display': True
122
+ }, {
123
+ 'left': '\\begin{CD}',
124
+ 'right': '\\end{CD}',
125
+ 'display': True
126
+ }, {
127
+ 'left': '\\[',
128
+ 'right': '\\]',
129
+ 'display': True
130
+ }])
131
+
132
+ input = mgr.MultimodalInput(placeholder=self.input_placeholder, upload_button_props=dict(file_types=[".pdf", ".doc", ".docx", ".ppt", ".pptx", ".txt", ".html"]))
133
+
134
+ with gr.Column(scale=1):
135
+ if len(self.agent_list) > 1:
136
+ agent_selector = gr.Dropdown(
137
+ [(agent.name, i) for i, agent in enumerate(self.agent_list)],
138
+ label='Agents',
139
+ info='选择一个Agent',
140
+ value=0,
141
+ interactive=True,
142
+ )
143
+
144
+ agent_info_block = self._create_agent_info_block()
145
+
146
+ # agent_plugins_block = self._create_agent_plugins_block()
147
+
148
+ if self.prompt_suggestions:
149
+ gr.Examples(
150
+ label='推荐对话',
151
+ examples=self.prompt_suggestions,
152
+ inputs=[input],
153
+ )
154
+
155
+ if len(self.agent_list) > 1:
156
+ agent_selector.change(
157
+ fn=self.change_agent,
158
+ inputs=[agent_selector],
159
+ outputs=[agent_selector, agent_info_block, agent_plugins_block],
160
+ queue=False,
161
+ )
162
+
163
+ input_promise = input.submit(
164
+ fn=self.add_text,
165
+ inputs=[input, chatbot, history],
166
+ outputs=[input, chatbot, history],
167
+ queue=False,
168
+ )
169
+
170
+ if len(self.agent_list) > 1 and enable_mention:
171
+ input_promise = input_promise.then(
172
+ self.add_mention,
173
+ [chatbot, agent_selector],
174
+ [chatbot, agent_selector],
175
+ ).then(
176
+ self.agent_run,
177
+ [chatbot, history, agent_selector],
178
+ [chatbot, history, agent_selector],
179
+ )
180
+ else:
181
+ input_promise = input_promise.then(
182
+ self.agent_run,
183
+ [chatbot, history],
184
+ [chatbot, history],
185
+ )
186
+
187
+ input_promise.then(self.flushed, None, [input])
188
+
189
+ demo.load(None)
190
+
191
+ demo.queue(default_concurrency_limit=concurrency_limit).launch(share=share,
192
+ server_name=server_name,
193
+ server_port=server_port)
194
+
195
+ def change_agent(self, agent_selector):
196
+ yield agent_selector, self._create_agent_info_block(agent_selector), self._create_agent_plugins_block(
197
+ agent_selector)
198
+
199
+ def add_text(self, _input, _chatbot, _history):
200
+ from qwen_agent.gui.gradio import gr
201
+ if _input.text == "/clear":
202
+ _chatbot = []
203
+ _history.clear()
204
+ yield gr.update(interactive=False, value=""), _chatbot, _history
205
+ return
206
+
207
+ _history.append({
208
+ ROLE: USER,
209
+ CONTENT: [{
210
+ 'text': _input.text
211
+ }],
212
+ })
213
+
214
+ if self.user_config[NAME]:
215
+ _history[-1][NAME] = self.user_config[NAME]
216
+
217
+ if _input.files:
218
+ for file in _input.files:
219
+ if file.mime_type.startswith('image/'):
220
+ _history[-1][CONTENT].append({IMAGE: 'file://' + file.path})
221
+ else:
222
+ _history[-1][CONTENT].append({FILE: file.path})
223
+
224
+ _chatbot.append([_input, None])
225
+
226
+ yield gr.update(interactive=False, value=None), _chatbot, _history
227
+
228
+ def add_mention(self, _chatbot, _agent_selector):
229
+ if len(self.agent_list) == 1:
230
+ yield _chatbot, _agent_selector
231
+
232
+ query = _chatbot[-1][0].text
233
+ match = re.search(r'@\w+\b', query)
234
+ if match:
235
+ _agent_selector = self._get_agent_index_by_name(match.group()[1:])
236
+
237
+ agent_name = self.agent_list[_agent_selector].name
238
+
239
+ if ('@' + agent_name) not in query and self.agent_hub is None:
240
+ _chatbot[-1][0].text = '@' + agent_name + ' ' + query
241
+
242
+ yield _chatbot, _agent_selector
243
+
244
+ def agent_run(self, _chatbot, _history, _agent_selector=None):
245
+ if not _history:
246
+ if _agent_selector is not None:
247
+ yield _chatbot, _history, _agent_selector
248
+ else:
249
+ yield _chatbot, _history
250
+ return
251
+
252
+
253
+ if self.verbose:
254
+ logger.info('agent_run input:\n' + pprint.pformat(_history, indent=2))
255
+
256
+ num_input_bubbles = len(_chatbot) - 1
257
+ num_output_bubbles = 1
258
+ _chatbot[-1][1] = [None for _ in range(len(self.agent_list))]
259
+
260
+ agent_runner = self.agent_list[_agent_selector or 0]
261
+ if self.agent_hub:
262
+ agent_runner = self.agent_hub
263
+ responses = []
264
+ for responses in agent_runner.run(_history, **self.run_kwargs):
265
+ # usage = responses.usage
266
+ # responses = [Message(ASSISTANT, responses.output.choices[0].message.content)]
267
+
268
+ if not responses:
269
+ continue
270
+ if responses[-1][CONTENT] == PENDING_USER_INPUT:
271
+ logger.info('Interrupted. Waiting for user input!')
272
+ break
273
+
274
+ display_responses = convert_fncall_to_text(responses)
275
+ # display_responses[-1][CONTENT] += "\n<summary>" + repr({"usage": usage}) + "</summary>"
276
+ if not display_responses:
277
+ continue
278
+ if display_responses[-1][CONTENT] is None:
279
+ continue
280
+
281
+ while len(display_responses) > num_output_bubbles:
282
+ # Create a new chat bubble
283
+ _chatbot.append([None, None])
284
+ _chatbot[-1][1] = [None for _ in range(len(self.agent_list))]
285
+ num_output_bubbles += 1
286
+
287
+ assert num_output_bubbles == len(display_responses)
288
+ assert num_input_bubbles + num_output_bubbles == len(_chatbot)
289
+
290
+ for i, rsp in enumerate(display_responses):
291
+ agent_index = self._get_agent_index_by_name(rsp[NAME])
292
+ _chatbot[num_input_bubbles + i][1][agent_index] = rsp[CONTENT]
293
+
294
+ if len(self.agent_list) > 1:
295
+ _agent_selector = agent_index
296
+
297
+ if _agent_selector is not None:
298
+ yield _chatbot, _history, _agent_selector
299
+ else:
300
+ yield _chatbot, _history
301
+
302
+ if responses:
303
+ for res in responses:
304
+ res['content'] = re.sub(r"\n<summary>input tokens.*</summary>", "", res['content'])
305
+ _history.extend([res for res in responses if res[CONTENT] != PENDING_USER_INPUT])
306
+
307
+ if _agent_selector is not None:
308
+ yield _chatbot, _history, _agent_selector
309
+ else:
310
+ yield _chatbot, _history
311
+
312
+ if self.verbose:
313
+ logger.info('agent_run response:\n' + pprint.pformat(responses, indent=2))
314
+
315
+ def flushed(self):
316
+ from qwen_agent.gui.gradio import gr
317
+
318
+ return gr.update(interactive=True)
319
+
320
+ def _get_agent_index_by_name(self, agent_name):
321
+ if agent_name is None:
322
+ return 0
323
+
324
+ try:
325
+ agent_name = agent_name.strip()
326
+ for i, agent in enumerate(self.agent_list):
327
+ if agent.name == agent_name:
328
+ return i
329
+ return 0
330
+ except Exception:
331
+ print_traceback()
332
+ return 0
333
+
334
+ def _create_agent_info_block(self, agent_index=0):
335
+ from qwen_agent.gui.gradio import gr
336
+
337
+ agent_config_interactive = self.agent_config_list[agent_index]
338
+
339
+ return gr.HTML(
340
+ format_cover_html(
341
+ bot_name=agent_config_interactive['name'],
342
+ bot_description=agent_config_interactive['description'],
343
+ bot_avatar=agent_config_interactive['avatar'],
344
+ ))
345
+
346
+ def _create_agent_plugins_block(self, agent_index=0):
347
+ from qwen_agent.gui.gradio import gr
348
+
349
+ agent_interactive = self.agent_list[agent_index]
350
+
351
+ if agent_interactive.function_map:
352
+ capabilities = [key for key in agent_interactive.function_map.keys()]
353
+ return gr.CheckboxGroup(
354
+ label='插件',
355
+ value=capabilities,
356
+ choices=capabilities,
357
+ interactive=False,
358
+ )
359
+
360
+ else:
361
+ return gr.CheckboxGroup(
362
+ label='插件',
363
+ value=[],
364
+ choices=[],
365
+ interactive=False,
366
+ )