Skip to content

Commit

Permalink
Merge branch 'TsinghuaDatabaseGroup:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
curtis-sun authored Mar 8, 2024
2 parents c3088a7 + a94d0f5 commit d26e89a
Show file tree
Hide file tree
Showing 15 changed files with 694 additions and 673 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ We thank all the contributors to this project. Do not hesitate if you would like
## Contact Information
👏🏻Welcome to our wechat group!
<div align= "center">
<img src="img/chat-2024-02-29.jpg" width="400px">
<img src="https://demo-show-1258191275.cos.ap-beijing.myqcloud.com/dbgpt_group.jpg" width="400px">
</div>

<!-- ## ⭐️ Star History
Expand Down
2 changes: 1 addition & 1 deletion README_Chinese.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,5 @@ https://github.com/chatchat-space/Langchain-Chatchat
## 联系我们
👏🏻欢迎加入我们的微信群
<div align= "center">
<img src="img/chat-2024-02-29.jpg" width="400px">
<img src="https://demo-show-1258191275.cos.ap-beijing.myqcloud.com/dbgpt_group.jpg" width="400px">
</div>
544 changes: 0 additions & 544 deletions alert_results/Qwen1.5-14B-Chat/1709608374.jsonl

This file was deleted.

544 changes: 544 additions & 0 deletions alert_results/Qwen1.5-14B-Chat/1709713292.jsonl

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion multiagents/agent_conf/config_qwen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ prompts:
-你的分析必须基于以下步骤:
首先使用工具"whether_is_abnormal_metric"判断是否有异常指标。
其次使用工具"match_diagnose_knowledge"进行故障分析。
根据上面几步结果,只要还能够继续往下深入诊断,就使用工具优化查询或者优化索引。
根据上面几步结果,只要还能够继续往下深入诊断,就使用工具优化查询或者优化索引,工具名必须是[${tool_choice}]之一
最后,基于上述所有结果,给出最终的根因和解决方案,按照如下模板,不要说多余的话。其中Final Answer是字典格式:
Thought: 我现在知道异常的根因了
Final Answer: {"diagnose": "你诊断的根因", "solution": "你得到的最优解决方案", "knowledge": "你参考的诊断知识"}
Expand Down
9 changes: 2 additions & 7 deletions multiagents/agent_conf/qwen_output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,10 @@ def parse(self, output) -> Union[AgentAction, AgentFinish]:

elif i < n: # 有Thought / Final Answer
assert j == -1 and k == -1
final_answer = text[n + len(special_answer_token):]
# 防止生成了final answer后还乱说其他的,可能还会有问题 TODO
# left = final_answer.find("{")
# right = final_answer.find("}")
# final_answer = final_answer[left:right + 1]
# final_answer = re.match(r"Final Answer: ({.*?})", final_answer, flags=re.DOTALL)
final_answer = text[n + len(special_answer_token):].strip()

try:

# final_answer = re.match(r"({.*?})", final_answer, flags=re.DOTALL).group()
final_answer = json.loads(final_answer)
except:
print("Error in parsing diagnosis results")
Expand Down
31 changes: 3 additions & 28 deletions multiagents/agents/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,7 @@ def initialize_report(self):

def update_diagnosis(self):
if self.language == 'zh':
prompt = f'''你正在撰写故障诊断报告。请根据上述审查建议,对下列根因分析过程进行精炼。原始根因分析过程如下:
{self.report["root cause"]}
请注意:
1. 输出必须是markdown格式。
2. 不要说多余的内容。
3. 只精炼根因分析过程,不要提任何和解决方案有关的内容。
'''
prompt = f'''你正在撰写故障诊断报告,现在需要给出最终的根因分析。请参考上述审查建议,并参考下列根因分析:\n{self.report["root cause"]} \n请注意:\n1. 输出必须是markdown格式,但不要说类似"这是markdown格式"等多余的内容。\n2. 只给出根因分析,不要提任何和解决方案有关的内容。\n'''
else:
prompt = "You are writing a report. Please give the refined root cause analysis based on the above review advice. The root cause analysis is as follows:\n" + self.report["root cause"] + "\n ===== \n Note 1. the output should be in markdown format.\n2. do not any additional content like 'Sure' and 'I will refine the anomaly diagnosis description based on the above advice. 3. Do not add anything about solutions!!!'\n"

Expand Down Expand Up @@ -124,17 +117,7 @@ def add_diagnosis_labels(self):
labels += root_causes[cause]

if self.language == 'zh':
prompt = f"""以下是一段根因分析过程:
{self.report["root cause"]}
以下是以列表形式列出的一些根因的标签:
{labels}
请基于根因分析过程的描述,选出对应的标签。请注意:
1. 输出必须是列表格式,不要附加任何多余内容。
2. 输出应严格排除根因分析中没有提及的标签。
3. 如果没有相关标签,请返回None。
"""
prompt = f"""以下是一段根因分析过程:\n{self.report["root cause"]}\n以下是以列表形式列出的一些根因的标签:\n{labels}\n请基于根因分析过程的描述,选出对应的标签。\n请注意:\n1. 输出必须是列表格式,不要附加任何多余内容。\n2. 输出应严格排除根因分析中没有提及的标签。\n3. 如果没有相关标签,请返回None。\n"""
else:
prompt = "Based on the description\n" + self.report["root cause"] + "\n\n Output all the labels mentioned in the description. The available labels are \n" + str(labels) + "===== \n Note 1. the output should be in list format. And do not output any additional information (output \"None\" if no label mentioned in the description)\n2. the output should strictly exclude labels not mentioned in the description."

Expand All @@ -153,15 +136,7 @@ def add_diagnosis_labels(self):

def update_solutions(self):
if self.language == 'zh':
prompt = f"""
你正在撰写故障诊断报告。请根据上述审查建议,对下列解决方案进行优化。原始解决方案如下:
{self.report["solutions"]}
请注意:
1. 输出必须是markdown格式。
2. 不要说多余的内容。
3. 只优化解决方案,不要提任何和根因分析有关的内容。
"""
prompt = f"""你正在撰写故障诊断报告,现在需要给出最终的解决方案。请参考上述审查建议,并参考下列解决方案:\n{self.report["solutions"]} \n请注意:\n1. 输出必须是markdown格式,但不要说类似"这是markdown格式"等多余的内容。\n2. 只给出解决方案,不要提任何和根因分析有关的内容。\n"""
else:
prompt = "You are writing a report. Please optimize the following solutions based on the above review advice. The solutions are:\n" + self.report["solutions"] + "\n ===== \n Note 1. the output should be in markdown format.\n2. do not any additional content like 'Sure' and 'I will refine the solutions based on the above advice'. 3. Do not add anything about root causes!!!\n"

Expand Down
9 changes: 8 additions & 1 deletion multiagents/agents/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,15 @@ def _fill_prompt_template(
]
)

tools = tools.replace("{{", "{").replace("}}", "}")
# tools = tools.replace("{{", "{").replace("}}", "}")

tool_names = ", ".join([tool for tool in relevant_tools])
tool_choice = ", ".join(
[
tool for tool in relevant_tools
if tool not in ["match_diagnose_knowledge", "whether_is_abnormal_metric"]
]
)

self.role_description = Template(self.role_description).safe_substitute(
{
Expand All @@ -305,6 +311,7 @@ def _fill_prompt_template(
"tools": tools,
"tool_names": tool_names,
"tool_observation": "\n".join(tool_observation),
"tool_choice": tool_choice
}

return Template(self.prompt_template).safe_substitute(input_arguments)
Expand Down
25 changes: 20 additions & 5 deletions multiagents/environments/dba.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,11 @@ async def step(
})

print(f'<flow>{{"title": "专家诊断", "content": "", "expertData": {json.dumps(expert_data)}, "isCompleted": 0, "isRuning": 1}}</flow>')
expert_select_desc = "Based on the task description, I decide to select the following experts to diagnose the problem:\n" + \
"\n".join([expert.name for expert in selected_experts])
if self.reporter.language == "zh":
expert_select_desc = "基于上述任务描述,我决定选择下列专家来诊断:\n"
else:
expert_select_desc = "Based on the task description, I decide to select the following experts to diagnose the problem:\n"
expert_select_desc += "\n".join([expert.name for expert in selected_experts])

self.reporter.record["anomalyAnalysis"]["RoleAssigner"]["messages"].append(
{"data": expert_select_desc, "time": time.strftime("%H:%M:%S", time.localtime())})
Expand Down Expand Up @@ -400,8 +403,12 @@ async def decision_making(

root_causes = str(diag["root cause"]).replace('\n', '<br>')

if self.reporter.language == "zh":
add_str = f"<br>由{diag['sender']}判断的根因是:<br>"
else:
add_str = f"<br>The root causes identified by {diag['sender']}:<br>"
self.reporter.report["root cause"] = str(
self.reporter.report["root cause"]) + f"<br>The root causes identified by {diag['sender']}:<br>" + root_causes + "<br>"
self.reporter.report["root cause"]) + add_str + root_causes + "<br>"

# # solution = str(diag["solutions"]).replace("\"","")
# # solution = solution.replace("\\n", "\n")
Expand All @@ -421,11 +428,19 @@ async def decision_making(

solutions = str(diag["solutions"]).replace('\n', '<br>')

if self.reporter.language == "zh":
add_str = f"<br>由{diag['sender']}推荐的解决方案是:<br>"
else:
add_str = f"<br>The solutions recommended by {diag['sender']}:<br>"
self.reporter.report["solutions"] = str(
self.reporter.report["solutions"]) + f"<br>The solutions recommended by {diag['sender']}:<br>" + solutions + "<br>"
self.reporter.report["solutions"]) + add_str + solutions + "<br>"

if self.reporter.language == "zh":
add_str = f"<br>{i+1}. {diag['sender']}的诊断过程是:<br>"
else:
add_str = f"<br>{i+1}. The diagnosis process of {diag['sender']}:<br>"
self.reporter.report["diagnosis process"] = str(
self.reporter.report["diagnosis process"]) + f"<br>{i+1}. The diagnosis process of {diag['sender']}:<br>"
self.reporter.report["diagnosis process"]) + add_str

for i, m_response in enumerate(diag["diagnosis process"]):
if m_response['role'] != "user":
Expand Down
25 changes: 14 additions & 11 deletions multiagents/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
if TYPE_CHECKING:
from agents import BaseAgent

LANGUAGE = "en"

def load_llm(llm_config: Dict):
llm_type = llm_config.pop("llm_type", "xxxx")
Expand All @@ -31,28 +32,28 @@ def load_memory(memory_config: Dict):


def load_tools(tool_config: List[Dict], max_api_num, agent_name):

if len(tool_config) == 0:
return []

caller = APICaller()

for tool in tool_config:

api_module = importlib.import_module(f"""multiagents.tools.{tool["tool_name"]}.api""")
register_functions_from_module(api_module, caller, max_api_num, agent_name)

return caller


def load_environment(env_config: Dict) -> BaseEnvironment:

env_type = env_config.pop("env_type", "basic")
return env_registry.build(env_type, **env_config)


def load_agent(agent_config: Dict) -> BaseAgent:

agent_type = agent_config.pop("agent_type", "conversation")
agent = agent_registry.build(agent_type, **agent_config)

Expand All @@ -61,19 +62,21 @@ def load_agent(agent_config: Dict) -> BaseAgent:

def prepare_task_config(task, args):
"""Read the yaml config of the given task in `tasks` directory."""

task_path = os.path.join(os.path.dirname(__file__), task)
config_path = os.path.join(task_path, args.config_file)

if not os.path.exists(task_path):
raise ValueError(f"Config {task} not found.")
if not os.path.exists(config_path):
raise ValueError(
"You should include the config.yaml file in the task directory"
)

task_config = yaml.safe_load(open(config_path,encoding='utf8'))


task_config = yaml.safe_load(open(config_path, encoding='utf8'))
global LANGUAGE
LANGUAGE = task_config.get("language", LANGUAGE)

# Build the output parser
parser = output_parser_registry.build(task)
task_config["output_parser"] = parser
Expand All @@ -85,7 +88,7 @@ def prepare_task_config(task, args):
agent_configs["tool_memory"] = load_memory(agent_configs["tool_memory"])
llm = load_llm(agent_configs.get("llm", "xxxx"))
agent_configs["llm"] = llm

agent_configs["tools"] = load_tools(agent_configs.get("tools", []), args.max_api_num, agent_configs['name'])

agent_configs["name"] = agent_configs['name']
Expand Down
43 changes: 18 additions & 25 deletions multiagents/llms/qwen_vllm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import openai
from multiagents.llms import llm_registry
from multiagents.llms.base import BaseChatModel
import time
Expand All @@ -7,6 +6,7 @@
import os
import re
import json
import requests


def remove_charts(text):
Expand All @@ -19,25 +19,19 @@ def remove_charts(text):
@llm_registry.register("qwen1.5_vllm")
class QwenVllmChat(BaseChatModel):
conversation_history: List = []
client: None = None
model_name_or_path: str = "Qwen1.5-14B-Chat"
generate_cfg: Dict = {'stop': ['Observation:', 'Observation:\n'], 'temperature': 0}
url: str = ""
model_name_or_path: str = ""
generate_cfg: Dict = {
'stop': ['Observation:', 'Observation:\n', '<|im_end|>'],
'temperature': 0,
'skip_special_tokens': False,
}

def __init__(self, **kwargs):
super().__init__(**kwargs)
import openai

assert not openai.__version__.startswith('0.')
api_kwargs = {}
api_key = ONLINE_LLM_MODEL["qwen1.5-vllm"].get("api_key", None)
api_base = ONLINE_LLM_MODEL["qwen1.5-vllm"].get("api_base_url", None)
if api_base:
api_kwargs['base_url'] = api_base
if api_key:
api_kwargs['api_key'] = api_key

self.client = openai.OpenAI(**api_kwargs)
self.model_name_or_path = ONLINE_LLM_MODEL["qwen1.5-vllm"].get("model_name", None)

self.url = ONLINE_LLM_MODEL["qwen1.5-vllm"]["api_base_url"] + "/chat/completions"
self.model_name_or_path = ONLINE_LLM_MODEL["qwen1.5-vllm"]["model_name"]
self.generate_cfg.update(kwargs)

@staticmethod
Expand Down Expand Up @@ -79,16 +73,15 @@ def parse(self):
return {"role": "assistant", "content": output, "time": time.strftime("%H:%M:%S", time.localtime())}

def chat(self, messages, **kwargs):
generate_cfg = self.generate_cfg.copy()
generate_cfg.update(kwargs)
cfg = self.generate_cfg.copy()
cfg.update(kwargs)
cfg["model"] = self.model_name_or_path
cfg["messages"] = messages

response = self.client.chat.completions.create(
model=self.model_name_or_path,
messages=messages,
**generate_cfg
)
response = requests.post(self.url, json=cfg)
response = json.loads(response.text)

output = response.choices[0].message.content
output = response['choices'][0]['message']['content']

if not os.path.exists(r"saved_msg_qwen"):
os.mkdir(r"saved_msg_qwen")
Expand Down
16 changes: 10 additions & 6 deletions multiagents/tools/index_advisor/api.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import os

from configs import POSTGRESQL_CONFIG
from multiagents.tools.index_advisor.index_selection.selection_utils.postgres_dbms import PostgresDatabaseConnector
from multiagents.tools.index_advisor.index_selection.selection_utils import selec_com
from multiagents.tools.index_advisor.configs import get_index_result
from multiagents.tools.metrics import advisor
from multiagents.tools.metrics import get_workload_statistics
import ast
from multiagents.initialization import LANGUAGE

FUNCTION_DEFINITION = {
"optimize_index_selection": {
"name": "optimize_index_selection",
"description": "执行索引优化,不需要输入参数,返回推荐的索引",
"parameters": {}
"description":
"使用索引选择算法返回推荐的索引。" if LANGUAGE == "zh"
else "returns the recommended index by running the index selection algorithm.",
"parameters": {'type': 'object', 'properties': {}}
}
}

Expand Down Expand Up @@ -47,7 +48,7 @@ def optimize_index_selection(**kwargs):

databases[database_name].append({"sql": query_template["sql"], "frequency": query_template["calls"]})

index_advice = f"Recommended indexes: \n"
index_advice = "推荐的索引是:\n" if LANGUAGE == "zh" else f"Recommended indexes: \n"

for dbname in databases:

Expand All @@ -74,6 +75,9 @@ def optimize_index_selection(**kwargs):
indexes, total_no_cost, total_ind_cost = get_index_result(advisor, workload, connector, columns)

if len(indexes) != 0:
index_advice += f"\t For {dbname}, the recommended indexes are: {indexes}, which reduces cost from {total_no_cost} to {total_ind_cost}.\n"
index_advice += (
f"对数据库{dbname},推荐的索引是:{indexes},cost从原来的{total_no_cost}减少到{total_ind_cost}\n" if LANGUAGE == "zh"
else f"\t For {dbname}, the recommended indexes are: {indexes}, which reduces cost from {total_no_cost} to {total_ind_cost}.\n"
)

return index_advice
Loading

0 comments on commit d26e89a

Please sign in to comment.