diff --git a/AI/Text2Sql/__pycache__/app.cpython-310.pyc b/AI/Text2Sql/__pycache__/app.cpython-310.pyc index 79caca5b..8e51a993 100644 Binary files a/AI/Text2Sql/__pycache__/app.cpython-310.pyc and b/AI/Text2Sql/__pycache__/app.cpython-310.pyc differ diff --git a/AI/Text2Sql/app.py b/AI/Text2Sql/app.py index b87d0ba2..86c33c14 100644 --- a/AI/Text2Sql/app.py +++ b/AI/Text2Sql/app.py @@ -17,8 +17,9 @@ from Text2Sql.Util.SaveToExcel import save_to_excel from Text2Sql.Util.VannaUtil import VannaUtil from fastapi.middleware.cors import CORSMiddleware # 导入跨域中间件 - vn = VannaUtil() + + # 初始化 FastAPI 应用 @asynccontextmanager async def lifespan(app: FastAPI): @@ -36,6 +37,7 @@ async def lifespan(app: FastAPI): # 关闭时释放连接池 await app.state.pool.close() + # 初始化 FastAPI app = FastAPI(lifespan=lifespan) @@ -51,11 +53,18 @@ app.add_middleware( # 挂载静态文件目录 app.mount("/static", StaticFiles(directory="static"), name="static") + # 依赖注入连接池 async def get_db(): async with app.state.pool.acquire() as connection: yield connection +# 初始化 OpenAI 客户端 +client = AsyncOpenAI( + api_key=MODEL_API_KEY, + base_url=MODEL_API_URL, +) + @app.post("/questions/get_excel") async def get_excel(question_id: str = Form(...), question_str: str = Form(...), db: asyncpg.Connection = Depends(get_db)): @@ -70,13 +79,18 @@ async def get_excel(question_id: str = Form(...), question_str: str = Form(...), ''' question = question_str + common_prompt - # 先删除后插入,防止重复插入 - await delete_question(db, question_id) - await insert_question(db, question_id, question) - # 获取完整 SQL sql = vn.generate_sql(question) print("生成的查询 SQL:\n", sql) + # 先删除后插入,防止重复插入 + await delete_question(db, question_id) + await insert_question(db, question_id, question) + # 检查,如果sql为空,则返回错误信息 + if not sql: + return {"success": False, "message": "无法生成相应的SQL语句!"} + # 检查,如果SQL无法正确执行,返回错误消息 + if not await get_data_by_sql(db, sql): + return {"success": False, "message": "无法生成相应的SQL语句!"} # 更新question_id await update_question_by_id(db, question_id=question_id, sql=sql, state_id=1) @@ -94,12 +108,6 @@ async def get_excel(question_id: str = Form(...), question_str: str = Form(...), # http://10.10.21.20:8000/questions/get_docx_stream?question_id_get=af15d834-e7f5-46b4-a0f6-15f1f888f443 -# 初始化 OpenAI 客户端 -client = AsyncOpenAI( - api_key=MODEL_API_KEY, - base_url=MODEL_API_URL, -) - @app.api_route("/questions/get_docx_stream", methods=["POST", "GET"]) async def get_docx_stream( diff --git a/AI/Text2Sql/static/docx/7bf78045-4ea0-4054-a693-2b37b32affb7.docx b/AI/Text2Sql/static/docx/7bf78045-4ea0-4054-a693-2b37b32affb7.docx new file mode 100644 index 00000000..4e2f09a2 Binary files /dev/null and b/AI/Text2Sql/static/docx/7bf78045-4ea0-4054-a693-2b37b32affb7.docx differ diff --git a/AI/Text2Sql/static/xlsx/0a17a6d7-83fd-4f3c-b06f-1db0c410b570.xlsx b/AI/Text2Sql/static/xlsx/0a17a6d7-83fd-4f3c-b06f-1db0c410b570.xlsx new file mode 100644 index 00000000..4de5d9bb Binary files /dev/null and b/AI/Text2Sql/static/xlsx/0a17a6d7-83fd-4f3c-b06f-1db0c410b570.xlsx differ diff --git a/AI/Text2Sql/static/xlsx/1f42d069-893d-423e-b046-b357685eb7cf.xlsx b/AI/Text2Sql/static/xlsx/1f42d069-893d-423e-b046-b357685eb7cf.xlsx new file mode 100644 index 00000000..0a19d0b6 Binary files /dev/null and b/AI/Text2Sql/static/xlsx/1f42d069-893d-423e-b046-b357685eb7cf.xlsx differ diff --git a/AI/Text2Sql/static/xlsx/5fcc4665-d7f4-4c3a-bb98-4ae6eb584423.xlsx b/AI/Text2Sql/static/xlsx/5fcc4665-d7f4-4c3a-bb98-4ae6eb584423.xlsx new file mode 100644 index 00000000..37ab73bf Binary files /dev/null and b/AI/Text2Sql/static/xlsx/5fcc4665-d7f4-4c3a-bb98-4ae6eb584423.xlsx differ diff --git a/AI/Text2Sql/static/xlsx/9cb186c9-3518-4b5d-a8e3-c30dd1a14433.xlsx b/AI/Text2Sql/static/xlsx/9cb186c9-3518-4b5d-a8e3-c30dd1a14433.xlsx new file mode 100644 index 00000000..c706ed2e Binary files /dev/null and b/AI/Text2Sql/static/xlsx/9cb186c9-3518-4b5d-a8e3-c30dd1a14433.xlsx differ diff --git a/AI/Text2Sql/static/xlsx/d8f4fedd-544e-4b6e-b6ab-3d6b4ea0f99c.xlsx b/AI/Text2Sql/static/xlsx/d8f4fedd-544e-4b6e-b6ab-3d6b4ea0f99c.xlsx new file mode 100644 index 00000000..bbe6ddd7 Binary files /dev/null and b/AI/Text2Sql/static/xlsx/d8f4fedd-544e-4b6e-b6ab-3d6b4ea0f99c.xlsx differ