Skip to content

fix: cannot automatically get embedding dim when create embedding provider#5442

Merged
Soulter merged 9 commits intoAstrBotDevs:masterfrom
exynos967:dev2251
Feb 25, 2026
Merged

fix: cannot automatically get embedding dim when create embedding provider#5442
Soulter merged 9 commits intoAstrBotDevs:masterfrom
exynos967:dev2251

Conversation

@exynos967
Copy link
Contributor

@exynos967 exynos967 commented Feb 25, 2026

Modifications / 改动点

后端:
openai_embedding_source.py
gemini_embedding_source.py
provider.py
config.py
default.py
前端:
Settings.vue
AstrBotConfig.vue
以及i18n有关文件

修复 WebUI 设置页“创建 API Key 后复制失败”问题,补充复制降级逻辑,并完善临时节点清理。
修复首次新增 Embedding 提供商时自动检测维度报“未找到适配器”的问题(检测前动态导入 provider 适配器)。
将 Embedding 维度自动检测改为真实探测:
新增统一 detect_dim() 探测入口;
OpenAI Embedding 支持探测可用最大维度;
Gemini Embedding 改为探测模型原生维度。
优化 OpenAI Embedding API Base URL 处理:自动补全 /v1,并处理末尾斜杠。
补全并修正 i18n:完善 embedding/proxy 相关描述与提示文案。
将 OpenAI/Gemini Embedding 的提示改为显示在 API Base URL 字段下方的小灰字(字段 hint),并按提供商类型显示对应文案。

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image image image

Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

改进嵌入(embedding)提供商的配置体验,并使嵌入维度检测更加健壮且感知不同提供商的差异。

新功能:

  • 在嵌入提供商上新增统一的异步入口函数 detect_dim,通过探测原生或最大支持的向量维度,而不是仅依赖单次嵌入调用进行推断。

错误修复:

  • 通过强化基于文档的复制回退逻辑并安全清理临时 DOM 节点,修复 Web UI 中 API 密钥复制失败的问题。
  • 在探测前动态导入缺失的提供商适配器,修复新添加提供商时初始嵌入维度自动检测失效的问题。
  • 通过规范化自定义 base URL、去除末尾斜杠并自动追加 /v1 后缀,修复 OpenAI 嵌入 API base 配置相关问题。

增强改进:

  • 为 OpenAI 嵌入实现提供商特定的维度探测逻辑,包括在支持的情况下检测最大可调维度。
  • 为 Gemini 嵌入实现原生维度探测,而不覆盖其输出维度。
  • 优化提供商配置提示,使 OpenAI 和 Gemini 的嵌入配置指引以字段级提示的形式展示在 API Base URL 下,同时对提供商级别的警告进行适当过滤。
  • 更新代理配置元数据,使用基于 i18n 的描述和提示键,以实现一致的多语言文案。
Original summary in English

Summary by Sourcery

Improve embedding provider configuration UX and make embedding dimension detection more robust and provider‑aware.

New Features:

  • Add a unified async detect_dim entrypoint on embedding providers to probe native or maximum supported vector dimensions instead of inferring from a single embedding call.

Bug Fixes:

  • Fix Web UI API key copy failures by hardening the document-based copy fallback and cleaning up temporary DOM nodes safely.
  • Fix initial embedding dimension auto-detection for newly added providers by dynamically importing missing provider adapters before probing.
  • Fix issues with OpenAI embedding API base configuration by normalizing custom base URLs, trimming trailing slashes, and auto-appending the /v1 suffix.

Enhancements:

  • Implement provider-specific dimension probing for OpenAI embeddings, including detection of the maximum adjustable dimensions when supported.
  • Implement native-dimension probing for Gemini embeddings without overriding output dimensionality.
  • Refine provider configuration hints so OpenAI and Gemini embedding guidance appears as field-level hints under the API Base URL, with provider-level alerts filtered appropriately.
  • Update proxy configuration metadata to use i18n-backed description and hint keys for consistent multilingual copy.

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Feb 25, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @exynos967, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求主要致力于提升用户体验和系统稳定性。它通过修复 WebUI 中 API Key 复制的可靠性问题,确保用户能够顺畅地管理凭证。同时,对 Embedding 服务的维度检测机制进行了全面优化,使其更加智能和准确,能够根据不同提供商的特性进行适配。此外,还改进了 OpenAI API Base URL 的处理逻辑,并完善了多语言提示信息,使界面更加友好和易懂。

Highlights

  • API Key 复制功能修复: 修复了 WebUI 设置页中 API Key 复制失败的问题,并增加了复制降级逻辑和临时节点清理,提升了用户体验。
  • Embedding 维度探测优化: 解决了首次添加 Embedding 提供商时,自动检测维度可能出现的“未找到适配器”错误,通过动态导入提供商适配器来解决。同时,将 Embedding 维度自动检测逻辑优化为更真实的探测方式,新增了统一的 detect_dim() 探测入口,并针对 OpenAI Embedding 支持探测其可用的最大维度,Gemini Embedding 则探测模型的原生维度。
  • OpenAI Embedding API Base URL 处理优化: 优化了 OpenAI Embedding API Base URL 的处理逻辑,现在会自动补全 /v1 并处理末尾斜杠,减少了用户配置错误的可能性。
  • 国际化 (i18n) 文本完善: 补充并修正了国际化文本,完善了 Embedding 和代理相关的描述和提示文案,使界面信息更加准确和易懂。
  • 界面提示信息改进: 将 OpenAI/Gemini Embedding 的提示信息显示在 API Base URL 字段下方,并根据提供商类型显示对应的文案,使提示更加直观。
Changelog
  • astrbot/core/config/default.py
    • 为 OpenAI 和 Gemini Embedding 提供商添加了 hint 字段
    • proxy 的描述和提示更新为国际化键
  • astrbot/core/provider/provider.py
    • 在基础 Provider 类中新增了默认的 detect_dim 异步方法,用于探测模型原生向量维度
  • astrbot/core/provider/sources/gemini_embedding_source.py
    • 为 Gemini Embedding 实现了 detect_dim 方法,用于探测模型的原生向量维度
  • astrbot/core/provider/sources/openai_embedding_source.py
    • 为 OpenAI Embedding 实现了 detect_dim 方法,用于探测模型可用的最大向量维度
    • 优化了 API Base URL 的处理,自动补全 /v1 并移除末尾斜杠
  • astrbot/dashboard/routes/config.py
    • 在进行 Embedding 维度检测前,增加了动态导入提供商适配器的逻辑
    • 切换到使用 detect_dim 方法进行维度探测
  • dashboard/src/components/shared/AstrBotConfig.vue
    • 修改了提示信息的显示逻辑,根据提供商类型为 embedding_api_base 字段显示特定提示
    • 更新了通用提供商提示的显示方式
  • dashboard/src/i18n/locales/en-US/features/config-metadata.json
    • 添加了 OpenAI/Gemini Embedding 提示的英文国际化条目
    • 添加了代理描述/提示的英文国际化条目
  • dashboard/src/i18n/locales/zh-CN/features/config-metadata.json
    • 添加了 OpenAI/Gemini Embedding 提示的中文国际化条目
    • 添加了代理描述/提示的中文国际化条目
  • dashboard/src/views/Settings.vue
    • 改进了 tryExecCommandCopy 函数,增加了对 document.body 的检查
    • 使用 removeChild 方法进行更健壮的 DOM 元素清理,以修复 API Key 复制失败的问题
Activity
  • 目前没有检测到此拉取请求的任何活动。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@dosubot dosubot bot added area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. area:webui The bug / feature is about webui(dashboard) of astrbot. labels Feb 25, 2026
@dosubot
Copy link

dosubot bot commented Feb 25, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我在这里给出了一些高层次的反馈:

  • 新的 OpenAIEmbeddingSource.detect_dim 实现会在指数/二分搜索循环中触发大量 API 调用;建议增加一个尝试次数的硬性上限,或在达到某些条件时进行日志记录/短路,以避免在边缘情况下产生过高的延迟或配额使用。
  • get_embedding_dim 中,动态加载 provider 失败产生的错误信息(ImportError)会直接返回给客户端,这可能会泄露内部细节;建议将其映射为更友好、更加通用的错误文案,同时在服务端记录完整的 traceback 日志。
给 AI Agent 的提示词
Please address the comments from this code review:

## Overall Comments
- The new `OpenAIEmbeddingSource.detect_dim` implementation can trigger a large number of API calls in the exponential/binary search loop; consider adding a hard cap on attempts or logging/short‑circuiting conditions to avoid excessive latency or quota usage in edge cases.
- In `get_embedding_dim`, the error message for dynamic provider import (`ImportError`) is returned directly to the client, which may leak internal details; consider mapping this to a more user‑friendly, generic error string while logging the full traceback server‑side.

Sourcery 对开源项目免费——如果你觉得我们的 Review 有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈来改进后续的 Review。
Original comment in English

Hey - I've left some high level feedback:

  • The new OpenAIEmbeddingSource.detect_dim implementation can trigger a large number of API calls in the exponential/binary search loop; consider adding a hard cap on attempts or logging/short‑circuiting conditions to avoid excessive latency or quota usage in edge cases.
  • In get_embedding_dim, the error message for dynamic provider import (ImportError) is returned directly to the client, which may leak internal details; consider mapping this to a more user‑friendly, generic error string while logging the full traceback server‑side.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `OpenAIEmbeddingSource.detect_dim` implementation can trigger a large number of API calls in the exponential/binary search loop; consider adding a hard cap on attempts or logging/short‑circuiting conditions to avoid excessive latency or quota usage in edge cases.
- In `get_embedding_dim`, the error message for dynamic provider import (`ImportError`) is returned directly to the client, which may leak internal details; consider mapping this to a more user‑friendly, generic error string while logging the full traceback server‑side.

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.

Copy link
Contributor

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

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 aims to improve system robustness, user experience, and internationalization by fixing API Key copy failures, optimizing Embedding dimension detection logic, and refining API Base URL hints. However, it introduces critical security vulnerabilities, including Server-Side Request Forgery (SSRF) via the embedding_api_base field in the get_embedding_dim endpoint, and potential insecure dynamic module loading through the provider_type field. Other improvements include enhanced API Key copying with secure DOM cleanup, more accurate embedding dimension detection for providers like OpenAI and Gemini, better handling of OpenAI Embedding's API Base URL, and optimized internationalization prompts.

Comment on lines +26 to +32
api_base = provider_config.get("embedding_api_base", "").strip()
if not api_base:
api_base = "https://api.openai.com/v1"
else:
api_base = api_base.removesuffix("/")
if not api_base.endswith("/v1"):
api_base = f"{api_base}/v1"
Copy link
Contributor

Choose a reason for hiding this comment

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

security-high high

The embedding_api_base is taken directly from the user-provided configuration and used as the base URL for the OpenAI client without any validation. This allows an attacker to perform Server-Side Request Forgery (SSRF) by providing internal IP addresses or malicious domains, which the server will then attempt to connect to during the dimension detection process.

Recommendation: Validate the api_base URL to ensure it does not point to internal or reserved IP addresses.

Comment on lines +760 to +762
self.core_lifecycle.provider_manager.dynamic_import_provider(
provider_type,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

security-medium medium

The provider_type is taken from the user-provided configuration and passed directly to dynamic_import_provider without validation. This could allow an attacker to trigger the loading of arbitrary modules if the provider_type is not properly sanitized or checked against an allow-list.

Recommendation: Validate provider_type against a list of known, safe provider types before attempting to import it.

return base_dim

# 3) 可调时探测上界:指数扩张 + 二分
max_cap = 32768
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

这里的 max_cap = 32768 是一个魔术数字。为了提高代码的可读性和可维护性,建议将其定义为一个具名常量,例如 MAX_DIMENSION_CAP

Suggested change
max_cap = 32768
MAX_DIMENSION_CAP = 32768

@exynos967
Copy link
Contributor Author

在动态加载 provider 失败时,前端返回改为通用文案,不再透出 ImportError 细节;服务端仍保留完整 traceback 日志。

在 OpenAIEmbeddingProvider.detect_dim 增加探测调用上限(max_probe_calls = 12),并在超限时记录 warning 后返回当前已确认维度,避免高延迟/高配额消耗。

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 25, 2026
@Soulter Soulter changed the title fix: 修复 API Key 复制失败并优化 Embedding 维度探测与 API Base URL 提示 fix: cannot automatically get embedding dim when create embedding provider Feb 25, 2026
@Soulter Soulter merged commit 69f2fb2 into AstrBotDevs:master Feb 25, 2026
6 checks passed
astrbot-doc-agent bot pushed a commit to AstrBotDevs/AstrBot-docs that referenced this pull request Feb 25, 2026
@astrbot-doc-agent
Copy link

Generated docs update PR (pending manual review):
AstrBotDevs/AstrBot-docs#149
Trigger: PR merged


AI change summary:

  • 更新 zh/use/knowledge-base.mden/use/knowledge-base.md
  • 补充 OpenAI Embedding API Base URL 配置说明,明确自动补全 /v1 后缀逻辑。
  • 补充 Gemini Embedding API Base URL 配置建议,说明无需手动添加 /v1beta
  • 新增创建嵌入模型提供商时自动检测向量维度的说明及故障排查提示。
  • i18n:中英文文档均已同步更新。

Experimental bot notice:

  • This output is generated by AstrBot-Doc-Agent for review only.
  • It does not represent the final documentation form.

KBVsent pushed a commit to KBVsent/AstrBot that referenced this pull request Feb 25, 2026
…vider (AstrBotDevs#5442)

* fix(dashboard): 强化 API Key 复制临时节点清理逻辑

* fix(embedding): 自动检测改为探测 OpenAI embedding 最大可用维度

* fix: normalize openai embedding base url and add hint key

* i18n: add embedding_api_base hint translations

* i18n: localize provider embedding/proxy metadata hints

* fix: show provider-specific embedding API Base URL hint as field subtitle

* fix(embedding): cap OpenAI detect_dim probes with early short-circuit

* fix(dashboard): return generic error on provider adapter import failure

* 回退检测逻辑
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. area:webui The bug / feature is about webui(dashboard) of astrbot. lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants