diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000000000000000000000000000000000000..070e5852fbcc2961db575453b995aa699631d427 --- /dev/null +++ b/README.en.md @@ -0,0 +1,200 @@ +# Agri-RAG: Agriculture Knowledge Question-Answering System + +An agriculture knowledge question-answering system based on RAG (Retrieval-Augmented Generation) architecture. + +## System Overview + +Agri-RAG combines LLM + RAG with FastAPI backend and Vue 3 frontend (Vanilla JS). + +## Key Features + +- **Intelligent Q&A**: Domain-specific agricultural question answering powered by a knowledge base +- **Vector Retrieval**: Semantic similarity matching +- **Rerank**: Multi-level retrieval ranking +- **Multimodal Support**: Image OCR and image analysis +- **Tool Integration**: Weather query, news search, price lookup +- **Conversation History**: Session management for multi-turn dialogues +- **Streaming Responses**: SSE-based streaming output +- **File Upload**: Supports PDF/DOCX/TXT/XLSX/CSV/HTML/PPTX +- **Real-time Monitoring**: Health checks and statistical analysis + +## Technology Stack + +- **Backend**: Python 3.10 / FastAPI / Uvicorn / Gunicorn +- **Large Language Model**: OpenAI API (compatible) +- **Vector Database**: FAISS / Chroma +- **Embedding Models**: HuggingFace Models +- **Frontend**: Vanilla JavaScript / HTML5 / CSS3 +- **Deployment**: Docker / Nginx + +## Quick Start + +### Prerequisites + +- Python 3.10+ +- Docker & Docker Compose +- API Key (OpenAI / Compatible) + +### Local Development + +```bash +# 1. Create virtual environment +python -m venv venv +source venv/bin/activate # Linux/Mac +# or venv\Scripts\activate # Windows + +# 2. Install dependencies +pip install -r backend/requirements.txt + +# 3. Configure environment variables +cp backend/.env.example backend/.env +# Edit backend/.env to set API_KEY and other values + +# 4. Start backend +cd backend +uvicorn app.main:app --reload + +# 5. Start frontend +# Use any static server to serve frontend/ +# Example: python -m http.server 8080 -d frontend/ +``` + +### Docker Deployment + +```bash +# Build and start +docker-compose up -d + +# View logs +docker-compose logs -f +``` + +## API Endpoints + +| Endpoint | Method | Description | +|----------|--------|-------------| +| `/api/health` | GET | Health check | +| `/api/v1/chat` | POST | Question answering | +| `/api/v1/chat/stream` | POST | Streaming question answering | +| `/api/v1/tools` | GET | List of available tools | +| `/api/v1/documents/upload` | POST | Upload document | +| `/api/v1/documents` | GET | List documents | +| `/api/v1/documents/{filename}` | DELETE | Delete document | +| `/api/v1/history/{session_id}` | GET | Conversation history | +| `/api/v1/stats` | GET | Statistical data | +| `/api/v1/metrics` | GET | Performance metrics | + +## Directory Structure + +``` +agri-rag-system/ +├── backend/ +│ ├── app/ +│ │ ├── __init__.py +│ │ ├── cache.py # Query cache +│ │ ├── config.py # Configuration management +│ │ ├── evaluator.py # RAG evaluator +│ │ ├── llm_client.py # LLM client +│ │ ├── main.py # FastAPI application +│ │ ├── models.py # Data models +│ │ ├── multimodal_processor.py # Multimodal processing +│ │ ├── rag_engine.py # RAG engine +│ │ ├── reranker.py # Reranker +│ │ ├── search_tool.py # Search tools +│ │ ├── tool_registry.py # Tool registry +│ │ ├── utils.py # Utility functions +│ │ └── vector_store.py # Vector storage +│ ├── data/ +│ │ ├── processed/ +│ │ ├── raw/ +│ │ └── vector_db/ +│ ├── Dockerfile +│ ├── requirements.txt +│ └── .env.example +├── deploy/ +│ ├── agri-rag.service +│ ├── gunicorn.conf.py +│ ├── manage.sh +│ ├── nginx.conf +│ └── setup.sh +├── frontend/ +│ ├── app.js +│ ├── index.html +│ └── style.css +├── docker-compose.yml +├── PRODUCTION_READINESS_REPORT.md +├── SYSTEM_EVALUATION_REPORT.md +├── UPGRADE_AND_DEPLOY_PLAN.md +└── DEPLOY_GUIDE.md +``` + +## Configuration Variables + +| Variable | Description | Default | +|----------|-------------|---------| +| `API_KEY` | LLM API Key | - | +| `API_BASE_URL` | API endpoint URL | https://api.openai.com/v1 | +| `MODEL_NAME` | LLM model name | gpt-3.5-turbo | +| `EMBEDDING_MODEL` | Embedding model | sentence-transformers | +| `HF_ENDPOINT` | HuggingFace mirror | https://hf-mirror.com | +| `ADMIN_PASSWORD` | Admin password | admin123 | +| `RATE_LIMIT_PER_MINUTE` | Rate limit per minute | 60 | + +## Usage Examples + +### Question Answering Request + +```bash +curl -X POST http://localhost:8000/api/v1/chat \ + -H "Content-Type: application/json" \ + -d '{ + "question": "How to prevent rice blast disease?", + "session_id": "default" + }' +``` + +### Streaming Question Answering + +```bash +curl -X POST http://localhost:8000/api/v1/chat/stream \ + -H "Content-Type: application/json" \ + -d '{ + "question": "What are the conditions for Fusarium head blight in wheat?", + "session_id": "default" + }' +``` + +### File Upload + +```bash +curl -X POST http://localhost:8000/api/v1/documents/upload \ + -F "file=@Rice_Cultivation_Technique.pdf" \ + -F "category=crops" +``` + +## Deployment Guide + +For detailed Alibaba Cloud deployment instructions, refer to [DEPLOY_GUIDE.md](DEPLOY_GUIDE.md). + +### Deployment Checklist + +1. Prepare Alibaba Cloud server (recommended: 4GB+ RAM) +2. Open security group ports (80, 443, 22) +3. Upload project files +4. Install dependencies +5. Configure environment variables (.env) +6. Build Docker image +7. Configure Nginx +8. Verify deployment + +## Security Recommendations + +1. **Change Default Password**: Always update `ADMIN_PASSWORD` in production +2. **Restrict API Access**: Configure `ALLOWED_ORIGINS` +3. **Enable HTTPS**: Use SSL certificates +4. **Disable Debug Mode**: Set `DEBUG=false` +5. **Log Auditing**: Regularly review access logs + +## License + +MIT License \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..682dfb75b945020758e41a2e2fefbb939998ed77 --- /dev/null +++ b/README.md @@ -0,0 +1,203 @@ + + +# 农业知识问答系统 (Agri-RAG) + + agriculture knowledge question-answering system based on RAG (Retrieval-Augmented Generation) architecture. + +## 系统简介 + + Agri-RAG , LLM + RAG , + FastAPI Vue 3 (Vanilla JS) + +## 主要特性 + +- **智能问答**:基于知识库的农业领域问答 +- **向量检索**:语义相似度匹配 +- **Rerank**:多级检索排序 +- **多模态支持**:图片 OCR 图像分析 +- **工具集成**:天气查询、新闻搜索、价格查询 +- **对话历史**:Session 管理多轮对话 +- **流式响应**:SSE 流式输出 +- **文件上传**:支持 PDF/DOCX/TXT/XLSX/CSV/HTML/PPTX +- **实时监控**:健康检查、统计分析 + +## 技术栈 + +- **后端**:Python 3.10 / FastAPI / Uvicorn / Gunicorn +- **大语言模型**:OpenAI API (兼容) +- **向量数据库**:FAISS / Chroma +- **Embedding**:HuggingFace Models +- **前端**:Vanilla JavaScript / HTML5 / CSS3 +- **部署**:Docker / Nginx + +## 快速开始 + +### 前置要求 + +- Python 3.10+ +- Docker & Docker Compose +- API Key (OpenAI / Compatible) + +### 本地开发 + +```bash +# 1. 创建虚拟环境 +python -m venv venv +source venv/bin/activate # Linux/Mac +# 或 venv\Scripts\activate # Windows + +# 2. 安装依赖 +pip install -r backend/requirements.txt + +# 3. 配置环境变量 +cp backend/.env.example backend/.env +# 编辑 backend/.env 设置 API_KEY 等 + +# 4. 启动后端 +cd backend +uvicorn app.main:app --reload + +# 5. 启动前端 +# 使用任意静态服务器 serve frontend/ +# 例如: python -m http.server 8080 -d frontend/ +``` + +### Docker 部署 + +```bash +# 构建并启动 +docker-compose up -d + +# 查看日志 +docker-compose logs -f +``` + +## API 接口 + +| 端点 | 方法 | 说明 | +|------|------|------| +| `/api/health` | GET | 健康检查 | +| `/api/v1/chat` | POST | 问答 | +| `/api/v1/chat/stream` | POST | 流式问答 | +| `/api/v1/tools` | GET | 可用工具列表 | +| `/api/v1/documents/upload` | POST | 上传文档 | +| `/api/v1/documents` | GET | 文档列表 | +| `/api/v1/documents/{filename}` | DELETE | 删除文档 | +| `/api/v1/history/{session_id}` | GET | 对话历史 | +| `/api/v1/stats` | GET | 统计数据 | +| `/api/v1/metrics` | GET | 性能指标 | + +## 目录结构 + +``` +agri-rag-system/ +├── backend/ +│ ├── app/ +│ │ ├── __init__.py +│ │ ├── cache.py # 查询缓存 +│ │ ├── config.py # 配置管理 +│ │ ├── evaluator.py # RAG 评估器 +│ │ ├── llm_client.py # LLM 客户端 +│ │ ├── main.py # FastAPI 应用 +│ │ ├── models.py # 数据模型 +│ │ ├── multimodal_processor.py # 多模态处理 +│ │ ├── rag_engine.py # RAG 引擎 +│ │ ├── reranker.py # 重排序 +│ │ ├── search_tool.py # 搜索工具 +│ │ ├── tool_registry.py # 工具注册 +│ │ ├── utils.py # 工具函数 +│ │ └── vector_store.py # 向量存储 +│ ├── data/ +│ │ ├── processed/ +│ │ ├── raw/ +│ │ └── vector_db/ +│ ├── Dockerfile +│ ├── requirements.txt +│ └── .env.example +├── deploy/ +│ ├── agri-rag.service +│ ├── gunicorn.conf.py +│ ├── manage.sh +│ ├── nginx.conf +│ └── setup.sh +├── frontend/ +│ ├── app.js +│ ├── index.html +│ └── style.css +├── docker-compose.yml +├── PRODUCTION_READINESS_REPORT.md +├── SYSTEM_EVALUATION_REPORT.md +├── UPGRADE_AND_DEPLOY_PLAN.md +└── DEPLOY_GUIDE.md +``` + +## 配置说明 + +| 变量 | 说明 | 默认值 | +|------|------|--------| +| `API_KEY` | LLM API Key | - | +| `API_BASE_URL` | API 地址 | https://api.openai.com/v1 | +| `MODEL_NAME` | 模型名称 | gpt-3.5-turbo | +| `EMBEDDING_MODEL` | Embedding 模型 | sentence-transformers | +| `HF_ENDPOINT` | HuggingFace 镜像 | https://hf-mirror.com | +| `ADMIN_PASSWORD` | 管理密码 | admin123 | +| `RATE_LIMIT_PER_MINUTE` | 速率限制 | 60 | + +## 使用示例 + +### 问答请求 + +```bash +curl -X POST http://localhost:8000/api/v1/chat \ + -H "Content-Type: application/json" \ + -d '{ + "question": "如何防治水稻稻瘟病?", + "session_id": "default" + }' +``` + +### 流式问答 + +```bash +curl -X POST http://localhost:8000/api/v1/chat/stream \ + -H "Content-Type: application/json" \ + -d '{ + "question": "小麦赤霉病的发病条件是什么?", + "session_id": "default" + }' +``` + +### 文件上传 + +```bash +curl -X POST http://localhost:8000/api/v1/documents/upload \ + -F "file=@水稻种植技术.pdf" \ + -F "category=crops" +``` + +## 部署指南 + +详细的阿里云部署步骤请参考 [DEPLOY_GUIDE.md](DEPLOY_GUIDE.md)。 + +### 部署 Checklist + +1. 准备阿里云服务器(推荐 4GB+ 内存) +2. 开放安全组端口(80, 443, 22) +3. 上传项目文件 +4. 安装依赖 +5. 配置环境变量(.env) +6. 构建 Docker 镜像 +7. 配置 Nginx +8. 验证部署 + +## 安全建议 + +1. **修改默认密码**:生产环境务必修改 `ADMIN_PASSWORD` +2. **限制 API 访问**:配置 `ALLOWED_ORIGINS` +3. **启用 HTTPS**:使用 SSL 证书 +4. **关闭调试**:设置 `DEBUG=false` +5. **日志审计**:定期检查访问日志 + +## 许可证 + +MIT License \ No newline at end of file