Skip to content

feat: 添加智能路由中间件,支持API路径自动规范化 #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 2, 2025

Conversation

chinrain
Copy link

🆕 新增文件

  • app/middleware/smart_routing_middleware.py — 智能路由中间件核心实现

📝 修改文件

  • app/middleware/middleware.py — 注册智能路由中间件
  • app/config/config.py — 添加 URL_NORMALIZATION_ENABLED 配置
  • Dockerfile — 添加环境变量默认配置

✨ 核心功能

  1. 智能 URL 检测和转换

    • 错误路径自动修复
      • /v1beta/v1/chat/completions/v1/chat/completions
      • /openai/chat/openai/v1/chat/completions
      • /hf/models/hf/v1/models
      • /gemini-pro/v1beta/models/gemini-pro:generateContent
  2. 格式优先级处理
    OpenAI > HF > Gemini > 默认

  3. 正确格式保护(已规范的不动)

    • /v1beta/models/gemini-2.0-flash:generateContent
    • /openai/v1/chat/completions
    • /v1/chat/completions

🔧 配置

  • 环境变量:URL_NORMALIZATION_ENABLED=true(默认 false
  • Docker:ENV URL_NORMALIZATION_ENABLED=false

🔄 向后兼容

  • 默认关闭,不影响现有部署
  • 通过环境变量控制
  • 不修改现有 API 端点

🐛 Fixes

  • 修复 /v1beta/v1/chat/completions 重复路径段
  • 解决模型名称大小写被意外修改
  • 修复 Gemini 检测边界情况

- 新增SmartRoutingMiddleware智能路由中间件
- 支持OpenAI/HF/Gemini/默认格式的自动检测和转换
- 修复错误URL路径格式,提升API兼容性
- 添加URL_NORMALIZATION_ENABLED配置开关,默认关闭
- 智能路由功能默认关闭,需手动启用
- 移除了不必要的判断流式请求的方法
@chinrain
Copy link
Author

chinrain commented Jul 2, 2025

改了改了

@snailyp snailyp merged commit 60b2d59 into snailyp:main Jul 2, 2025
@zeabur zeabur bot temporarily deployed to production July 2, 2025 16:46 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants