From c4641fb0a58b02f84a5343ae27757ba7529b7d22 Mon Sep 17 00:00:00 2001 From: Xuanhe Zhou Date: Mon, 22 May 2023 14:33:51 +0800 Subject: [PATCH] add: agentverse for database diagnosis --- agentverse/tasks/__init__.py | 1 + agentverse/tasks/db_diag/README.md | 28 +++ agentverse/tasks/db_diag/config.yaml | 252 ++++++++++++++++++++++ agentverse/tasks/db_diag/output_parser.py | 40 ++++ 4 files changed, 321 insertions(+) create mode 100644 agentverse/tasks/db_diag/README.md create mode 100644 agentverse/tasks/db_diag/config.yaml create mode 100644 agentverse/tasks/db_diag/output_parser.py diff --git a/agentverse/tasks/__init__.py b/agentverse/tasks/__init__.py index 59d68c992..5acc48ae0 100644 --- a/agentverse/tasks/__init__.py +++ b/agentverse/tasks/__init__.py @@ -6,3 +6,4 @@ from .nlp_classroom_9players.output_parser import NlpClassroom9PlayersParser from .nlp_classroom_3players_withtool.output_parser import NlpClassroom3PlayersWithtoolParser from .nlp_classroom_9players_group.output_parser import NlpClassroom9PlayersGroupParser +from .db_diag.output_parser import DBDiag \ No newline at end of file diff --git a/agentverse/tasks/db_diag/README.md b/agentverse/tasks/db_diag/README.md new file mode 100644 index 000000000..2439a248f --- /dev/null +++ b/agentverse/tasks/db_diag/README.md @@ -0,0 +1,28 @@ +# Database Diagnosis + +Inherited from *nlp_classroom_3players_withtool_nolc* + +### Changes + +- Roles + + - *Chief DBA*: In charge of anomaly detection and diagnosis scheduling + - *XXX Agent*: In charge of a specific diagnosis region (e.g., Memory Agent handles problems of high memory usage) + +- Actions + + - We remove *RaiseHand* and *CallOn* actions, and each agent can annouce their analysis by order + +- Tools + + - We support the *[DB_diag](https://github.com/OpenBMB/BMTools/tree/main/bmtools/tools/db_diag)* tool in bmtools + +- Memory + + - In the prompt of each agent, we place the memory for *conversation history* before *tool_observation*, which is extremely important to conduct actions with close relations (e.g., diagnosis and speak) + - Use *chat_history* for memory_type + +- LLM + + - In current version, gpt-4 shows superior performance over text-davinci-003 + - Increase max_tokens for complex analysis tasks (e.g., 512 or 1024) \ No newline at end of file diff --git a/agentverse/tasks/db_diag/config.yaml b/agentverse/tasks/db_diag/config.yaml new file mode 100644 index 000000000..ef5362462 --- /dev/null +++ b/agentverse/tasks/db_diag/config.yaml @@ -0,0 +1,252 @@ +prompts: + chief_dba_format_prompt: &chief_dba_format_prompt |- + You are in a company whose databases meet anomalies and it depends on you to collaborate with other agents to diagnose the root causes. ${role_description} + + # Rules and Format Instructions for Response + + - When you are speaking, you must use the following format: + Thought: (your thought) + Action: Speak + Action Input: (what you want to say) + + - Most important, you need to call the tool api to get the start and end time of an anomaly + Thought: First, I need to obtain the start and end time of the anomaly + Action: obtain_start_and_end_time_of_anomaly + Action Input: {"input": "json dict string"} + Observation: {"start_time":"1684600070","end_time":"1684600074"} + + - Once an agent has explained the root causes he found, it is your responsibility to memorize the root causes. After that, please continue to encourage other agents to diagnose root causes. + + - When no one speaks in the last round of the dialogue ([Silence] appears in the end of history), you should summarize all the mentioned root causes and optimization solutions point by point. + + - You must follow the following format with three fields "Thought", "Action" and "Action Input" for your response in ANY case + Thought: (your thought) + Action: (an action name, it can be one of [Speak, obtain_start_and_end_time_of_anomaly]) + Action Input: (argument for the action) + + Here is the conversation history + ${chat_history} + + Remember to pay attention to the response format instructions, and strictly follow the rules specified above! + Based on the above history, what will you, ${agent_name}, do next? + + + # - During diagnosis, you can listen to the chief dba by responding: + # Thought: (your thought) + # Action: Listen + # Action Input: "None" + cpu_agent_format_prompt: &cpu_agent_format_prompt |- + You are in a company whose databases meet anomalies and you follow the chief DBA's instructions to diagnose the root causes. ${role_description} + + # Rules and Format Instructions for Response + + - During diagnosis, you have access to the following tools: + ${tools} + + =============== + + - You can respond as follows to use tool: + Thought: (your thought) + Action: (an action name, it can be one of [whether_is_abnormal_metric, cpu_diagnosis_agent, Speak], pay attention to the capitalization) + Action Input: (argument for the action) + + You can first determine abnormal metrics by using the tools, and use the following format: + Thought: Now that I have obtained the start and end time of the anomaly, check whether the CPU usage is abnormal during that time period. + Action: whether_is_abnormal_metric + Action Input: {"start_time": 1684646375, "end_time": 1684646378, "metric_name": "cpu_usage"} + + Next you can diagnose root causes by using the tools, and use the following format: + Thought:The CPU usage is abnormal, so I need to diagnose the cause of the anomaly using cpu_diagnosis_agent. + Action: cpu_diagnosis_agent + Action Input: {"start_time": 1684646375, "end_time": 1684646378} + + After you have got the observation from cpu_diagnosis_agent, announce it to the chief DBA, and use the following format: + Thought: I now know the root cause of the anomaly, and i need to report it to the chief DBA + Action: Speak + Action Input: (the root cause you found) + + =============== + + Here is the conversation history + ${chat_history} + + Here is the execution log of tools + ${tool_observation} + + Remember to pay attention to the response format instructions, and strictly follow the rules specified above! + Based on the above history, what will you, ${agent_name}, do next? + + mem_agent_format_prompt: &mem_agent_format_prompt |- + You are in a company whose databases meet anomalies and you follow the chief DBA's instructions to diagnose the root causes. ${role_description} + + # Rules and Format Instructions for Response + + - During diagnosis, you have access to the following tools: + ${tools} + + =============== + + - You can respond as follows to use tool: + Thought: (your thought) + Action: (an action name, it can be one of [whether_is_abnormal_metric, memory_diagnosis_agent, Speak], pay attention to the capitalization) + Action Input: (argument for the action) + + You can first determine abnormal metrics by using the tools, and use the following format: + Thought: Now that I have obtained the start and end time of the anomaly, check whether the memory usage is abnormal during that time period. + Action: whether_is_abnormal_metric + Action Input: {"start_time": 1684646375, "end_time": 1684646378, "metric_name": "memory_usage"} + + Next you can diagnose root causes by using the tools, and use the following format: + Thought:The memory usage is abnormal, so I need to diagnose the cause of the anomaly using memory_diagnosis_agent. + Action: memory_diagnosis_agent + Action Input: {"start_time": 1684646375, "end_time": 1684646378} + + After you have got the observation from memory_diagnosis_agent, announce it to the chief DBA, and use the following format: + Thought: I now know the root cause of the anomaly, and i need to report it to the chief DBA + Action: Speak + Action Input: (the root cause you found) + + =============== + + Here is the conversation history + ${chat_history} + + Here is the execution log of tools + ${tool_observation} + + Remember to pay attention to the response format instructions, and strictly follow the rules specified above! + Based on the above history, what will you, ${agent_name}, do next? + + summary_prompt: &summary_prompt | + Progressively summarize the lines of a record that you uses tools, which contains inputs for certain tools and the results returned by these tools. Based on the current summary, you need to summarize from the record the goals that the you intended to solve with each call to the tool, add it onto the previous summary, and eventually return a new summary. + + EXAMPLE + Current summary: + + New lines: + Thought: First, I need to obtain the start and end time of the anomaly. + Action: obtain_start_and_end_time_of_anomaly + Action Input: {"input": "json dict string"} + Observation: {"start_time":"1684600070","end_time":"1684600074"} + + New summary: + - I now know the start and end time of the anomaly. + + Current summary: + - I know the start and end time of the anomaly. + + New lines: + Thought: Now that I have the start and end time of the anomaly, I need to diagnose the causes of the anomaly + Action: whether_is_abnormal_metric + Action Input: {"start_time": 1684600070, "end_time": 1684600074, "metric_name": "cpu_usage"} + Observation: "The metric is abnormal" + + New summary: + - I now know the start and end time of the anomaly. + - I searched for whether_is_abnormal_metric, and I now know that the CPU usage is abnormal. + END OF EXAMPLE + + Now, try to summarize the following record. + + Current summary: + ${summary} + + New lines: + ${new_lines} + + New summary: + + +tools: &tools +- + tool_name: db_diag, + tool_url: http://127.0.0.1:8079/tools/db_diag/ + +name: Classroom 3 DBAs with Tools + +environment: + env_type: basic + max_turns: 30 + rule: + order: + type: sequential + visibility: + type: all + selector: + type: basic + updater: + type: basic + describer: + type: basic + +agents: + - agent_type: conversation + name: Chief DBA + role_description: |- + You are a Chief DBA with much database diagnosis experience. Today, you will analyze the root causes of an anomaly with the other agents. Here is the outline of the procedure: + 1. Identify the start and end time of an anomaly (using obtain_start_and_end_time_of_anomaly in db_diag tool) and tell the information to other agents. + 2. Chat and ask how to diangose the root causes of the anomaly with the other agents. + 3. Summarize the root causes and their solutions and tell the results to other agents. + Your answer need to be concise and accurate. + prompt_template: *chief_dba_format_prompt + memory: + memory_type: chat_history + tool_memory: + memory_type: chat_history + llm: + llm_type: gpt-4 + model: gpt-4 + temperature: 0.7 + prompt_template: *summary_prompt + recursive: true + llm: + llm_type: gpt-4 + model: gpt-4 + temperature: 0.7 + max_tokens: 1024 + tools: *tools + verbose: true + - + agent_type: tool + name: CPU Agent + role_description: You are a CPU agent that can use the db_diag tool to check CPU usage (whether_is_abnormal_metric) and analyze the root causes of high CPU usage (cpu_diagnosis_agent). + prompt_template: *cpu_agent_format_prompt + memory: + memory_type: chat_history + tool_memory: + memory_type: chat_history + llm: + llm_type: gpt-4 + model: gpt-4 + temperature: 0.7 + prompt_template: *summary_prompt + recursive: true + llm: + llm_type: gpt-4 + model: gpt-4 + temperature: 0.7 + max_tokens: 512 + tools: *tools + verbose: true + - + agent_type: tool + name: Memory Agent + role_description: You are a memory agent that can use the db_diag tool to check memory usage (whether_is_abnormal_metric) and analyze the root causes of high memory usage (memory_diagnosis_agent). + prompt_template: *mem_agent_format_prompt + memory: + memory_type: chat_history + tool_memory: + memory_type: chat_history + llm: + llm_type: gpt-4 + model: gpt-4 + temperature: 0.7 + prompt_template: *summary_prompt + recursive: true + llm: + llm_type: gpt-4 + model: gpt-4 + temperature: 0.7 + max_tokens: 512 + tools: *tools + verbose: true \ No newline at end of file diff --git a/agentverse/tasks/db_diag/output_parser.py b/agentverse/tasks/db_diag/output_parser.py new file mode 100644 index 000000000..9fad13f78 --- /dev/null +++ b/agentverse/tasks/db_diag/output_parser.py @@ -0,0 +1,40 @@ +from __future__ import annotations + +import re +from typing import Union + + +# from langchain.schema import AgentAction, AgentFinish +from agentverse.utils import AgentAction, AgentFinish + +from agentverse.parser import OutputParserError, output_parser_registry +from agentverse.parser import OutputParser +from agentverse.llms.base import LLMResult + + +@output_parser_registry.register("db_diag") +class DBDiag(OutputParser): + def parse(self, output: LLMResult) -> Union[AgentAction, AgentFinish]: + text = output.content + cleaned_output = text.strip() + cleaned_output = re.sub(r"\n+", "\n", cleaned_output) + cleaned_output = cleaned_output.split("\n") + if not ( + len(cleaned_output) == 3 + and cleaned_output[0].startswith("Thought:") + and cleaned_output[1].startswith("Action:") + and cleaned_output[2].startswith("Action Input:") + ): + raise OutputParserError(text) + action = cleaned_output[1][len("Action:") :].strip() + action_input = cleaned_output[2][len("Action Input:") :].strip() + if action in ["Speak"]: + return AgentFinish({"output": action_input}, text) + elif action == "CallOn": + return AgentFinish({"output": "[CallOn] " + action_input}, text) + elif action == "RaiseHand": + return AgentFinish({"output": "[RaiseHand] " + action_input}, text) + elif action == "Listen": + return AgentFinish({"output": ""}, text) + else: + return AgentAction(action.lower(), action_input, text)