Skip to content

Commit 8662bf7

Browse files
update the gradio version of function call agent (#2785)
#2778 --------- Co-authored-by: Ekaterina Aidova <ekaterina.aidova@intel.com>
1 parent a331785 commit 8662bf7

File tree

5 files changed

+193
-151
lines changed

5 files changed

+193
-151
lines changed

.ci/ignore_treon_docker.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ notebooks/llm-rag-langchain/llm-rag-langchain-genai.ipynb
8484
notebooks/ltx-video/ltx-video.ipynb
8585
notebooks/outetts-text-to-speech/outetts-text-to-speech.ipynb
8686
notebooks/phi-4-multimodal/phi-4-multimodal.ipynb
87-
notebooks/glm4-v/glm4-v.ipynb
87+
notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb
88+
notebooks/glm4-v/glm4-v.ipynb

.ci/skipped_notebooks.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@
499499
- macos-13
500500
- ubuntu-22.04
501501
- windows-2019
502+
- notebook: notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb
503+
skips:
504+
- python:
505+
- '3.9'
506+
- os:
507+
- macos-13
502508
- notebook: notebooks/glm4-v/glm4-v.ipynb
503509
skips:
504510
- os:
@@ -510,4 +516,4 @@
510516
- os:
511517
- windows-2019
512518
- python:
513-
- '3.12'
519+
- '3.12'

notebooks/llm-agent-functioncall/gradio_helper.py

+144-86
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import os
12
from pathlib import Path
23
import requests
34
from PIL import Image
45
from typing import List
56
from qwen_agent.llm.schema import CONTENT, ROLE, USER, Message
67
from qwen_agent.gui.utils import convert_history_to_chatbot
7-
from qwen_agent.gui.gradio import gr, mgr
8+
from qwen_agent.gui.gradio_dep import gr, mgr
89
from qwen_agent.gui import WebUI
910

1011

@@ -30,100 +31,157 @@
3031

3132

3233
class OpenVINOUI(WebUI):
33-
def request_cancel(self):
34-
self.agent_list[0].llm.ov_model.request.cancel()
35-
36-
def clear_history(self):
37-
return []
38-
39-
def add_text(self, _input, _chatbot, _history):
40-
_history.append(
41-
{
42-
ROLE: USER,
43-
CONTENT: [{"text": _input}],
44-
}
45-
)
46-
_chatbot.append([_input, None])
47-
yield gr.update(interactive=False, value=None), _chatbot, _history
48-
49-
def run(
50-
self,
51-
messages: List[Message] = None,
52-
share: bool = False,
53-
server_name: str = None,
54-
server_port: int = None,
55-
**kwargs,
56-
):
34+
"""A Common chatbot application for agent."""
35+
36+
def run(self,
37+
messages: List[Message] = None,
38+
share: bool = False,
39+
server_name: str = None,
40+
server_port: int = None,
41+
concurrency_limit: int = 10,
42+
enable_mention: bool = False,
43+
**kwargs):
5744
self.run_kwargs = kwargs
5845

46+
from qwen_agent.gui.gradio_dep import gr, mgr, ms
47+
48+
customTheme = gr.themes.Default(
49+
primary_hue=gr.themes.utils.colors.blue,
50+
radius_size=gr.themes.utils.sizes.radius_none,
51+
)
52+
5953
with gr.Blocks(
60-
theme=gr.themes.Soft(),
61-
css=".disclaimer {font-variant-caps: all-small-caps;}",
62-
) as self.demo:
63-
gr.Markdown("""<h1><center>OpenVINO Qwen Agent </center></h1>""")
54+
css=os.path.join(os.path.dirname(__file__), 'assets/appBot.css'),
55+
theme=customTheme,
56+
) as demo:
6457
history = gr.State([])
58+
with ms.Application():
59+
with gr.Row(elem_classes='container'):
60+
with gr.Column(scale=4):
61+
chatbot = mgr.Chatbot(value=convert_history_to_chatbot(messages=messages),
62+
avatar_images=[
63+
self.user_config,
64+
self.agent_config_list,
65+
],
66+
height=900,
67+
avatar_image_width=80,
68+
flushing=False,
69+
show_copy_button=True,
70+
latex_delimiters=[{
71+
'left': '\\(',
72+
'right': '\\)',
73+
'display': True
74+
}, {
75+
'left': '\\begin{equation}',
76+
'right': '\\end{equation}',
77+
'display': True
78+
}, {
79+
'left': '\\begin{align}',
80+
'right': '\\end{align}',
81+
'display': True
82+
}, {
83+
'left': '\\begin{alignat}',
84+
'right': '\\end{alignat}',
85+
'display': True
86+
}, {
87+
'left': '\\begin{gather}',
88+
'right': '\\end{gather}',
89+
'display': True
90+
}, {
91+
'left': '\\begin{CD}',
92+
'right': '\\end{CD}',
93+
'display': True
94+
}, {
95+
'left': '\\[',
96+
'right': '\\]',
97+
'display': True
98+
}])
99+
100+
input = mgr.MultimodalInput(placeholder=self.input_placeholder,)
101+
102+
with gr.Column(scale=1):
103+
if len(self.agent_list) > 1:
104+
agent_selector = gr.Dropdown(
105+
[(agent.name, i) for i, agent in enumerate(self.agent_list)],
106+
label='Agents',
107+
info='Select an Agent',
108+
value=0,
109+
interactive=True,
110+
)
111+
112+
agent_info_block = self._create_agent_info_block()
113+
114+
agent_plugins_block = self._create_agent_plugins_block()
115+
116+
if self.prompt_suggestions:
117+
gr.Examples(
118+
label='Example questions',
119+
examples=self.prompt_suggestions,
120+
inputs=[input],
121+
)
122+
123+
if len(self.agent_list) > 1:
124+
agent_selector.change(
125+
fn=self.change_agent,
126+
inputs=[agent_selector],
127+
outputs=[agent_selector, agent_info_block, agent_plugins_block],
128+
queue=False,
129+
)
65130

66-
with gr.Row():
67-
with gr.Column(scale=4):
68-
chatbot = mgr.Chatbot(
69-
value=convert_history_to_chatbot(messages=messages),
70-
avatar_images=[
71-
self.user_config,
72-
self.agent_config_list,
73-
],
74-
height=900,
75-
avatar_image_width=80,
76-
flushing=False,
77-
show_copy_button=True,
131+
input_promise = input.submit(
132+
fn=self.add_text,
133+
inputs=[input, chatbot, history],
134+
outputs=[input, chatbot, history],
135+
queue=False,
78136
)
79-
with gr.Column():
80-
input = gr.Textbox(
81-
label="Chat Message Box",
82-
placeholder="Chat Message Box",
83-
show_label=False,
84-
container=False,
137+
138+
if len(self.agent_list) > 1 and enable_mention:
139+
input_promise = input_promise.then(
140+
self.add_mention,
141+
[chatbot, agent_selector],
142+
[chatbot, agent_selector],
143+
).then(
144+
self.agent_run,
145+
[chatbot, history, agent_selector],
146+
[chatbot, history, agent_selector],
85147
)
86-
with gr.Column():
87-
with gr.Row():
88-
submit = gr.Button("Submit", variant="primary")
89-
stop = gr.Button("Stop")
90-
clear = gr.Button("Clear")
91-
with gr.Column(scale=1):
92-
agent_interactive = self.agent_list[0]
93-
capabilities = [key for key in agent_interactive.function_map.keys()]
94-
gr.CheckboxGroup(
95-
label="Tools",
96-
value=capabilities,
97-
choices=capabilities,
98-
interactive=False,
99-
)
100-
with gr.Row():
101-
gr.Examples(self.prompt_suggestions, inputs=[input], label="Click on any example and press the 'Submit' button")
102-
103-
input_promise = submit.click(
104-
fn=self.add_text,
105-
inputs=[input, chatbot, history],
106-
outputs=[input, chatbot, history],
107-
queue=False,
108-
)
109-
input_promise = input_promise.then(
110-
self.agent_run,
111-
[chatbot, history],
112-
[chatbot, history],
113-
)
114-
input_promise.then(self.flushed, None, [input])
115-
stop.click(
116-
fn=self.request_cancel,
117-
inputs=None,
118-
outputs=None,
119-
cancels=[input_promise],
120-
queue=False,
121-
)
122-
clear.click(lambda: None, None, chatbot, queue=False).then(self.clear_history, None, history)
148+
else:
149+
input_promise = input_promise.then(
150+
self.agent_run,
151+
[chatbot, history],
152+
[chatbot, history],
153+
)
154+
155+
input_promise.then(self.flushed, None, [input])
156+
157+
demo.load(None)
158+
159+
demo.queue(default_concurrency_limit=concurrency_limit).launch(share=share,
160+
server_name=server_name,
161+
server_port=server_port)
162+
123163

124-
self.demo.load(None)
164+
def _create_agent_plugins_block(self, agent_index=0):
165+
from qwen_agent.gui.gradio_dep import gr
125166

126-
self.demo.launch(share=share, server_name=server_name, server_port=server_port)
167+
agent_interactive = self.agent_list[agent_index]
168+
169+
if agent_interactive.function_map:
170+
capabilities = [key for key in agent_interactive.function_map.keys()]
171+
return gr.CheckboxGroup(
172+
label='Tools',
173+
value=capabilities,
174+
choices=capabilities,
175+
interactive=False,
176+
)
177+
178+
else:
179+
return gr.CheckboxGroup(
180+
label='Tools',
181+
value=[],
182+
choices=[],
183+
interactive=False,
184+
)
127185

128186

129187
def make_demo(bot):

0 commit comments

Comments
 (0)