> ## Documentation Index
> Fetch the complete documentation index at: https://docs.siliconflow.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# DB-GPT

> 更新日期：2025年2月10日

## 1.关于 DB-GPT

[DB-GPT](https://github.com/eosphoros-ai/DB-GPT) **是一个开源的AI原生数据应用开发框架(AI Native Data App Development framework with AWEL(Agentic Workflow Expression Language) and Agents)。**

目的是构建大模型领域的基础设施，通过开发多模型管理(SMMF)、Text2SQL效果优化、RAG框架以及优化、Multi-Agents框架协作、AWEL(智能体工作流编排)等多种技术能力，让围绕数据库构建大模型应用更简单，更方便。

## 2.获取 API Key

2.1 打开 [SiliconFlow 官网](https://cloud.siliconflow.cn/) 并注册账号（如果注册过，直接登录即可）。

2.2 完成注册后，打开[API密钥](https://cloud.siliconflow.cn/account/ak) ，创建新的 API Key，点击密钥进行复制，以备后续使用。

## 3.部署 DB-GPT

### 3.1 克隆 DB-GPT 源码

```bash theme={null}
git clone https://github.com/eosphoros-ai/DB-GPT.git
```

### 3.2 创建虚拟环境并安装依赖

```bash theme={null}
# cd 到 DB-GPT 源码根目录
cd DB-GPT

# DB-GPT 要求python >= 3.10
conda create -n dbgpt_env python=3.10
conda activate dbgpt_env

# 这里选择代理模型类依赖安装
pip install -e ".[proxy]"
```

### 3.3 配置基础的环境变量

```bash theme={null}
# 复制模板 env 文件为 .env
cp .env.template .env
```

### 3.4 修改环境变量文件`.env`，配置 SiliconFlow 模型

```bash theme={null}
#  使用 SiliconFlow 的代理模型
LLM_MODEL=siliconflow_proxyllm
# 配置具体使用的模型名称
SILICONFLOW_MODEL_VERSION=Qwen/Qwen2.5-Coder-32B-Instruct
SILICONFLOW_API_BASE=https://api.siliconflow.cn/v1
# 记得填写您在步骤2中获取的 API Key
SILICONFLOW_API_KEY={your-siliconflow-api-key}

# 配置使用 SiliconFlow 的 Embedding 模型
EMBEDDING_MODEL=proxy_http_openapi
PROXY_HTTP_OPENAPI_PROXY_SERVER_URL=https://api.siliconflow.cn/v1/embeddings
# 记得填写您在步骤2中获取的 API Key
PROXY_HTTP_OPENAPI_PROXY_API_KEY={your-siliconflow-api-key}
# 配置具体的 Embedding 模型名称
PROXY_HTTP_OPENAPI_PROXY_BACKEND=BAAI/bge-large-zh-v1.5


# 配置使用 SiliconFlow 的 rerank 模型
RERANK_MODEL=rerank_proxy_siliconflow
RERANK_PROXY_SILICONFLOW_PROXY_SERVER_URL=https://api.siliconflow.cn/v1/rerank
# 记得填写您在步骤2中获取的 API Key
RERANK_PROXY_SILICONFLOW_PROXY_API_KEY={your-siliconflow-api-key}
# 配置具体的 rerank 模型名称
RERANK_PROXY_SILICONFLOW_PROXY_BACKEND=BAAI/bge-reranker-v2-m3
```

注意，上述的 `SILICONFLOW_API_KEY`、 `PROXY_HTTP_OPENAPI_PROXY_SERVER_URL` 和`RERANK_PROXY_SILICONFLOW_PROXY_API_KEY`环境变量是您在步骤 2 中获取的 SiliconFlow 的 Api Key。语言模型（`SILICONFLOW_MODEL_VERSION`)、 Embedding 模型（`PROXY_HTTP_OPENAPI_PROXY_BACKEND`）和 rerank 模型(`RERANK_PROXY_SILICONFLOW_PROXY_BACKEND`) 可以从 [获取用户模型列表 - SiliconFlow](https://docs.siliconflow.cn/api-reference/models/get-model-list)  中获取。

### 3.5 启动 DB-GPT 服务

```bash theme={null}
dbgpt start webserver --port 5670
```

在浏览器打开地址 [http://127.0.0.1:5670/](http://127.0.0.1:5670/) 即可访问部署好的 DB-GPT

## 4.通过 DB-GPT Python SDK 使用 SiliconFlow  的模型

### 4.1  安装 DB-GPT Python 包

```bash theme={null}
pip install "dbgpt>=0.6.3rc2" openai requests numpy
```

为了后续验证，额外安装相关依赖包。

### 4.2. 使用 SiliconFlow  的大语言模型

```python theme={null}
import asyncio
import os
from dbgpt.core import ModelRequest
from dbgpt.model.proxy import SiliconFlowLLMClient

model = "Qwen/Qwen2.5-Coder-32B-Instruct"
client = SiliconFlowLLMClient(
    api_key=os.getenv("SILICONFLOW_API_KEY"),
    model_alias=model
)

res = asyncio.run(
    client.generate(
        ModelRequest(
            model=model,
            messages=[
                {"role": "system", "content": "你是一个乐于助人的 AI 助手。"},
                {"role": "human", "content": "你好"},
            ]
        )
    )
)
print(res)
```

### 4.3 使用 SiliconFlow 的 Embedding 模型

```python theme={null}
import os
from dbgpt.rag.embedding import OpenAPIEmbeddings

openai_embeddings = OpenAPIEmbeddings(
    api_url="https://api.siliconflow.cn/v1/embeddings",
    api_key=os.getenv("SILICONFLOW_API_KEY"),
    model_name="BAAI/bge-large-zh-v1.5",
)

texts = ["Hello, world!", "How are you?"]
res = openai_embeddings.embed_documents(texts)
print(res)
```

### 4.4 使用 SiliconFlow 的 rerank 模型

```python theme={null}
import os
from dbgpt.rag.embedding import SiliconFlowRerankEmbeddings

embedding = SiliconFlowRerankEmbeddings(
    api_key=os.getenv("SILICONFLOW_API_KEY"),
    model_name="BAAI/bge-reranker-v2-m3",
)
res = embedding.predict("Apple", candidates=["苹果", "香蕉", "水果", "蔬菜"])
print(res)
```

## 5. 上手指南

以数据对话案例为例，数据对话能力是通过自然语言与数据进行对话，目前主要是结构化与半结构化数据的对话，可以辅助做数据分析与洞察。以下为具体操作流程：

### 1. 添加数据源

首先选择左侧数据源添加，添加数据库，目前DB-GPT支持多种数据库类型。选择对应的数据库类型添加即可。这里我们选择的是MySQL作为演示，演示的测试数据参见测试样例（[https://github.com/eosphoros-ai/DB-GPT/tree/main/docker/examples/sqls）。](https://github.com/eosphoros-ai/DB-GPT/tree/main/docker/examples/sqls）。)　

<Frame>
  <img src="https://mintcdn.com/siliconflow-37161621/tCRvDTXiZEw0OYru/images/usercases/db-gpt/image_1.webp?fit=max&auto=format&n=tCRvDTXiZEw0OYru&q=85&s=e7a073c233a5728f2fbdcb33fb91160d" width="1080" height="898" data-path="images/usercases/db-gpt/image_1.webp" />
</Frame>

### 2. 选择对话类型

选择ChatData对话类型。

<Frame>
  <img src="https://mintcdn.com/siliconflow-37161621/tCRvDTXiZEw0OYru/images/usercases/db-gpt/image_2.webp?fit=max&auto=format&n=tCRvDTXiZEw0OYru&q=85&s=e21b54349ea52902b472bb3ee48ecdc9" width="1080" height="868" data-path="images/usercases/db-gpt/image_2.webp" />
</Frame>

### 3. 开始数据对话

注意：在对话时，选择对应的模型与数据库。同时DB-GPT也提供了预览模式与编辑模式。　

<Frame>
  <img src="https://mintcdn.com/siliconflow-37161621/tCRvDTXiZEw0OYru/images/usercases/db-gpt/image_3.webp?fit=max&auto=format&n=tCRvDTXiZEw0OYru&q=85&s=352a1f88af6d1fdcc745d61ad45b7758" width="1080" height="765" data-path="images/usercases/db-gpt/image_3.webp" />
</Frame>

编辑模式：

<Frame>
  <img src="https://mintcdn.com/siliconflow-37161621/tCRvDTXiZEw0OYru/images/usercases/db-gpt/image_4.webp?fit=max&auto=format&n=tCRvDTXiZEw0OYru&q=85&s=eb24cf25598d87e439bc80c1b887f22a" width="1080" height="521" data-path="images/usercases/db-gpt/image_4.webp" />
</Frame>
