From 5241f6819063071891aa0b482aea8704cb359d77 Mon Sep 17 00:00:00 2001 From: KiriAky 107 Date: Tue, 24 Feb 2026 21:07:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=20API=20Key=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=EF=BC=8C=E5=AE=8C=E6=88=90=20FastAP?= =?UTF-8?q?I=20=E5=9F=BA=E7=A1=80=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/.env.example | 15 ++ .../app/__pycache__/config.cpython-312.pyc | Bin 0 -> 1294 bytes backend/app/__pycache__/main.cpython-312.pyc | Bin 0 -> 940 bytes backend/{main.py => app/api/__init__.py} | 0 backend/app/config.py | 31 ++++ backend/app/main.py | 19 ++ backend/app/service/__init__.py | 0 backend/readme.md | 165 ++++++++++++++++++ 8 files changed, 230 insertions(+) create mode 100644 backend/.env.example create mode 100644 backend/app/__pycache__/config.cpython-312.pyc create mode 100644 backend/app/__pycache__/main.cpython-312.pyc rename backend/{main.py => app/api/__init__.py} (100%) create mode 100644 backend/app/config.py create mode 100644 backend/app/main.py create mode 100644 backend/app/service/__init__.py diff --git a/backend/.env.example b/backend/.env.example new file mode 100644 index 0000000..8b4ebf1 --- /dev/null +++ b/backend/.env.example @@ -0,0 +1,15 @@ +# 基础配置 +APP_NAME="FilesReadSystem" +DEBUG=true + +# 数据库 +MONGODB_URL="mongodb://username:password@host:port" +REDIS_URL="redis://localhost:6379/0" + +# 大模型 API +LLM_API_KEY="" +LLM_BASE_URL="" + +# 文件存储配置 +UPLOAD_DIR="./data/uploads" +MAX_UPLOAD_SIZE=104857600 # 100MB \ No newline at end of file diff --git a/backend/app/__pycache__/config.cpython-312.pyc b/backend/app/__pycache__/config.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6391ad23075c503cd0cda5891b286baa623b2d23 GIT binary patch literal 1294 zc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/app/api/__init__.py similarity index 100% rename from backend/main.py rename to backend/app/api/__init__.py diff --git a/backend/app/config.py b/backend/app/config.py new file mode 100644 index 0000000..2778e25 --- /dev/null +++ b/backend/app/config.py @@ -0,0 +1,31 @@ +from pydantic_settings import BaseSettings, SettingsConfigDict +from pathlib import Path + +class Settings(BaseSettings): + # 应用基础配置 + APP_NAME: str = "FilesReadSystem" + DEBUG: bool = True + API_V1_STR: str = "/api/v1" + + # 数据库 + MONGODB_URL: str + MONGODB_DB_NAME: str + REDIS_URL: str + + # AI 相关 + LLM_API_KEY: str + LLM_BASE_URL: str + LLM_MODEL_NAME: str + + # 文件路径 + BASE_DIR: Path = Path(__file__).resolve().parent.parent.parent + UPLOAD_DIR: str = "data/uploads" + + # 允许 Pydantic 从 .env 文件读取 + model_config = SettingsConfigDict( + env_file=Path(__file__).parent.parent / ".env", + env_file_encoding='utf-8', + extra='ignore' + ) + +settings = Settings() \ No newline at end of file diff --git a/backend/app/main.py b/backend/app/main.py new file mode 100644 index 0000000..acece94 --- /dev/null +++ b/backend/app/main.py @@ -0,0 +1,19 @@ +from fastapi import FastAPI +from config import settings + +app = FastAPI( + title=settings.APP_NAME, + openapi_url=f"{settings.API_V1_STR}/openapi.json" +) + +@app.get("/") +async def root(): + return { + "message": f"Welcome to {settings.APP_NAME}", + "status": "online", + "debug_mode": settings.DEBUG + } + +if __name__ == "__main__": + import uvicorn + uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True) \ No newline at end of file diff --git a/backend/app/service/__init__.py b/backend/app/service/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/readme.md b/backend/readme.md index 8739b25..a72b681 100644 --- a/backend/readme.md +++ b/backend/readme.md @@ -1,3 +1,8 @@ +> [!TIP] +> +> 注意,本文档仅为开发过程中团队交流使用,非正式的readme文档。 + + ## 技术栈 | 层次 | 组件 | 说明 | @@ -36,4 +41,164 @@ pip install -r requirements.txt ``` 以安装项目需要的依赖包 +如果你用的是vscode,那么现在我们要配置自动启动python的虚拟环境 +在项目的根目录下(即与backend同级)创建一个名为.vscode的文件夹 +在.vscode文件夹中创建一个名为settings.json的文件 +settings.json内容如下: +```json +{ +"python.defaultInterpreterPath": "${workspaceFolder}/backend/venv/Scripts/python.exe" +} +``` +保存即可 +## 关于.gitignore +为了在上传git仓库时,不把venv中的软件包和其他关于项目的特殊api key暴露,请将.gitignore文件放在项目根目录下,并添加以下内容: +```bash +/.git/ +/.gitignore +/.idea/ +/.vscode/ +/backend/venv/ +/backend/command/ +/backend/.env +/backend/.env.local +/backend/.env.*.local +``` + +## 关于env的说明 +为了数据安全,请不要把api key暴露,请将api key保存在.env文件中,并添加到.gitignore中(正如前文所示),这样git就不会将api key上传到git仓库中。 +但,可以保留.env.example文件,以示需要调用的api key + +### 预计项目结构: +```bash +FilesReadSystem/ +├── backend/ # 后端服务(Python + FastAPI) +│ ├── app/ # 主应用代码 +│ │ ├── api/ # API 路由层 +│ │ │ ├── __init__.py +│ │ │ ├── endpoints/ # 各功能模块的路由 +│ │ │ │ ├── __init__.py +│ │ │ │ ├── upload.py # 文件上传接口 +│ │ │ │ ├── task.py # 任务状态查询接口 +│ │ │ │ ├── result.py # 结果获取接口 +│ │ │ │ ├── instruction.py # 自然语言指令接口 +│ │ │ │ └── template.py # 表格模板管理接口 +│ │ │ └── dependencies.py # 通用依赖(如数据库会话) +│ │ ├── core/ # 核心业务逻辑 +│ │ │ ├── __init__.py +│ │ │ ├── document_parser/ # 文档解析模块(第二阶段) +│ │ │ │ ├── __init__.py +│ │ │ │ ├── base.py # 解析器基类 +│ │ │ │ ├── docx_parser.py # .docx 解析 +│ │ │ │ ├── xlsx_parser.py # .xlsx 解析 +│ │ │ │ ├── md_parser.py # .md 解析 +│ │ │ │ ├── txt_parser.py # .txt 解析 +│ │ │ │ └── utils.py # 清洗、分块等工具函数 +│ │ │ ├── rag/ # RAG 检索增强生成模块(第三阶段) +│ │ │ │ ├── __init__.py +│ │ │ │ ├── embeddings.py # Embedding 模型封装 +│ │ │ │ ├── vector_store.py # faiss 向量索引管理 +│ │ │ │ ├── retriever.py # 检索器(从 faiss 取回 + MongoDB 补充元数据) +│ │ │ │ └── llm_chain.py # LLM 调用链(Prompt 模板、解析输出) +│ │ │ ├── table_filler/ # 表格自动填写模块(第三阶段) +│ │ │ │ ├── __init__.py +│ │ │ │ ├── template_reader.py # 读取表格模板结构 +│ │ │ │ ├── data_extractor.py # 根据模板提取数据(调用 RAG + LLM) +│ │ │ │ └── excel_writer.py # 将数据回填到 Excel +│ │ │ └── instruction/ # 自然语言指令解析模块(第一阶段) +│ │ │ ├── __init__.py +│ │ │ ├── intent_parser.py # 意图识别 +│ │ │ └── executor.py # 指令执行(调用其他核心模块) +│ │ ├── models/ # 数据库模型(MongoDB 文档模型) +│ │ │ ├── __init__.py +│ │ │ ├── document.py # 文档元数据模型 +│ │ │ ├── chunk.py # 文档块模型(用于 faiss 元数据) +│ │ │ ├── task.py # 异步任务模型 +│ │ │ └── result.py # 提取结果模型 +│ │ ├── schemas/ # Pydantic 模型(请求/响应校验) +│ │ │ ├── __init__.py +│ │ │ ├── file.py +│ │ │ ├── task.py +│ │ │ ├── result.py +│ │ │ └── instruction.py +│ │ ├── services/ # 服务层(封装数据库操作等) +│ │ │ ├── __init__.py +│ │ │ ├── mongo_service.py # MongoDB 通用操作 +│ │ │ └── file_service.py # 文件存储服务 +│ │ ├── tasks/ # Celery 异步任务定义 +│ │ │ ├── __init__.py +│ │ │ ├── celery_app.py # Celery 实例 +│ │ │ ├── document_tasks.py # 文档处理任务(解析、向量化、提取) +│ │ │ └── table_tasks.py # 表格填写任务 +│ │ ├── utils/ # 工具函数 +│ │ │ ├── __init__.py +│ │ │ ├── logger.py # 日志配置 +│ │ │ ├── file_utils.py # 文件操作 +│ │ │ └── exceptions.py # 自定义异常 +│ │ ├── config.py # 应用配置(环境变量读取) +│ │ ├── main.py # FastAPI 应用入口 +│ │ └── dependencies.py # 全局依赖 +│ ├── tests/ # 测试目录(原有 MongoDBConnectionTest.py 保留) +│ │ ├── __init__.py +│ │ ├── conftest.py # pytest fixtures +│ │ ├── test_api/ # API 测试 +│ │ │ ├── test_upload.py +│ │ │ └── ... +│ │ ├── test_core/ # 核心模块单元测试 +│ │ │ ├── test_document_parser.py +│ │ │ └── ... +│ │ ├── test_tasks/ # 任务测试 +│ │ └── MongoDBConnectionTest.py # 已存在的 MongoDB 连接测试 +│ ├── venv/ # Python 虚拟环境(gitignore) +│ ├── requirements.txt # Python 依赖 +│ ├── requirements-dev.txt # 开发依赖(如 pytest, black) +│ ├── .env.example # 环境变量示例 +│ ├── Dockerfile # 后端 Docker 镜像构建文件 +│ ├── docker-compose.yml # 本地开发服务编排(MongoDB, Redis, 后端) +│ └── README.md # 后端说明文档 +│ +├── frontend/ # 前端项目(Vue/React) +│ ├── public/ +│ ├── src/ +│ │ ├── api/ # 后端 API 调用封装 +│ │ ├── components/ # 通用组件 +│ │ ├── views/ # 页面视图 +│ │ │ ├── UploadView.vue # 文件上传页 +│ │ │ ├── TaskStatusView.vue # 任务状态页 +│ │ │ ├── ResultView.vue # 结果展示页 +│ │ │ └── InstructionView.vue # 自然语言指令交互页 +│ │ ├── router/ # 路由配置 +│ │ ├── store/ # 状态管理 +│ │ ├── App.vue +│ │ └── main.js +│ ├── package.json +│ ├── vue.config.js / vite.config.js +│ ├── Dockerfile # 前端 Docker 镜像构建文件 +│ └── README.md +│ +├── data/ # 本地数据目录(gitignore) +│ ├── uploads/ # 上传文件临时存储 +│ ├── faiss_index/ # faiss 索引文件保存位置 +│ └── outputs/ # 生成的表格文件下载缓存 +│ +├── docs/ # 项目文档(提交材料准备) +│ ├── 01_项目概要介绍.md +│ ├── 02_项目简介PPT.pptx +│ ├── 03_项目详细方案.md +│ ├── 04_演示视频说明.md +│ ├── 05_企业要求材料/ +│ │ ├── 训练素材来源说明.md +│ │ ├── 关键模块概要设计与创新要点.md +│ │ └── Demo运行说明.md +│ └── 06_其他补充材料/ +│ +├── scripts/ # 辅助脚本 +│ ├── build_faiss_index.py # 预构建向量索引(基于已有文档集) +│ ├── generate_test_data.py # 生成模拟测试数据 +│ └── evaluate.py # 准确率评估脚本 +│ +├── .gitignore +├── README.md # 项目总 README +└── docker-compose.yml # 全局编排(包含前端、后端、数据库、Redis) +```