Skip to content

Commit

Permalink
Merge pull request #94 from DeepInsight-AI/new_pre
Browse files Browse the repository at this point in the history
V1.2.3
  • Loading branch information
LiChengxin007 committed Feb 7, 2024
2 parents f096e33 + bb84ffc commit 2f6bb0d
Show file tree
Hide file tree
Showing 58 changed files with 2,777 additions and 620 deletions.
83 changes: 68 additions & 15 deletions ai/agents/agent_instance_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

max_retry_times = CONFIG.max_retry_times
language_chinese = CONFIG.language_chinese
language_english = CONFIG.language_chinese
language_english = CONFIG.language_english
language_japanese = CONFIG.language_japanese
default_language_mode = CONFIG.default_language_mode
local_base_postgresql_info = CONFIG.local_base_postgresql_info
local_base_xls_info = CONFIG.local_base_xls_info
Expand Down Expand Up @@ -61,26 +62,21 @@ def __init__(
self.openai_proxy = None
self.db_id = db_id

def set_api_key(self, api_key, api_host=None):
def set_api_key(self, api_key, api_host=None, in_use=CONFIG.apikey_openai):
self.api_key = api_key
if api_host is not None:
# api_base = "https://api.openai.com/"
print('api_host: ', api_host)

if in_use == CONFIG.apikey_openai:
self.config_list_gpt4 = [
{
'model': 'gpt-4',
'api_key': api_key,
'api_base': api_host,
'api_type': 'openai',
},
]

self.config_list_gpt4_turbo = [
{
'model': 'gpt-4-1106-preview',
'api_key': self.api_key,
'api_base': api_host,

},
]
Expand All @@ -89,34 +85,82 @@ def set_api_key(self, api_key, api_host=None):
{
'model': 'gpt-3.5-turbo-1106',
'api_key': self.api_key,
'api_base': api_host,
},
]

if api_host is not None:
# api_base = "https://api.openai.com/"
print('api_host: ', api_host)
self.config_list_gpt4[0]['api_base'] = api_host
self.config_list_gpt4_turbo[0]['api_base'] = api_host
self.config_list_gpt35_turbo[0]['api_base'] = api_host

elif in_use == CONFIG.apikey_deepinsight:
self.config_list_gpt4 = [
{
'model': 'gpt-4',
'api_key': api_key,
},
]

else:
self.config_list_gpt4_turbo = [
{
'model': 'gpt-4-1106-preview',
'api_key': self.api_key,

},
]

self.config_list_gpt35_turbo = [
{
'model': 'gpt-3.5-turbo-1106',
'api_key': self.api_key,
},
]

if api_host is not None:
print('api_host: ', api_host)
self.config_list_gpt4[0]['api_base'] = api_host
self.config_list_gpt4_turbo[0]['api_base'] = api_host
self.config_list_gpt35_turbo[0]['api_base'] = api_host

elif in_use == CONFIG.apikey_azure:
self.config_list_gpt4 = [
{
'model': 'gpt-4',
'api_key': api_key,
'api_type': 'azure',
'model': 'gpt-4',
'api_version': "2023-07-01-preview",
},
]

self.config_list_gpt4_turbo = [
{
'model': 'gpt-4-1106-preview',
'api_key': self.api_key,
'api_type': 'azure',
'model': 'gpt-4',
'api_version': "2023-07-01-preview",
},
]

self.config_list_gpt35_turbo = [
{
'model': 'gpt-3.5-turbo-1106',
'api_key': self.api_key,
'api_type': 'azure',
'model': 'gpt-4',
'api_version': "2023-07-01-preview",
},
]

if api_host is not None:
print('api_host: ', api_host)
self.config_list_gpt4[0]['api_base'] = api_host
self.config_list_gpt4_turbo[0]['api_base'] = api_host
self.config_list_gpt35_turbo[0]['api_base'] = api_host

self.gpt4_turbo_config = {
"seed": 42, # change the seed for different trials
"temperature": 0,
Expand Down Expand Up @@ -277,17 +321,19 @@ def get_agent_chart_presenter(self):
There are currently several types of charts that can be used, including line, column, area, pie, scanner, bubble, heatmap, box, and table.
For example, selecting a set of data to display in a column chart format and specifying the x and y axis data.
Usually, there can only be one set of x-axis data, while there can be multiple sets of y-axis data.
Hand over your code to the Executor for execution.
There can only be x-axis and y-axis mappings in columnMapping
In columnMapping, some or all data can be mapped.
Please check the SQL statement in context. If a gourp by is included in the SQL statement, the chart must use fields that are not designated as x-axis or y-axis as gourp by values.
There can only be one mapping on the x-axis, such as {"mon": "x"}.
There can be one or more mappings on the y-axis, such as {"prao": "y", "prbo": "y"}.
gourp by can only have one mapping, such as
{"fix":"series"}
The output should be formatted as a JSON instance that conforms to the JSON schema below, the JSON is a list of dict,
[
{"globalSeriesType":"box","columnMapping":{"mon":"x","prao":"y","prbo":"y","prco":"y"}}
{"globalSeriesType":"box","columnMapping":{"mon":"x","prao":"y","prbo":"y","prco":"y","fix":"series"}}
].
If there is no suitable chart, or if the user requests a table, use the table to display, and the returned results are as follows:
If there is no suitable chart, or if the user requests a table, use the table to display, and the returned results are as follows:
[
{"globalSeriesType": "table", "columnMapping": ""}
]
Expand Down Expand Up @@ -936,6 +982,7 @@ def get_agent_mysql_echart_assistant(self, use_cache=True, report_file_name=None
Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.
When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
If you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.
If you need to use %Y-%M to query the date or timestamp, please use %Y-%M. You cannot use %%Y-%%M.(For example you should use SELECT * FROM your_table WHERE DATE_FORMAT(your_date_column, '%Y-%M') = '2024-February'; instead of SELECT * FROM your_table WHERE DATE_FORMAT(your_date_column, '%%Y-%%M') = '2024-%%M';)
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.
Reply "TERMINATE" in the end when everything is done.
Expand Down Expand Up @@ -1218,6 +1265,12 @@ def set_language_mode(self, language_mode):
self.question_ask = ' 以下是我的问题,请用中文回答: '
self.quesion_answer_language = '用中文回答问题.'
self.data_analysis_error = '分析数据失败,请检查相关数据是否充分'

elif self.language_mode == language_japanese:
self.error_message_timeout = "申し訳ありませんが、今回のAI-GPTインターフェース呼び出しがタイムアウトしました。もう一度お試しください。"
self.question_ask = ' これが私の質問です。: '
self.quesion_answer_language = '日本語で質問に答える。'
self.data_analysis_error = 'データの分析に失敗しました。関連データが十分かどうかを確認してください。'

def set_base_csv_info(self, db_info):
csv_content = []
Expand Down
36 changes: 34 additions & 2 deletions ai/agents/agentchat/bi_proxy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from ai.backend.util.write_log import logger
from ai.backend.util.token_util import num_tokens_from_messages
import traceback
from ai.backend.util import base_util


try:
from termcolor import colored
Expand Down Expand Up @@ -1170,6 +1172,9 @@ async def run_chart_code(self, chart_code_str: str):
str_obj = ast.literal_eval(chart_code_str)
json_str = json.dumps(str_obj)

if not isinstance(str_obj, list) or not str_obj:
return "Failed to generate chart. Please make sure valid chart configuration is provided. Retry."

if str_obj[0].get("globalSeriesType") == 'table':
result_message = {
'state': 200,
Expand All @@ -1182,6 +1187,31 @@ async def run_chart_code(self, chart_code_str: str):

}
else:

###################
axis_x_num = 0
axis_y_num = 0

for config in str_obj:
if 'columnMapping' in config and isinstance(config['columnMapping'], dict) and config[
'columnMapping']:
for variable, axis in config['columnMapping'].items():
print('axis :', axis)
if axis in ["x"]:
axis_x_num = axis_x_num + 1
elif axis in ["y"]:
axis_y_num = axis_y_num + 1
else:
return "Failed to generate chart. Please make sure valid chart configuration is provided. Try again with the original chart type."

if not axis_x_num == 1:
error_mess = "There should be one and only one X-axis in the chart."
return error_mess + '\n' + "Failed to generate chart. Please make sure valid chart configuration is provided. Try again with the original chart type."
elif not axis_y_num > 0:
error_mess = "There should be at least one Y-axis in the chart."
return error_mess + '\n' + "Failed to generate chart. Please make sure valid chart configuration is provided. Try again with the original chart type."
###################

result_message = {
'state': 200,
'receiver': 'bi',
Expand Down Expand Up @@ -1455,9 +1485,11 @@ async def run_echart_code(self, chart_code_str: str, name: str):
current_timestamp = int(time.time())
# print(current_timestamp)

chart_code_str = chart_code_str.replace("\n", "")
if base_util.is_json(chart_code_str):
str_obj = json.loads(chart_code_str)
else:
str_obj = ast.literal_eval(chart_code_str)

str_obj = ast.literal_eval(chart_code_str)
json_str = json.dumps(str_obj)

result_message = {
Expand Down
19 changes: 19 additions & 0 deletions ai/agents/agentchat/chart_presenter_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,27 @@ async def generate_reply(
'function_call': {'name': 'bi_run_chart_code',
'arguments': json.dumps(arguments)}}
return suggest_function
else:
# 在这里添加第三种方法的代码,匹配 JSON 大括号,提取相关数据
extracted_json = self.extract_json_data(reply)

# 判断是否提取到了有效的 JSON 数据
if extracted_json and 'globalSeriesType' in extracted_json:
# 提取到有效的数据,构建 JSON 字典
arguments = {"chart_code_str": json.dumps(extracted_json)}

suggest_function = {
'role': 'assistant',
'content': None,
'function_call': {
'name': 'bi_run_chart_code',
'arguments': json.dumps(arguments)
}
}
return suggest_function
except Exception as e:
traceback.print_exc()


return reply
# return suggest_function
Expand Down
6 changes: 6 additions & 0 deletions ai/agents/agentchat/python_proxy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,16 @@ def generate_code_execution_reply(
exitcode2str = "execution succeeded" if exitcode == 0 else "execution failed"

length = 10000
length1 = 10001
if not str(logs).__contains__('echart_name'):
if len(logs) > length:
print(' ++++++++++ Length exceeds 10000 characters limit, cropped +++++++++++++++++')
logs = logs[:length]
else:
if len(logs) > length1:
print(' ++++++++++ Length exceeds 10001 characters limit, cropped +++++++++++++++++')
logs = "The echarts code is too long, please simplify the code or data (for example, only keep two decimal places), and ensure that the echarts code length does not exceed 10001"


return True, f"exitcode: {exitcode} ({exitcode2str})\nCode output: {logs}"

Expand Down
24 changes: 14 additions & 10 deletions ai/agents/agentchat/task_selector_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from ai.backend.util.write_log import logger
from .conversable_agent import ConversableAgent
from .agent import Agent

from ai.backend.base_config import agents_functions
import re

class TaskSelectorAgent(ConversableAgent):
"""(In preview) A class for generic conversable agents which can be configured as assistant or user proxy.
Expand Down Expand Up @@ -143,20 +144,23 @@ async def generate_reply(
# {"qustion_message":"\nWhat is the most common house layout in the dataset?"}
# **********************************************
print('messages[-1][content] :', messages[-1]['content'])

# suggest_function = {'role': 'assistant', 'content': None, 'function_call': {'name': 'task_base',
# 'arguments': '{"qustion_message":"\\nWhat is the most common house layout in the dataset?"}'}}
# Check if reply is in agents_functions
if reply in agents_functions:
suggest_function = {'role': 'assistant', 'content': None, 'function_call': {'name': reply,
'arguments': '{"qustion_message":"' + str(
messages[-1][
'content']) + '"}'}}

suggest_function = {'role': 'assistant', 'content': None, 'function_call': {'name': reply,
'arguments': '{"qustion_message":"' + str(
messages[-1][
'content']) + '"}'}}
# {"qustion_message": " """ + str(messages[-1]['content']) + """"}

# {"qustion_message": " """ + str(messages[-1]['content']) + """"}
print('reply : ', reply)

print('reply : ', reply)
# return reply
return suggest_function

# return reply
return suggest_function

# return messages
return self._default_auto_reply
3 changes: 1 addition & 2 deletions ai/agents/oai/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ def _get_response(cls, config: Dict, raise_on_ratelimit_or_timeout=False, use_ca
print('_get_response function +++++++++++++++++++')
# print("config :", config)
# print("config.get('api_base')", config.get('api_base'))
if config.get('api_base') is not None:
# and str(config['api_base']).__contains__('apiserver.deep-thought.io')\
if config.get('api_base') is not None and str(config['api_base']).__contains__('apiserver.deep-thought.io'):

if config.get('functions'):
data = {
Expand Down
Loading

0 comments on commit 2f6bb0d

Please sign in to comment.