Commit 396700dd authored by chenzk's avatar chenzk
Browse files

v1.0

parents
Pipeline #2603 failed with stages
in 0 seconds
create database test_case_info character set utf8;
use test_case_info;
CREATE TABLE test_cases (
case_id INT AUTO_INCREMENT PRIMARY KEY,
scenario_name VARCHAR(100) COMMENT '场景名称',
scenario_description TEXT COMMENT '场景描述',
test_question VARCHAR(500) COMMENT '测试问题',
expected_sql TEXT COMMENT '预期SQL',
correct_output TEXT COMMENT '正确输出'
) COMMENT '测试用例表';
INSERT INTO test_cases (scenario_name, scenario_description, test_question, expected_sql, correct_output) VALUES
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询所有学生的姓名,专业和成绩,按成绩降序排序', 'SELECT students.student_name, students.major, scores.score FROM students JOIN scores ON students.student_id = scores.student_id ORDER BY scores.score DESC;', '返回所有学生的姓名,专业和成绩,按成绩降序排序的结果'),
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询计算机科学专业的学生的平均成绩', 'SELECT AVG(scores.score) as avg_score FROM students JOIN scores ON students.student_id = scores.student_id WHERE students.major = ''计算机科学'';', '返回计算机科学专业学生的平均成绩'),
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询哪些学生在2023年秋季学期的课程学分总和超过15', 'SELECT students.student_name FROM students JOIN scores ON students.student_id = scores.student_id JOIN courses ON scores.course_id = courses.course_id WHERE scores.semester = ''2023年秋季'' GROUP BY students.student_id HAVING SUM(courses.credit) > 15;', '返回在2023年秋季学期的课程学分总和超过15的学生的姓名'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询每个用户的总订单数量', 'SELECT users.user_name, COUNT(orders.order_id) as order_count FROM users JOIN orders ON users.user_id = orders.user_id GROUP BY users.user_id;', '返回每个用户的总订单数量'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询每种商品的总销售额', 'SELECT products.product_name, SUM(products.product_price * orders.quantity) as total_sales FROM products JOIN orders ON products.product_id = orders.product_id GROUP BY products.product_id;', '返回每种商品的总销售额'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询2023年最受欢迎的商品(订单数量最多的商品)', 'SELECT products.product_name FROM products JOIN orders ON products.product_id = orders.product_id WHERE YEAR(orders.order_date) = 2023 GROUP BY products.product_id ORDER BY COUNT(orders.order_id) DESC LIMIT 1;', '返回2023年最受欢迎的商品(订单数量最多的商品)的名称');
CREATE TABLE test_cases (
case_id INTEGER PRIMARY KEY AUTOINCREMENT,
scenario_name VARCHAR(100),
scenario_description TEXT,
test_question VARCHAR(500),
expected_sql TEXT,
correct_output TEXT
);
INSERT INTO test_cases (scenario_name, scenario_description, test_question, expected_sql, correct_output) VALUES
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询所有学生的姓名,专业和成绩,按成绩降序排序', 'SELECT students.student_name, students.major, scores.score FROM students JOIN scores ON students.student_id = scores.student_id ORDER BY scores.score DESC;', '返回所有学生的姓名,专业和成绩,按成绩降序排序的结果'),
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询计算机科学专业的学生的平均成绩', 'SELECT AVG(scores.score) as avg_score FROM students JOIN scores ON students.student_id = scores.student_id WHERE students.major = ''计算机科学'';', '返回计算机科学专业学生的平均成绩'),
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询哪些学生在2023年秋季学期的课程学分总和超过15', 'SELECT students.student_name FROM students JOIN scores ON students.student_id = scores.student_id JOIN courses ON scores.course_id = courses.course_id WHERE scores.semester = ''2023年秋季'' GROUP BY students.student_id HAVING SUM(courses.credit) > 15;', '返回在2023年秋季学期的课程学分总和超过15的学生的姓名'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询每个用户的总订单数量', 'SELECT users.user_name, COUNT(orders.order_id) as order_count FROM users JOIN orders ON users.user_id = orders.user_id GROUP BY users.user_id;', '返回每个用户的总订单数量'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询每种商品的总销售额', 'SELECT products.product_name, SUM(products.product_price * orders.quantity) as total_sales FROM products JOIN orders ON products.product_id = orders.product_id GROUP BY products.product_id;', '返回每种商品的总销售额'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询2023年最受欢迎的商品(订单数量最多的商品)', 'SELECT products.product_name FROM products JOIN orders ON products.product_id = orders.product_id WHERE YEAR(orders.order_date) = 2023 GROUP BY products.product_id ORDER BY COUNT(orders.order_id) DESC LIMIT 1;', '返回2023年最受欢迎的商品(订单数量最多的商品)的名称');
CREATE SCHEMA test_case_info;
COMMENT ON SCHEMA test_case_info is '测试用例信息';
SET SEARCH_PATH = test_case_info;
CREATE TABLE test_cases (
case_id SERIAL /*INT AUTO_INCREMENT*/ PRIMARY KEY,
scenario_name VARCHAR(100),
scenario_description VARCHAR(6500),
test_question VARCHAR(500),
expected_sql VARCHAR(6500),
correct_output VARCHAR(6500)
);
COMMENT ON TABLE test_cases IS '测试用例表';
COMMENT ON COLUMN test_cases.scenario_name IS '场景名称';
COMMENT ON COLUMN test_cases.scenario_description IS '场景描述';
COMMENT ON COLUMN test_cases.test_question IS '测试问题';
COMMENT ON COLUMN test_cases.expected_sql IS '预期SQL';
COMMENT ON COLUMN test_cases.correct_output IS '正确输出';
INSERT INTO test_cases (scenario_name, scenario_description, test_question, expected_sql, correct_output) VALUES
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询所有学生的姓名,专业和成绩,按成绩降序排序', 'SELECT students.student_name, students.major, scores.score FROM students JOIN scores ON students.student_id = scores.student_id ORDER BY scores.score DESC;', '返回所有学生的姓名,专业和成绩,按成绩降序排序的结果'),
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询计算机科学专业的学生的平均成绩', 'SELECT AVG(scores.score) as avg_score FROM students JOIN scores ON students.student_id = scores.student_id WHERE students.major = ''计算机科学'';', '返回计算机科学专业学生的平均成绩'),
('学校管理系统', '测试SQL助手的联合查询,条件查询和排序功能', '查询哪些学生在2023年秋季学期的课程学分总和超过15', 'SELECT students.student_name FROM students JOIN scores ON students.student_id = scores.student_id JOIN courses ON scores.course_id = courses.course_id WHERE scores.semester = ''2023年秋季'' GROUP BY students.student_id HAVING SUM(courses.credit) > 15;', '返回在2023年秋季学期的课程学分总和超过15的学生的姓名'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询每个用户的总订单数量', 'SELECT users.user_name, COUNT(orders.order_id) as order_count FROM users JOIN orders ON users.user_id = orders.user_id GROUP BY users.user_id;', '返回每个用户的总订单数量'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询每种商品的总销售额', 'SELECT products.product_name, SUM(products.product_price * orders.quantity) as total_sales FROM products JOIN orders ON products.product_id = orders.product_id GROUP BY products.product_id;', '返回每种商品的总销售额'),
('电商系统', '测试SQL助手的数据聚合和分组功能', '查询2023年最受欢迎的商品(订单数量最多的商品)', 'SELECT products.product_name FROM products JOIN orders ON products.product_id = orders.product_id WHERE YEAR(orders.order_date) = 2023 GROUP BY products.product_id ORDER BY COUNT(orders.order_id) DESC LIMIT 1;', '返回2023年最受欢迎的商品(订单数量最多的商品)的名称');
COMMIT;
USE mysql;
UPDATE user SET Host='%' WHERE User='root';
FLUSH PRIVILEGES;
\ No newline at end of file
docker run -it -p 5670:5670 --shm-size=64G -v $PWD/DB-GPT:/home/DB-GPT -v /public/DL_DATA/AI:/home/AI -v /opt/hyhal:/opt/hyhal:ro --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name dbgpt e77c15729879 bash
# Dependencies
/node_modules
# Production
/build
# Generated files
.docusaurus
.cache-loader
# Misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.idea
/bk
.history
\ No newline at end of file
FROM node:lts
WORKDIR /app
COPY . /app
RUN yarn install
RUN npm run build
EXPOSE 3000
CMD ["npm", "run", "serve"]
FROM node:lts-alpine as build
RUN apk add --no-cache git
ARG NPM_REGISTRY=https://registry.npmjs.org
ENV NPM_REGISTRY=$NPM_REGISTRY
# Set github CI environment variable
ARG CI=true
ENV CI=$CI
WORKDIR /app
# Copy package.json and package-lock.json to a separate build directory
COPY docs/package*.json /app-build/docs/
# Install dependencies in the separate build directory
RUN cd /app-build/docs && \
npm config set registry $NPM_REGISTRY && \
npm ci
# Copy the rest of the application to /app and /app-build
COPY . /app-build
COPY . /app
# Make sure we have the latest version of the repository
RUN if [ "$CI" = "true" ]; then \
# Check if the repo is shallow before trying to unshallow it
if git rev-parse --is-shallow-repository | grep -q 'true'; then \
git fetch --prune --unshallow; \
else \
echo "Repository is already complete, skipping unshallow"; \
git fetch --prune; \
fi; \
fi
ARG NUM_VERSION=2
ENV NUM_VERSION=$NUM_VERSION
# Commit the changes to the repository, just for local testing
# Sometimes, we just want to test the changes in the Dockerfile
RUN git config --global user.email "dbgpt@example.com" && \
git config --global user.name "DB-GPT" && \
git add . && git commit --no-verify -m "Commit message" || exit 1
# New logic for building versions directly in Dockerfile
RUN git config --global --add safe.directory /app && \
# Record the current position
CURRENT_POSITION=$(git rev-parse --abbrev-ref HEAD) && \
# Get the latest tags
TAGS=$(git tag --sort=-creatordate | head -n $NUM_VERSION | tac) && \
# If there are no tags, get the latest commits
if [ -z "$TAGS" ]; then \
TAGS=$(git log --format="%h" -n $NUM_VERSION | tac); \
fi && \
for TAG in $TAGS; do \
echo "Creating version $TAG"; \
cd /app/docs && git checkout . && git checkout $TAG; \
echo "Checked out to tag: $TAG"; \
# Check if there is a patch for the current version in app-build
echo "Checking patch in /app-build/docs/patchs..." && \
if [ -f "/app-build/docs/patchs/fix_${TAG/v/}.patch" ]; then \
echo "Found patch for version $TAG in /app-build/docs/patchs, applying..."; \
cd /app && \
git apply "/app-build/docs/patchs/fix_${TAG/v/}.patch" && \
echo "Patch applied successfully" || \
echo "Failed to apply patch for $TAG"; \
echo "Current sidebars.js content:"; \
cat /app/docs/sidebars.js; \
else \
echo "No patch found for $TAG in /app-build/docs/patchs"; \
fi; \
# Copy the necessary files to the build directory for each tag
rm -rf /app-build/docs/docs /app-build/docs/sidebars.js /app-build/docs/static /app-build/docs/src && \
cp -r /app/docs/docs /app-build/docs/ && \
cp /app/docs/sidebars.js /app-build/docs/ && \
cp -r /app/docs/static /app-build/docs/ && \
cp -r /app/docs/src /app-build/docs/; \
# Create a new version
cd /app-build/docs && npm run docusaurus docs:version $TAG || exit 1; \
done && \
# Return to the original position, build dev version
cd /app && git checkout . && git checkout $CURRENT_POSITION && \
cd /app/docs && \
rm -rf /app-build/docs/docs /app-build/docs/sidebars.js /app-build/docs/static /app-build/docs/src && \
cp -r /app/docs/docs /app-build/docs/ && \
cp /app/docs/sidebars.js /app-build/docs/ && \
cp -r /app/docs/static /app-build/docs/ && \
cp -r /app/docs/src /app-build/docs/ || exit 1; \
cd /app-build/docs && npm run build && \
echo $TAGS | tr ' ' '\n' | tac > /app-build/docs/build/versions.txt && \
echo "latest" >> /app-build/docs/build/versions.txt && \
echo "Built versions:" && \
cat /app-build/docs/build/versions.txt
# For production
FROM nginx:alpine
# Copy the nginx configuration file
# COPY nginx.conf /etc/nginx/nginx.conf
# Copy the build output to replace the default nginx contents.
COPY --from=build /app-build/docs/build /usr/share/nginx/html
COPY --from=build /app-build/docs/versioned_docs/ /usr/share/nginx/html/versioned_docs/
COPY --from=build /app-build/docs/versioned_sidebars/ /usr/share/nginx/html/versioned_sidebars/
RUN echo '#!/bin/sh' > /usr/share/nginx/html/versions.sh && \
echo 'echo "Available versions:"' >> /usr/share/nginx/html/versions.sh && \
echo 'cat /usr/share/nginx/html/versions.txt' >> /usr/share/nginx/html/versions.sh && \
chmod +x /usr/share/nginx/html/versions.sh
EXPOSE 80
# Start Nginx server
CMD ["nginx", "-g", "daemon off;"]
\ No newline at end of file
.env
./.mypy_cache/
models/
plugins/
pilot/data
pilot/message
logs/
venv/
web/node_modules/
web/.next/
web/.env
docs/node_modules/
build/
docs/build/
docs/Dockerfile-deploy
\ No newline at end of file
# DB-GPT documentation
## Quick Start
### Install dependencies
- Clone current project firstly!
- Install docusaurus dependencies, generate node_modules folder.
```
yarn install
```
### launch
```
yarn start
```
The default service starts on port `3000`, visit `localhost:3000`
## Deploy Multi-Version Documentation
We can deploy multiple versions of the documentation by docker.
### Build Docker Image
Firstly, build the docker image in `DB-GPT` project root directory.
```bash
# Use the default NPM_REGISTRY=https://registry.npmjs.org
# Use https://www.npmmirror.com/
NPM_REGISTRY=https://registry.npmmirror.com
docker build -f docs/Dockerfile-deploy \
-t eosphorosai/dbgpt-docs \
--build-arg NPM_REGISTRY=$NPM_REGISTRY \
--build-arg CI=false \
--build-arg NUM_VERSION=2 .
```
### Run Docker Container
Run the docker container with the following command:
```bash
docker run -it --rm -p 8089:8089 \
--name my-dbgpt-docs \
-v $(pwd)/docs/nginx/nginx-docs.conf:/etc/nginx/nginx.conf \
eosphorosai/dbgpt-docs
```
Open the browser and visit `localhost:8089` to see the documentation.
\ No newline at end of file
module.exports = {
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
};
---
slug: db-gpt-llama-3.1-support
title: DB-GPT Now Supports Meta Llama 3.1 Series Models
authors: fangyinc
tags: [llama, LLM]
---
We are thrilled to announce that DB-GPT now supports inference with the Meta Llama 3.1 series models!
## Introducing Meta Llama 3.1
Meta Llama 3.1 is a state-of-the-art series of language models developed by Meta AI. Designed with cutting-edge techniques, the Llama 3.1 models offer unparalleled performance and versatility. Here are some of the key highlights:
- **Variety of Models**: Meta Llama 3.1 is available in 8B, 70B, and 405B versions, each with both instruction-tuned and base models, supporting contexts up to 128k tokens.
- **Multilingual Support**: Supports 8 languages, including English, German, and French.
- **Extensive Training**: Trained on over 1.5 trillion tokens, utilizing 250 million human and synthetic samples for fine-tuning.
- **Flexible Licensing**: Permissive model output usage allows for adaptation into other large language models (LLMs).
- **Quantization Support**: Available in FP8, AWQ, and GPTQ quantized versions for efficient inference.
- **Performance**: The Llama 3 405B version has outperformed GPT-4 in several benchmarks.
- **Enhanced Efficiency**: The 8B and 70B models have seen a 12% improvement in coding and instruction-following capabilities.
- **Tool and Function Call Support**: Supports tool usage and function calling.
## How to Access Meta Llama 3.1
Your can access the Meta Llama 3.1 models according to [Access to Hugging Face](https://github.com/meta-llama/llama-models?tab=readme-ov-file#access-to-hugging-face).
For comprehensive documentation and additional details, please refer to the [model card](https://github.com/meta-llama/llama-models/blob/main/models/llama3_1/MODEL_CARD.md).
## Using Meta Llama 3.1 in DB-GPT
Please read the [Source Code Deployment](../docs/installation/sourcecode) to learn how to install DB-GPT from source code.
Llama 3.1 needs upgrade your transformers >= 4.43.0, please upgrade your transformers:
```bash
pip install --upgrade "transformers>=4.43.0"
```
Please cd to the DB-GPT root directory:
```bash
cd DB-GPT
```
We assume that your models are stored in the `models` directory, e.g., `models/Meta-Llama-3.1-8B-Instruct`.
Then modify your `.env` file:
```env
LLM_MODEL=meta-llama-3.1-8b-instruct
# LLM_MODEL=meta-llama-3.1-70b-instruct
# LLM_MODEL=meta-llama-3.1-405b-instruct
## you can also specify the model path
# LLM_MODEL_PATH=models/Meta-Llama-3.1-8B-Instruct
## Quantization settings
# QUANTIZE_8bit=False
# QUANTIZE_4bit=True
## You can configure the maximum memory used by each GPU.
# MAX_GPU_MEMORY=16Gib
```
Then you can run the following command to start the server:
```bash
dbgpt start webserver
```
Open your browser and visit `http://localhost:5670` to use the Meta Llama 3.1 models in DB-GPT.
Enjoy the power of Meta Llama 3.1 in DB-GPT!
# DB-GPT V0.6.0, Defining new standards for AI-native data applications.
## Introduction
DB-GPT is an open source AI native data application development framework with AWEL and agents. In the V0.6.0 version, we further provide flexible and scalable AI native data application management and development capabilities around large models, which can help enterprises quickly build and deploy intelligent AI data applications, and achieve enterprise digital transformation and business growth through intelligent data analysis, insights and decisions
### The V0.6.0 version mainly adds and enhances the following core features
- AWEL protocol upgrade 2.0, supporting more complex orchestration
- Supports the creation and lifecycle management of data applications, and supports multiple application construction modes, such as: multi-agent automatic planning mode, task flow orchestration mode, single agent mode, and native application mode
- GraphRAG supports graph community summary and hybrid retrieval, and the graph index cost is reduced by 50% compared to Microsoft GraphRAG.
- Supports multiple Agent Memories, such as perceptual memory, short-term memory, long-term memory, hybrid memory, etc.
- Supports intent recognition and prompt management, and newly added support for Text2NLU and Text2GQL fine-tuning
- GPT-Vis front-end visualization upgrade to support richer visualization charts
## Features
**AWEL protocol upgrade 2.0 supports more complex orchestration and optimizes front-end visualization and interaction capabilities.**
AWEL (Agentic Workflow Expression Language) is an agent-based workflow expression language designed specifically for large model application development, providing powerful functions and flexibility. Through the AWEL API, developers can focus on large model application logic development without having to pay attention to cumbersome model, environment and other details. In AWEL2.0, we support more complex orchestration and visualization
<p align="center">
<img src={'/img/app/agent_prompt_awel_v0.6.jpg'} width="800px" />
</p>
**Supports the creation and life cycle management of data applications, and supports multiple modes to build applications, such as: multi-agent automatic planning mode, task flow orchestration mode, single agent mode, and native application mode**
<p align="center">
<img src={'/img/app/app_manage_mode_v0.6.jpg'} width="800px" />
</p>
<p align="center">
<img src={'/img/app/app_manage_app_v0.6.jpg'} width="800px" />
</p>
**GraphRAG supports graph community summarization and hybrid retrieval.**
The graph construction and retrieval performance have obvious advantages compared to community solutions, and it supports cool visualization. GraphRAG is an enhanced retrieval generation system based on knowledge graphs. Through the construction and retrieval of knowledge graphs, it further enhances the accuracy of retrieval and the stability of recall, while reducing the illusion of large models and enhancing the effects of domain applications. DB-GPT combines with TuGraph to build efficient retrieval enhancement generation capabilities
<p align="center">
<img src={'/img/app/graph_rag_pipeline_v0.6.png'} width="800px" />
</p>
Based on the universal RAG framework launched in DB-GPT version 0.5.6 that integrates vector index, graph index, and full-text index, DB-GPT version 0.6.0 has enhanced the capabilities of graph index (GraphRAG) to support graph community summary and hybrid retrieval. ability. In the new version, we introduced TuGraph’s built-in Leiden community discovery algorithm, combined with large models to extract community subgraph summaries, and finally used similarity recall of community summaries to cope with generalized questioning scenarios, namely QFS (Query Focused Summarization). question. In addition, in the knowledge extraction stage, we upgraded the original triple extraction to graph extraction with point edge information summary, and optimized cross-text block associated information extraction through text block history to further enhance the information density of the knowledge graph.
Based on the above design, we used the open source knowledge graph corpus (OSGraph) provided by the TuGraph community and the product introduction materials of DB-GPT and TuGraph (about 43k tokens in total), and conducted comparative tests with Microsoft's GraphRAG system. Finally, DB-GPT It only consumes 50% of the token overhead and generates a knowledge graph of the same scale. And on the premise that the quality of the question and answer test is equivalent, the global search performance has been significantly improved.
<p align="center">
<img src={'/img/app/graph_rag_v0.6.png'} width="800px" />
</p>
For the final generated knowledge graph, we used AntV's G6 engine to upgrade the front-end rendering logic, which can intuitively preview the knowledge graph data and community segmentation results.
<p align="center">
<img src={'/img/app/graph_rag_display_v0.6.png'} width="800px" />
</p>
**GPT-Vis: GPT-Vis is an interactive visualization solution for LLM and data, supporting rich visual chart display and intelligent recommendations**
<p align="center">
<img src={'/img/app/app_chat_v0.6.jpg'} width="800px" />
</p>
**Text2GQL and Text2NLU fine-tuning: Newly supports fine-tuning from natural language to graph language, as well as fine-tuning for semantic classification.**
<p align="center">
<img src={'/img/ft/ft_pipeline.jpg'} width="800px" />
</p>
## Acknowledgements
This iteration is inseparable from the participation of developers and users in the community, and it also further cooperates with the [TuGraph](https://github.com/TuGraph-family) and [AntV](https://github.com/antvis) communities. Thanks to all the contributors who made this release possible!
@Aries-ckt, @Dreammy23, @Hec-gitHub, @JxQg, @KingSkyLi, @M1n9X, @bigcash, @chaplinthink, @csunny, @dusens, @fangyinc, @huangjh131, @hustcc, @lhwan, @whyuds and @yhjun1026
## Reference
- [中文手册](https://www.yuque.com/eosphoros/dbgpt-docs/ym574wh2hddunfbd)
\ No newline at end of file
---
slug: db-gpt-v070-release
tags: [DeepSeek, LLM, MCP]
---
# DB-GPT V0.7.0, MCP + DeepSeek R1: Bringing More Possibilities to LLM Applications
**DB-GPT V0.7.0 Release: MCP Protocol Support, DeepSeek R1 Model Integration, Complete Architecture Upgrade, GraphRAG Retrieval Chain Enhancement, and More..**
`DB-GPT` is an open-source AI Native Data App Development framework with AWEL and Agents. In version `V0.7.0`, we have reorganized the `DB-GPT` module packages, splitting the original modules, restructuring the entire framework configuration system, and providing a clearer, more flexible, and more extensible management and development capability for building AI native data applications around large models.
## V0.7.0 version mainly adds and enhances the following core features
🍀 **Support for** `MCP(Model Context Protocol)` **protocol.**
🍀 **Integrated** `DeepSeek R1`, `QWQ` **inference models, all original** `DB-GPT` **Chat scenarios now cover deep thinking capabilities.**
🍀 `GraphRAG` **retrieval chain enhancement: support for "Vector" and "Intent Recognition+Text2GQL" graph retrievers.**
🍀 `DB-GPT` **module package restructuring, original** `dbgpt` **package split into** `dbgpt-core`, `dbgpt-ext`, `dbgpt-serve`, `dbgpt-client`, `dbgpt-acclerator`, `dbgpt-app`.
🍀 **Reconstructed** `DB-GPT` **configuration system, configuration files changed to "**`.toml`**" format, abolishing the original** `.env` **configuration logic.**
## ✨New Features
### 1. **Support for** `MCP(Model Context Protocol)` **protocol**
Usage instructions:
a. Run the MCP SSE Server gateway:
```bash
npx -y supergateway --stdio "uvx mcp-server-fetch"
```
Here we are running the web scraping `mcp-server-fetch`
b. Create a `Multi-agent`+ `Auto-Planning`+ `MCP` web page scraping and summarization APP.
<p align="center">
<img src={'/img/v070/mcp_create_app.png'} width="800px"/>
</p>
c. Configure the APP, select the `ToolExpert` and `Summarizer` agents, and add a resource of type `tool(mcp(sse))` to `ToolExpert`, where `mcp_servers` should be filled with the service address started in step a (default is: `http://127.0.0.1:8000/sse`), then save the application.
<p align="center">
<img src={'/img/v070/mcp_config_app.png'} width="800px"/>
</p>
d. Select the newly created `MCP Web Fetch` APP to chat, provide a webpage for the APP to summarize:
<p align="center">
<img src={'/img/v070/mcp_chat_app.png'} width="800px"/>
</p>
The example input question is: `What does this webpage talk about https://www.cnblogs.com/fnng/p/18744210"`
### 2. Integrated DeepSeek R1 inference model
**And all Chat scenarios in original DB-GPT now have deep thinking capabilities.**
For quick usage reference: [http://docs.dbgpt.cn/docs/next/quickstart](http://docs.dbgpt.cn/docs/next/quickstart)
Data analysis scenario:
<p align="center">
<img src={'/img/v070/data_analysis.png'} width="800px" />
</p>
Knowledge base scenario:
<p align="center">
<img src={'/img/v070/Knowledge_thinking.png'} width="800px" />
</p>
### 3. `GraphRAG` **retrieval chain enhancement: support for "Vector" and "Intent Recognition+Text2GQL" graph retrievers.**
+ **"Vector" graph retriever**
During the knowledge graph construction process, vectors are added to all nodes and edges and indexes are established. When querying, the question is vectorized and through TuGraph-DB's built-in vector indexing capability, based on the HNSW algorithm, topk related nodes and edges are queried. Compared to keyword graph retrieval, it can identify more ambiguous questions.
<p align="center">
<img src={'/img/v070/graphrag_1.png'} width="800px"/>
</p>
Configuration example:
```toml
[rag.storage.graph]
type = "TuGraph"
host="127.0.0.1"
port=7687
username="admin"
password="73@TuGraph"
enable_summary="True"
triplet_graph_enabled="True"
document_graph_enabled="True"
# Vector graph retrieval configuration items
enable_similarity_search="True"
knowledge_graph_embedding_batch_size=20
similarity_search_topk=5
extract_score_threshold=0.7
```
+ **"Intent Recognition+Text2GQL" graph retriever**
The question is rewritten through the intent recognition module, extracting true intent and involved entities and relationships, and then translated using the Text2GQL model into GQL statements for direct querying. It can perform more precise graph queries and display corresponding query statements. In addition to calling large model API services, you can also use ollama to call local Text2GQL models.
<p align="center">
<img src={'/img/v070/graphrag_2.png'} width="800px"/>
</p>
Configuration example:
```toml
[rag.storage.graph]
type = "TuGraph"
host="127.0.0.1"
port=7687
username="admin"
password="73@TuGraph"
enable_summary="True"
triplet_graph_enabled="True"
document_graph_enabled="True"
# Intent Recognition+Text2GQL graph retrieval configuration items
enable_text_search="True"
# Use Ollama to deploy independent text2gql model, enable the following configuration items
# text2gql_model_enabled="True"
# text2gql_model_name="tugraph/CodeLlama-7b-Cypher-hf:latest"
```
### 4. `DB-GPT` module package restructuring
Original `dbgpt` package split into `dbgpt-core`, `dbgpt-ext`, `dbgpt-serve`, `dbgpt-client`, `dbgpt-acclerator`, `dbgpt-app`
As dbgpt has gradually developed, the service modules have increased, making functional regression testing difficult and compatibility issues more frequent. Therefore, the original dbgpt content has been modularized:
+ **dbgpt-core**: Mainly responsible for core module interface definitions of dbgpt's awel, model, agent, rag, storage, datasource, etc., releasing Python SDK.
+ **dbgpt-ext**: Mainly responsible for implementing dbgpt extension content, including datasource extensions, vector-storage, graph-storage extensions, and model access extensions, making it easier for community developers to quickly use and extend new module content, releasing Python SDK.
+ **dbgpt-serve**: Mainly provides Restful interfaces for dbgpt's atomized services of each module, making it easy for community users to quickly integrate. No Python SDK is released at this time.
+ **dbgpt-app**: Mainly responsible for business scenario implementations such as `App`, `ChatData`, `ChatKnowledge`, `ChatExcel`, `Dashboard`, etc., with no Python SDK.
+ **dbgpt-client**: Provides a unified Python SDK client for integration.
+ **dbgpt-accelerator:** Model inference acceleration module, including compatibility and adaptation for different versions (different torch versions, etc.), platforms (Windows, MacOS, and Linux), hardware environments (CPU, CUDA, and ROCM), inference frameworks (vLLM, llama.cpp), quantization methods (AWQ, bitsandbytes, GPTQ), and other acceleration modules (accelerate, flash-attn), providing cross-platform, installable underlying environments on-demand for other DB-GPT modules.
### 5. Restructured DB-GPT configuration system
The new configuration files using `".toml"` format, abolishing the original `.env` configuration logic, each module can have its own configuration class, and automatically generate front-end configuration pages.
For quick usage reference: [http://docs.dbgpt.cn/docs/next/quickstart](http://docs.dbgpt.cn/docs/next/quickstart)
For all configurations reference: [http://docs.dbgpt.cn/docs/next/config-reference/app/config_chatdashboardconfig_2480d0](http://docs.dbgpt.cn/docs/next/config-reference/app/config_chatdashboardconfig_2480d0)
```toml
[system]
# Load language from environment variable(It is set by the hook)
language = "${env:DBGPT_LANG:-zh}"
api_keys = []
encrypt_key = "your_secret_key"
# Server Configurations
[service.web]
host = "0.0.0.0"
port = 5670
[service.web.database]
type = "sqlite"
path = "pilot/meta_data/dbgpt.db"
[service.model.worker]
host = "127.0.0.1"
[rag.storage]
[rag.storage.vector]
type = "chroma"
persist_path = "pilot/data"
# Model Configurations
[models]
[[models.llms]]
name = "deepseek-reasoner"
# name = "deepseek-chat"
provider = "proxy/deepseek"
api_key = "your_deepseek_api_key"
```
### 6. **Support for S3, OSS storage**
DB-GPT unified storage extension OSS and S3 implementation, where the S3 implementation supports most cloud storage compatible with the S3 protocol. DB-GPT knowledge base original files, Chat Excel related intermediate files, AWEL Flow node parameter files, etc. all support cloud storage.
Configuration example:
```toml
[[serves]]
type = "file"
# Default backend for file server
default_backend = "s3"
[[serves.backends]]
type = "oss"
endpoint = "https://oss-cn-beijing.aliyuncs.com"
region = "oss-cn-beijing"
access_key_id = "${env:OSS_ACCESS_KEY_ID}"
access_key_secret = "${env:OSS_ACCESS_KEY_SECRET}"
fixed_bucket = "{your_bucket_name}"
[[serves.backends]]
# Use Tencent COS s3 compatible API as the file server
type = "s3"
endpoint = "https://cos.ap-beijing.myqcloud.com"
region = "ap-beijing"
access_key_id = "${env:COS_SECRETID}"
access_key_secret = "${env:COS_SECRETKEY}"
fixed_bucket = "{your_bucket_name}
```
For detailed configuration instructions, please refer to: [http://docs.dbgpt.cn/docs/next/config-reference/utils/config_s3storageconfig_f0cdc9](http://docs.dbgpt.cn/docs/next/config-reference/utils/config_s3storageconfig_f0cdc9)
### 7. **Production-level llama.cpp inference support**
Based on llama.cpp HTTP Server, supporting continuous batching, multi-user parallel inference, etc., llama.cpp inference moves towards production systems.
Configuration example:
```toml
# Model Configurations
[models]
[[models.llms]]
name = "DeepSeek-R1-Distill-Qwen-1.5B"
provider = "llama.cpp.server"
# If not provided, the model will be downloaded from the Hugging Face model hub
# uncomment the following line to specify the model path in the local file system
# https://huggingface.co/bartowski/DeepSeek-R1-Distill-Qwen-1.5B-GGUF
# path = "the-model-path-in-the-local-file-system"
path = "models/DeepSeek-R1-Distill-Qwen-1.5B-Q4_K_M.gguf
```
### 8. **Multi-model deployment persistence**
Currently, most models can be integrated on the DB-GPT page, with configuration information persistently saved and models automatically loaded when the service starts.
<p align="center">
<img src={'/img/v070/model_deploy.png'} width="800px"/>
</p>
### 9. **LLM, Embedding, Reranker extension capability enhancement**
Optimized the model extension approach, requiring only a few lines of code to integrate new models.
### 10. **Native scenario support for conversation-round and token-based memory, with independent configuration support for each scenario**
Configuration example:
```toml
[app]
# Unified temperature configuration for all scenarios
temperature = 0.6
[[app.configs]]
name = "chat_excel"
# Use custom temperature configuration
temperature = 0.1
duckdb_extensions_dir = []
force_install = true
[[app.configs]]
name = "chat_normal"
memory = {type="token", max_token_limit=20000}
[[app.configs]]
name = "chat_with_db_qa"
schema_retrieve_top_k = 50
memory = {type="window", keep_start_rounds=0, keep_end_rounds=10}
```
### 11. **Chat Excel, Chat Data & Chat DB and Chat Dashboard native scenario optimization**
+ Chat Data, Chat Dashboard support for streaming output.
+ Optimization of library table field knowledge processing and recall
+ Chat Excel optimization, supporting more complex table understanding and chart conversations, even small parameter-scale open-source LLMs can handle it well.
<p align="center">
<img src={'/img/v070/chat_excel.png'} width="800px"/>
</p>
### 12. **Front-end page support for LaTeX mathematical formula rendering**
<p align="center">
<img src={'/img/v070/chat_latex.png'} width="800px"/>
</p>
### 13. **AWEL Flow support for simple conversation templates**
<p align="center">
<img src={'/img/v070/simple_awel.png'} width="800px"/>
</p>
### 14. **Support for lightweight Docker images containing only proxy models (arm64 & amd64)**
One-click deployment command for DB-GPT:
```bash
docker run -it --rm -e SILICONFLOW_API_KEY=${SILICONFLOW_API_KEY} \
-p 5670:5670 --name dbgpt eosphorosai/dbgpt-openai
```
You can also use the build script to build your own image:
```bash
bash docker/base/build_image.sh --install-mode openai
```
For details, see the documentation: [http://docs.dbgpt.cn/docs/next/installation/docker-build-guide](http://docs.dbgpt.cn/docs/next/installation/docker-build-guide)
### 15. **DB-GPT API compatible with OpenAI SDK**
```python
from openai import OpenAI
DBGPT_API_KEY = "dbgpt"
client = OpenAI(
api_key=DBGPT_API_KEY,
base_url="http://localhost:5670/api/v2",
)
messages = [
{
"role": "user",
"content": "Hello, how are you?",
},
]
has_thinking = False
reasoning_content = ""
for chunk in client.chat.completions.create(
model="deepseek-chat",
messages=messages,
extra_body={
"chat_mode": "chat_normal",
},
stream=True,
max_tokens=4096,
):
delta_content = chunk.choices[0].delta.content
if hasattr(chunk.choices[0].delta, "reasoning_content"):
reasoning_content = chunk.choices[0].delta.reasoning_content
if reasoning_content:
if not has_thinking:
print("<thinking>", flush=True)
print(reasoning_content, end="", flush=True)
has_thinking = True
if delta_content:
if has_thinking:
print("</thinking>", flush=True)
print(delta_content, end="", flush=True)
has_thinking = False
```
### 16. **Data source extension capability enhancement**
After the backend supports new data sources, the frontend can automatically identify and dynamically configure them.
<p align="center">
<img src={'/img/v070/datasource.png'} width="800px"/>
</p>
### 17. **Agent resource support for dynamic parameter configuration**
Frontend automatically identifies resource configuration parameters while remaining compatible with old configurations.
### 18. **ReAct Agent support, Agent tool calling capability enhancement**
### 19. **IndexStore extension capability enhancement**
IndexStore configuration restructuring, new storage implementations automatically scanned and discovered
### 20. **AWEL flow compatibility enhancement**
Cross-version compatibility for AWEL flow based on multi-version metadata.
## 🐞 Bug Fixes
Chroma support for Chinese knowledge base spaces, AWEL Flow issue fixes, fixed multi-platform Lyric installation error issues and local embedding model error issues, along with **40+** other bugs.
## 🛠️Others
Support for Ruff code formatting, multi-version documentation building, unit test fixes, and **20+** other issue fixes or feature enhancements.
**Upgrade Guide:**
### 1. Metadata database upgrade
For SQLite upgrades, table structures will be automatically upgraded by default. For MySQL upgrades, DDL needs to be executed manually. The `assets/schema/dbgpt.sql` file contains the complete DDL for the current version. Specific version change DDLs can be found in the `assets/schema/upgrade` directory. For example, if you are upgrading from `v0.6.3` to `v0.7.0`, you can execute the following DDL:
```bash
mysql -h127.0.0.1 -uroot -p{your_password} < ./assets/schema/upgrade/v0_7_0/upgrade_to_v0.7.0.sql
```
### 2. Vector database upgrade
Due to underlying changes in Chroma storage in v0.7.0, version 0.7.0 does not support reading content from older versions. Please re-import knowledge bases and refresh data sources. Other vector storage solutions are not affected.
## ✨Official Documentation
**English**
[Overview | DB-GPT](http://docs.dbgpt.site/docs/overview)
**Chinese**
[概览](https://www.yuque.com/eosphoros/dbgpt-docs/bex30nsv60ru0fmx)
## ✨Acknowledgements
Thanks to all contributors for making this release possible!
**283569391@qq.com, @15089677014, @Aries-ckt, @FOkvj, @Jant1L, @SonglinLyu, @TenYearOldJAVA, @Weaxs, @cinjoseph, @csunny, @damonqin, @dusx1981, @fangyinc, @geebytes, @haawha, @utopia2077, @vnicers, @xuxl2024, @yhjun1026, @yunfeng1993, @yyhhyyyyyy and tam**
<p align="center">
<img src={'/img/v070/contributors.png'} width="800px"/>
</p>
This version took nearly three months to develop and has been merged to the main branch for over a month. Hundreds of users participated in testing version 0.7.0, with GitHub receiving hundreds of issues feedback. Some users directly submitted PR fixes. The DB-GPT community sincerely thanks every user and contributor who participated in version 0.7.0!
## ✨Appendix
+ [Quick Start](http://docs.dbgpt.cn/docs/next/quickstart)
+ [Docker Quick Deployment](http://docs.dbgpt.cn/docs/next/installation/docker)
\ No newline at end of file
fangyinc:
name: Fangyin Cheng
title: DB-GPT Core Team
url: https://github.com/fangyinc
image_url: https://avatars.githubusercontent.com/u/22972572?v=4
\ No newline at end of file
llama:
label: LLama
permalink: /llama
description: A series of language models developed by Meta AI
LLM:
label: LLM
permalink: /llm
description: Large Language Models
# Calculator With Agents
In this example, we will show you how to use an agent as your calculator.
## Installations
Install the required packages by running the following command:
```bash
pip install "dbgpt[agent]>=0.5.6rc1" -U
pip install openai
```
## Code
Create a new Python file and add the following code:
```python
import asyncio
from dbgpt.agent import AgentContext, AgentMemory, LLMConfig, UserProxyAgent
from dbgpt.agent.expand.code_assistant_agent import CodeAssistantAgent
from dbgpt.model.proxy import OpenAILLMClient
async def main():
llm_client = OpenAILLMClient(model_alias="gpt-3.5-turbo")
context: AgentContext = AgentContext(conv_id="test123")
# Create an agent memory, default memory is ShortTermMemory
agent_memory: AgentMemory = AgentMemory()
# Create a code assistant agent
coder = (
await CodeAssistantAgent()
.bind(context)
.bind(LLMConfig(llm_client=llm_client))
.bind(agent_memory)
.build()
)
# Create a user proxy agent
user_proxy = await UserProxyAgent().bind(context).bind(agent_memory).build()
# Initiate a chat with the user proxy agent
await user_proxy.initiate_chat(
recipient=coder,
reviewer=user_proxy,
message="calculate the result of 321 * 123",
)
# Obtain conversation history messages between agents
print(await agent_memory.gpts_memory.one_chat_completions("test123"))
if __name__ == "__main__":
asyncio.run(main())
```
You will see the following output:
````bash
Prompt manager is not available.
Prompt manager is not available.
Prompt manager is not available.
Prompt manager is not available.
Prompt manager is not available.
--------------------------------------------------------------------------------
User (to Turing)-[]:
"calculate the result of 321 * 123"
--------------------------------------------------------------------------------
un_stream ai response: ```python
# filename: calculate_multiplication.py
result = 321 * 123
print(result)
```
>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
execute_code was called without specifying a value for use_docker. Since the python docker package is not available, code will be run natively. Note: this fallback behavior is subject to change
un_stream ai response: True
--------------------------------------------------------------------------------
Turing (to User)-[gpt-3.5-turbo]:
"```python\n# filename: calculate_multiplication.py\n\nresult = 321 * 123\nprint(result)\n```"
>>>>>>>>Turing Review info:
Pass(None)
>>>>>>>>Turing Action report:
execution succeeded,
39483
--------------------------------------------------------------------------------
```agent-plans
[{"name": "calculate the result of 321 * 123", "num": 1, "status": "complete", "agent": "Human", "markdown": "```agent-messages\n[{\"sender\": \"CodeEngineer\", \"receiver\": \"Human\", \"model\": \"gpt-3.5-turbo\", \"markdown\": \"```vis-code\\n{\\\"exit_success\\\": true, \\\"language\\\": \\\"python\\\", \\\"code\\\": [[\\\"python\\\", \\\"# filename: calculate_multiplication.py\\\\n\\\\nresult = 321 * 123\\\\nprint(result)\\\"]], \\\"log\\\": \\\"\\\\n39483\\\\n\\\"}\\n```\"}]\n```"}]
```
````
\ No newline at end of file
# Multi-Agents Conversation
Here we will show you how to write a multi-agents conversation program.
## Two Agents Conversation
First, create common `LLMConfig` and `AgentMemory` for both agents.
```python
import os
from dbgpt.agent import AgentContext, AgentMemory
from dbgpt.model.proxy import OpenAILLMClient
llm_client = OpenAILLMClient(
model_alias="gpt-4o",
api_base=os.getenv("OPENAI_API_BASE"),
api_key=os.getenv("OPENAI_API_KEY"),
)
context: AgentContext = AgentContext(
conv_id="test123",
language="en",
temperature=0.9,
max_new_tokens=2048,
max_chat_round=4,
)
# Create an agent memory, default memory is ShortTermMemory
agent_memory: AgentMemory = AgentMemory()
system_prompt_template = """\
You are a {{ role }}, {% if name %}named {{ name }}, {% endif %}your goal is {{ goal }}.
*** IMPORTANT REMINDER ***
{% if language == 'zh' %}\
Please answer in simplified Chinese.
{% else %}\
Please answer in English.
{% endif %}\
""" # noqa
user_prompt_template = """\
{% if most_recent_memories %}\
Most recent observations:
{{ most_recent_memories }}
{% endif %}\
{% if question %}\
user: {{ question }}
{% endif %}
"""
```
In above code, we set `max_chat_round=4` in `AgentContext`, which means the conversation
will end after 4 rounds.
And here we set `system_prompt_template` and `user_prompt_template` for both agents for a simple conversation, we
will introduce it in profile module later.
Then, create two agents, `Bob` and `Alice`, and initiate a chat between them.
```python
import asyncio
from dbgpt.agent import ConversableAgent, ProfileConfig, LLMConfig, BlankAction
async def main():
bob_profile = ProfileConfig(
name="Bob",
role="Comedians",
system_prompt_template=system_prompt_template,
user_prompt_template=user_prompt_template,
)
bob = (
await ConversableAgent(profile=bob_profile)
.bind(context)
.bind(LLMConfig(llm_client=llm_client))
.bind(agent_memory)
.bind(BlankAction)
.build()
)
alice_profile = ProfileConfig(
name="Alice",
role="Comedians",
system_prompt_template=system_prompt_template,
user_prompt_template=user_prompt_template,
)
alice = (
await ConversableAgent(profile=alice_profile)
.bind(context)
.bind(LLMConfig(llm_client=llm_client))
.bind(agent_memory)
.bind(BlankAction)
.build()
)
await bob.initiate_chat(alice, message="Tell me a joke.")
if __name__ == "__main__":
asyncio.run(main())
```
Run the code, you will see the conversation between `Bob` and `Alice`:
```bash
--------------------------------------------------------------------------------
Bob (to Alice)-[]:
"Tell me a joke."
--------------------------------------------------------------------------------
un_stream ai response: Why don't scientists trust atoms?
Because they make up everything!
--------------------------------------------------------------------------------
Alice (to Bob)-[gpt-4o]:
"Why don't scientists trust atoms?\n\nBecause they make up everything!"
>>>>>>>>Alice Review info:
Pass(None)
>>>>>>>>Alice Action report:
execution succeeded,
Why don't scientists trust atoms?
Because they make up everything!
--------------------------------------------------------------------------------
un_stream ai response: That's a classic! You know, it's always good to have a few science jokes in your toolbox—they have the potential energy to make everyone laugh, and they rarely get a negative reaction!
--------------------------------------------------------------------------------
Bob (to Alice)-[gpt-4o]:
"That's a classic! You know, it's always good to have a few science jokes in your toolbox—they have the potential energy to make everyone laugh, and they rarely get a negative reaction!"
>>>>>>>>Bob Review info:
Pass(None)
>>>>>>>>Bob Action report:
execution succeeded,
That's a classic! You know, it's always good to have a few science jokes in your toolbox—they have the potential energy to make everyone laugh, and they rarely get a negative reaction!
--------------------------------------------------------------------------------
un_stream ai response: Absolutely, science jokes have a universal appeal! Here's another one for your collection:
Why did the biologist go to the beach?
Because they wanted to study the current events!
--------------------------------------------------------------------------------
Alice (to Bob)-[gpt-4o]:
"Absolutely, science jokes have a universal appeal! Here's another one for your collection:\n\nWhy did the biologist go to the beach?\n\nBecause they wanted to study the current events!"
>>>>>>>>Alice Review info:
Pass(None)
>>>>>>>>Alice Action report:
execution succeeded,
Absolutely, science jokes have a universal appeal! Here's another one for your collection:
Why did the biologist go to the beach?
Because they wanted to study the current events!
--------------------------------------------------------------------------------
un_stream ai response: Haha, that's a good one! You know, biologists at the beach must have some serious kelp issues, too. They just can’t help but dive into their work—whether it's in the lab or lounging in the sand!
--------------------------------------------------------------------------------
Bob (to Alice)-[gpt-4o]:
"Haha, that's a good one! You know, biologists at the beach must have some serious kelp issues, too. They just can’t help but dive into their work—whether it's in the lab or lounging in the sand!"
>>>>>>>>Bob Review info:
Pass(None)
>>>>>>>>Bob Action report:
execution succeeded,
Haha, that's a good one! You know, biologists at the beach must have some serious kelp issues, too. They just can’t help but dive into their work—whether it's in the lab or lounging in the sand!
--------------------------------------------------------------------------------
```
\ No newline at end of file
# Write Your Custom Agent
## Introduction
In this example, we will show you how to create a custom agent that can be used as a
summarizer.
## Installations
Install the required packages by running the following command:
```bash
pip install "dbgpt[agent]>=0.5.6rc1" -U
pip install openai
```
## Create a Custom Agent
### Initialize The Agent
In most cases, you just need to inherit basic agents and override the corresponding
methods.
```python
from dbgpt.agent import ConversableAgent
class MySummarizerAgent(ConversableAgent):
def __init__(self, **kwargs):
super().__init__(**kwargs)
```
### Define the profile
Before designing each Agent, it is necessary to define its role, identity, and
functional role. The specific definitions are as follows:
```python
from dbgpt.agent import ConversableAgent, ProfileConfig
class MySummarizerAgent(ConversableAgent):
profile: ProfileConfig = ProfileConfig(
# The name of the agent
name="Aristotle",
# The role of the agent
role="Summarizer",
# The core functional goals of the agent tell LLM what it can do with it.
goal=(
"Summarize answer summaries based on user questions from provided "
"resource information or from historical conversation memories."
),
# Introduction and description of the agent, used for task assignment and display.
# If it is empty, the goal content will be used.
desc=(
"You can summarize provided text content according to user's questions"
" and output the summarization."
),
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
```
### Supplementary Prompt Constraints
Agent's prompt is assembled using a fixed template by default(an external template can
be bound if there are some special requirements). which mainly includes:
1. Identity definition(automatically constructed)
2. Resource information(automatically constructed)
3. Constraint logic
4. Reference case (optional)
5. Output format templates and constraints (automatically constructed)
So, we can define the constraints of the agent's prompt as follows:
```python
from dbgpt.agent import ConversableAgent, ProfileConfig
class MySummarizerAgent(ConversableAgent):
profile: ProfileConfig = ProfileConfig(
# The name of the agent
name="Aristotle",
# The role of the agent
role="Summarizer",
# The core functional goals of the agent tell LLM what it can do with it.
goal=(
"Summarize answer summaries based on user questions from provided "
"resource information or from historical conversation memories."
),
# Introduction and description of the agent, used for task assignment and display.
# If it is empty, the goal content will be used.
desc=(
"You can summarize provided text content according to user's questions"
" and output the summarization."
),
# Refer to the following. It can contain multiple constraints and reasoning
# restriction logic, and supports the use of parameter template {{ param_name }}.
constraints=[
"Prioritize the summary of answers to user questions from the improved resource"
" text. If no relevant information is found, summarize it from the historical "
"dialogue memory given. It is forbidden to make up your own.",
"You need to first detect user's question that you need to answer with your"
" summarization.",
"Extract the provided text content used for summarization.",
"Then you need to summarize the extracted text content.",
"Output the content of summarization ONLY related to user's question. The "
"output language must be the same to user's question language.",
"If you think the provided text content is not related to user questions at "
"all, ONLY output '{{ not_related_message }}'!!.",
]
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
```
### Prompt Template Format
If dynamic parameters are used in the prompt, the actual dialogue process is required
to assemble the values, and the following interface (`_init_reply_message`) needs to be
overloaded and implemented:
```python
from dbgpt.agent import AgentMessage, ConversableAgent, ProfileConfig
NOT_RELATED_MESSAGE = "Did not find the information you want."
class MySummarizerAgent(ConversableAgent):
profile: ProfileConfig = ProfileConfig(
# The name of the agent
name="Aristotle",
# The role of the agent
role="Summarizer",
# The core functional goals of the agent tell LLM what it can do with it.
goal=(
"Summarize answer summaries based on user questions from provided "
"resource information or from historical conversation memories."
),
# Introduction and description of the agent, used for task assignment and display.
# If it is empty, the goal content will be used.
desc=(
"You can summarize provided text content according to user's questions"
" and output the summarization."
),
# Refer to the following. It can contain multiple constraints and reasoning
# restriction logic, and supports the use of parameter template {{ param_name }}.
constraints=[
"Prioritize the summary of answers to user questions from the improved resource"
" text. If no relevant information is found, summarize it from the historical "
"dialogue memory given. It is forbidden to make up your own.",
"You need to first detect user's question that you need to answer with your"
" summarization.",
"Extract the provided text content used for summarization.",
"Then you need to summarize the extracted text content.",
"Output the content of summarization ONLY related to user's question. The "
"output language must be the same to user's question language.",
"If you think the provided text content is not related to user questions at "
"all, ONLY output '{{ not_related_message }}'!!.",
],
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
def _init_reply_message(self, received_message: AgentMessage) -> AgentMessage:
reply_message = super()._init_reply_message(received_message)
# Fill in the dynamic parameters in the prompt template
reply_message.context = {"not_related_message": NOT_RELATED_MESSAGE}
return reply_message
```
### Resource Preloading (Optional)
If there are some specific resources, the bound resources must be loaded in advance
when the agent is initialized. You can refer to the following implementation.
It is determined based on the actual situation of the resources. In most cases, it is
not necessary.
```python
from dbgpt.agent import ConversableAgent, AgentMessage
class MySummarizerAgent(ConversableAgent):
# ... other code
async def preload_resource(self) -> None:
# Load the required resources
for resource in self.resources:
# Load your resource, please write your own code here
pass
```
### Result Checking (Optional)
If the action execution results need to be strictly verified and verified, there are
two modes: code logic verification and LLM verification. Of course, verification is not
necessary and the default pass is not implemented. Here is an example using LL verification:
```python
from typing import Tuple, Optional
from dbgpt.agent import ConversableAgent, AgentMessage
from dbgpt.core import ModelMessageRoleType
CHECK_RESULT_SYSTEM_MESSAGE = (
"You are an expert in analyzing the results of a summary task."
"Your responsibility is to check whether the summary results can summarize the "
"input provided by the user, and then make a judgment. You need to answer "
"according to the following rules:\n"
" Rule 1: If you think the summary results can summarize the input provided"
" by the user, only return True.\n"
" Rule 2: If you think the summary results can NOT summarize the input "
"provided by the user, return False and the reason, split by | and ended "
"by TERMINATE. For instance: False|Some important concepts in the input are "
"not summarized. TERMINATE"
)
class MySummarizerAgent(ConversableAgent):
# ... other code
async def correctness_check(
self, message: AgentMessage
) -> Tuple[bool, Optional[str]]:
current_goal = message.current_goal
action_report = message.action_report
task_result = ""
if action_report:
task_result = action_report.get("content", "")
check_result, model = await self.thinking(
messages=[
AgentMessage(
role=ModelMessageRoleType.HUMAN,
content=(
"Please understand the following user input and summary results"
" and give your judgment:\n"
f"User Input: {current_goal}\n"
f"Summary Results: {task_result}"
),
)
],
prompt=CHECK_RESULT_SYSTEM_MESSAGE,
)
fail_reason = ""
if check_result and (
"true" in check_result.lower() or "yes" in check_result.lower()
):
success = True
else:
success = False
try:
_, fail_reason = check_result.split("|")
fail_reason = (
"The summary results cannot summarize the user input due"
f" to: {fail_reason}. Please re-understand and complete the summary"
" task."
)
except Exception:
fail_reason = (
"The summary results cannot summarize the user input. "
"Please re-understand and complete the summary task."
)
return success, fail_reason
```
## Create A Custom Action
### Initialize The Action
All Agent's operations on the external environment and the real world are implemented
through `Action`. Action defines the Agent's output content structure and actually
performs the corresponding operations. The specific `Action` implementation inherits
the `Action` base class, as follows:
```python
from typing import Optional
from pydantic import BaseModel, Field
from dbgpt.vis import Vis
from dbgpt.agent import Action, ActionOutput, AgentResource, ResourceType
from dbgpt.agent.util import cmp_string_equal
NOT_RELATED_MESSAGE = "Did not find the information you want."
# The parameter object that the Action that the current Agent needs to execute needs to output.
class SummaryActionInput(BaseModel):
summary: str = Field(
...,
description="The summary content",
)
class SummaryAction(Action[SummaryActionInput]):
def __init__(self):
super().__init__()
@property
def resource_need(self) -> Optional[ResourceType]:
# The resource type that the current Agent needs to use
# here we do not need to use resources, just return None
return None
@property
def render_protocol(self) -> Optional[Vis]:
# The visualization rendering protocol that the current Agent needs to use
# here we do not need to use visualization rendering, just return None
return None
@property
def out_model_type(self):
return SummaryActionInput
async def run(
self,
ai_message: str,
resource: Optional[AgentResource] = None,
rely_action_out: Optional[ActionOutput] = None,
need_vis_render: bool = True,
**kwargs,
) -> ActionOutput:
"""Perform the action.
The entry point for actual execution of Action. Action execution will be
automatically initiated after model inference.
"""
try:
# Parse the input message
param: SummaryActionInput = self._input_convert(ai_message, SummaryActionInput)
except Exception:
return ActionOutput(
is_exe_success=False,
content="The requested correctly structured answer could not be found, "
f"ai message: {ai_message}",
)
# Check if the summary content is not related to user questions
if param.summary and cmp_string_equal(
param.summary,
NOT_RELATED_MESSAGE,
ignore_case=True,
ignore_punctuation=True,
ignore_whitespace=True,
):
return ActionOutput(
is_exe_success=False,
content="the provided text content is not related to user questions at all."
f"ai message: {ai_message}",
)
else:
return ActionOutput(
is_exe_success=True,
content=param.summary,
)
```
### Binding Action to Agent
After the development and definition of agent and action are completed,
bind the action to the corresponding agent.
```python
from pydantic import BaseModel
from dbgpt.agent import Action,ConversableAgent
class SummaryActionInput(BaseModel):
...
class SummaryAction(Action[SummaryActionInput]):
...
class MySummarizerAgent(ConversableAgent):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([SummaryAction])
```
### Action Extended Parameter Processing
```python
from typing import Optional, Dict, Any
from pydantic import BaseModel
from dbgpt.agent import Action, ActionOutput, AgentResource, ConversableAgent
class SummaryActionInput(BaseModel):
...
class SummaryAction(Action[SummaryActionInput]):
...
async def run(
self,
ai_message: str,
resource: Optional[AgentResource] = None,
rely_action_out: Optional[ActionOutput] = None,
need_vis_render: bool = True,
**kwargs,
) -> ActionOutput:
# Read the extended parameters passed in by the agent
extra_param = kwargs.get("action_extra_param_key", None)
pass
class MySummarizerAgent(ConversableAgent):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([SummaryAction])
def prepare_act_param(self) -> Dict[str, Any]:
return {"action_extra_param_key": "this is extra param"}
```
## Use Your Custom Agent
After the custom agent is created, you can use it in the following way:
```python
import asyncio
from dbgpt.agent import AgentContext, ConversableAgent, AgentMemory, LLMConfig, UserProxyAgent
from dbgpt.model.proxy import OpenAILLMClient
class MySummarizerAgent(ConversableAgent):
...
async def main():
llm_client = OpenAILLMClient(model_alias="gpt-3.5-turbo")
context: AgentContext = AgentContext(conv_id="summarize")
agent_memory: AgentMemory = AgentMemory()
summarizer = (
await MySummarizerAgent()
.bind(context)
.bind(LLMConfig(llm_client=llm_client))
.bind(agent_memory)
.build()
)
user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
await user_proxy.initiate_chat(
recipient=summarizer,
reviewer=user_proxy,
message="""I want to summarize advantages of Nuclear Power according to the following content.
Nuclear power in space is the use of nuclear power in outer space, typically either small fission systems or radioactive decay for electricity or heat. Another use is for scientific observation, as in a Mössbauer spectrometer. The most common type is a radioisotope thermoelectric generator, which has been used on many space probes and on crewed lunar missions. Small fission reactors for Earth observation satellites, such as the TOPAZ nuclear reactor, have also been flown.[1] A radioisotope heater unit is powered by radioactive decay and can keep components from becoming too cold to function, potentially over a span of decades.[2]
The United States tested the SNAP-10A nuclear reactor in space for 43 days in 1965,[3] with the next test of a nuclear reactor power system intended for space use occurring on 13 September 2012 with the Demonstration Using Flattop Fission (DUFF) test of the Kilopower reactor.[4]
After a ground-based test of the experimental 1965 Romashka reactor, which used uranium and direct thermoelectric conversion to electricity,[5] the USSR sent about 40 nuclear-electric satellites into space, mostly powered by the BES-5 reactor. The more powerful TOPAZ-II reactor produced 10 kilowatts of electricity.[3]
Examples of concepts that use nuclear power for space propulsion systems include the nuclear electric rocket (nuclear powered ion thruster(s)), the radioisotope rocket, and radioisotope electric propulsion (REP).[6] One of the more explored concepts is the nuclear thermal rocket, which was ground tested in the NERVA program. Nuclear pulse propulsion was the subject of Project Orion.[7]
Regulation and hazard prevention[edit]
After the ban of nuclear weapons in space by the Outer Space Treaty in 1967, nuclear power has been discussed at least since 1972 as a sensitive issue by states.[8] Particularly its potential hazards to Earth's environment and thus also humans has prompted states to adopt in the U.N. General Assembly the Principles Relevant to the Use of Nuclear Power Sources in Outer Space (1992), particularly introducing safety principles for launches and to manage their traffic.[8]
Benefits
Both the Viking 1 and Viking 2 landers used RTGs for power on the surface of Mars. (Viking launch vehicle pictured)
While solar power is much more commonly used, nuclear power can offer advantages in some areas. Solar cells, although efficient, can only supply energy to spacecraft in orbits where the solar flux is sufficiently high, such as low Earth orbit and interplanetary destinations close enough to the Sun. Unlike solar cells, nuclear power systems function independently of sunlight, which is necessary for deep space exploration. Nuclear-based systems can have less mass than solar cells of equivalent power, allowing more compact spacecraft that are easier to orient and direct in space. In the case of crewed spaceflight, nuclear power concepts that can power both life support and propulsion systems may reduce both cost and flight time.[9]
Selected applications and/or technologies for space include:
Radioisotope thermoelectric generator
Radioisotope heater unit
Radioisotope piezoelectric generator
Radioisotope rocket
Nuclear thermal rocket
Nuclear pulse propulsion
Nuclear electric rocket
""",
)
print(await agent_memory.gpts_memory.one_chat_completions("summarize"))
if __name__ == "__main__":
asyncio.run(main())
```
Full code as follows:
```python
import asyncio
from typing import Any, Dict, Optional, Tuple
from dbgpt.agent import (
Action,
ActionOutput,
AgentContext,
AgentMemory,
AgentMessage,
AgentResource,
ConversableAgent,
LLMConfig,
ProfileConfig,
ResourceType,
UserProxyAgent,
)
from dbgpt.agent.util import cmp_string_equal
from dbgpt.core import ModelMessageRoleType
from dbgpt.model.proxy import OpenAILLMClient
from dbgpt.vis import Vis
from pydantic import BaseModel, Field
NOT_RELATED_MESSAGE = "Did not find the information you want."
CHECK_RESULT_SYSTEM_MESSAGE = (
"You are an expert in analyzing the results of a summary task."
"Your responsibility is to check whether the summary results can summarize the "
"input provided by the user, and then make a judgment. You need to answer "
"according to the following rules:\n"
" Rule 1: If you think the summary results can summarize the input provided"
" by the user, only return True.\n"
" Rule 2: If you think the summary results can NOT summarize the input "
"provided by the user, return False and the reason, split by | and ended "
"by TERMINATE. For instance: False|Some important concepts in the input are "
"not summarized. TERMINATE"
)
class MySummarizerAgent(ConversableAgent):
profile: ProfileConfig = ProfileConfig(
# The name of the agent
name="Aristotle",
# The role of the agent
role="Summarizer",
# The core functional goals of the agent tell LLM what it can do with it.
goal=(
"Summarize answer summaries based on user questions from provided "
"resource information or from historical conversation memories."
),
# Introduction and description of the agent, used for task assignment and display.
# If it is empty, the goal content will be used.
desc=(
"You can summarize provided text content according to user's questions"
" and output the summarization."
),
# Refer to the following. It can contain multiple constraints and reasoning
# restriction logic, and supports the use of parameter template {{ param_name }}.
constraints=[
"Prioritize the summary of answers to user questions from the improved resource"
" text. If no relevant information is found, summarize it from the historical "
"dialogue memory given. It is forbidden to make up your own.",
"You need to first detect user's question that you need to answer with your"
" summarization.",
"Extract the provided text content used for summarization.",
"Then you need to summarize the extracted text content.",
"Output the content of summarization ONLY related to user's question. The "
"output language must be the same to user's question language.",
"If you think the provided text content is not related to user questions at "
"all, ONLY output '{{ not_related_message }}'!!.",
],
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([SummaryAction])
def _init_reply_message(self, received_message: AgentMessage) -> AgentMessage:
reply_message = super()._init_reply_message(received_message)
# Fill in the dynamic parameters in the prompt template
reply_message.context = {"not_related_message": NOT_RELATED_MESSAGE}
return reply_message
def prepare_act_param(self) -> Dict[str, Any]:
return {"action_extra_param_key": "this is extra param"}
async def correctness_check(
self, message: AgentMessage
) -> Tuple[bool, Optional[str]]:
current_goal = message.current_goal
action_report = message.action_report
task_result = ""
if action_report:
task_result = action_report.get("content", "")
check_result, model = await self.thinking(
messages=[
AgentMessage(
role=ModelMessageRoleType.HUMAN,
content=(
"Please understand the following user input and summary results"
" and give your judgment:\n"
f"User Input: {current_goal}\n"
f"Summary Results: {task_result}"
),
)
],
prompt=CHECK_RESULT_SYSTEM_MESSAGE,
)
fail_reason = ""
if check_result and (
"true" in check_result.lower() or "yes" in check_result.lower()
):
success = True
else:
success = False
try:
_, fail_reason = check_result.split("|")
fail_reason = (
"The summary results cannot summarize the user input due"
f" to: {fail_reason}. Please re-understand and complete the summary"
" task."
)
except Exception:
fail_reason = (
"The summary results cannot summarize the user input. "
"Please re-understand and complete the summary task."
)
return success, fail_reason
# The parameter object that the Action that the current Agent needs to execute needs to output.
class SummaryActionInput(BaseModel):
summary: str = Field(
...,
description="The summary content",
)
class SummaryAction(Action[SummaryActionInput]):
def __init__(self):
super().__init__()
@property
def resource_need(self) -> Optional[ResourceType]:
# The resource type that the current Agent needs to use
# here we do not need to use resources, just return None
return None
@property
def render_protocol(self) -> Optional[Vis]:
# The visualization rendering protocol that the current Agent needs to use
# here we do not need to use visualization rendering, just return None
return None
@property
def out_model_type(self):
return SummaryActionInput
async def run(
self,
ai_message: str,
resource: Optional[AgentResource] = None,
rely_action_out: Optional[ActionOutput] = None,
need_vis_render: bool = True,
**kwargs,
) -> ActionOutput:
"""Perform the action.
The entry point for actual execution of Action. Action execution will be
automatically initiated after model inference.
"""
extra_param = kwargs.get("action_extra_param_key", None)
try:
# Parse the input message
param: SummaryActionInput = self._input_convert(
ai_message, SummaryActionInput
)
except Exception:
return ActionOutput(
is_exe_success=False,
content="The requested correctly structured answer could not be found, "
f"ai message: {ai_message}",
)
# Check if the summary content is not related to user questions
if param.summary and cmp_string_equal(
param.summary,
NOT_RELATED_MESSAGE,
ignore_case=True,
ignore_punctuation=True,
ignore_whitespace=True,
):
return ActionOutput(
is_exe_success=False,
content="the provided text content is not related to user questions at all."
f"ai message: {ai_message}",
)
else:
return ActionOutput(
is_exe_success=True,
content=param.summary,
)
async def main():
llm_client = OpenAILLMClient(model_alias="gpt-3.5-turbo")
context: AgentContext = AgentContext(conv_id="summarize")
agent_memory: AgentMemory = AgentMemory()
summarizer = (
await MySummarizerAgent()
.bind(context)
.bind(LLMConfig(llm_client=llm_client))
.bind(agent_memory)
.build()
)
user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
await user_proxy.initiate_chat(
recipient=summarizer,
reviewer=user_proxy,
message="""I want to summarize advantages of Nuclear Power according to the following content.
Nuclear power in space is the use of nuclear power in outer space, typically either small fission systems or radioactive decay for electricity or heat. Another use is for scientific observation, as in a Mössbauer spectrometer. The most common type is a radioisotope thermoelectric generator, which has been used on many space probes and on crewed lunar missions. Small fission reactors for Earth observation satellites, such as the TOPAZ nuclear reactor, have also been flown.[1] A radioisotope heater unit is powered by radioactive decay and can keep components from becoming too cold to function, potentially over a span of decades.[2]
The United States tested the SNAP-10A nuclear reactor in space for 43 days in 1965,[3] with the next test of a nuclear reactor power system intended for space use occurring on 13 September 2012 with the Demonstration Using Flattop Fission (DUFF) test of the Kilopower reactor.[4]
After a ground-based test of the experimental 1965 Romashka reactor, which used uranium and direct thermoelectric conversion to electricity,[5] the USSR sent about 40 nuclear-electric satellites into space, mostly powered by the BES-5 reactor. The more powerful TOPAZ-II reactor produced 10 kilowatts of electricity.[3]
Examples of concepts that use nuclear power for space propulsion systems include the nuclear electric rocket (nuclear powered ion thruster(s)), the radioisotope rocket, and radioisotope electric propulsion (REP).[6] One of the more explored concepts is the nuclear thermal rocket, which was ground tested in the NERVA program. Nuclear pulse propulsion was the subject of Project Orion.[7]
Regulation and hazard prevention[edit]
After the ban of nuclear weapons in space by the Outer Space Treaty in 1967, nuclear power has been discussed at least since 1972 as a sensitive issue by states.[8] Particularly its potential hazards to Earth's environment and thus also humans has prompted states to adopt in the U.N. General Assembly the Principles Relevant to the Use of Nuclear Power Sources in Outer Space (1992), particularly introducing safety principles for launches and to manage their traffic.[8]
Benefits
Both the Viking 1 and Viking 2 landers used RTGs for power on the surface of Mars. (Viking launch vehicle pictured)
While solar power is much more commonly used, nuclear power can offer advantages in some areas. Solar cells, although efficient, can only supply energy to spacecraft in orbits where the solar flux is sufficiently high, such as low Earth orbit and interplanetary destinations close enough to the Sun. Unlike solar cells, nuclear power systems function independently of sunlight, which is necessary for deep space exploration. Nuclear-based systems can have less mass than solar cells of equivalent power, allowing more compact spacecraft that are easier to orient and direct in space. In the case of crewed spaceflight, nuclear power concepts that can power both life support and propulsion systems may reduce both cost and flight time.[9]
Selected applications and/or technologies for space include:
Radioisotope thermoelectric generator
Radioisotope heater unit
Radioisotope piezoelectric generator
Radioisotope rocket
Nuclear thermal rocket
Nuclear pulse propulsion
Nuclear electric rocket
""",
)
print(await agent_memory.gpts_memory.one_chat_completions("summarize"))
if __name__ == "__main__":
asyncio.run(main())
```
\ No newline at end of file
# Agents With Database
Most of the time, we want the agent to answer questions based on the data in the database,
or make decisions based on the data in the database. In this case, we need to connect
the agent to the database.
## Installation
To use the database in the agent, you need to install the dependencies with the following command:
```bash
pip install "dbgpt[simple_framework]>=0.5.9rc0"
```
## Create A Database Connector
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
<Tabs
defaultValue="sqlite_temp"
values={[
{label: 'SQLite(Temporary)', value: 'sqlite_temp'},
{label: 'SQLite', value: 'sqlite'},
{label: 'MySQL', value: 'mysql'},
]}>
<TabItem value="sqlite_temp" label="sqlite_temp">
:::tip NOTE
We provide a temporary SQLite database for testing. The temporary database will be
created in temporary directory and will be deleted after the program exits.
:::
```python
from dbgpt.datasource.rdbms.conn_sqlite import SQLiteTempConnector
connector = SQLiteTempConnector.create_temporary_db()
connector.create_temp_tables(
{
"user": {
"columns": {
"id": "INTEGER PRIMARY KEY",
"name": "TEXT",
"age": "INTEGER",
},
"data": [
(1, "Tom", 10),
(2, "Jerry", 16),
(3, "Jack", 18),
(4, "Alice", 20),
(5, "Bob", 22),
],
}
}
)
```
</TabItem>
<TabItem value="sqlite" label="sqlite">
:::tip NOTE
We connect to the SQLite database by giving the database file path, please make sure the file path is correct.
:::
```python
from dbgpt.datasource.rdbms.conn_sqlite import SQLiteConnector
connector = SQLiteConnector.from_file_path("path/to/your/database.db")
```
</TabItem>
<TabItem value="mysql" label="MySQL">
:::tip NOTE
We connect to the MySQL database by giving the database connection information, please
make sure the connection information is correct.
:::
```python
from dbgpt.datasource.rdbms.conn_mysql import MySQLConnector
connector = MySQLConnector.from_uri_db(
host="localhost",
port=3307,
user="root",
pwd="********",
db_name="user_manager",
engine_args={"connect_args": {"charset": "utf8mb4"}},
)
```
</TabItem>
</Tabs>
## Create A Database Resource
```python
from dbgpt.agent.resource import RDBMSConnectorResource
db_resource = RDBMSConnectorResource("user_manager", connector=connector)
```
As previously mentioned, the **Database** is a kind of resource, we can use most database
which supported in DB-GPT(like SQLite, MySQL, ClickHouse, ApacheDoris, DuckDB, Hive,
MSSQL, OceanBase, PostgreSQL, StarRocks, Vertica, etc.) as the resource.
## Use Database In Your Agent
```python
import asyncio
import os
from dbgpt.agent import AgentContext, AgentMemory, LLMConfig, UserProxyAgent
from dbgpt.agent.expand.data_scientist_agent import DataScientistAgent
from dbgpt.model.proxy import OpenAILLMClient
async def main():
llm_client = OpenAILLMClient(
model_alias="gpt-3.5-turbo", # or other models, eg. "gpt-4o"
api_base=os.getenv("OPENAI_API_BASE"),
api_key=os.getenv("OPENAI_API_KEY"),
)
context: AgentContext = AgentContext(
conv_id="test123", language="en", temperature=0.5, max_new_tokens=2048
)
agent_memory = AgentMemory()
user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
sql_boy = (
await DataScientistAgent()
.bind(context)
.bind(LLMConfig(llm_client=llm_client))
.bind(db_resource)
.bind(agent_memory)
.build()
)
await user_proxy.initiate_chat(
recipient=sql_boy,
reviewer=user_proxy,
message="What is the name and age of the user with age less than 18",
)
## dbgpt-vis message infos
print(await agent_memory.gpts_memory.one_chat_completions("test123"))
if __name__ == "__main__":
asyncio.run(main())
```
The output will be like this:
``````bash
--------------------------------------------------------------------------------
User (to Edgar)-[]:
"What is the name and age of the user with age less than 18"
--------------------------------------------------------------------------------
un_stream ai response: {
"display_type": "response_table",
"sql": "SELECT name, age FROM user WHERE age < 18",
"thought": "I have selected a response_table to display the names and ages of users with an age less than 18. The SQL query retrieves the name and age columns from the user table where the age is less than 18."
}
--------------------------------------------------------------------------------
Edgar (to User)-[gpt-3.5-turbo]:
"{\n \"display_type\": \"response_table\",\n \"sql\": \"SELECT name, age FROM user WHERE age < 18\",\n \"thought\": \"I have selected a response_table to display the names and ages of users with an age less than 18. The SQL query retrieves the name and age columns from the user table where the age is less than 18.\"\n}"
>>>>>>>>Edgar Review info:
Pass(None)
>>>>>>>>Edgar Action report:
execution succeeded,
{"display_type":"response_table","sql":"SELECT name, age FROM user WHERE age < 18","thought":"I have selected a response_table to display the names and ages of users with an age less than 18. The SQL query retrieves the name and age columns from the user table where the age is less than 18."}
--------------------------------------------------------------------------------
```agent-plans
[{"name": "What is the name and age of the user with age less than 18", "num": 1, "status": "complete", "agent": "Human", "markdown": "```agent-messages\n[{\"sender\": \"DataScientist\", \"receiver\": \"Human\", \"model\": \"gpt-3.5-turbo\", \"markdown\": \"```vis-db-chart\\n{\\\"sql\\\": \\\"SELECT name, age FROM user WHERE age < 18\\\", \\\"type\\\": \\\"response_table\\\", \\\"title\\\": \\\"\\\", \\\"describe\\\": \\\"I have selected a response_table to display the names and ages of users with an age less than 18. The SQL query retrieves the name and age columns from the user table where the age is less than 18.\\\", \\\"data\\\": [{\\\"name\\\": \\\"Tom\\\", \\\"age\\\": 10}, {\\\"name\\\": \\\"Jerry\\\", \\\"age\\\": 16}]}\\n```\"}]\n```"}]
```
``````
Let's parse the result from above output, we just focus on the last part
(output with [GPT-Vis](https://github.com/eosphoros-ai/GPT-Vis) protocol):
```json
[
{
"name": "What is the name and age of the user with age less than 18",
"num": 1,
"status": "complete",
"agent": "Human",
"markdown": "```agent-messages\n[{\"sender\": \"DataScientist\", \"receiver\": \"Human\", \"model\": \"gpt-3.5-turbo\", \"markdown\": \"```vis-db-chart\\n{\\\"sql\\\": \\\"SELECT name, age FROM user WHERE age < 18\\\", \\\"type\\\": \\\"response_table\\\", \\\"title\\\": \\\"\\\", \\\"describe\\\": \\\"I have selected a response_table to display the names and ages of users with an age less than 18. The SQL query retrieves the name and age columns from the user table where the age is less than 18.\\\", \\\"data\\\": [{\\\"name\\\": \\\"Tom\\\", \\\"age\\\": 10}, {\\\"name\\\": \\\"Jerry\\\", \\\"age\\\": 16}]}\\n```\"}]\n```"
}
]
```
What is GPT-Vis? GPT-Vis is a collection components for GPTs, generative AI, and LLM projects.
It provides a protocol(a custom code syntax in markdown) to describe the output of the AI model,
and be able to render the output in rich UI components.
Here, the output is a table, which contains the name and age of the users with age less than 18.
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment