Hudsonmathew1910/Hero-s-AI

GitHub: Hudsonmathew1910/Hero-s-AI

基于 Django 的生产级 AI 助手平台,通过 Baymax 三层模型回退调度器实现高可用的多模型对话服务。

Stars: 2 | Forks: 0

# Hero's AI 🤖 **一个生产级的、基于 Django 的 AI 助手平台**,具备智能多模型路由、语音功能、RAG 分析和企业级安全性。 Baymax(核心 AI 调度器)在**三层模型回退**(Gemini → OpenRouter → Groq)之间智能路由请求,通过加密的 API key 管理和持久化的聊天记录确保 **99.9% 的正常运行时间**。 ## ✨ 核心功能 - 🧠 **Baymax AI 调度器** — 具备三层回退机制的意图感知路由(Gemini → OpenRouter → Groq) - 💬 **多模型聊天** — 在主要模型和回退模型之间自动切换 - 🎙️ **语音聊天** — 语音转文字输入,配备智能意图路由 - 🔍 **网页搜索** — 基于 LLM 的查询重写和实时答案合成 - 📄 **文件处理** — 图像 OCR、PDF 解析、DOCX 分析和数据提取 - 🔐 **加密 API Keys** — 基于 Fernet 的用户凭证加密 - 👤 **OAuth + 认证** — Google OAuth 集成,配备安全的会话管理 - 💾 **持久化聊天记录** — 基于会话的对话上下文,支持可选的临时聊天 - 📊 **Infinsight RAG 分析** — 针对分析的数据集感知检索增强生成 - 🎯 **意图检测** — 基于 NLP 的路由,实现最佳任务处理 ## 🏗️ 技术栈 | 组件 | 技术 | |-----------|-----------| | **后端** | Django 5.2+ 与 DRF | | **数据库** | PostgreSQL (Neon) 与 psycopg2 | | **AI/LLM** | Google Gemini API, OpenRouter (多模型), Groq | | **NLP** | 意图检测与路由引擎 | | **OCR** | 通过 pytesseract 调用 Tesseract | | **前端** | JavaScript (36.7%), CSS (21.9%), HTML (16.3%) | | **安全** | Cryptography (Fernet), django-cryptography | | **部署** | Gunicorn, WhiteNoise, 支持 Procfile | ## 🚀 快速开始 ### 前置条件 - Python 3.11+ - PostgreSQL(或 [Neon](https://neon.tech) serverless) - 已安装 Tesseract OCR - API keys:Google Gemini、OpenRouter 或 Groq ### 安装 <<<<<<< HEAD ``` # 1. Clone repository git clone https://github.com/Hudsonmathew1910/Hero-s-AI.git cd Hero-s-AI/hero_ai ======= ````bash # 1. Clone repository git clone https://github.com/Hudsonmathew1910/hero-ai.git cd hero-ai/hero_ai >>>>>>> e8bef80 (Hero's AI - V2.6 - Plus - [ Add message encryption ]) # 2. 创建虚拟环境 python -m venv .venv source .venv/bin/activate # macOS/Linux # OR .venv\Scripts\activate # Windows # 3. 安装依赖 pip install -r requirements.txt # 4. 配置环境 cp .env.example .env # 使用你的凭证编辑 .env(见下文) # 5. Run migrations python manage.py migrate # 6. 启动开发服务器 python manage.py runserver ``` 访问 `http://localhost:8000` ## ⚙️ 环境变量 将 `.env.example` 复制为 `.env` 并进行配置: | 变量 | 描述 | 示例 | |----------|-----------|---------| | `NEON_DB_NAME` | PostgreSQL 数据库名称 | `hero_db` | | `NEON_DB_USER` | 数据库用户 | `neon_user` | | `NEON_DB_PASSWORD` | 数据库密码 | — | | `NEON_DB_HOST` | 数据库主机 | `ep-xxx.neon.tech` | | `NEON_DB_PORT` | 数据库端口 | `5432` | | `SECRET_KEY` | Django 密钥(重新生成) | — | | `DEBUG` | 开发模式 | `False` (生产环境) | | `ALLOWED_HOSTS` | 允许的域名 | `localhost,yourdomain.com` | | `ENCRYPTION_KEY` | 用于 API key 加密的 Fernet 密钥 | — | | `GOOGLE_CLIENT_ID` | Google OAuth 客户端 ID | — | | `GOOGLE_CLIENT_SECRET` | Google OAuth 密钥 | — | | `SITE_URL` | 你的站点 URL | `https://yourdomain.com` | **生成密钥:** ``` <<<<<<< HEAD # Django secret key python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())" # Fernet encryption key python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())" ``` ======= # 在生产环境中启动 Gunicorn gunicorn hero_ai.wsgi:application --workers 3 --bind 0.0.0.0:8000 ``` ### 部署配置 - **Procfile**: Included for easy deployment to Railway, Render, or Heroku. - **Static Files**: Remember to run `python manage.py collectstatic` as part of your deployment pipeline. - **Environment**: Set `DEBUG=False` in your production environment variables. ``` >>>>>>> e8bef80 (Hero's AI - V2.6 - Plus - [ Add message encryption ]) ## 📁 项目结构 ``` hero_ai/ <<<<<<< HEAD ├── backend/ # Django application │ ├── hero_model.py # Baymax AI dispatcher & 3-tier fallback │ ├── Nlp.py # Intent detection & routing │ ├── views.py # API endpoints │ ├── handle_file.py # File upload, OCR, PDF parsing │ ├── models.py # Database models │ ├── models_task/ # Specialized handlers (web search, etc.) │ ├── migrations/ # Database migrations │ └── tests.py # Unit & integration tests │ ├── hero_ai/ # Django project config │ ├── settings.py # Project settings │ ├── urls.py # URL routing │ └── wsgi.py # WSGI config │ ├── static/ # CSS, JS, images ├── templates/ # HTML templates ├── logs/ # Application logs (gitignored) ├── .env.example # Environment template ├── requirements.txt # Python dependencies ├── manage.py # Django management └── Procfile # Production deployment config ======= ├── backend/ # Django app — models, views, AI logic │ ├── hero_model.py # Baymax AI dispatcher + model fallback │ ├── views.py # API endpoints │ ├── Nlp.py # Intent detection (NLP routing) │ ├── handle_file.py # File upload & OCR handling │ └── models_task/ # Specialized task handlers (web search, etc.) ├── hero_ai/ # Django project settings ├── static/ # CSS, JS, images ├── templates/ # HTML templates ├── logs/ # Application logs (gitignored) └── manage.py >>>>>>> e8bef80 (Hero's AI - V2.6 - Plus - [ Add message encryption ]) ``` --- ## 🔧 核心组件 ### Baymax AI Dispatcher (`hero_model.py`) - **3-Tier Fallback**: Gemini → OpenRouter → Groq - **Dynamic Prompts**: Different system prompts for text/coding/voice/web-search - **Token Management**: Intelligent token budgeting per model - **Session Awareness**: Maintains conversation context <<<<<<< HEAD ```python # 示例用法 baymax = Baymax() response = baymax.dispatch( user_msg="Explain quantum computing", history=[...], keys={"gemini": "...", "openrouter": "..."}, task="text", primary_model="gemini-2.5-flash" ) ``` ### Intent Detection (`Nlp.py`) Routes requests to appropriate handlers: - `text` — General conversation - `coding` — Code generation & debugging - `voice` — Voice-to-text responses - `websearch` — Real-time web information - `file_analysis` — Document & image processing --- ## 🚀 生产环境部署 ### 使用 Gunicorn ```bash gunicorn hero_ai.wsgi:application \ --workers 3 \ --bind 0.0.0.0:8000 \ --timeout 120 ``` ### 使用 Procfile (Railway, Render, Heroku) Included `Procfile` automatically deploys with: ``` web: gunicorn hero_ai.wsgi:application ``` ### 部署前检查清单 - [ ] Set `DEBUG=False` - [ ] Generate new `SECRET_KEY` and `ENCRYPTION_KEY` - [ ] Configure `ALLOWED_HOSTS` - [ ] Enable HTTPS and set `SESSION_COOKIE_SECURE=True` - [ ] Run `python manage.py collectstatic` - [ ] Set up PostgreSQL backups (Neon has built-in backups) - [ ] Configure monitoring & error tracking (e.g., Sentry) ```bash python manage.py collectstatic --noinput ``` --- ## 🔒 安全 ### 最佳实践 - ✅ **Never commit `.env`** — already in `.gitignore` - ✅ **Encrypt sensitive data** — Fernet encryption for stored API keys - ✅ **Use environment variables** — All secrets from `.env` - ✅ **HTTPS only** — Set `SESSION_COOKIE_SECURE=True` in production - ✅ **Rate limiting** — Django rate limit middleware included - ✅ **CORS protection** — Configured per deployment ### API Key 加密 User API keys are encrypted using Fernet before storage: ```python from cryptography.fernet import Fernet cipher = Fernet(ENCRYPTION_KEY) encrypted = cipher.encrypt(api_key.encode()) ``` --- ## 📦 依赖 ### 关键库 - **Django 5.2+** — Web framework - **google-genai** — Google Gemini API - **requests** — HTTP client for OpenRouter/Groq - **cryptography** — Fernet encryption - **pandas, numpy, scikit-learn** — Data analysis - **pypdf, pdfplumber, python-docx** — Document parsing - **ddgs, wikipedia** — Web search - **pytesseract** — OCR See `requirements.txt` for complete list. --- ## 🧪 测试 Run unit and integration tests: ```bash python manage.py test backend ``` Tests cover: - Intent detection accuracy - Model fallback behavior - API key encryption/decryption - File parsing & OCR --- ## 📊 语言组成 | Language | Percentage | |----------|-----------| | JavaScript | 36.7% | | Python | 25.1% | | CSS | 21.9% | | HTML | 16.3% | --- ## 🤝 贡献 1. Fork the repository 2. Create a feature branch (`git checkout -b feature/amazing-feature`) 3. Commit changes (`git commit -m 'Add amazing feature'`) 4. Push to branch (`git push origin feature/amazing-feature`) 5. Open a Pull Request --- ## 📝 许可证 MIT License — see `LICENSE` file for details --- ## 🙋 支持 For issues, feature requests, or questions: - 📧 Open an [Issue](https://github.com/Hudsonmathew1910/Hero-s-AI/issues) - 💬 Start a [Discussion](https://github.com/Hudsonmathew1910/Hero-s-AI/discussions) --- **Built with ❤️ by Hudson Mathew** ======= MIT ``` >>>>>>> e8bef80 (Hero's AI - V2.6 - Plus - [ Add message encryption ]) ```
标签:数据可视化, 测试用例, 自定义脚本, 逆向工具