Skip to content

TyroneXie/DB-GPT

Β 
Β 

Repository files navigation

LLM As Database Administrator

Demo β€’ QuickStart β€’ Alerts And Anomalies β€’ Knowledge And Tools β€’ FAQ β€’ Community β€’ Contributors

【English | 中文】

🦾 Build your personal database administrator (D-Bot)πŸ§‘β€πŸ’», which is good at reading documents, using various tools, writing analysis reports!

πŸ—Ί Online Demo

In the online website (http://dbgpt.dbmind.cn), you can browse all the historical diagnosis results, used metrics, and the detailed diagnosis processes.

frontend_v2

frontend_v2

πŸ“° Updates

  • Upgrade the LLM-based diagnosis mechanism:

    • Task Dispatching -> Concurrent Diagnosis -> Cross Review -> Report Generation
  • Add typical anomalies and alerts (Pigsty) πŸ”— link

  • An end-to-end framework is available! πŸš€ link

  • Support monitoring and optimization tools in multiple levels πŸ”— link

    • Monitoring metrics (Prometheus)
    • Diagnosis knowledge retrieval (dbmind)
    • Logical query transformations (Calcite)
    • Index optimization algorithms (for PostgreSQL)
    • Physical operator hints (for PostgreSQL)
    • Backup and Point-in-time Recovery (Pigsty)
  • Our vision papers are released (continuously update)

This project is evolving with new features πŸ‘«πŸ‘«
Don't forget to star ⭐ and watch πŸ‘€ to stay up to date :)

πŸ•Ή QuickStart

Folder Structure

.
β”œβ”€β”€ multiagents
β”‚   β”œβ”€β”€ agent_conf                        # Settings of each agent
β”‚   β”œβ”€β”€ agents                            # Implementation of different agent types 
β”‚   β”œβ”€β”€ environments                      # E.g., diag orders / diag update / terminal conditions
β”‚   β”œβ”€β”€ knowledge                         # Diagnosis experience from documents
β”‚   β”œβ”€β”€ llms                              # Supported models
β”‚   β”œβ”€β”€ memory                            # The content and summary of chat history
β”‚   β”œβ”€β”€ reasoning_algorithms              # The available algorithm for single LLM reasoning
β”‚   β”œβ”€β”€ response_formalize_scripts        # Useless content removal of model response
β”‚   β”œβ”€β”€ tools                             # External monitoring/optimization tools for models
β”‚   └── utils                             # Other functions (e.g., database/json/yaml operations)
β”œβ”€β”€ web_service                           # Web services to view diagnostic reports
β”‚   β”œβ”€β”€ backend                           # Web services backend
β”‚   β”œβ”€β”€ frontend                          # Web services frontend 
β”œβ”€β”€ webhook                               # Use the webhook to save the results of the alert to a file

Website Side

We provide a local website to browse historical diagnosis reports and procedures. You can easily launch it with

  • install nodejs (16.13.1 and above is recommended)
npm install -g n
sudo n 16.18.1
  • install dependencies for the first run。
# install frontend environment
cd web_service/frontend
rm -rf node_modules/
rm -r package-lock.json
# install dependencies for the first run
npm install  --legacy-peer-deps
  • run
# cd service directory
cd web_service
# launch the local server and open the website
sh run_service.sh

Modify the "python app.py" command within run_service.sh if multiple versions of Python are installed (e.g., ).

After successfully launching the local server, visit http://127.0.0.1:8025/ to browse the diagnosis reports.

Diagnosis Side

1. Prerequisites

  • PostgreSQL v12 or higher

    Make sure your database supports remote connection (link)

    Additionally, install extensions like pg_stat_statements (track frequent queries), pg_hint_plan (optimize physical operators), and hypopg (create hypothetical Indexes).

    Note pg_stat_statements continuosly accumulate query statistics over time. So you need to clear the statistics from time to time: 1) To discard all the statistics, execute "SELECT pg_stat_statements_reset();"; 2) To discard the statistics of specific query, execute "SELECT pg_stat_statements_reset(userid, dbid, queryid);".

  • Enable slow query log in PostgreSQL (link)

    (1) For "systemctl restart postgresql", the service name can be different (e.g., postgresql-12.service);

    (2) Use absolute log path name like "log_directory = '/var/lib/pgsql/12/data/log'";

    (3) Set "log_line_prefix = '%m [%p] [%d]'" in postgresql.conf (to record the database names of different queries).

  • Prometheus

    Check prometheus.md for detailed installation guides.

2. Package Installation

Step 1: Install python packages.

pip install -r requirements.txt

Step 2: Configure environment variables.

  • Export your OpenAI API key
# macos
export OPENAI_API_KEY="your_api_key_here"
# windows
set OPENAI_API_KEY="your_api_key_here"

Step 3: Add database/anomaly/prometheus settings into tool_config_example.yaml and rename into tool_config.yaml:

```bash
POSTGRESQL:
  host: 182.92.xxx.x
  port: 5432
  user: xxxx
  password: xxxxx
  dbname: postgres

DATABASESERVER:
  server_address: 182.92.xxx.x
  username: root
  password: xxxxx
  remote_directory: /var/lib/pgsql/12/data/log

PROMETHEUS:
  api_url: http://8.131.xxx.xx:9090/
  postgresql_exporter_instance: 172.27.xx.xx:9187
  node_exporter_instance: 172.27.xx.xx:9100
```

remote_directory in the DATABASESERVER setting indicates where the slow query log file is located at (link).

  • If accessing openai service via vpn, execute this command:
# macos
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890
  • Test your openai key
cd others
python openai_test.py

3. Generate New Diagnosis Report

  • Test single case
python main.py
  • Test in batch
python batch_main.py

🎩 Alerts And Anomalies

Alert Management

We support AlertManager for Prometheus. You can find more information about how to configure alertmanager here: alertmanager.md.

  • We provide AlertManager-related configuration files, including alertmanager.yml, node_rules.yml, and pgsql_rules.yml. The path is in the config folder in the root directory, which you can deploy to your Prometheus server to retrieve the associated exceptions.
  • We also provide webhook server that supports getting alerts. The path is a webhook folder in the root directory that you can deploy to your server to get and store Prometheus's alerts in files.
  • Currently, the alert file is obtained using SSh. You need to configure your server information in the tool_config.yaml in the config folder.
  • node_rules.yml and pgsql_rules.yml is a reference https://github.com/Vonng/pigsty code in this open source project, their monitoring do very well, thank them for their effort.

Anomaly Simulation

Script-Triggered Anomalies

We offer scripts that could incur typical anomalies. Check out different anomaly cases in http://dbgpt.dbmind.cn

Root Cause Description Potential Alerts
INSERT_LARGE_DATA Long execution time for large data insert
FETCH_LARGE_DATA Long execution time for large data fetch
REDUNDANT_INDEX Unnecessary and redundant indexes in tables
VACUUM Unused space caused by data modifications
POOR_JOIN_PERFORMANCE Poor performance of join operators
CORRELATED_SUBQUERY Non-promotable subqueries in SQL statements ,
LOCK_CONTENTION Lock contention issues
CPU_CONTENTION Severe CPU resource contention
IO_CONTENTION IO resource contention affecting SQL performance
COMMIT_CONTENTION Highly concurrent commits affecting SQL execution
SMALL_MEMORY_ALLOC Too small allocated memory space

Manually Designed Anomalies

Click to check 29 typical anomalies together with expert analysis (supported by the DBMind team)

πŸ“Ž Customize Knowledge And Tools

1. Knowledge Extraction (Zui Chen)

Step 1. Rename doc2knowledge/config_template.json into doc2knowledge/config.json. And add the value for "api_key" ("organization" is optional)

GPT-4 is necessary to utilize the function calling feature. I will try to solve this limit.

Step 2. Split documents into separated section files by the section indexes (e.g., section1, section1.1, section2 ...). And copy the section files into the docs/<your_document_name>/raw/. For example:

.
β”œβ”€β”€ docs
β”‚   β”œβ”€β”€ report_example
|   β”‚   β”œβ”€β”€ raw    
|   β”‚   |    β”œβ”€β”€ 1 title.txt    
|   β”‚   |    β”œβ”€β”€ 1.1 category.txt

It is a laborious work and hard to find a better way than manually splitting the given document

You can jump over this step and directly run the report_example case

Step 3. Modify the arguments in doc2knowledge.py script and run the script:

cd doc2knowledge/
python doc2knowledge.py

The summary for the same document sections is cached. You can delete this file if do not like to reuse the previous caches.

2. Tool Preparation

  • Tool APIs (for optimization)

    Module Functions
    index_selection (equipped) heuristic algorithm
    query_rewrite (equipped) 45 rules
    physical_hint (equipped) 15 parameters

    For functions within [query_rewrite, physical_hint], you can use api_test.py script to verify the effectiveness.

    If the function actually works, append it to the api.py of corresponding module.

πŸ’ FAQ

🀨 The '.sh' script command cannot be executed on windows system. Switch the shell to *git bash* or use *git bash* to execute the '.sh' script.
🀨 "No module named 'xxx'" on windows system. This error is caused by issues with the Python runtime environment path. You need to perform the following steps:

Step 1: Check Environment Variables.

You must configure the "Scripts" in the environment variables.

Step 2: Check IDE Settings.

For VS Code, download the Python extension for code. For PyCharm, specify the Python version for the current project.

⏱ Todo

  • Project cleaning
  • Support more anomalies
  • Strictly constrain the llm outputs (excessive irrelevant information) based on the matched knowledge
  • Query log option (potential to take up disk space and we need to consider it carefully)
  • Add more communication mechanisms
  • Support more knowledge sources
  • Localized model that reaches D-bot(gpt4)'s capability
  • Prometheus-as-a-Service
  • Support other databases (e.g., mysql/redis)

πŸ‘« Community

πŸ€— Relevant Projects

https://github.com/OpenBMB/AgentVerse

https://github.com/Vonng/pigsty

πŸ“’ Citation

Feel free to cite us if you like this project.

@misc{zhou2023llm4diag,
      title={LLM As DBA}, 
      author={Xuanhe Zhou, Guoliang Li, Zhiyuan Liu},
      year={2023},
      eprint={2308.05481},
      archivePrefix={arXiv},
      primaryClass={cs.DB}
}
@misc{zhou2023dbgpt,
      title={DB-GPT: Large Language Model Meets Database}, 
      author={Xuanhe Zhou, Zhaoyan Sun, Guoliang Li},
      year={2023},
      archivePrefix={Data Science and Engineering},
}

πŸ“§ Contributors

Other Collaborators: Wei Zhou, Kunyi Li.

We thank all the contributors to this project. Do not hesitate if you would like to get involved or contribute!

⭐️ Star History

Star History Chart

About

An LLM Based Database Diagnose System

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • Python 85.1%
  • Vue 8.6%
  • JavaScript 5.1%
  • SCSS 0.6%
  • CSS 0.3%
  • Shell 0.2%
  • HTML 0.1%