# lpb-spring **Repository Path**: lpbs/lpb-spring ## Basic Information - **Project Name**: lpb-spring - **Description**: 千里之行始于足下-手写spring,源码悟道 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2022-07-28 - **Last Updated**: 2025-07-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 大模型接入网关 (LLM Gateway) 一个基于Spring Boot的大模型接入网关,提供统一的大模型调用接口,支持分布式流量控制、多模型实时配置接入和用量统计功能。 ## 功能特性 ### 🚀 核心功能 - **统一接口**: 对外提供统一的大模型调用接口 - **多模型支持**: 支持OpenAI、Anthropic等多种大模型 - **分布式流量控制**: 基于并发数和请求频率的流量控制 - **实时配置管理**: 支持动态添加、更新、删除模型配置 - **详细用量统计**: 按模型、用户、应用维度的用量统计 ### 📊 监控统计 - 请求成功率统计 - 响应时间统计 - Token使用量统计 - 并发数监控 - 实时流量监控 ### 🔧 管理功能 - 模型配置动态管理 - 模型状态实时切换 - 参数配置热更新 - 健康检查接口 ## 快速开始 ### 1. 环境要求 - Java 17+ - Maven 3.6+ - Spring Boot 3.1.5 ### 2. 配置API密钥 在 `application.yml` 中配置您的API密钥: ```yaml models: openai: gpt-3.5-turbo: api-key: ${OPENAI_API_KEY:your-openai-api-key} anthropic: claude-3-sonnet-20240229: api-key: ${ANTHROPIC_API_KEY:your-anthropic-api-key} ``` ### 3. 启动应用 ```bash mvn spring-boot:run ``` ### 4. 调用示例 #### 同步调用 ```bash curl -X POST http://localhost:8080/api/llm/chat \ -H "Content-Type: application/json" \ -d '{ "modelName": "gpt-3.5-turbo", "messages": [ {"role": "user", "content": "你好,请介绍一下自己"} ], "maxTokens": 1000, "temperature": 0.7, "userId": "user123", "appId": "app456" }' ``` #### 异步调用 ```bash curl -X POST http://localhost:8080/api/llm/chat/async \ -H "Content-Type: application/json" \ -d '{ "modelName": "gpt-3.5-turbo", "messages": [ {"role": "user", "content": "请写一首诗"} ] }' ``` #### 流式调用 ```bash curl -X POST http://localhost:8080/api/llm/chat/stream \ -H "Content-Type: application/json" \ -d '{ "modelName": "gpt-3.5-turbo", "messages": [ {"role": "user", "content": "请写一个故事"} ], "stream": true }' ``` ## API接口 ### 大模型调用接口 | 接口 | 方法 | 描述 | |------|------|------| | `/api/llm/chat` | POST | 同步调用大模型 | | `/api/llm/chat/async` | POST | 异步调用大模型 | | `/api/llm/chat/stream` | POST | 流式调用大模型 | ### 统计查询接口 | 接口 | 方法 | 描述 | |------|------|------| | `/api/llm/statistics/model/{modelName}` | GET | 获取模型统计信息 | | `/api/llm/statistics/user/{userId}` | GET | 获取用户统计信息 | | `/api/llm/statistics/app/{appId}` | GET | 获取应用统计信息 | | `/api/llm/statistics/all` | GET | 获取所有统计信息 | ### 配置管理接口 | 接口 | 方法 | 描述 | |------|------|------| | `/api/config/models` | GET | 获取所有模型配置 | | `/api/config/models/{modelName}` | GET | 获取指定模型配置 | | `/api/config/models` | POST | 添加新模型配置 | | `/api/config/models/{modelName}` | PUT | 更新模型配置 | | `/api/config/models/{modelName}` | DELETE | 删除模型配置 | | `/api/config/models/{modelName}/status` | PATCH | 更新模型状态 | ### 健康检查接口 | 接口 | 方法 | 描述 | |------|------|------| | `/api/llm/health` | GET | 健康检查 | ## 配置说明 ### 模型配置参数 ```yaml models: openai: gpt-3.5-turbo: model-type: OPENAI # 模型类型 api-key: your-api-key # API密钥 base-url: https://api.openai.com # API基础URL enabled: true # 是否启用 max-concurrency: 50 # 最大并发数 timeout: 30000 # 超时时间(毫秒) retry-count: 3 # 重试次数 weight: 100 # 负载均衡权重 ``` ### 网关配置参数 ```yaml llm: gateway: default: timeout: 30000 # 默认超时时间 max-concurrency: 100 # 默认最大并发数 retry-count: 3 # 默认重试次数 rate-limit: enabled: true # 启用流量控制 default-requests-per-minute: 1000 # 每分钟请求限制 default-requests-per-hour: 10000 # 每小时请求限制 ``` ## 项目结构 ``` src/main/java/com/lpb/spring/llmGetwagv2/ ├── config/ # 配置类 │ ├── LLMGatewayConfig.java │ └── ModelConfig.java ├── controller/ # 控制器 │ ├── LLMGatewayController.java │ └── ConfigController.java ├── exception/ # 异常处理 │ ├── LLMGatewayException.java │ └── GlobalExceptionHandler.java ├── model/ # 数据模型 │ ├── LLMRequest.java │ └── LLMResponse.java └── service/ # 服务层 ├── LLMProvider.java ├── LLMGatewayService.java ├── UsageStatisticsService.java └── impl/ ├── OpenAIProvider.java └── AnthropicProvider.java ``` ## 扩展开发 ### 添加新的模型提供者 1. 实现 `LLMProvider` 接口: ```java @Service public class CustomProvider implements LLMProvider { @Override public LLMResponse call(LLMRequest request) { // 实现具体的模型调用逻辑 } @Override public String getModelName() { return "custom-model"; } @Override public boolean isAvailable() { return true; } } ``` 2. 在配置中添加模型配置: ```yaml models: custom: custom-model: model-type: CUSTOM api-key: your-api-key base-url: https://api.custom.com enabled: true ``` ### 自定义流量控制策略 继承 `LLMGatewayService` 并重写 `checkRateLimit` 方法: ```java @Override protected boolean checkRateLimit(LLMRequest request) { // 实现自定义的流量控制逻辑 return super.checkRateLimit(request); } ``` ## 监控和运维 ### 日志配置 ```yaml logging: level: com.lpb.spring.llmGetwagv2: INFO file: name: logs/llm-gateway.log max-size: 100MB max-history: 30 ``` ### 健康检查 ```bash curl http://localhost:8080/api/llm/health ``` ### 统计信息查询 ```bash # 获取模型统计 curl http://localhost:8080/api/llm/statistics/model/gpt-3.5-turbo # 获取用户统计 curl http://localhost:8080/api/llm/statistics/user/user123 # 获取应用统计 curl http://localhost:8080/api/llm/statistics/app/app456 ``` ## 部署建议 ### 生产环境配置 1. **数据库持久化**: 建议使用数据库存储统计信息 2. **缓存优化**: 使用Redis缓存模型配置和统计信息 3. **负载均衡**: 使用Nginx进行负载均衡 4. **监控告警**: 集成Prometheus + Grafana监控 5. **日志收集**: 使用ELK Stack收集日志 ### 性能优化 1. **连接池**: 配置HTTP连接池 2. **异步处理**: 使用异步处理提高并发性能 3. **缓存策略**: 合理使用缓存减少重复请求 4. **限流策略**: 根据实际需求调整限流参数 ## 许可证 MIT License ## 贡献 欢迎提交Issue和Pull Request来改进这个项目。