-
Notifications
You must be signed in to change notification settings - Fork 416
/
assistant_add_custom_tool.py
102 lines (81 loc) · 2.7 KB
/
assistant_add_custom_tool.py
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
"""An image generation agent implemented by assistant"""
import json
import os
import urllib.parse
import json5
from qwen_agent.agents import Assistant
from qwen_agent.gui import WebUI
from qwen_agent.tools.base import BaseTool, register_tool
ROOT_RESOURCE = os.path.join(os.path.dirname(__file__), 'resource')
# Add a custom tool named my_image_gen:
@register_tool('my_image_gen')
class MyImageGen(BaseTool):
description = 'AI painting (image generation) service, input text description, and return the image URL drawn based on text information.'
parameters = [{
'name': 'prompt',
'type': 'string',
'description': 'Detailed description of the desired image content, in English',
'required': True,
}]
def call(self, params: str, **kwargs) -> str:
prompt = json5.loads(params)['prompt']
prompt = urllib.parse.quote(prompt)
return json.dumps(
{'image_url': f'https://image.pollinations.ai/prompt/{prompt}'},
ensure_ascii=False,
)
def init_agent_service():
llm_cfg = {'model': 'qwen-max'}
system = ("According to the user's request, you first draw a picture and then automatically "
'run code to download the picture and select an image operation from the given document '
'to process the image')
tools = [
'my_image_gen',
'code_interpreter',
] # code_interpreter is a built-in tool in Qwen-Agent
bot = Assistant(
llm=llm_cfg,
name='AI painting',
description='AI painting service',
system_message=system,
function_list=tools,
files=[os.path.join(ROOT_RESOURCE, 'doc.pdf')],
)
return bot
def test(query: str = 'draw a dog'):
# Define the agent
bot = init_agent_service()
# Chat
messages = [{'role': 'user', 'content': query}]
for response in bot.run(messages=messages):
print('bot response:', response)
def app_tui():
# Define the agent
bot = init_agent_service()
# Chat
messages = []
while True:
query = input('user question: ')
messages.append({'role': 'user', 'content': query})
response = []
for response in bot.run(messages=messages):
print('bot response:', response)
messages.extend(response)
def app_gui():
# Define the agent
bot = init_agent_service()
chatbot_config = {
'prompt.suggestions': [
'画一只猫的图片',
'画一只可爱的小腊肠狗',
'画一幅风景画,有湖有山有树',
]
}
WebUI(
bot,
chatbot_config=chatbot_config,
).run()
if __name__ == '__main__':
# test()
# app_tui()
app_gui()