配置 API Key 环境变量,完成 FastAPI 基础配置

This commit is contained in:
2026-02-24 21:07:05 +08:00
parent 9de6c935fa
commit 5241f68190
8 changed files with 230 additions and 0 deletions

15
backend/.env.example Normal file
View File

@@ -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

Binary file not shown.

Binary file not shown.

31
backend/app/config.py Normal file
View File

@@ -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()

19
backend/app/main.py Normal file
View File

@@ -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)

View File

View File

@@ -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
```