soneeee22000/SafeGen.dev

GitHub: soneeee22000/SafeGen.dev

面向 LLM 应用的合规中间件,通过多层验证引擎和 RAG 驱动的策略规则,实时拦截 PII 泄露、偏见内容和安全风险,确保模型输出符合企业和监管要求。

Stars: 0 | Forks: 0

# SafeGen [![在线演示](https://img.shields.io/badge/Live%20Demo-safe--gen--dev.vercel.app-blue?style=for-the-badge)](https://safe-gen-dev.vercel.app) [![许可证: MIT](https://img.shields.io/badge/License-MIT-green?style=for-the-badge)](LICENSE) **适用于 LLM 应用程序的负责任 AI 合规中间件。** SafeGen 是一个 Serverless 管道,位于您的应用程序和 Azure OpenAI 之间,在将响应提供给最终用户之前,根据可配置的安全性、偏见、PII(个人身份信息)和监管规则验证每个 LLM 响应。规则通过 RAG 加载 —— 只需上传文档即可更新合规策略,无需重新部署。 ## 核心功能 - **多层合规引擎** —— PII 检测、偏见检查、安全过滤和基于 RAG 的规则评估按顺序在每个 LLM 响应上运行 - **RAG 驱动的策略检索** —— 上复合规文档(PDF/DOCX/MD),自动分块并在 FAISS 中建立索引,以便在推理时进行语义规则匹配 - **交互式 Playground** —— 输入提示词并提交,观察合规引擎实时验证 LLM 响应,支持示例提示词和类别切换 - **实时监控仪表盘** —— React/TypeScript SPA,包含 KPI 卡片、趋势图表、标记分类和分页审计日志 - **完整审计追踪** —— 每个验证结果都记录了请求/响应负载、合规分数和标记详情,以供监管审查 - **动态规则更新** —— 无需重新部署即可添加或修改合规规则;规则在验证时通过语义搜索检索 ## 架构 [![架构图](https://img.shields.io/badge/View%20Interactive%20Diagram-Excalidraw-6965db?style=flat-square&logo=excalidraw)](https://excalidraw.com/#json=AYeGdzU2odcXQHa4tp_DM,rXcDX6ii-NgvHYrm16a_Rg) ``` graph TB subgraph Frontend["Frontend (React 19 + TypeScript)"] Dashboard[Dashboard] Playground[Playground] AuditUI[Audit Log] RulesUI[Rules Mgmt] end Client[Client App] subgraph API["Azure Functions (Python 3.10)"] Validate["POST /validate"] Ingest["POST /rules/ingest"] Rules["GET /rules"] Audit["GET /audit"] Metrics["GET /metrics"] end subgraph Core["Core Business Logic (zero Azure imports)"] OpenAI[OpenAI Client] Engine["Compliance Engine
PII · Bias · Safety · Rules"] RAG[RAG Pipeline] Logger[Audit Logger] end subgraph External["External Services"] GPT["Azure OpenAI
GPT-4o"] FAISS["FAISS Index
(embeddings)"] HF["HuggingFace
all-MiniLM-L6-v2"] Blob["Azure Blob
Storage"] end Frontend -->|HTTP| API Client -->|POST /validate| Validate Validate --> OpenAI Validate --> Engine Ingest --> RAG Audit --> Logger Metrics --> Logger Rules --> RAG OpenAI --> GPT Engine -->|validate| RAG RAG --> FAISS RAG --> HF Logger --> Blob style Frontend fill:#dbe4ff,stroke:#4a9eed style API fill:#e5dbff,stroke:#8b5cf6 style Core fill:#d3f9d8,stroke:#22c55e style External fill:#ffd8a8,stroke:#f59e0b ``` ## 技术栈 | 层级 | 技术 | | -------------- | -------------------------------------------------- | | **Runtime** | Azure Functions v2 (Python 3.10), serverless | | **LLM** | Azure OpenAI GPT-4o | | **RAG** | FAISS 向量存储, HuggingFace `all-MiniLM-L6-v2` | | **验证** | 基于正则的 PII, 关键词偏见, 模式安全 | | **存储** | Azure Blob Storage (审计日志, 规则文档) | | **Frontend** | React 19, TypeScript, Vite, Tailwind CSS, Recharts | | **UI Kit** | shadcn/ui (Radix + Tailwind 组件) | | **测试** | pytest (后端, 150 个测试), vitest (前端, 53 个) | ## 项目结构 ``` safegen/ ├── backend/ │ ├── function_app.py # Azure Functions entry point (blueprint registration) │ ├── core/ # Business logic (zero Azure Functions imports) │ │ ├── models.py # Pydantic v2 models (request/response schemas) │ │ ├── openai_client.py # Azure OpenAI wrapper (GenerationResult) │ │ ├── rag_pipeline.py # Text extraction → chunking → embedding → FAISS │ │ ├── blob_storage.py # Azure Blob Storage CRUD │ │ ├── compliance_engine.py # Orchestrates all validators, computes score │ │ ├── validators.py # PIIDetector, BiasChecker, SafetyFilter │ │ └── audit_logger.py # Dual-backend audit store (File/Blob) │ ├── functions/ # HTTP triggers (thin wrappers over core/) │ │ ├── validate.py # POST /api/validate │ │ ├── ingest_rules.py # POST /api/rules/ingest │ │ ├── list_rules.py # GET /api/rules │ │ ├── audit.py # GET /api/audit │ │ └── metrics.py # GET /api/metrics │ └── tests/ # 150 tests across 10 modules │ ├── conftest.py # Shared fixtures (mock_env, mock clients) │ ├── test_models.py # Pydantic validation (17 tests) │ ├── test_openai_client.py # Azure OpenAI wrapper (7 tests) │ ├── test_validate.py # /api/validate endpoint (13 tests) │ ├── test_rag_pipeline.py # RAG pipeline (16 tests) │ ├── test_ingest_rules.py # /api/rules/ingest (8 tests) │ ├── test_compliance_engine.py # Compliance scoring (27 tests) │ ├── test_validators.py # PII/bias/safety (40 tests) │ ├── test_audit.py # /api/audit endpoint (10 tests) │ ├── test_audit_logger.py # Audit store (6 tests) │ └── test_metrics.py # /api/metrics endpoint (6 tests) │ ├── frontend/ │ ├── vite.config.ts # Vite + Tailwind + API proxy config │ ├── vitest.config.ts # Test config (jsdom + path aliases) │ ├── components.json # shadcn/ui configuration │ └── src/ │ ├── App.tsx # BrowserRouter + route definitions │ ├── main.tsx # React DOM entry point │ ├── index.css # Tailwind v4 + light/dark design tokens │ ├── types/index.ts # TypeScript interfaces (1:1 backend mirror) │ ├── services/api.ts # Typed API client with error handling │ ├── hooks/ │ │ ├── use-api.ts # Generic useApi data-fetching hook │ │ └── use-theme.ts # Dark mode toggle (localStorage) │ ├── lib/ │ │ ├── utils.ts # cn() class merge helper │ │ ├── constants.ts # Named constants, nav items, thresholds │ │ └── format.ts # Score/date/duration formatters │ ├── components/ │ │ ├── ui/ # shadcn components (button, card, table, etc.) │ │ ├── layout/ # Sidebar, Header, AppLayout │ │ ├── dashboard/ # KpiCard, TrendChart, FlagBreakdownChart, ScoreGauge │ │ ├── playground/ # PromptInput, ResultPanel, FlagList, ExamplePrompts │ │ ├── audit/ # AuditFilters, AuditTable, AuditPagination, AuditDetailModal │ │ └── rules/ # RuleUploader (drag-and-drop), RuleList │ ├── pages/ │ │ ├── DashboardPage.tsx # KPI cards + charts, 60s auto-refresh │ │ ├── PlaygroundPage.tsx # Live compliance validation playground │ │ ├── AuditPage.tsx # Filterable table + detail modal │ │ └── RulesPage.tsx # Upload zone + rule card grid │ └── test/ │ ├── setup.ts # jest-dom matchers │ └── mocks.ts # Factory functions for mock data │ ├── rules/ # Sample compliance rule documents │ ├── gdpr_content_rules.md # 5 GDPR rules │ ├── bias_detection_policy.md # 5 bias detection rules │ └── pii_handling_rules.md # 4 PII handling rules │ ├── docker-compose.yml # Full stack: backend + frontend + Azurite ├── .github/workflows/ci.yml # GitHub Actions CI (7 jobs) ├── .env.example # Required environment variables ├── ARCHITECTURE.md # System design + technical decisions ├── BUILDPLAN.md # Phase-by-phase build progress └── CLAUDE.md # Developer guide for AI-assisted coding ``` ## API 端点 | 方法 | 端点 | 描述 | | ------- | ------------------- | ------------------------------------------------ | | `POST` | `/api/validate` | 发送提示词,获取经合规验证的 LLM 响应 | | `POST` | `/api/rules/ingest` | 上传合规文档 (PDF/DOCX/MD/TXT) | | `GET` | `/api/rules` | 列出所有已摄取的规则及其分块数量 | | `GET` | `/api/audit` | 支持日期/状态过滤的分页审计日志 | | `GET` | `/api/metrics` | 聚合统计,合规率,时间序列 | ### 示例:验证提示词 ``` curl -X POST http://localhost:7071/api/validate \ -H "Content-Type: application/json" \ -d '{"prompt": "Explain data privacy best practices", "rules_category": "all"}' ``` 响应: ``` { "response": "Data privacy best practices include...", "compliance": { "passed": true, "score": 0.95, "flags": [], "layers_run": ["pii", "bias", "safety"] }, "model": "gpt-4o" } ``` ## 合规引擎 四个验证层按顺序在每个 LLM 响应上运行: | 层级 | 检查内容 | 严重程度 | | ----------- | ------------------------------------------------------------ | ------------ | | **PII** | 电子邮件、电话、SSN、信用卡、IPv4(含智能排除) | Critical | | **Bias** | 性别化职位、残障歧视语言、刻板印象模式 | Warning | | **Safety** | 仇恨言论、暴力指导、自残内容 | Critical | | **Rules** | RAG 检索已上传的策略文档以进行规则合规性检查 | 可配置 | **评分:** 起始分 1.0,每个 critical 标记扣 0.3,每个 warning 标记扣 0.1。通过阈值:无 critical 标记。 ## 快速开始 ### 前置条件 - Python 3.10+ - Node.js 18+ - Azure Functions Core Tools v4 - 拥有 GPT-4o 部署的 Azure OpenAI 资源 ### 后端 ``` cd backend python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate pip install -r requirements.txt cp local.settings.example.json local.settings.json # 填写 Azure OpenAI + Blob Storage 凭据 func start # Runs on http://localhost:7071 ``` ### 前端 ``` cd frontend npm install npm run dev # Runs on http://localhost:5173, proxies /api to :7071 ``` ### 使用 Docker 运行 ``` cp .env.example .env # 在 .env 中填写您的 Azure OpenAI 凭据 docker-compose up --build # 后端:http://localhost:7071 # 前端:http://localhost:5173 # Azurite:http://localhost:10000 (Blob 模拟器) ``` ### 运行测试 ``` # 后端 (150 个测试) cd backend && python -m pytest tests/ -v --tb=short # 前端 (53 个测试) cd frontend && npm run test:run ``` ## 环境变量 ``` AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/ AZURE_OPENAI_API_KEY=your-key AZURE_OPENAI_DEPLOYMENT=gpt-4o AZURE_STORAGE_CONNECTION_STRING=your-connection-string AZURE_STORAGE_CONTAINER_RULES=compliance-rules AZURE_STORAGE_CONTAINER_AUDIT=audit-logs EMBEDDING_MODEL=all-MiniLM-L6-v2 ``` ## 构建进度 - [x] **阶段 1:** 核心后端 —— Azure Functions + Azure OpenAI 代理 - [x] **阶段 2:** RAG 管道 —— 文本提取、分块、FAISS 索引 - [x] **阶段 3:** 合规引擎 —— PII 检测、偏见检查、安全过滤 - [x] **阶段 4:** 指标与审计 —— 双后端日志记录、分页检索、聚合统计 - [x] **阶段 5:** React 仪表盘 —— KPI 卡片、趋势图表、审计日志、规则管理 - [x] **阶段 6:** Docker + CI/CD —— Dockerfiles、docker-compose (全栈)、GitHub Actions 管道 - [x] **阶段 7:** 交互式 Playground —— 带有示例提示词和类别切换的实时合规验证 - [x] **已部署** —— 前端在 [Vercel](https://safe-gen-dev.vercel.app),后端在 Azure Functions ## 设计决策 | 决策 | 选择 | 原因 | | ------------------------ | ------------------------------- | -------------------------------------------------------- | | Serverless runtime | Azure Functions v2 (Python) | 可缩容至零,按需付费,无需管理基础设施 | | 向量存储 | FAISS (内存) | 快速,零基础设施,足以应对策略规模的数据集 | | Embeddings | HuggingFace `all-MiniLM-L6-v2` | 免费,快速,语义搜索质量良好 | | Frontend framework | React + Vite + TypeScript | 快速开发周期,类型安全,庞大生态系统 | | UI 组件 | shadcn/ui (复制粘贴, 非 npm) | 完全控制,Tailwind 原生,无运行时依赖 | | 后端/前端边界 | Clean Architecture | `core/` 零 Azure 导入;完全可测试 | | 类型策略 | TypeScript 中的 snake_case | 与 JSON 响应完全匹配;无需转换层 | | 容器化 | Docker + docker-compose | 使用 Azurite blob 模拟器实现可重现的本地开发 | | CI pipeline | GitHub Actions (7 个作业) | 后端 + 前端 + Docker 的并行 lint/test/build | ## 许可证 MIT
标签:AI治理, Azure Functions, Azure OpenAI, FAISS向量数据库, GPT-4o, Naabu, PII检测, RAG检索增强生成, React, Serverless无服务器, Syscalls, TypeScript, 企业合规, 偏见检测, 内容安全, 合规中间件, 大语言模型合规, 大语言模型蜜罐, 安全插件, 安全过滤, 实时仪表盘, 审计追踪, 监管科技, 网络安全, 请求拦截, 逆向工具, 隐私保护