forked from QwenLM/Qwen-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassistant_qwen3.py
More file actions
132 lines (117 loc) · 4.42 KB
/
Copy pathassistant_qwen3.py
File metadata and controls
132 lines (117 loc) · 4.42 KB
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
# Copyright 2023 The Qwen team, Alibaba Group. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An agent implemented by assistant with qwen3"""
import os # noqa
from qwen_agent.agents import Assistant
from qwen_agent.gui import WebUI
from qwen_agent.utils.output_beautify import typewriter_print
def init_agent_service():
llm_cfg = {
# Use the model service provided by DashScope:
'model': 'qwen3-235b-a22b',
'model_type': 'qwen_dashscope',
# 'generate_cfg': {
# # When using the Dash Scope API, pass the parameter of whether to enable thinking mode in this way
# 'enable_thinking': True,
# },
}
# llm_cfg = {
# # Use the OpenAI-compatible model service provided by DashScope:
# 'model': 'qwen3-235b-a22b',
# 'model_server': 'https://dashscope.aliyuncs.com/compatible-mode/v1',
# 'api_key': os.getenv('DASHSCOPE_API_KEY'),
#
# # 'generate_cfg': {
# # # When using Dash Scope OAI API, pass the parameter of whether to enable thinking mode in this way
# # 'extra_body': {
# # 'enable_thinking': False
# # },
# # },
# }
# llm_cfg = {
# # Use your own model service compatible with OpenAI API by vLLM/SGLang:
# 'model': 'Qwen/Qwen3-32B',
# 'model_server': 'http://localhost:8000/v1', # api_base
# 'api_key': 'EMPTY',
#
# 'generate_cfg': {
# # When using vLLM/SGLang OAI API, pass the parameter of whether to enable thinking mode in this way
# 'extra_body': {
# 'chat_template_kwargs': {'enable_thinking': False}
# },
#
# # Add: When the content is `<think>this is the thought</think>this is the answer`
# # Do not add: When the response has been separated by reasoning_content and content
# # This parameter will affect the parsing strategy of tool call
# # 'thought_in_content': True,
# },
# }
tools = [
{
'mcpServers': { # You can specify the MCP configuration file
'time': {
'command': 'uvx',
'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
},
'fetch': {
'command': 'uvx',
'args': ['mcp-server-fetch']
}
}
},
'code_interpreter', # Built-in tools
]
bot = Assistant(llm=llm_cfg,
function_list=tools,
name='Qwen3 Tool-calling Demo',
description="I'm a demo using the Qwen3 tool calling. Welcome to add and play with your own tools!")
return bot
def test(query: str = 'What time is it?'):
# Define the agent
bot = init_agent_service()
# Chat
messages = [{'role': 'user', 'content': query}]
response_plain_text = ''
for response in bot.run(messages=messages):
response_plain_text = typewriter_print(response, response_plain_text)
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 = []
response_plain_text = ''
for response in bot.run(messages=messages):
response_plain_text = typewriter_print(response, response_plain_text)
messages.extend(response)
def app_gui():
# Define the agent
bot = init_agent_service()
chatbot_config = {
'prompt.suggestions': [
'What time is it?',
'https://github.com/orgs/QwenLM/repositories Extract markdown content of this page, then draw a bar chart to display the number of stars.'
]
}
WebUI(
bot,
chatbot_config=chatbot_config,
).run()
if __name__ == '__main__':
# test()
# app_tui()
app_gui()