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 摘要

通用修复 embedding_dimensions 为 0(或默认值)时引发的兼容性问题与建库阻塞问题。移除了针对单一服务商的硬编码,提升了对类 OpenAI 接口(如 SiliconFlow, vLLM)的泛用性兼容。

🐛 问题根源

  1. 接口参数不兼容:原逻辑在未配置维度时,会向 API 发送无效的 dimensions: 0。导致严格校验该参数的平台(如 SiliconFlow)返回 HTTP 400 错误。
  2. 预检逻辑误拦截:当修复接口调用后,API 成功返回真实维度(如 1024)。但 knowledge_base_service.py 严格校验 1024 != config_dim(0),导致正常模型被系统误判拦截,知识库创建失败。

🛠️ 修复方案

  1. openai_embedding_source.py (接口层)
    • 参数过滤:仅当 embedding_dimensions > 0 时,才向 kwargs 注入 dimensions 参数。
    • 移除特判:删除了此前针对 api.siliconflow.cn 的特定域名拦截逻辑,改为通用处理。支持 dimensions 的官方接口不受影响,拒绝无效维度的接口自动规避。
  2. knowledge_base_service.py (业务层)
    • 放宽默认值预检:修改判断逻辑为 if configured_dim != 0 and actual_dim != configured_dim:。当配置为 0 时,信任并接纳 API 探测返回的真实维度用于 Faiss 初始化;仅在用户显式配置错误维度时拦截。

📊 测试验证

测试模型:SiliconFlow (BAAI/bge-m3)

配置值 (embedding_dimensions) 接口 Payload 表现 维度预检结果 最终动作
0 / 留空 不发送 dimensions 跳过拦截,获取实际维度 1024 ✅ 成功基于 1024 维建库
1024 (正例) 发送 dimensions: 1024 1024 == 1024,校验通过 ✅ 成功建库
768 (反例) 发送 dimensions: 768 1024 != 768 ❌ 抛出 ValueError,拦截成功

@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