├── .env
├── .env_temple
├── .gitignore
├── CODE_OF_CONDUCT.md
├── Code of conduct.md
├── Dockerfile
├── LICENSE
├── README.md
├── README_en.md
├── agents
├── __init__.py
├── chat_db.py
├── chat_manager.py
└── query_executor.py
├── api.py
├── chat_db.py
├── cli_demo.py
├── configs
└── log_config.py
├── deploy
├── dev.env
├── prod.env
├── qa.env
└── sql
│ ├── schema_demo.sql
│ └── sql_data_demo.sql
├── docker
├── README.md
├── docker-compose.yml
└── sql
│ └── init.sql
├── img
├── WECHAT.md
├── cli_01.jpg
├── cli_02.jpg
├── logo.jpg
├── qr_code_account.jpg
├── qr_code_wechat.jpg
└── web.jpg
├── langfusion
├── __init__.py
└── call_back_handler.py
├── memory
├── __init__.py
└── memory_chat_message_history.py
├── prompts
├── sys_message.py
└── temple
│ ├── __init__.py
│ └── db_expert.py
├── requirements.txt
├── tools
└── __init__.py
├── utils
├── __init__.py
└── database_utils.py
├── wait-for-it.sh
├── web
└── app
│ ├── routes
│ ├── __init__.py
│ └── db_gpt_route.py
│ └── service
│ ├── __init__.py
│ └── db_gpt_service.py
└── web_demo.py
/.env:
--------------------------------------------------------------------------------
1 | OPENAI_API_KEY=""
2 | OPENAI_API_BASE=""
3 | MYSQL_HOST=""
4 | MYSQL_PORT=""
5 | MYSQL_USER=""
6 | MYSQL_PASSWORD=""
7 | MYSQL_DATABASE=""
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.env_temple:
--------------------------------------------------------------------------------
1 | OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxx"
2 | OPENAI_API_BASE="https://xxxxxxxx/v1"
3 | MYSQL_HOST=127.0.0.1
4 | MYSQL_PORT=3306
5 | MYSQL_USER=root
6 | MYSQL_PASSWORD=ls1234qwer
7 | MYSQL_DATABASE=db_gpt
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | share/python-wheels/
24 | *.egg-info/
25 | .installed.cfg
26 | *.egg
27 | MANIFEST
28 |
29 | # PyInstaller
30 | # Usually these files are written by a python script from a template
31 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
32 | *.manifest
33 | *.spec
34 |
35 | # Installer logs
36 | pip-log.txt
37 | pip-delete-this-directory.txt
38 |
39 | # Unit test / coverage reports
40 | htmlcov/
41 | .tox/
42 | .nox/
43 | .coverage
44 | .coverage.*
45 | .cache
46 | nosetests.xml
47 | coverage.xml
48 | *.cover
49 | *.py,cover
50 | .hypothesis/
51 | .pytest_cache/
52 | cover/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | .pybuilder/
76 | target/
77 |
78 | # Jupyter Notebook
79 | .ipynb_checkpoints
80 |
81 | # IPython
82 | profile_default/
83 | ipython_config.py
84 |
85 | # pyenv
86 | # For a library or package, you might want to ignore these files since the code is
87 | # intended to run in multiple environments; otherwise, check them in:
88 | # .python-version
89 |
90 | # pipenv
91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
94 | # install all needed dependencies.
95 | #Pipfile.lock
96 |
97 | # poetry
98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99 | # This is especially recommended for binary packages to ensure reproducibility, and is more
100 | # commonly ignored for libraries.
101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102 | #poetry.lock
103 |
104 | # pdm
105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106 | #pdm.lock
107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108 | # in version control.
109 | # https://pdm.fming.dev/#use-with-ide
110 | .pdm.toml
111 |
112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113 | __pypackages__/
114 |
115 | # Celery stuff
116 | celerybeat-schedule
117 | celerybeat.pid
118 |
119 | # SageMath parsed files
120 | *.sage.py
121 |
122 | # Environments
123 |
124 | # Spyder project settings
125 | .spyderproject
126 | .spyproject
127 |
128 | # Rope project settings
129 | .ropeproject
130 |
131 | # mkdocs documentation
132 | /site
133 |
134 | # mypy
135 | .mypy_cache/
136 | .dmypy.json
137 | dmypy.json
138 |
139 | # Pyre type checker
140 | .pyre/
141 |
142 | # pytype static type analyzer
143 | .pytype/
144 |
145 | # Cython debug symbols
146 | cython_debug/
147 |
148 | # PyCharm
149 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
150 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
151 | # and can be added to the global gitignore or merged into this file. For a more nuclear
152 | # option (not recommended) you can uncomment the following to ignore the entire idea folder.
153 | #.idea/
154 | target
155 | .classpath
156 | .project
157 | .idea
158 | .settings
159 | *.iml
160 | logs
161 | .DS_store
162 | .docker/mysql
163 | .node_modules
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # chat_DB Project Contributor Code of Conduct
2 |
3 | ## Our Goal
4 |
5 | In the chat_DB community, we are dedicated to building an open and inclusive environment where everyone can participate regardless of age, body size, disability, ethnicity, gender identity, sexual orientation, experience level, educational background, socio-economic status, nationality, or religious beliefs.
6 |
7 | ## Standards of Behavior
8 |
9 | We expect all community members to adhere to the following standards of behavior:
10 |
11 | - Engage with others in a friendly and respectful manner.
12 | - Understand and respect different perspectives and suggestions.
13 | - Maintain a constructive and professional approach when giving or receiving feedback.
14 | - Own up to our mistakes and strive for improvement.
15 | - Prioritize the community's collective well-being over individual interests.
16 |
17 | ## Unacceptable Behavior
18 |
19 | The following behaviors are explicitly prohibited:
20 |
21 | - Use of inappropriate sexual language or imagery and any form of sexual harassment.
22 | - Any form of harassment, whether in public or private spaces.
23 | - Insulting, discriminatory, or derogatory comments.
24 | - Publicly sharing others' private information, such as addresses or email addresses, without permission.
25 | - Any behavior that could be considered inappropriate in a professional setting.
26 |
27 | ## Maintainers' Responsibility
28 |
29 | Project maintainers are responsible for clarifying and enforcing our standards of behavior and will take appropriate action in response to any behavior they deem inappropriate. Maintainers have the authority to edit, reject, or remove comments, commits, code, wiki edits, issues, and other contributions that do not align with this Code of Conduct.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies to all spaces of the chat_DB community, including but not limited to our GitHub repository, mailing lists, chat rooms, and any public area labeled with the chat_DB project.
34 |
35 | ## Reporting and Enforcement
36 |
37 | Instances of abusive, harassing, or other unacceptable behavior can be reported to the chat_DB project team. All reports will be taken seriously and handled with the necessary discretion and investigation.
38 |
39 | ## Contact Information
40 |
41 | Please reach out to us through the following channels to report any violations of the Code of Conduct:
42 |
43 | - Email: [Your Email Address]
44 | - GitHub Repository Issue: [Your Repository Issue Link]
45 |
46 | ## Privacy and Protection
47 |
48 | We will protect the privacy of reporters and ensure confidentiality during the investigation process.
49 |
50 | ## Enforcement Actions
51 |
52 | Violations of the Code of Conduct will be handled according to the following guidelines:
53 |
54 | ### 1. Warning
55 |
56 | For first-time or minor offenses, a warning will be issued, and a public apology may be requested.
57 |
58 | ### 2. Temporary Ban
59 |
60 | For more severe or persistent disrespect or harassment, a temporary ban will be imposed, prohibiting participation in community activities for a specified period.
61 |
62 | ### 3. Permanent Ban
63 |
64 | For serious or continuous violations of community standards, such as harassment or aggressive behavior, a permanent ban will be implemented.
65 |
66 | ## Version and Attribution
67 |
68 | This Code of Conduct is adapted from the Contributor Covenant, version 2.1, and has been adjusted to fit the needs of the chat_DB project. The community impact guidelines are inspired by Mozilla's code of conduct enforcement ladder.
69 |
70 | ## Frequently Asked Questions (FAQ)
71 |
72 | For more information about this Code of Conduct, please refer to the Contributor Covenant's [FAQ](https://www.contributor-covenant.org/faq).
73 |
74 | ## Translations
75 |
76 | Translations of this Code of Conduct can be found on the [Contributor Covenant translations page](https://www.contributor-covenant.org/translations).
77 |
78 | ## Other Formats
79 |
80 | This Code of Conduct is also available in the following formats: text/markdown, text/plaintext, asciidoc.
--------------------------------------------------------------------------------
/Code of conduct.md:
--------------------------------------------------------------------------------
1 | # chat_DB Project Contributor Code of Conduct
2 |
3 | ## Our Goal
4 |
5 | In the chat_DB community, we are dedicated to building an open and inclusive environment where everyone can participate regardless of age, body size, disability, ethnicity, gender identity, sexual orientation, experience level, educational background, socio-economic status, nationality, or religious beliefs.
6 |
7 | ## Standards of Behavior
8 |
9 | We expect all community members to adhere to the following standards of behavior:
10 |
11 | - Engage with others in a friendly and respectful manner.
12 | - Understand and respect different perspectives and suggestions.
13 | - Maintain a constructive and professional approach when giving or receiving feedback.
14 | - Own up to our mistakes and strive for improvement.
15 | - Prioritize the community's collective well-being over individual interests.
16 |
17 | ## Unacceptable Behavior
18 |
19 | The following behaviors are explicitly prohibited:
20 |
21 | - Use of inappropriate sexual language or imagery and any form of sexual harassment.
22 | - Any form of harassment, whether in public or private spaces.
23 | - Insulting, discriminatory, or derogatory comments.
24 | - Publicly sharing others' private information, such as addresses or email addresses, without permission.
25 | - Any behavior that could be considered inappropriate in a professional setting.
26 |
27 | ## Maintainers' Responsibility
28 |
29 | Project maintainers are responsible for clarifying and enforcing our standards of behavior and will take appropriate action in response to any behavior they deem inappropriate. Maintainers have the authority to edit, reject, or remove comments, commits, code, wiki edits, issues, and other contributions that do not align with this Code of Conduct.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies to all spaces of the chat_DB community, including but not limited to our GitHub repository, mailing lists, chat rooms, and any public area labeled with the chat_DB project.
34 |
35 | ## Reporting and Enforcement
36 |
37 | Instances of abusive, harassing, or other unacceptable behavior can be reported to the chat_DB project team. All reports will be taken seriously and handled with the necessary discretion and investigation.
38 |
39 | ## Contact Information
40 |
41 | Please reach out to us through the following channels to report any violations of the Code of Conduct:
42 |
43 | - Email: [Your Email Address]
44 | - GitHub Repository Issue: [Your Repository Issue Link]
45 |
46 | ## Privacy and Protection
47 |
48 | We will protect the privacy of reporters and ensure confidentiality during the investigation process.
49 |
50 | ## Enforcement Actions
51 |
52 | Violations of the Code of Conduct will be handled according to the following guidelines:
53 |
54 | ### 1. Warning
55 |
56 | For first-time or minor offenses, a warning will be issued, and a public apology may be requested.
57 |
58 | ### 2. Temporary Ban
59 |
60 | For more severe or persistent disrespect or harassment, a temporary ban will be imposed, prohibiting participation in community activities for a specified period.
61 |
62 | ### 3. Permanent Ban
63 |
64 | For serious or continuous violations of community standards, such as harassment or aggressive behavior, a permanent ban will be implemented.
65 |
66 | ## Version and Attribution
67 |
68 | This Code of Conduct is adapted from the Contributor Covenant, version 2.1, and has been adjusted to fit the needs of the chat_DB project. The community impact guidelines are inspired by Mozilla's code of conduct enforcement ladder.
69 |
70 | ## Frequently Asked Questions (FAQ)
71 |
72 | For more information about this Code of Conduct, please refer to the Contributor Covenant's [FAQ](https://www.contributor-covenant.org/faq).
73 |
74 | ## Translations
75 |
76 | Translations of this Code of Conduct can be found on the [Contributor Covenant translations page](https://www.contributor-covenant.org/translations).
77 |
78 | ## Other Formats
79 |
80 | This Code of Conduct is also available in the following formats: text/markdown, text/plaintext, asciidoc.
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | # 使用官方Python镜像作为基础镜像
2 | FROM python:3.9-slim
3 |
4 | # 设置工作目录
5 | WORKDIR /app
6 |
7 | # 安装系统依赖
8 | RUN apt-get update && apt-get install -y \
9 | gcc \
10 | libmariadb-dev \
11 | pkg-config \
12 | netcat-openbsd \
13 | tzdata \
14 | && rm -rf /var/lib/apt/lists/*
15 |
16 | # 复制wait-for-it.sh脚本到容器中
17 | COPY wait-for-it.sh /app/wait-for-it.sh
18 | RUN chmod +x /app/wait-for-it.sh
19 |
20 |
21 | # 仅复制requirements.txt文件到容器中
22 | COPY requirements.txt .
23 |
24 | # 安装Python依赖
25 | RUN pip install --no-cache-dir --upgrade pip \
26 | && pip install --no-cache-dir -r requirements.txt
27 |
28 | # 复制应用程序代码到容器中
29 | COPY . /app
30 |
31 | # 暴露gradio运行的端口
32 | EXPOSE 7860
33 |
34 | # 运行应用程序
35 | CMD ["python", "web_demo.py"]
36 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 sql-agi
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DB-GPT:通过自然语言管理数据库,替代传统企业的web管理后台界面
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
30 |
31 | ## Introduction
32 | 🤖 DB-GPT是一个开源的数据应用程序开发框架,旨在利用大型语言模型(LLM)技术通过自然语言与数据库进行交互,取代了传统的web管理后端。
33 |
34 | 🌐 目前我们只开放了查询权限,为了满足更复杂的业务需求,包括创建、读取、更新和删除(CRUD)功能,我们目前正在进行内部测试,并期待着在未来能给大家带来更多的惊喜。
35 |
36 | 🚀 在Data 3.0时代,我们的产品致力于利用模型和数据库技术,使企业和开发人员能够用更少的代码构建自定义应用程序。让开发人员更专注于复杂的C端业务从而取代传统的web管理后台系统。
37 |
38 | ## 快速上手
39 | 找一个项目存储的目录,将项目克隆下来,克隆命令如下:
40 | ```shell
41 | git clone https://github.com/sql-agi/DB-GPT
42 | ```
43 | ### Docker deploy
44 | 首先,配置.env文件,可以参考 templates.env_temple;
45 |
46 | 只需要配置OPENAI_API_KEY、OPENAI_API_BASE这两个属性即可(建议使用官方的API_KEY);
47 |
48 | 然后切换到docker目录下,参考docker目录下的README.md,切换命令如下:
49 |
50 | ```shell
51 | cd DB-GPT/docker
52 | ```
53 | 最后执行README.md的命令即可;
54 |
55 | 注意:如果你需要自定义数据库表结构和表数据,只需要更改根目录下/docker/sql/init.sql 文件即可。
56 |
57 | ### Web & CLi
58 | 首先将用conda创建新环境并将环境切换到db-gpt,命令如下:
59 | ```shell
60 | conda create --name db-gpt python=3.9
61 | conda activate db-gpt
62 | pip install -r requirements.txt
63 | ```
64 |
65 | 配置.env文件,可以参考 templates.env_temple
66 |
67 | 主要配置包括这几个属性:OPENAI_API_KEY、OPENAI_API_BASE、MYSQL_HOST、MYSQL_PORT、MYSQL_USER、MYSQL_PASSWORD、MYSQL_DATABASE
68 |
69 | 🔥🔥🔥强烈建议大家用官方的API_KEY 经过测试有些中转的key支持的效果并不好
70 |
71 |
72 | #### web demo
73 | 我们提供了一种基于 [Gradio]( https://gradio.app )网络版演示和命令行演示如下:
74 |
75 | 
76 |
77 | 然后在存储库中运行[web_demo.py]:
78 |
79 | ```shell
80 | python web_demo.py
81 | ```
82 |
83 | 程序会运行一个 Web Server,并输出地址。在浏览器中打开输出的地址即可使用。最新版 Demo 实现了打字机效果,速度体验大大提升。注意,由于国内 Gradio 的网络访问较为缓慢,启用 demo.queue().launch(share=True, inbrowser=True) 时所有网络会经过 Gradio 服务器转发,导致打字机体验大幅下降,现在默认启动方式已经改为 share=False,如有需要公网访问的需求,可以重新修改为 share=True 启动。
84 |
85 | #### cli demo
86 | 
87 |
88 | 
89 |
90 | 在存储库中运行[cli_demo.py](cli_demo.py):
91 |
92 | ```shell
93 | python cli_demo.py
94 | ```
95 |
96 | 该程序将在命令行上进行交互式对话。输入指令并按命令行上的Enter键生成回复,然后输入“quit”终止程序。
97 |
98 | ### API Deploy
99 |
100 | Run [api.py](api.py): in the repository:
101 |
102 | ```shell
103 | python api.py
104 | ```
105 | 默认情况下在端口8000上本地部署,通过POST方法调用
106 |
107 | ```shell
108 | curl -X POST "http://127.0.0.1:8000/chat/db" \
109 | -H "Content-Type: application/json" \
110 | -d '{"input": "你好"}'
111 | ```
112 | 得到的返回值为
113 |
114 | ```shell
115 | {
116 | "reply": "你好!请问有什么可以帮助您的?"
117 | }
118 | ```
119 |
120 | ## 未来计划
121 | 🔥🔥🔥 前端:我们致力于开发更优秀的前端UI界面,进一步支持更多类型的数据库以及LLM(其中包含开源大模型),以提升用户体验和系统灵活性。
122 |
123 | 🔥🔥🔥 后端:我们将进一步深度测试更复杂的CRUD场景,增加区分(切换)环境、设置角色权限等功能从而保证LLM操作的准确性和稳定性。
124 |
125 | 🔥🔥🔥 总结:希望更多的用户体验并提供反馈,我们会根据用户的反馈进一步优化我们的产品,并希望感兴趣的小伙伴加入我们的开源团队。
126 |
127 | ## 联系我们
128 |
129 | ### 项目交流群
130 |
131 |
132 | 🎉 Chat_DB 项目微信交流群,如果你也对本项目感兴趣,欢迎加入群聊参与讨论交流。
133 |
134 | ### 公众号
135 |
136 |
137 |
138 | 🎉 Chat_DB 项目官方公众号,欢迎扫码关注。
139 |
140 | ## 🤗 Reference project
141 | https://github.com/langchain-ai/langchain
--------------------------------------------------------------------------------
/README_en.md:
--------------------------------------------------------------------------------
1 | # DB-GPT:Using natural language to manage databases, replacing traditional enterprise web management backend interfaces
2 |
3 |
4 |
5 |
25 |
26 |
27 | ## Introduction
28 |
29 | 🤖 DB-GPT is an open-source data application development framework aimed at utilizing Large Language Model (LLM) technology to interact with databases through natural language, replacing traditional web management backend.
30 |
31 | 🌐 At present, we only have access to query permissions. In order to meet more complex business requirements, including the Create, Read, Update, and Delete (CRUD) functionality, we are currently undergoing internal testing and we look forward to bringing more surprises to everyone in the future.
32 |
33 | 🚀 In the era of Data 3.0, our products are committed to utilizing model and database technologies to enable enterprises and developers to build custom applications with less code. Enable developers to focus more on complex C-end business and replace traditional web management backend systems.
34 |
35 | ## 快速上手
36 | Find a directory where the project is stored and clone it. The cloning command is as follows:
37 | ```shell
38 | git clone https://github.com/sql-agi/DB-GPT
39 | ```
40 | ### Docker deploy
41 | Firstly, configure the .env file, which can refer to templates .env_temple;
42 |
43 | You only need to configure the OPENAI-API_KEY and OPENAI-API_BASE attributes (it is recommended to use the official API_KEY);
44 |
45 | Then switch to the Docker directory and refer to README.md in the Docker directory. The switching command is as follows:
46 |
47 | ```Shell
48 | cd DB-GPT/docker
49 | ```
50 | Finally, execute the command README.md;
51 |
52 | Note: If you need to customize the database table structure and table data, simply change the/Docker/SQL/init. SQL file in the root directory
53 |
54 | ### Web & CLi
55 |
56 | Firstly, create a new environment using Conda and switch the environment to db-gpt. The command is as follows:
57 |
58 | ```Shell
59 | conda create --name db-gpt python=3.9
60 | conda activate db-gpt
61 | pip install -r requirements.txt
62 | ```
63 |
64 | Configure the .env file, you can refer to templates .env_temple
65 |
66 | The main configuration includes these attributes: OPENAI-API_KEY OPENAI_API_BASE、MYSQL_HOST、MYSQL_PORT、MYSQL_USER、MYSQL_PASSWORD、MYSQL_DATABASE
67 |
68 | 🔥🔥🔥 I strongly recommend that everyone use the official API_KEY. After testing, some intermediate keys do not support good results
69 |
70 | #### web demo
71 |
72 | We provide a method based on [Gradio]( https://gradio.app )The web version of the demo and a command-line demo:
73 |
74 | 
75 |
76 | Then run [web_demo.py] in the repository:
77 |
78 | ```shell
79 | python web_demo.py
80 | ```
81 |
82 | The program will run a web server and output the address. Open the output address in the browser to use it. The latest version of the demo has achieved a typewriter effect, greatly improving the speed experience. Note that due to slow network access in domestic Gradio, when 'demo. queue(). launch (share=True, inbrowser=True)' is enabled, all networks will be forwarded through the Gradio server, resulting in a significant decrease in the typewriter experience. The default startup method has now been changed to 'share=False'. If there is a need for public network access, it can be changed to 'share=True' startup.
83 |
84 | #### cli demo
85 | 
86 |
87 | 
88 |
89 | Run in the repository [cli_demo.py] (cli_demo.py):
90 |
91 | ```shell
92 | python cli_demo.py
93 | ```
94 |
95 | The program will have an interactive conversation on the command line. Enter instructions and press enter on the command line to generate a reply, and enter 'quit' to terminate the program.
96 |
97 | ### API Deploy
98 |
99 | Run [api.py](api.py): in the repository:
100 |
101 | ```shell
102 | python api.py
103 | ```
104 | Deployed locally on port 8000 by default, called through POST method
105 |
106 | ```shell
107 | curl -X POST "http://127.0.0.1:8000/chat/db" \
108 | -H "Content-Type: application/json" \
109 | -d '{"input": "你好"}'
110 | ```
111 | The obtained return value is
112 |
113 | ```shell
114 | {
115 | "reply": "你好!请问有什么可以帮助您的?"
116 | }
117 | ```
118 |
119 | ## 未来计划
120 | 🔥🔥🔥 Front end UI interface:We are committed to developing better front-end UI interfaces, further supporting a wider range of databases and LLMs (including open-source big models), to enhance user experience and system flexibility.
121 |
122 | 🔥🔥🔥Backend: We will further conduct in-depth testing on more complex CRUD scenarios, adding functions such as distinguishing (switching) environments and setting role permissions to ensure the accuracy and stability of LLM operations.
123 |
124 | 🔥🔥🔥Summary: We hope to provide more user experience and feedback, and we will further optimize our products based on user feedback. We also hope that interested partners can join our open source team.
125 |
126 | ## contact us
127 |
128 | ### Project communication group
129 |
130 |
131 |
132 | 🎉 Chat_DB Project WeChat communication group. If you are also interested in this project, please join the group chat to participate in discussions and exchanges.
133 |
134 | ### 公众号
135 |
136 |
137 |
138 | 🎉 Chat_DB The official official account of the project, welcome to scan the code.
139 |
140 | ## 🤗 Reference project
141 | https://github.com/langchain-ai/langchain
--------------------------------------------------------------------------------
/agents/__init__.py:
--------------------------------------------------------------------------------
1 | from .chat_db import ChatDBAgent
--------------------------------------------------------------------------------
/agents/chat_db.py:
--------------------------------------------------------------------------------
1 | from langchain_community.agent_toolkits import create_sql_agent
2 | # 从URI创建SQLDatabase实例
3 | # 这里的"../../../../../notebooks/Chinook.db"是数据库文件的相对路径
4 | from langchain.sql_database import SQLDatabase
5 | from langchain_community.agent_toolkits import SQLDatabaseToolkit
6 | from langchain_openai import ChatOpenAI
7 | # 加载环境变量
8 | from dotenv import load_dotenv, find_dotenv
9 | _ = load_dotenv(find_dotenv())
10 |
11 | from memory.memory_chat_message_history import MemoryChatMessageHistory
12 | from langchain_core.runnables.history import RunnableWithMessageHistory
13 | from .query_executor import QueryExecutor
14 | from .chat_manager import ChatAgentManager
15 |
16 | llm = ChatOpenAI(
17 | model="gpt-4",
18 | temperature=0,
19 | # 将 seed 参数作为模型参数传递
20 | model_kwargs={
21 | "seed": 666
22 | }
23 | )
24 |
25 |
26 | class ChatDBAgent:
27 | @classmethod
28 | def chat_db(cls) -> RunnableWithMessageHistory:
29 | """
30 | 创建一个聊天管理器实例,并执行查询及管理聊天历史。
31 |
32 | 这是一个类方法,用于初始化和管理聊天代理,该代理执行必要的语言模型逻辑和聊天历史的管理。
33 |
34 | 返回:
35 | RunnableWithMessageHistory: 一个执行聊天操作并管理历史记录的对象。
36 | """
37 | # 执行查询并管理聊天历史
38 | chat_manager = ChatAgentManager(llm=llm)
39 | return chat_manager.execute_and_manage()
40 |
41 | @classmethod
42 | def db_gpt(cls, input: str) -> str:
43 | """
44 | 处理输入字符串,并通过聊天代理生成响应。
45 | 此方法利用 ChatAgentManager 来执行查询并管理聊天历史,然后调用聊天代理
46 | 的 invoke 方法来处理输入,并生成输出。
47 | Args:
48 | input (str): 输入到聊天代理的字符串。
49 |
50 | Returns:
51 | str: 聊天代理处理后的输出结果。
52 | 该方法主要以接口的形式用户调用
53 | """
54 | # 执行查询并管理聊天历史
55 | chat_manager = ChatAgentManager(llm=llm)
56 | agent_with_chat_history = chat_manager.execute_and_manage()
57 |
58 | reply = agent_with_chat_history.invoke(
59 | {"input": input},
60 | config={"configurable": {"session_id": "dbgpt-session"}},
61 | )
62 | output = reply['output']
63 | return output
64 |
65 |
66 | if __name__ == '__main__':
67 | output_only = ChatDBAgent.db_gpt("把我们系统的用户信息告诉我")
68 | # output_only = query_db.nlp_query_db("给我插入一条用户数据")
69 | print("------------------------")
70 | print(output_only)
71 |
--------------------------------------------------------------------------------
/agents/chat_manager.py:
--------------------------------------------------------------------------------
1 | from .query_executor import QueryExecutor
2 | from memory.memory_chat_message_history import MemoryChatMessageHistory
3 | from langchain_core.runnables.history import RunnableWithMessageHistory
4 |
5 |
6 | class ChatAgentManager:
7 | def __init__(self, llm):
8 | self.query_executor = QueryExecutor(llm=llm)
9 |
10 | def execute_and_manage(self) -> RunnableWithMessageHistory:
11 | """
12 | 执行查询并对返回的代理进行聊天历史管理。
13 |
14 | 此方法首先通过 `query_executor` 执行查询,获取一个代理执行器。
15 | 然后,将该代理执行器传递给 `MemoryChatMessageHistory` 类的 `manage_chat_history` 方法,
16 | 以便管理聊天历史并返回一个能够持续执行和记录聊天历史的对象。
17 |
18 | 返回:
19 | RunnableWithMessageHistory: 一个能执行聊天操作并维护聊天历史的对象。
20 | """
21 | # 执行查询并管理聊天历史
22 | agent_executor = self.query_executor.execute_query()
23 | agent_with_chat_history = MemoryChatMessageHistory.manage_chat_history(agent_executor)
24 | return agent_with_chat_history
25 |
--------------------------------------------------------------------------------
/agents/query_executor.py:
--------------------------------------------------------------------------------
1 | import os
2 | from langchain_community.agent_toolkits import create_sql_agent
3 | # 从URI创建SQLDatabase实例
4 | # 这里的"../../../../../notebooks/Chinook.db"是数据库文件的相对路径
5 | from langchain.sql_database import SQLDatabase
6 | from langchain_community.agent_toolkits import SQLDatabaseToolkit
7 | from prompts.temple import DBExpert
8 | from langchain.agents.agent import AgentExecutor
9 | from utils import DatabaseUtils
10 | class QueryExecutor:
11 | def __init__(self, llm):
12 | self.llm = llm
13 | # self.db = SQLDatabase.from_uri(os.getenv("MYSQL_URL"))
14 | self.db = SQLDatabase.from_uri(DatabaseUtils.create_mysql_url())
15 | self.toolkit = SQLDatabaseToolkit(db=self.db, llm=self.llm)
16 |
17 | def execute_query(self) -> AgentExecutor:
18 | """
19 | 构造查询提示并创建 SQL 执行代理。
20 |
21 | 此方法首先从 toolkit 获取当前的上下文环境。
22 | 接着,获取数据库专家系统定义的聊天提示,根据当前上下文环境调整这些提示。
23 | 最后,使用提供的语言模型(llm)、toolkit、和定制的提示来创建一个 SQL 查询执行代理。
24 |
25 | 返回:
26 | Agent: 配置好的 SQL 执行代理,具备执行查询的能力并能详细记录操作过程。
27 | """
28 | context = self.toolkit.get_context()
29 | prompt = DBExpert.chat_prompt_message()
30 | prompt = prompt.partial(**context)
31 | return create_sql_agent(
32 | llm=self.llm,
33 | toolkit=self.toolkit,
34 | agent_type="openai-tools",
35 | prompt=prompt,
36 | verbose=True)
37 |
--------------------------------------------------------------------------------
/api.py:
--------------------------------------------------------------------------------
1 |
2 | import uvicorn
3 | from fastapi.middleware.cors import CORSMiddleware
4 | from fastapi import FastAPI
5 | from web.app.routes import db_gpt_route
6 |
7 | app = FastAPI()
8 |
9 | origins = ["http://localhost:8000"]
10 |
11 | app.add_middleware(
12 | CORSMiddleware,
13 | allow_origins=origins,
14 | allow_credentials=True,
15 | allow_methods=["*"],
16 | allow_headers=["*"],
17 | )
18 |
19 | app.include_router(db_gpt_route.router)
20 |
21 | if __name__ == '__main__':
22 | uvicorn.run("api:app", host='127.0.0.1', port=8000, log_level="info")
23 | print("running")
--------------------------------------------------------------------------------
/chat_db.py:
--------------------------------------------------------------------------------
1 | from langchain_community.agent_toolkits import create_sql_agent
2 | # 从URI创建SQLDatabase实例
3 | # 这里的"../../../../../notebooks/Chinook.db"是数据库文件的相对路径
4 |
5 | from langchain_openai import ChatOpenAI
6 | # 加载环境变量
7 | from dotenv import load_dotenv, find_dotenv
8 | _ = load_dotenv(find_dotenv())
9 |
10 | from langchain_core.runnables.history import RunnableWithMessageHistory
11 | from .chat_manager import ChatAgentManager
12 |
13 | llm = ChatOpenAI(
14 | model="gpt-4",
15 | temperature=0,
16 | # 将 seed 参数作为模型参数传递
17 | model_kwargs={
18 | "seed": 666
19 | }
20 | )
21 |
22 |
23 | class ChatDBAgent:
24 | @classmethod
25 | def chat_db(cls) -> RunnableWithMessageHistory:
26 | """
27 | 创建一个聊天管理器实例,并执行查询及管理聊天历史。
28 |
29 | 这是一个类方法,用于初始化和管理聊天代理,该代理执行必要的语言模型逻辑和聊天历史的管理。
30 |
31 | 返回:
32 | RunnableWithMessageHistory: 一个执行聊天操作并管理历史记录的对象。
33 | """
34 | # 执行查询并管理聊天历史
35 | chat_manager = ChatAgentManager(llm=llm)
36 | return chat_manager.execute_and_manage()
37 |
38 | @classmethod
39 | def db_gpt(cls, input: str) -> str:
40 | """
41 | 处理输入字符串,并通过聊天代理生成响应。
42 | 此方法利用 ChatAgentManager 来执行查询并管理聊天历史,然后调用聊天代理
43 | 的 invoke 方法来处理输入,并生成输出。
44 | Args:
45 | input (str): 输入到聊天代理的字符串。
46 |
47 | Returns:
48 | str: 聊天代理处理后的输出结果。
49 | 该方法主要以接口的形式用户调用
50 | """
51 | # 执行查询并管理聊天历史
52 | chat_manager = ChatAgentManager(llm=llm)
53 | agent_with_chat_history = chat_manager.execute_and_manage()
54 |
55 | reply = agent_with_chat_history.invoke(
56 | {"input": input},
57 | config={"configurable": {"session_id": "dbgpt-session"}},
58 | )
59 | output = reply['output']
60 | return output
61 |
62 |
63 | if __name__ == '__main__':
64 | output_only = ChatDBAgent.db_gpt("把我们系统的用户信息告诉我")
65 | # output_only = query_db.nlp_query_db("给我插入一条用户数据")
66 | print("------------------------")
67 | print(output_only)
68 |
--------------------------------------------------------------------------------
/cli_demo.py:
--------------------------------------------------------------------------------
1 | from agents.chat_db import ChatDBAgent
2 | # 日志配置
3 | # from configs import log_config
4 | # logger = log_config.logger
5 |
6 | def chat_db_agent():
7 | human_icon = "\U0001F468"
8 | ai_icon = "\U0001F916"
9 |
10 | agent_with_chat_history = ChatDBAgent.chat_db()
11 | while True:
12 | # text_input = input("User: ")
13 | task = input(f"{ai_icon}:有什么可以帮您?\n{human_icon}:")
14 | if task.strip().lower() == "quit":
15 | break
16 | reply = agent_with_chat_history.invoke(
17 | {"input": task},
18 | config={"configurable": {"session_id": "test-session1"}},
19 | )
20 | print(reply)
21 | output_only = reply['output']
22 |
23 | print(f"{ai_icon}:{output_only}\n")
24 |
25 | if __name__ == '__main__':
26 | chat_db_agent()
27 |
--------------------------------------------------------------------------------
/configs/log_config.py:
--------------------------------------------------------------------------------
1 | import logging
2 | import os
3 | from logging.handlers import RotatingFileHandler
4 | from datetime import datetime
5 | import pytz # 引入 pytz 库
6 |
7 |
8 | LOG_FORMAT = "%(levelname) -5s %(asctime)s" "-1d: %(message)s"
9 | logger = logging.getLogger()
10 | logger.setLevel(logging.INFO)
11 | logging.basicConfig(format=LOG_FORMAT)
12 |
13 | # 清除已有的处理器
14 | logger.handlers.clear()
15 |
16 | # 设置时区为东八区
17 | tz = pytz.timezone('Asia/Shanghai')
18 | os.environ['TZ'] = 'Asia/Shanghai'
19 |
20 | # 强制刷新时区设置
21 | datetime.now().astimezone(tz)
22 |
23 | # 获取当前日期,并将其格式化为字符串
24 | current_date = datetime.now(tz).strftime("%Y-%m-%d")
25 |
26 | # 创建日志文件夹
27 | log_folder = os.path.dirname(os.getcwd()) + "/logs"
28 | # if os.getenv("ENV") != "dev":
29 | # log_folder = "/apps/logs"
30 | os.makedirs(log_folder, exist_ok=True)
31 |
32 | # 创建一个 RotatingFileHandler 实例,指定日志文件路径、模式(追加或覆盖)和文件大小阈值
33 | file_handler = RotatingFileHandler(os.path.join(log_folder, f"logfile_{current_date}.log"), mode='a',
34 | maxBytes=1024 * 1024 * 500, backupCount=5)
35 |
36 | # 设置文件处理器的格式
37 | formatter = logging.Formatter(LOG_FORMAT)
38 | file_handler.setFormatter(formatter)
39 |
40 | # 将文件处理器添加到日志记录器
41 | logger.addHandler(file_handler)
42 |
43 | # 输出到控制台
44 | to_console = logging.StreamHandler()
45 | to_console.setFormatter(formatter)
46 | logger.addHandler(to_console)
47 |
48 | if __name__ == '__main__':
49 | import os
50 |
51 | project_root = os.path.dirname(os.getcwd())
52 | print("当前路径为:%s" % project_root)
53 |
--------------------------------------------------------------------------------
/deploy/dev.env:
--------------------------------------------------------------------------------
1 | ENV = "dev"
2 | MYSQL_URL = ""
--------------------------------------------------------------------------------
/deploy/prod.env:
--------------------------------------------------------------------------------
1 | ENV = "dev"
2 | MYSQL_URL = ""
--------------------------------------------------------------------------------
/deploy/qa.env:
--------------------------------------------------------------------------------
1 | ENV = "qa"
2 | MYSQL_URL = ""
--------------------------------------------------------------------------------
/deploy/sql/schema_demo.sql:
--------------------------------------------------------------------------------
1 |
2 | CREATE TABLE `user` (
3 | `id` int NOT NULL AUTO_INCREMENT COMMENT '用户的唯一标识符',
4 | `username` varchar(255) NOT NULL COMMENT '用户的用户名,必须唯一',
5 | `password` varchar(255) NOT NULL COMMENT '用户的密码,存储加密形式',
6 | `email` varchar(255) NOT NULL COMMENT '用户的电子邮箱地址',
7 | `is_del` tinyint DEFAULT '0' COMMENT '标记用户是否被删除,0为未删除,1为已删除',
8 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '用户的创建时间',
9 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
10 | PRIMARY KEY (`id`)
11 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='用户信息表'
12 |
13 | CREATE TABLE `product` (
14 | `id` int NOT NULL AUTO_INCREMENT COMMENT '产品的唯一标识符',
15 | `product_name` varchar(255) NOT NULL COMMENT '产品名称',
16 | `description` text COMMENT '产品描述',
17 | `price` decimal(10,2) NOT NULL COMMENT '产品价格',
18 | `stock_quantity` int NOT NULL COMMENT '产品库存数量',
19 | `is_del` tinyint DEFAULT '0' COMMENT '标记产品是否被删除,0为未删除,1为已删除',
20 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
21 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
22 | PRIMARY KEY (`id`)
23 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='产品信息表'
24 |
25 | CREATE TABLE `order` (
26 | `id` int NOT NULL AUTO_INCREMENT COMMENT '订单的唯一标识符',
27 | `user_id` int NOT NULL COMMENT '下单的用户ID',
28 | `product_id` int NOT NULL COMMENT '订单中的产品ID',
29 | `quantity` int NOT NULL COMMENT '购买数量',
30 | `status` int NOT NULL COMMENT '订单状态(0-待支付,1-已支付,2-发货中,3-已完成,4-已取消)',
31 | `order_date` datetime NOT NULL COMMENT '订单生成的日期和时间',
32 | `is_del` tinyint DEFAULT '0' COMMENT '标记订单是否被删除,0为未删除,1为已删除',
33 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
34 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
35 | PRIMARY KEY (`id`)
36 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='订单信息表'
37 |
38 | CREATE TABLE `order_details` (
39 | `id` int NOT NULL AUTO_INCREMENT COMMENT '订单详情的唯一标识符',
40 | `order_id` int NOT NULL COMMENT '关联的订单ID',
41 | `product_id` int NOT NULL COMMENT '关联的产品ID',
42 | `quantity` int NOT NULL COMMENT '购买数量',
43 | `unit_price` decimal(10,2) NOT NULL COMMENT '产品的单价',
44 | `subtotal` decimal(10,2) NOT NULL COMMENT '此项的总金额',
45 | `is_del` tinyint DEFAULT '0' COMMENT '标记订单详情是否被删除,0为未删除,1为已删除',
46 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
47 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
48 | PRIMARY KEY (`id`)
49 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='订单详情表'
50 |
51 |
52 |
53 | CREATE TABLE `payment` (
54 | `id` int NOT NULL AUTO_INCREMENT COMMENT '支付信息的唯一标识符',
55 | `order_id` int NOT NULL COMMENT '关联的订单ID',
56 | `status` int NOT NULL COMMENT '支付状态(0-未支付,1-支付成功,2-支付失败,3-退款中,4-已退款)',
57 | `amount` decimal(10,2) NOT NULL COMMENT '支付金额',
58 | `payment_date` datetime NOT NULL COMMENT '支付日期',
59 | `is_del` tinyint DEFAULT '0' COMMENT '标记支付信息是否被删除,0为未删除,1为已删除',
60 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
61 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
62 | PRIMARY KEY (`id`)
63 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT '支付信息表';
64 |
65 | CREATE TABLE `review` (
66 | `id` int NOT NULL AUTO_INCREMENT COMMENT '评论的唯一标识符',
67 | `order_id` int NOT NULL COMMENT '关联的订单ID',
68 | `rating` int NOT NULL COMMENT '用户给出的评分',
69 | `comment` text COMMENT '评论内容',
70 | `is_del` tinyint DEFAULT '0' COMMENT '标记评论是否被删除,0为未删除,1为已删除',
71 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
72 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
73 | PRIMARY KEY (`id`)
74 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT '评论信息表';
75 |
76 |
--------------------------------------------------------------------------------
/deploy/sql/sql_data_demo.sql:
--------------------------------------------------------------------------------
1 | INSERT INTO `user` (username, password, email, is_del)
2 | VALUES
3 | ('张三', 'password123', 'zhangsan@example.com', 0),
4 | ('李四', 'password123', 'lisi@example.com', 0),
5 | ('王五', 'password123', 'wangwu@example.com', 0),
6 | ('赵六', 'password123', 'zhaoliu@example.com', 0),
7 | ('钱七', 'password123', 'qianqi@example.com', 0),
8 | ('孙八', 'password123', 'sunba@example.com', 0),
9 | ('周九', 'password123', 'zhoujiu@example.com', 0),
10 | ('吴十', 'password123', 'wushi@example.com', 0),
11 | ('郑十一', 'password123', 'zhengshiyi@example.com', 0),
12 | ('冯十二', 'password123', 'fengshier@example.com', 0),
13 | ('陈十三', 'password123', 'chenshisan@example.com', 0),
14 | ('楚十四', 'password123', 'chushi@example.com', 0),
15 | ('卫十五', 'password123', 'weishiwu@example.com', 0),
16 | ('蒋十六', 'password123', 'jiangshiliu@example.com', 0),
17 | ('沈十七', 'password123', 'shenshiqi@example.com', 0),
18 | ('韩十八', 'password123', 'hanshiba@example.com', 0),
19 | ('杨十九', 'password123', 'yangshijiu@example.com', 0),
20 | ('朱二十', 'password123', 'zhuershi@example.com', 0),
21 | ('秦二一', 'password123', 'qineri@example.com', 0),
22 | ('尤二二', 'password123', 'youerer@example.com', 0);
23 |
24 |
25 | INSERT INTO `product` (product_name, description, price, stock_quantity, is_del)
26 | VALUES
27 | ('红茶', '传统的中国红茶', 10.00, 50, 0),
28 | ('绿茶', '绿茶清香四溢', 15.50, 40, 0),
29 | ('咖啡', '来自哥伦比亚的优质咖啡', 25.75, 30, 0),
30 | ('乌龙茶', '最好的乌龙茶叶', 12.00, 20, 0),
31 | ('普洱茶', '陈年的普洱', 30.00, 15, 0),
32 | ('黑咖啡', '纯正的黑咖啡', 22.00, 25, 0),
33 | ('拿铁', '香浓的拿铁', 20.00, 30, 0),
34 | ('卡布奇诺', '经典卡布奇诺', 18.50, 20, 0),
35 | ('白茶', '高级的白茶', 40.00, 10, 0),
36 | ('花茶', '花香四溢的花茶', 17.00, 25, 0);
37 |
38 |
39 | INSERT INTO `order`
40 | (id, user_id, product_id, quantity, status, order_date, is_del, create_time, update_time)
41 | VALUES
42 | (1, 1, 1, 2, 3, NOW(), 0, NOW(), NOW()),
43 | (2, 2, 2, 1, 1, NOW(), 0, NOW(), NOW()),
44 | (3, 3, 3, 3, 2, NOW(), 0, NOW(), NOW()),
45 | (4, 4, 4, 1, 3, NOW(), 0, NOW(), NOW()),
46 | (5, 5, 5, 5, 3, NOW(), 0, NOW(), NOW()),
47 | (6, 6, 6, 2, 0, NOW(), 0, NOW(), NOW()),
48 | (7, 7, 7, 1, 1, NOW(), 0, NOW(), NOW()),
49 | (8, 8, 8, 3, 2, NOW(), 0, NOW(), NOW()),
50 | (9, 9, 9, 1, 3, NOW(), 0, NOW(), NOW()),
51 | (10, 10, 10, 2, 4, NOW(), 0, NOW(), NOW());
52 |
53 |
54 | INSERT INTO `order_details` (order_id, product_id, quantity, unit_price, subtotal, is_del)
55 | VALUES
56 | (1, 1, 2, 10.00, 20.00, 0),
57 | (2, 2, 1, 15.50, 15.50, 0),
58 | (3, 3, 3, 25.75, 77.25, 0),
59 | (4, 4, 1, 12.00, 12.00, 0),
60 | (5, 5, 5, 30.00, 150.00, 0),
61 | (6, 6, 2, 22.00, 44.00, 0),
62 | (7, 7, 1, 20.00, 20.00, 0),
63 | (8, 8, 3, 18.50, 55.50, 0),
64 | (9, 9, 1, 40.00, 40.00, 0),
65 | (10, 10, 2, 17.00, 34.00, 0);
66 |
67 |
68 | INSERT INTO `payment` (order_id, status, amount, payment_date, is_del)
69 | VALUES
70 | (1, 1, 20.00, NOW(), 0),
71 | (2, 1, 15.50, NOW(), 0),
72 | (3, 2, 77.25, NOW(), 0),
73 | (4, 1, 12.00, NOW(), 0),
74 | (5, 1, 150.00, NOW(), 0),
75 | (6, 0, 44.00, NOW(), 0),
76 | (7, 1, 20.00, NOW(), 0),
77 | (8, 2, 55.50, NOW(), 0),
78 | (9, 1, 40.00, NOW(), 0),
79 | (10, 1, 34.00, NOW(), 0);
80 |
81 | INSERT INTO `review` (id, order_id, rating, comment, is_del, create_time, update_time)
82 | VALUES
83 | (1, 1, 5, '非常好的红茶', 0, NOW(), NOW()),
84 | (2, 2, 4, '很清新的绿茶', 0, NOW(), NOW()),
85 | (3, 3, 3, '咖啡味道不错', 0, NOW(), NOW()),
86 | (4, 4, 5, '乌龙茶香气浓郁', 0, NOW(), NOW()),
87 | (5, 5, 5, '普洱味道深厚', 0, NOW(), NOW()),
88 | (6, 6, 4, '黑咖啡很纯', 0, NOW(), NOW()),
89 | (7, 7, 5, '拿铁非常香', 0, NOW(), NOW()),
90 | (8, 8, 4, '卡布奇诺味道好', 0, NOW(), NOW()),
91 | (9, 9, 5, '白茶非常好喝', 0, NOW(), NOW()),
92 | (10, 10, 4, '花茶的香味很舒服', 0, NOW(), NOW()),
93 | (11, 1, 4, '还会再买的', 0, NOW(), NOW()),
94 | (12, 2, 4, '满意', 0, NOW(), NOW()),
95 | (13, 3, 5, '三次购买了', 0, NOW(), NOW()),
96 | (14, 4, 5, '推荐给朋友了', 0, NOW(), NOW()),
97 | (15, 5, 4, '价格有点贵', 0, NOW(), NOW()),
98 | (16, 6, 4, '比预想的好', 0, NOW(), NOW()),
99 | (17, 7, 5, '味道一级棒', 0, NOW(), NOW()),
100 | (18, 8, 1, '不太好', 0, NOW(), NOW()),
101 | (19, 9, 2, '凑合', 0, NOW(), NOW()),
102 | (20, 10, 4, '会再次购买', 0, NOW(), NOW());
103 |
104 |
105 |
106 |
--------------------------------------------------------------------------------
/docker/README.md:
--------------------------------------------------------------------------------
1 | # docker 目录下执行
2 | - docker-compose build --no-cache
3 | - docker-compose up
--------------------------------------------------------------------------------
/docker/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: '3.8'
2 |
3 | services:
4 | mysql:
5 | image: mysql:8.0.33
6 | container_name: mysql_db
7 | environment:
8 | MYSQL_ROOT_PASSWORD: 'ls1234qwer'
9 | MYSQL_DATABASE: db_gpt
10 | ports:
11 | - "3306:3306"
12 | volumes:
13 | - ./mysql/mysql_data:/var/lib/mysql/
14 | - ./mysql/conf/:/etc/mysql/conf.d/
15 | - ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
16 | command:
17 | # 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配)
18 | --default-authentication-plugin=caching_sha2_password
19 | --character-set-server=utf8mb4
20 | --collation-server=utf8mb4_general_ci
21 | --explicit_defaults_for_timestamp=true
22 | --lower_case_table_names=1
23 | networks:
24 | - app_network
25 | healthcheck:
26 | test: ["CMD-SHELL", "mysqladmin ping -h localhost"]
27 | interval: 30s
28 | timeout: 10s
29 | retries: 5
30 |
31 | gradio:
32 | build:
33 | context: ..
34 | dockerfile: Dockerfile # 指定 Dockerfile 的相对路径
35 | container_name: gradio_app
36 | ports:
37 | - "7860:7860"
38 | environment:
39 | - MYSQL_HOST=mysql
40 | - MYSQL_PORT=3306
41 | - MYSQL_USER=root
42 | - MYSQL_PASSWORD=ls1234qwer
43 | - MYSQL_DATABASE=db_gpt
44 | depends_on:
45 | mysql:
46 | condition: service_healthy
47 | networks:
48 | - app_network
49 | command: ["/app/wait-for-it.sh", "mysql", "3306", "--", "python", "web_demo.py"]
50 |
51 | volumes:
52 | mysql_data:
53 |
54 | networks:
55 | app_network:
56 |
--------------------------------------------------------------------------------
/docker/sql/init.sql:
--------------------------------------------------------------------------------
1 | CREATE DATABASE IF NOT EXISTS db_gpt CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2 |
3 | use db_gpt;
4 |
5 |
6 | CREATE TABLE `user` (
7 | `id` int NOT NULL AUTO_INCREMENT COMMENT '用户的唯一标识符',
8 | `username` varchar(255) NOT NULL COMMENT '用户的用户名,必须唯一',
9 | `password` varchar(255) NOT NULL COMMENT '用户的密码,存储加密形式',
10 | `email` varchar(255) NOT NULL COMMENT '用户的电子邮箱地址',
11 | `is_del` tinyint DEFAULT '0' COMMENT '标记用户是否被删除,0为未删除,1为已删除',
12 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '用户的创建时间',
13 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
14 | PRIMARY KEY (`id`)
15 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户信息表';
16 |
17 | CREATE TABLE `product` (
18 | `id` int NOT NULL AUTO_INCREMENT COMMENT '产品的唯一标识符',
19 | `product_name` varchar(255) NOT NULL COMMENT '产品名称',
20 | `description` text COMMENT '产品描述',
21 | `price` decimal(10,2) NOT NULL COMMENT '产品价格',
22 | `stock_quantity` int NOT NULL COMMENT '产品库存数量',
23 | `is_del` tinyint DEFAULT '0' COMMENT '标记产品是否被删除,0为未删除,1为已删除',
24 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
25 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
26 | PRIMARY KEY (`id`)
27 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='产品信息表';
28 |
29 | CREATE TABLE `order` (
30 | `id` int NOT NULL AUTO_INCREMENT COMMENT '订单的唯一标识符',
31 | `user_id` int NOT NULL COMMENT '下单的用户ID',
32 | `product_id` int NOT NULL COMMENT '订单中的产品ID',
33 | `quantity` int NOT NULL COMMENT '购买数量',
34 | `status` int NOT NULL COMMENT '订单状态(0-待支付,1-已支付,2-发货中,3-已完成,4-已取消)',
35 | `order_date` datetime NOT NULL COMMENT '订单生成的日期和时间',
36 | `is_del` tinyint DEFAULT '0' COMMENT '标记订单是否被删除,0为未删除,1为已删除',
37 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
38 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
39 | PRIMARY KEY (`id`)
40 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单信息表';
41 |
42 | CREATE TABLE `order_details` (
43 | `id` int NOT NULL AUTO_INCREMENT COMMENT '订单详情的唯一标识符',
44 | `order_id` int NOT NULL COMMENT '关联的订单ID',
45 | `product_id` int NOT NULL COMMENT '关联的产品ID',
46 | `quantity` int NOT NULL COMMENT '购买数量',
47 | `unit_price` decimal(10,2) NOT NULL COMMENT '产品的单价',
48 | `subtotal` decimal(10,2) NOT NULL COMMENT '此项的总金额',
49 | `is_del` tinyint DEFAULT '0' COMMENT '标记订单详情是否被删除,0为未删除,1为已删除',
50 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
51 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
52 | PRIMARY KEY (`id`)
53 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单详情表';
54 |
55 |
56 |
57 | CREATE TABLE `payment` (
58 | `id` int NOT NULL AUTO_INCREMENT COMMENT '支付信息的唯一标识符',
59 | `order_id` int NOT NULL COMMENT '关联的订单ID',
60 | `status` int NOT NULL COMMENT '支付状态(0-未支付,1-支付成功,2-支付失败,3-退款中,4-已退款)',
61 | `amount` decimal(10,2) NOT NULL COMMENT '支付金额',
62 | `payment_date` datetime NOT NULL COMMENT '支付日期',
63 | `is_del` tinyint DEFAULT '0' COMMENT '标记支付信息是否被删除,0为未删除,1为已删除',
64 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
65 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
66 | PRIMARY KEY (`id`)
67 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '支付信息表';
68 |
69 | CREATE TABLE `review` (
70 | `id` int NOT NULL AUTO_INCREMENT COMMENT '评论的唯一标识符',
71 | `order_id` int NOT NULL COMMENT '关联的订单ID',
72 | `rating` int NOT NULL COMMENT '用户给出的评分',
73 | `comment` text COMMENT '评论内容',
74 | `is_del` tinyint DEFAULT '0' COMMENT '标记评论是否被删除,0为未删除,1为已删除',
75 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录的创建时间',
76 | `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录的最后更新时间',
77 | PRIMARY KEY (`id`)
78 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '评论信息表';
79 |
80 | INSERT INTO `user` (username, password, email, is_del)
81 | VALUES
82 | ('张三', 'password123', 'zhangsan@example.com', 0),
83 | ('李四', 'password123', 'lisi@example.com', 0),
84 | ('王五', 'password123', 'wangwu@example.com', 0),
85 | ('赵六', 'password123', 'zhaoliu@example.com', 0),
86 | ('钱七', 'password123', 'qianqi@example.com', 0),
87 | ('孙八', 'password123', 'sunba@example.com', 0),
88 | ('周九', 'password123', 'zhoujiu@example.com', 0),
89 | ('吴十', 'password123', 'wushi@example.com', 0),
90 | ('郑十一', 'password123', 'zhengshiyi@example.com', 0),
91 | ('冯十二', 'password123', 'fengshier@example.com', 0),
92 | ('陈十三', 'password123', 'chenshisan@example.com', 0),
93 | ('楚十四', 'password123', 'chushi@example.com', 0),
94 | ('卫十五', 'password123', 'weishiwu@example.com', 0),
95 | ('蒋十六', 'password123', 'jiangshiliu@example.com', 0),
96 | ('沈十七', 'password123', 'shenshiqi@example.com', 0),
97 | ('韩十八', 'password123', 'hanshiba@example.com', 0),
98 | ('杨十九', 'password123', 'yangshijiu@example.com', 0),
99 | ('朱二十', 'password123', 'zhuershi@example.com', 0),
100 | ('秦二一', 'password123', 'qineri@example.com', 0),
101 | ('尤二二', 'password123', 'youerer@example.com', 0);
102 |
103 |
104 | INSERT INTO `product` (product_name, description, price, stock_quantity, is_del)
105 | VALUES
106 | ('红茶', '传统的中国红茶', 10.00, 50, 0),
107 | ('绿茶', '绿茶清香四溢', 15.50, 40, 0),
108 | ('咖啡', '来自哥伦比亚的优质咖啡', 25.75, 30, 0),
109 | ('乌龙茶', '最好的乌龙茶叶', 12.00, 20, 0),
110 | ('普洱茶', '陈年的普洱', 30.00, 15, 0),
111 | ('黑咖啡', '纯正的黑咖啡', 22.00, 25, 0),
112 | ('拿铁', '香浓的拿铁', 20.00, 30, 0),
113 | ('卡布奇诺', '经典卡布奇诺', 18.50, 20, 0),
114 | ('白茶', '高级的白茶', 40.00, 10, 0),
115 | ('花茶', '花香四溢的花茶', 17.00, 25, 0);
116 |
117 |
118 | INSERT INTO `order`
119 | (id, user_id, product_id, quantity, status, order_date, is_del, create_time, update_time)
120 | VALUES
121 | (1, 1, 1, 2, 3, NOW(), 0, NOW(), NOW()),
122 | (2, 2, 2, 1, 1, NOW(), 0, NOW(), NOW()),
123 | (3, 3, 3, 3, 2, NOW(), 0, NOW(), NOW()),
124 | (4, 4, 4, 1, 3, NOW(), 0, NOW(), NOW()),
125 | (5, 5, 5, 5, 3, NOW(), 0, NOW(), NOW()),
126 | (6, 6, 6, 2, 0, NOW(), 0, NOW(), NOW()),
127 | (7, 7, 7, 1, 1, NOW(), 0, NOW(), NOW()),
128 | (8, 8, 8, 3, 2, NOW(), 0, NOW(), NOW()),
129 | (9, 9, 9, 1, 3, NOW(), 0, NOW(), NOW()),
130 | (10, 10, 10, 2, 4, NOW(), 0, NOW(), NOW());
131 |
132 |
133 | INSERT INTO `order_details` (order_id, product_id, quantity, unit_price, subtotal, is_del)
134 | VALUES
135 | (1, 1, 2, 10.00, 20.00, 0),
136 | (2, 2, 1, 15.50, 15.50, 0),
137 | (3, 3, 3, 25.75, 77.25, 0),
138 | (4, 4, 1, 12.00, 12.00, 0),
139 | (5, 5, 5, 30.00, 150.00, 0),
140 | (6, 6, 2, 22.00, 44.00, 0),
141 | (7, 7, 1, 20.00, 20.00, 0),
142 | (8, 8, 3, 18.50, 55.50, 0),
143 | (9, 9, 1, 40.00, 40.00, 0),
144 | (10, 10, 2, 17.00, 34.00, 0);
145 |
146 |
147 | INSERT INTO `payment` (order_id, status, amount, payment_date, is_del)
148 | VALUES
149 | (1, 1, 20.00, NOW(), 0),
150 | (2, 1, 15.50, NOW(), 0),
151 | (3, 2, 77.25, NOW(), 0),
152 | (4, 1, 12.00, NOW(), 0),
153 | (5, 1, 150.00, NOW(), 0),
154 | (6, 0, 44.00, NOW(), 0),
155 | (7, 1, 20.00, NOW(), 0),
156 | (8, 2, 55.50, NOW(), 0),
157 | (9, 1, 40.00, NOW(), 0),
158 | (10, 1, 34.00, NOW(), 0);
159 |
160 | INSERT INTO `review` (id, order_id, rating, comment, is_del, create_time, update_time)
161 | VALUES
162 | (1, 1, 5, '非常好的红茶', 0, NOW(), NOW()),
163 | (2, 2, 4, '很清新的绿茶', 0, NOW(), NOW()),
164 | (3, 3, 3, '咖啡味道不错', 0, NOW(), NOW()),
165 | (4, 4, 5, '乌龙茶香气浓郁', 0, NOW(), NOW()),
166 | (5, 5, 5, '普洱味道深厚', 0, NOW(), NOW()),
167 | (6, 6, 4, '黑咖啡很纯', 0, NOW(), NOW()),
168 | (7, 7, 5, '拿铁非常香', 0, NOW(), NOW()),
169 | (8, 8, 4, '卡布奇诺味道好', 0, NOW(), NOW()),
170 | (9, 9, 5, '白茶非常好喝', 0, NOW(), NOW()),
171 | (10, 10, 4, '花茶的香味很舒服', 0, NOW(), NOW()),
172 | (11, 1, 4, '还会再买的', 0, NOW(), NOW()),
173 | (12, 2, 4, '满意', 0, NOW(), NOW()),
174 | (13, 3, 5, '三次购买了', 0, NOW(), NOW()),
175 | (14, 4, 5, '推荐给朋友了', 0, NOW(), NOW()),
176 | (15, 5, 4, '价格有点贵', 0, NOW(), NOW()),
177 | (16, 6, 4, '比预想的好', 0, NOW(), NOW()),
178 | (17, 7, 5, '味道一级棒', 0, NOW(), NOW()),
179 | (18, 8, 1, '不太好', 0, NOW(), NOW()),
180 | (19, 9, 2, '凑合', 0, NOW(), NOW()),
181 | (20, 10, 4, '会再次购买', 0, NOW(), NOW());
182 |
183 |
184 |
185 |
186 |
187 |
--------------------------------------------------------------------------------
/img/WECHAT.md:
--------------------------------------------------------------------------------
1 |
2 |

3 |
4 |
加入「Chat_DB」交流群」
5 |
Scan the QR code and join the Chat_DB discussion group
6 |
7 |
8 |
--------------------------------------------------------------------------------
/img/cli_01.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/img/cli_01.jpg
--------------------------------------------------------------------------------
/img/cli_02.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/img/cli_02.jpg
--------------------------------------------------------------------------------
/img/logo.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/img/logo.jpg
--------------------------------------------------------------------------------
/img/qr_code_account.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/img/qr_code_account.jpg
--------------------------------------------------------------------------------
/img/qr_code_wechat.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/img/qr_code_wechat.jpg
--------------------------------------------------------------------------------
/img/web.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/img/web.jpg
--------------------------------------------------------------------------------
/langfusion/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/langfusion/__init__.py
--------------------------------------------------------------------------------
/langfusion/call_back_handler.py:
--------------------------------------------------------------------------------
1 | from langfuse.callback import CallbackHandler
2 |
3 | class CallbackHandler:
4 | """
5 | 创建并返回一个 CallbackHandler 实例。
6 | 此方法为类方法,用于初始化并返回一个 CallbackHandler 对象,配置了必要的安全凭据和连接信息。这个回调处理器用于处理来自特定主机的回调请求,常用于处理异步事件或集成第三方服务时的安全通信。
7 |
8 | 参数:
9 | - secret_key (str): 用于加密或验证来自外部系统的数据的私密密钥。
10 | - public_key (str): 公共密钥,可能用于与外部系统共享,以便它们验证由我们系统发送的数据。
11 | - host (str): CallbackHandler 将监听或发送回调请求的主机地址。
12 | - trace_name (str): 用于日志或跟踪回调处理的标识名称。
13 | - user_id (str): 标识回调处理关联的用户的唯一标识符。
14 |
15 | 返回:
16 | - CallbackHandler: 配置完成的 CallbackHandler 实例,可用于立即处理回调。
17 | """
18 | @classmethod
19 | def call_back_handler(cls) -> CallbackHandler:
20 | handler = CallbackHandler(
21 | secret_key="sk-xxxxxxx",
22 | public_key="pk-xxxxxxx",
23 | host="",
24 | trace_name="",
25 | user_id="jiayuan"
26 | )
27 | return handler
--------------------------------------------------------------------------------
/memory/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/memory/__init__.py
--------------------------------------------------------------------------------
/memory/memory_chat_message_history.py:
--------------------------------------------------------------------------------
1 | from langchain.memory import ChatMessageHistory
2 | from langchain_core.runnables.history import RunnableWithMessageHistory
3 | from langchain.agents.agent import AgentExecutor
4 |
5 | dbgpt_history = ChatMessageHistory(session_id="dbgpt-session")
6 |
7 |
8 | class MemoryChatMessageHistory:
9 |
10 | @classmethod
11 | def manage_chat_history(cls, agent_executor: AgentExecutor) -> RunnableWithMessageHistory:
12 | """
13 | 创建并返回一个关联了聊天历史管理的执行代理。
14 | 此类方法首先创建一个聊天消息历史实例,随后将此历史与提供的执行代理关联起来,形成一个能够同时管理聊天历史的执行代理。
15 | 所创建的代理适用于需要追踪会话历史的场景,例如在对话系统中保存会话内容。
16 | 参数:
17 | agent_executor (AgentExecutor): 已有的代理执行器,负责执行具体的聊天任务。
18 | 返回:
19 | RunnableWithMessageHistory: 一个结合了聊天历史管理的执行代理,可以在执行聊天操作时维护历史记录。
20 | 注解:
21 | 在实际应用中,通常需要会话ID来跟踪和管理不同用户的聊天历史。这里虽然使用了简单的内存中聊天历史,
22 | 但结构设计上保留了使用会话ID的可能性,以便在更复杂的实现中使用。
23 | """
24 |
25 | agent_with_chat_history = RunnableWithMessageHistory(
26 | agent_executor,
27 | # This is needed because in most real world scenarios, a session id is needed
28 | # It isn't really used here because we are using a simple in memory ChatMessageHistory
29 | lambda session_id: dbgpt_history,
30 | input_messages_key="input",
31 | history_messages_key="history",
32 | )
33 | return agent_with_chat_history
34 |
--------------------------------------------------------------------------------
/prompts/sys_message.py:
--------------------------------------------------------------------------------
1 | SYSTEM_MESSAGE = """
2 | #Role:
3 | You are an expert SQL data analyst with professional skills mainly focused on database queries, and have the ability to explain and guide users on how to correctly handle specific fields in queries \n
4 | #Execute permissions:
5 | You only have the authority to query the database and are proficient in solving various database related problems for users and performing corresponding database operations \n
6 | #Key warning
7 | If there is a field is_del in the database table, it is used to mark whether the data has been deleted. If the value of "is_del" is 1, it indicates that the data has been logically deleted; If its value is 0, it indicates that the data still exists and has not been deleted yet \n
8 | Please analyze step by step \n
9 | answer the user's questions as best as possible \n
10 | #Format instructions:
11 | If there are multiple pieces of data, please organize them into a table and let me know in Chinese
12 | """
--------------------------------------------------------------------------------
/prompts/temple/__init__.py:
--------------------------------------------------------------------------------
1 | from .db_expert import DBExpert
--------------------------------------------------------------------------------
/prompts/temple/db_expert.py:
--------------------------------------------------------------------------------
1 | from langchain_core.prompts.chat import (
2 | ChatPromptTemplate,
3 | MessagesPlaceholder,
4 | )
5 | from prompts.sys_message import SYSTEM_MESSAGE
6 |
7 | class DBExpert:
8 |
9 | @classmethod
10 | def chat_prompt_message(cls) -> ChatPromptTemplate:
11 | prompt = ChatPromptTemplate.from_messages(
12 | [
13 | ("system", SYSTEM_MESSAGE),
14 | MessagesPlaceholder(variable_name="history"),
15 | ("human", "{input}"),
16 | MessagesPlaceholder(variable_name="agent_scratchpad")
17 | ]
18 | )
19 | return prompt
20 |
21 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | langchain==0.1.17
2 | pydantic==2.7.1
3 | openai==1.25.0
4 | python-dotenv==1.0.1
5 | fastapi==0.110.3
6 | uvicorn==0.29.0
7 | mysqlclient==2.2.4
8 | pytz==2024.1
9 | langfuse==2.28.0
10 | langchain-openai==0.1.6
11 | gradio==4.29.0
12 | socksio==1.0.0
13 | PyMySQL==1.1.1
14 | cryptography==42.0.7
--------------------------------------------------------------------------------
/tools/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/tools/__init__.py
--------------------------------------------------------------------------------
/utils/__init__.py:
--------------------------------------------------------------------------------
1 | from .database_utils import DatabaseUtils
--------------------------------------------------------------------------------
/utils/database_utils.py:
--------------------------------------------------------------------------------
1 | # utils/database_utils.py
2 | import os
3 |
4 | class DatabaseUtils:
5 | """
6 | Utility class for database-related operations.
7 | """
8 |
9 | @staticmethod
10 | def create_mysql_url() -> str:
11 | """
12 | Assemble MySQL connection URL from environment variables.
13 |
14 | :return: MySQL connection URL as a string
15 | """
16 | host = os.getenv("MYSQL_HOST")
17 | port = os.getenv("MYSQL_PORT")
18 | user = os.getenv("MYSQL_USER")
19 | password = os.getenv("MYSQL_PASSWORD")
20 | database = os.getenv("MYSQL_DATABASE")
21 | return f"mysql://{user}:{password}@{host}:{port}/{database}"
22 |
--------------------------------------------------------------------------------
/wait-for-it.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | # Use this script to test if a given TCP host/port are available
3 |
4 | set -e
5 |
6 | HOST=$1
7 | PORT=$2
8 | shift 2
9 | cmd="$@"
10 |
11 | until nc -z "$HOST" "$PORT"; do
12 | echo "Waiting for $HOST:$PORT..."
13 | sleep 1
14 | done
15 |
16 | >&2 echo "$HOST:$PORT is available"
17 | exec $cmd
18 |
--------------------------------------------------------------------------------
/web/app/routes/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sql-agi/DB-GPT/962f29fc2bdd150768f554993080433c68d48d38/web/app/routes/__init__.py
--------------------------------------------------------------------------------
/web/app/routes/db_gpt_route.py:
--------------------------------------------------------------------------------
1 |
2 | from fastapi import APIRouter, Request, HTTPException
3 | from web.app.service import DBGptService
4 | from typing import Dict
5 |
6 | # 获取配置中的logger对象
7 | from configs import log_config
8 | logger = log_config.logger
9 |
10 | router = APIRouter(
11 | prefix="/chat",
12 | tags=["error"],
13 | responses={404: {"description": "404 Not Found"}},
14 | )
15 |
16 |
17 | class DBGptRoute:
18 | @router.post("/db")
19 | async def db_gpt(request: Request) -> Dict[str, str]:
20 | """
21 | 处理通过 POST 请求发送到 '/db' 路径的数据,并返回聊天生成的回复。
22 | 此异步方法接收一个 JSON 格式的请求体,从中提取 'input' 字段作为输入,
23 | 然后调用 `DBGptService.db_gpt` 方法处理这个输入,并返回处理结果。
24 | 参数:
25 | request (Request): FastAPI的请求对象,包含客户端发送的HTTP请求信息。
26 | 返回:
27 | dict: 包含键 'reply' 的字典,其值为处理输入得到的回复。
28 | 抛出:
29 | HTTPException: 如果在处理请求或生成回复过程中发生任何异常,将返回500错误代码及异常详情。
30 | """
31 | try:
32 | body = await request.json()
33 | reply = DBGptService.db_gpt(body.get('input'))
34 | return {"reply": reply}
35 | except Exception as e:
36 | raise HTTPException(status_code=500, detail=str(e))
--------------------------------------------------------------------------------
/web/app/service/__init__.py:
--------------------------------------------------------------------------------
1 | from .db_gpt_service import DBGptService
--------------------------------------------------------------------------------
/web/app/service/db_gpt_service.py:
--------------------------------------------------------------------------------
1 | from fastapi import Request, HTTPException
2 | from agents import ChatDBAgent
3 |
4 |
5 | class DBGptService:
6 |
7 | @classmethod
8 | def db_gpt(cls, input: str) -> str:
9 | """
10 | 处理给定输入字符串,通过聊天数据库代理生成回复。
11 |
12 | 此类方法接收一个字符串输入,通过 `ChatDBAgent.db_gpt` 方法来处理这个输入,并尝试返回生成的回复。
13 | 如果在处理过程中遇到任何异常,会抛出 HTTP 异常。
14 | 参数:
15 | input (str): 需要处理的输入字符串。
16 | 返回:
17 | str: 聊天代理根据输入生成的回复。
18 | 抛出:
19 | HTTPException: 如果在处理输入或生成回复的过程中发生异常,将返回状态码为500的HTTP异常,并包含异常的详细信息。
20 | """
21 | try:
22 | reply = ChatDBAgent.db_gpt(input)
23 | return reply
24 | except Exception as e:
25 | raise HTTPException(status_code=500, detail=str(e))
26 |
--------------------------------------------------------------------------------
/web_demo.py:
--------------------------------------------------------------------------------
1 | import gradio as gr
2 | from agents import ChatDBAgent
3 |
4 |
5 |
6 |
7 |
8 | def predict(message, history):
9 | """后续会重构,过渡阶段"""
10 | print(message)
11 | response = ChatDBAgent.db_gpt(message)
12 | partial_message = ""
13 | print(response)
14 | for chunk in response:
15 | if chunk is not None:
16 | partial_message = partial_message + chunk
17 | yield partial_message
18 |
19 |
20 | gr.ChatInterface(
21 | predict,
22 | title="CHAT_DB",
23 | description="Ask DB_BOT any question",
24 | theme="soft"
25 | ).launch(server_name="0.0.0.0", server_port=7860)
26 |
--------------------------------------------------------------------------------