feat: 实现 RAG SQL生成系统的后端接口和前端页面
- 新增后端 API,使用 FastAPI 框架实现 - 创建前端聊天界面,使用 HTML 和 JavaScript 实现 - 添加环境变量配置文件,支持不同环境的配置 - 实现与 MySQL 和 pgvector 数据库的连接和查询 - 集成大语言模型和向量模型,用于生成 SQL 语句main
parent
1025da7b26
commit
e4e938d73b
@ -0,0 +1,58 @@
|
|||||||
|
# -------- 应用配置 --------
|
||||||
|
# 应用运行环境
|
||||||
|
APP_ENV = 'dev'
|
||||||
|
# 应用名称
|
||||||
|
APP_NAME = '规则生成系统'
|
||||||
|
# 应用代理路径
|
||||||
|
# APP_ROOT_PATH = '/rule'
|
||||||
|
APP_ROOT_PATH = ''
|
||||||
|
# 应用主机
|
||||||
|
APP_HOST = '0.0.0.0'
|
||||||
|
# 应用端口
|
||||||
|
APP_PORT = 9099
|
||||||
|
# 应用版本
|
||||||
|
APP_VERSION= '1.0.0'
|
||||||
|
# 应用是否开启热重载
|
||||||
|
APP_RELOAD = true
|
||||||
|
# 应用是否开启IP归属区域查询
|
||||||
|
APP_IP_LOCATION_QUERY = true
|
||||||
|
# 应用是否允许账号同时登录
|
||||||
|
APP_SAME_TIME_LOGIN = true
|
||||||
|
# 是否允许接口直接执行sql进行测试
|
||||||
|
APP_TSET_SQL = false
|
||||||
|
|
||||||
|
# -------- mysql数据库配置 --------
|
||||||
|
# 数据库主机
|
||||||
|
MYSQL_DB_HOST = 'ngsk.tech'
|
||||||
|
# 数据库端口
|
||||||
|
MYSQL_DB_PORT = 33306
|
||||||
|
# 数据库用户名
|
||||||
|
MYSQL_DB_USERNAME = 'root'
|
||||||
|
# 数据库密码
|
||||||
|
MYSQL_DB_PASSWORD = 'ngsk0809cruise'
|
||||||
|
# 数据库名称
|
||||||
|
MYSQL_DB_DATABASE = 'data_governance'
|
||||||
|
|
||||||
|
|
||||||
|
# -------- pgvector数据库配置 --------
|
||||||
|
# 数据库主机
|
||||||
|
PG_DB_HOST = '192.168.5.30'
|
||||||
|
# 数据库端口
|
||||||
|
PG_DB_PORT = 5432
|
||||||
|
# 数据库用户名
|
||||||
|
PG_DB_USERNAME = 'myuser'
|
||||||
|
# 数据库密码
|
||||||
|
PG_DB_PASSWORD = 'mypassword'
|
||||||
|
# 数据库名称
|
||||||
|
PG_DB_DATABASE = 'vectordb'
|
||||||
|
|
||||||
|
# -------- 大语言模型配置 --------
|
||||||
|
llm_base_url = 'http://192.168.5.20:4090/v1'
|
||||||
|
llm_api_key = 'gpustack_951f92355e6781a5_5d17650a3e7135c5430512e5117362fb'
|
||||||
|
llm_model = 'qwen3-30b-a3b-instruct-2507'
|
||||||
|
|
||||||
|
# -------- 向量模型配置 --------
|
||||||
|
emb_base_url = 'http://192.168.5.20:4090/v1'
|
||||||
|
emb_api_key = 'gpustack_951f92355e6781a5_5d17650a3e7135c5430512e5117362fb'
|
||||||
|
emb_model = 'bge-m3'
|
||||||
|
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
# 使用官方 Python 基础镜像
|
||||||
|
FROM hub.1panel.dev/library/python:3.12-slim
|
||||||
|
|
||||||
|
# 设置工作目录
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 将当前目录下的所有文件复制到容器中的 /app 目录
|
||||||
|
COPY . /app
|
||||||
|
|
||||||
|
# 安装依赖
|
||||||
|
#RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
|
||||||
|
RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt
|
||||||
|
|
||||||
|
|
||||||
|
# 暴露应用程序运行的端口(如果需要)
|
||||||
|
EXPOSE 9099
|
||||||
|
|
||||||
|
# 启动命令
|
||||||
|
CMD ["python", "app.py","--env=prod"]
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
# backend.py
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from fastapi import FastAPI, HTTPException, Request, APIRouter
|
||||||
|
from pydantic import BaseModel
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from starlette.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
from rag import rag_generate_rule
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app : FastAPI):
|
||||||
|
# logger.info(f'{AppConfig.app_name}开始启动,当前运行环境{os.environ.get('APP_ENV', '未读取到环境变量APP_ENV')}')
|
||||||
|
print(f'开始启动')
|
||||||
|
print(f'启动成功')
|
||||||
|
yield
|
||||||
|
print(f'关闭成功')
|
||||||
|
|
||||||
|
app = FastAPI(title="RAG SQL Generator API", description="调用RAG生成SQL语句的后端服务", lifespan=lifespan)
|
||||||
|
|
||||||
|
rule_router = APIRouter(prefix="/rule")
|
||||||
|
|
||||||
|
# 添加CORS中间件 - 允许所有来源(开发环境)
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"], # 在生产环境中应该指定具体的域名
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
class QueryRequest(BaseModel):
|
||||||
|
query: str
|
||||||
|
|
||||||
|
class QueryResponse(BaseModel):
|
||||||
|
sql: str
|
||||||
|
|
||||||
|
@rule_router.post("/generate_sql", response_model=QueryResponse)
|
||||||
|
async def generate_sql(request: QueryRequest):
|
||||||
|
"""
|
||||||
|
接收用户问题并调用RAG生成SQL语句
|
||||||
|
|
||||||
|
- **query**: 用户的自然语言问题
|
||||||
|
- 返回生成的SQL语句
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# 调用rag_generate_rule方法生成SQL
|
||||||
|
result = await rag_generate_rule(request.query)
|
||||||
|
return QueryResponse(sql=result)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=f"生成SQL时出错: {str(e)}")
|
||||||
|
|
||||||
|
@rule_router.get("/health")
|
||||||
|
async def health_check():
|
||||||
|
"""
|
||||||
|
健康检查端点
|
||||||
|
"""
|
||||||
|
return {"status": "healthy"}
|
||||||
|
|
||||||
|
|
||||||
|
# 注册带前缀的路由器
|
||||||
|
app.include_router(rule_router)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import uvicorn
|
||||||
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
aiomysql==0.2.0
|
||||||
|
fastapi==0.116.1
|
||||||
|
numpy==2.3.2
|
||||||
|
openai==1.99.6
|
||||||
|
pandas==2.3.1
|
||||||
|
psycopg2_binary==2.9.10
|
||||||
|
pydantic==2.11.7
|
||||||
|
starlette==0.47.2
|
||||||
|
uvicorn==0.35.0
|
||||||
Loading…
Reference in New Issue