Skip to content

fix(embedding): 通用修复 embedding_dimensions 参数校验,解决 SiliconFlow 等兼容接口报 400 的问题#8807

Open
Rat0323 wants to merge 3 commits into
AstrBotDevs:masterfrom
Rat0323:fix/openai-emb-dim-validation
Open

fix(embedding): 通用修复 embedding_dimensions 参数校验,解决 SiliconFlow 等兼容接口报 400 的问题#8807
Rat0323 wants to merge 3 commits into
AstrBotDevs:masterfrom
Rat0323:fix/openai-emb-dim-validation

Conversation

@Rat0323

@Rat0323 Rat0323 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

📝 PR 摘要 (Summary)

本次提交是对嵌入维度校验与参数处理的通用端到端修复,旨在彻底解决当用户未配置嵌入维度(即 embedding_dimensions: 0)时,系统向接口发送非法参数(导致 400 错误)以及知识库创建被阻塞的问题。该修复为通用方案,不针对单一服务商。


🐛 问题背景与时间线 (Problem Context & Timeline)

  1. 第一阶段:API 拒绝请求

    • 最初,当用户配置维度为 0(或留空默认值)时,系统会将 "dimensions": 0 发送给 API。
    • 对于严格校验该参数的类 OpenAI 接口(如 SiliconFlow、vLLM),由于大部分模型不接受无效的 0 维度,API 直接返回 HTTP 400 错误。
  2. 第二阶段:发现深层阻塞逻辑 (本次修复的核心)

    • 当上述 API 请求被修复后(不发 0,API 成功返回 1024 维的向量),暴露出一个更深层的问题:
    • 在新建知识库时,knowledge_base_service.py 内部存在一个强校验:if len(vec) != provider.get_dim(): raise ValueError(...)
    • 因为系统配置里维度是 0,而 API 返回了真实的 1024 维,1024 != 0,从而抛出错误:测试嵌入模型失败: 嵌入向量维度不匹配,实际是 1024,然而配置是 0
    • 结果:即使用户的 API 已经通了,由于配置了默认的 0,知识库依然被系统强行拦截,无法创建。

🛠️ 完整的通用修复方案 (Generic Complete Fix)

为了让默认的 0 维度配置能够真正做到“开箱即用”,且对支持 dimensions 的提供商(如 OpenAI 官方)无影响;对拒绝该参数的提供商(如 SiliconFlow、vLLM)自动规避,本次提交进行了双层修复:

1. 业务层:引入“智能维度探测” (knowledge_base_service.py)

  • 修改了预检拦截的判断条件:if configured_dim != 0 and actual_dim != configured_dim:
  • 目的:当用户配置为 0(留空)时,系统将其视为“未知”,自动接纳探测 API 返回的实际维度(如 1024)用于后续数据库初始化。只有当用户明确填写了错误的非零维度时,才执行强拦截以保护数据库。

2. 接口层:通用化参数过滤 (openai_embedding_source.py)

  • 在构建 kwargs 时,确保:if dim_val > 0: kwargs["dimensions"] = dim_val
  • 目的:彻底杜绝将无效的 0 发送给任何后端。移除了之前为了规避警告而写的特定域名(api.siliconflow.cn)硬编码,采用极简的通用逻辑,提升了代码的向下兼容性与整洁度。

📊 接口实测证据 (Empirical Evidence)

针对此完整的端到端修复,实际使用的模型为 SiliconFlow 的 BAAI/bge-m3,进行了全面回归验证:

用户配置维度 (embedding_dimensions) 步骤一:API Payload 表现 步骤二:建库强校验表现 最终结果
0 (或留空默认) 不发送 dimensions 字段,获取真实维度 1024 识别为 0,绕过严格拦截,采用 1024 成功创建 (体验丝滑)
1024 (正确填写) 发送 dimensions: 1024,请求正常 识别为 1024,匹配通过 成功创建
768 (错误填写) 发送 dimensions: 768,请求响应 实际 1024 != 配置 768 成功拦截报错 (保障安全)

✅ 结论 (Conclusion)

本次提交彻底打通了 embedding_dimensions = 0 的默认使用链路。在保留数据库安全拦截底线的同时,用通用的方式解决了底层 API 的参数兼容性问题与顶层建库逻辑的逻辑“死锁”,使得系统更加智能、稳定。

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Jun 15, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request simplifies the _embedding_kwargs method in openai_embedding_source.py by removing a temporary workaround for the SiliconFlow provider and ensuring that the dimensions parameter is only set if its value is greater than zero. Feedback suggests checking for None or empty string values for embedding_dimensions before attempting to convert it to an integer, which prevents unnecessary warning logs from flooding the console when the field is left blank in the WebUI.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread astrbot/core/provider/sources/openai_embedding_source.py

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • When embedding_dimensions is configured as 0 or a negative value it is now silently ignored; consider logging a low-level warning or info in that case so misconfigurations are easier to spot while still avoiding the SiliconFlow 400s.
  • Since the provider-specific SiliconFlow branch is removed, it might be worth adding a short inline comment near the dim_val > 0 check to clarify that omitting dimensions is intentional for OpenAI-compatible providers that reject this parameter entirely.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When `embedding_dimensions` is configured as `0` or a negative value it is now silently ignored; consider logging a low-level warning or info in that case so misconfigurations are easier to spot while still avoiding the SiliconFlow 400s.
- Since the provider-specific SiliconFlow branch is removed, it might be worth adding a short inline comment near the `dim_val > 0` check to clarify that omitting `dimensions` is intentional for OpenAI-compatible providers that reject this parameter entirely.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Rat0323 Rat0323 force-pushed the fix/openai-emb-dim-validation branch from ec362e4 to 85a3543 Compare June 15, 2026 16:12
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jun 15, 2026
@Rat0323 Rat0323 force-pushed the fix/openai-emb-dim-validation branch from 85a3543 to 2b52c22 Compare June 15, 2026 16:17
@Rat0323 Rat0323 force-pushed the fix/openai-emb-dim-validation branch from 2b52c22 to 3c4ac17 Compare June 15, 2026 16:28
- Allow automatic dimension inference when configured_dim is 0 to prevent blocking new KB creation.
- Filter out dimensions parameter in OpenAI embedding requests when value is 0 to improve compatibility with providers like SiliconFlow.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 17, 2026
@Rat0323 Rat0323 changed the title fix(embedding): 修复 OpenAI 兼容接口维度参数导致 SiliconFlow 等提供商报 400 及日志误报的问题 fix(embedding): 优化嵌入维度强校验逻辑,解决默认配置阻塞建库与冗余日志警告 Jun 17, 2026
@Rat0323 Rat0323 changed the title fix(embedding): 优化嵌入维度强校验逻辑,解决默认配置阻塞建库与冗余日志警告 fix(knowledge_base): 优化嵌入模型维度校验与提供商参数兼容性 Jun 17, 2026
@Rat0323 Rat0323 changed the title fix(knowledge_base): 优化嵌入模型维度校验与提供商参数兼容性 fix(embedding): 完善维度为 0 时的端到端处理 (API兼容性与建库校验) Jun 17, 2026
@Rat0323 Rat0323 changed the title fix(embedding): 完善维度为 0 时的端到端处理 (API兼容性与建库校验) fix(embedding): 通用修复 embedding_dimensions 参数校验,解决 SiliconFlow 等兼容接口报 400 的问题 Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant