forked from QwenLM/Qwen-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact_data_analysis.py
More file actions
99 lines (81 loc) · 3.19 KB
/
react_data_analysis.py
File metadata and controls
99 lines (81 loc) · 3.19 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
# 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.
"""A data analysis example implemented by assistant"""
import os
from pprint import pprint
from typing import Optional
from qwen_agent.agents import ReActChat
from qwen_agent.gui import WebUI
ROOT_RESOURCE = os.path.join(os.path.dirname(__file__), 'resource')
def init_agent_service():
llm_cfg = {
# 'model': 'Qwen/Qwen1.5-72B-Chat',
# 'model_server': 'https://api.together.xyz',
# 'api_key': os.getenv('TOGETHER_API_KEY'),
'model': 'qwen-max',
'model_server': 'dashscope',
'api_key': os.getenv('DASHSCOPE_API_KEY'),
}
tools = ['code_interpreter']
bot = ReActChat(llm=llm_cfg,
name='code interpreter',
description='This agent can run code to solve the problem',
function_list=tools)
return bot
def test(query: str = 'pd.head the file first and then help me draw a line chart to show the changes in stock prices',
file: Optional[str] = os.path.join(ROOT_RESOURCE, 'stock_prices.csv')):
# Define the agent
bot = init_agent_service()
# Chat
messages = []
if not file:
messages.append({'role': 'user', 'content': query})
else:
messages.append({'role': 'user', 'content': [{'text': query}, {'file': file}]})
for response in bot.run(messages):
pprint(response, indent=2)
def app_tui():
# Define the agent
bot = init_agent_service()
# Chat
messages = []
while True:
# Query example: pd.head the file first and then help me draw a line chart to show the changes in stock prices
query = input('user question: ')
# File example: resource/stock_prices.csv
file = input('file url (press enter if no file): ').strip()
if not query:
print('user question cannot be empty!')
continue
if not file:
messages.append({'role': 'user', 'content': query})
else:
messages.append({'role': 'user', 'content': [{'text': query}, {'file': file}]})
response = []
for response in bot.run(messages):
print('bot response:', response)
messages.extend(response)
def app_gui():
bot = init_agent_service()
chatbot_config = {
'prompt.suggestions': [{
'text': 'pd.head the file first and then help me draw a line chart to show the changes in stock prices',
'files': [os.path.join(ROOT_RESOURCE, 'stock_prices.csv')]
}, 'Draw a line graph y=x^2']
}
WebUI(bot, chatbot_config=chatbot_config).run()
if __name__ == '__main__':
# test()
# app_tui()
app_gui()