Skip to content

feat: 添加更多嵌入提供商 OpenAI-兼容 embedding provider 和智谱字节ollama提供商#6642

Open
whatevertogo wants to merge 8 commits intoAstrBotDevs:masterfrom
whatevertogo:feat/more-embedding-providers
Open

feat: 添加更多嵌入提供商 OpenAI-兼容 embedding provider 和智谱字节ollama提供商#6642
whatevertogo wants to merge 8 commits intoAstrBotDevs:masterfrom
whatevertogo:feat/more-embedding-providers

Conversation

@whatevertogo
Copy link
Contributor

@whatevertogo whatevertogo commented Mar 19, 2026

Fixes #6638
Fix #3790
Fix #4226

一、PR 概述

新增通用 OpenAI Compatible Embedding 提供商适配器,并内置智谱(Zhipu)和火山引擎(Volcengine)两个预设配置,让用户可以方便地接入更多嵌入向量服务。


二、背景 / 动机

  • 问题现象:之前 AstrBot 只支持原生 OpenAI Embedding 和 Gemini Embedding 两种嵌入服务,用户无法使用智谱、火山引擎、硅基流动等兼容 OpenAI API 的国内嵌入服务。
  • 影响范围:知识库、记忆搜索等依赖 embedding 的功能。
  • 原生 openai_embedding 的局限:其 API base 处理逻辑会强制在所有地址后追加 /v1,导致智谱 /api/paas/v4、火山 /api/v3 等带路径的地址被破坏。

三、改动内容

  • 新增 OpenAICompatibleEmbeddingProvider,智能处理 API base URL(纯域名自动补 /v1,已有路径则保留原样)
  • 新增 send_dimensions_param 开关,控制是否将 embedding_dimensions 透传给上游接口(部分兼容服务不支持 dimensions 参数)
  • 内置智谱 Embedding(embedding-3)和火山引擎 Embedding(doubao-embedding-vision)预设配置
  • 将原 OpenAI Embedding 替换为 OpenAI Compatible Embedding 作为通用选项
  • 新增单元测试(API base 归一化、dimensions 透传、Dashboard 集成)
  • 更新中/英/俄三语 i18n 配置和知识库文档

改动类型:

  • 新功能
  • 缺陷修复
  • 重构/整理
  • 性能优化
  • 文档更新
  • 测试补充
  • 依赖/配置变更

四、实现说明

  • 核心思路:创建一个通用的 OpenAI 兼容 embedding 适配器,通过智能 URL 归一化(normalize_openai_compatible_embedding_api_base)区分纯域名和带路径的地址,再用 send_dimensions_param 开关解决不同服务对 dimensions 参数的兼容性差异。
  • 关键设计/权衡
    • 不修改原有 openai_embedding 适配器,保持向后兼容;新增独立的 openai_compatible_embedding 类型
    • send_dimensions_param 默认关闭(opt-in),避免对不期望接收该参数的上游服务造成影响
    • 智谱和火山通过复用 openai_compatible_embedding 类型 + 预设配置实现,无需额外代码
  • 主要涉及文件/模块
    • astrbot/core/provider/sources/openai_compatible_embedding_source.py — 新增适配器实现
    • astrbot/core/config/default.py — 新增预设配置和 schema
    • astrbot/core/provider/manager.py — 注册 lazy import
    • dashboard/src/i18n/locales/*/features/config-metadata.json — 三语 i18n
    • docs/zh/use/knowledge-base.mddocs/en/use/knowledge-base.md — 文档更新
    • tests/test_openai_compatible_embedding_source.py — 单元测试
    • tests/test_dashboard.py — Dashboard 集成测试

五、行为变化(对外影响)

  • 之前是:只能选择 OpenAI Embedding 或 Gemini Embedding
  • 现在是:可选择 OpenAI Compatible Embedding(通用)、智谱 Embedding、火山引擎 Embedding 或 Gemini Embedding
  • 兼容性影响:原 OpenAI Embedding 配置项 ID 变更为 openai_compatible_embedding,已有用户需要重新配置嵌入提供商(原 openai_embedding 类型仍可使用,不会被删除)

六、测试与验证

自测结果:

  • 已本地验证通过
  • 已跑单测(test_openai_compatible_embedding_source.py + test_dashboard.py

测试命令:

python -m pytest tests/test_openai_compatible_embedding_source.py tests/test_dashboard.py -v

七、Checklist

  • 代码风格符合项目规范
  • 已添加必要的注释
  • 已更新相关文档(中/英知识库文档)
  • 已添加/更新测试用例
  • 无 Breaking Changes(原 openai_embedding 类型保留,但默认配置模板已替换)
  • 已关联相关 Issue

Summary by Sourcery

Introduce a generic OpenAI-compatible embedding provider and wire it into the provider manager, dashboard, and docs, with presets for major third-party services.

New Features:

  • Add an OpenAICompatibleEmbeddingProvider adapter that normalizes API base URLs and supports optional dimensions parameters for OpenAI-compatible services.
  • Provide built-in embedding templates for OpenAI-compatible, Zhipu, Volcengine, and Ollama providers in the default configuration and dashboard.
  • Expose a send_dimensions_param option in embedding configuration to control whether the local embedding_dimensions is sent upstream as the dimensions parameter.

Enhancements:

  • Update the dashboard hint handling to rely on per-field metadata hints instead of hardcoded special-casing for specific embedding providers.
  • Extend dynamic provider loading to support the new openai_compatible_embedding type.

Documentation:

  • Document the new OpenAI-compatible embedding flow, presets (Zhipu, Volcengine, Ollama), and send_dimensions_param behavior in the Chinese and English knowledge-base guides.

Tests:

  • Add unit tests for the OpenAI-compatible embedding provider’s URL normalization, dimensions parameter behavior, and embedding retrieval.
  • Add dashboard integration tests to verify exposure of the new embedding presets and support for openai_compatible_embedding in embedding dimension detection.

Copilot AI review requested due to automatic review settings March 19, 2026 14:14
@auto-assign auto-assign bot requested review from Raven95676 and Soulter March 19, 2026 14:14
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Mar 19, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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!

此拉取请求通过引入一个通用的 OpenAI 兼容 Embedding 提供商,显著扩展了 AstrBot 对嵌入服务的支持范围。它解决了现有实现中对非标准 API Base URL 处理的限制,并提供了灵活的配置选项,如 send_dimensions_param,以适应各种兼容服务。这一改进使用户能够更便捷地集成智谱、火山引擎等国内嵌入服务,从而增强了知识库和记忆搜索等依赖 Embedding 功能的灵活性和可用性。

Highlights

  • 新增通用 OpenAI 兼容 Embedding 提供商: 引入了一个新的 OpenAICompatibleEmbeddingProvider,以支持更广泛的 OpenAI 兼容嵌入服务,解决了之前对非标准 API Base URL 处理的局限性。
  • 智能 API Base URL 处理: 新的提供商能够智能处理 API Base URL,对于纯域名会自动补齐 /v1,而对于已包含路径的 URL(如智谱的 /api/paas/v4 或火山的 /api/v3)则会保留原样。
  • 新增 send_dimensions_param 开关: 添加了一个 send_dimensions_param 配置项,允许用户控制是否将 embedding_dimensions 参数传递给上游嵌入接口,以适应不同服务对该参数的支持情况。
  • 内置智谱、火山引擎和 Ollama 预设: 为智谱(Zhipu Embedding)、火山引擎(Volcengine Embedding)和 Ollama Embedding 内置了预设配置,方便用户快速接入这些服务。
  • 更新默认配置和文档: 将原有的 OpenAI Embedding 替换为新的 OpenAI Compatible Embedding 作为通用选项,并更新了中、英、俄三语的 i18n 配置和知识库文档。
  • 完善单元测试: 新增了针对 API Base URL 归一化、dimensions 参数透传以及 Dashboard 集成的单元测试,确保新功能的稳定性和正确性。
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.

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 the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 19, 2026
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 - I've found 2 issues, and left some high level feedback:

  • In normalize_openai_compatible_embedding_api_base, a host without scheme (e.g. api.openai.com) is treated as a path and returned unchanged, which produces an invalid base URL; consider either auto-prefixing https:// or explicitly validating and rejecting scheme-less values to avoid confusing runtime failures.
  • The new send_dimensions_param flag is parsed quite liberally via should_send_dimensions_param (accepting various stringy truthy values); if config is only ever stored as a native boolean, you could simplify this to a direct bool read and avoid surprising behavior when unconventional strings are passed in.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `normalize_openai_compatible_embedding_api_base`, a host without scheme (e.g. `api.openai.com`) is treated as a path and returned unchanged, which produces an invalid base URL; consider either auto-prefixing `https://` or explicitly validating and rejecting scheme-less values to avoid confusing runtime failures.
- The new `send_dimensions_param` flag is parsed quite liberally via `should_send_dimensions_param` (accepting various stringy truthy values); if config is only ever stored as a native boolean, you could simplify this to a direct bool read and avoid surprising behavior when unconventional strings are passed in.

## Individual Comments

### Comment 1
<location path="tests/test_openai_compatible_embedding_source.py" line_range="72-81" />
<code_context>
     assert data["status"] == "ok" and "platform" in data["data"]


+@pytest.mark.asyncio
+async def test_provider_template_exposes_openai_compatible_embedding_presets(
+    app: Quart,
</code_context>
<issue_to_address>
**suggestion (testing):** Add assertions that `terminate()` properly closes the underlying AsyncOpenAI client

Current tests call `terminate()` in `finally` blocks but never verify that the underlying client is closed. Since `_FakeAsyncOpenAI` tracks `closed`, please add assertions (e.g., `assert provider_without_dimensions.client.closed is True` or via `instances`) after `terminate()` to validate resource cleanup and catch regressions in connection handling.
</issue_to_address>

### Comment 2
<location path="docs/en/use/knowledge-base.md" line_range="25" />
<code_context>
+> `OpenAI Compatible Embedding` includes a `send_dimensions_param` switch. When enabled, AstrBot sends `embedding_dimensions` to the upstream embedding API as the `dimensions` parameter. Disable it for OpenAI-compatible services that only need the local vector size and do not support `dimensions`.
+
+> [!NOTE]
+> The Volcengine preset defaults to `doubao-embedding-vision`. AstrBot's knowledge-base pipeline is still text chunking plus text embedding only, so this integration uses the model with text input only and does not add multimodal knowledge-base support yet,although it is a multimodal embedding model.
+
+> [!NOTE]
</code_context>
<issue_to_address>
**issue (typo):** Add a space after the comma in "yet,although" to fix the typo.

Please insert a space after the comma so it reads "yet, although it is a multimodal embedding model."

```suggestion
> The Volcengine preset defaults to `doubao-embedding-vision`. AstrBot's knowledge-base pipeline is still text chunking plus text embedding only, so this integration uses the model with text input only and does not add multimodal knowledge-base support yet, although it is a multimodal embedding model.
```
</issue_to_address>

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

本次 PR 新增了通用的 OpenAICompatibleEmbeddingProvider,并内置了智谱和火山引擎的预设,这是一个非常实用的功能增强,极大地扩展了嵌入服务的可选择范围。代码实现考虑周全,特别是对 API Base URL 的智能处理和 send_dimensions_param 开关的设计,显示了对不同服务兼容性的深入思考。同时,相关的单元测试、文档和国际化配置都一并更新,做得非常完整。

我在代码中发现了一些可以改进的地方,主要集中在新添加的 OpenAICompatibleEmbeddingProvider 中,涉及资源管理和配置解析的健壮性。具体的建议已在代码注释中提出,希望能帮助您进一步完善代码。

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a generic OpenAI-compatible embedding provider adapter (with smarter API base handling) and ships Zhipu and Volcengine embedding presets, plus corresponding Dashboard/i18n/docs and test coverage.

Changes:

  • Introduce openai_compatible_embedding provider with API base normalization and an opt-in send_dimensions_param toggle.
  • Add built-in provider templates/presets (OpenAI-compatible, Zhipu, Volcengine, Ollama) and expose them through the Dashboard.
  • Add unit/integration tests and update docs + i18n strings.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
astrbot/core/provider/sources/openai_compatible_embedding_source.py New OpenAI-compatible embedding adapter (API base normalization + optional dimensions forwarding).
astrbot/core/provider/manager.py Registers lazy import for the new provider type.
astrbot/core/config/default.py Adds provider templates/presets and schema field for send_dimensions_param.
dashboard/src/components/shared/AstrBotConfig.vue Simplifies how provider/item hints are surfaced in the config UI.
dashboard/src/i18n/locales/zh-CN/features/config-metadata.json Adds hint/label strings for new embedding presets and send_dimensions_param.
dashboard/src/i18n/locales/en-US/features/config-metadata.json Same i18n updates (English).
dashboard/src/i18n/locales/ru-RU/features/config-metadata.json Same i18n updates (Russian).
docs/zh/use/knowledge-base.md Documents new embedding options and the send_dimensions_param behavior.
docs/en/use/knowledge-base.md English documentation update for the new embedding options.
tests/test_openai_compatible_embedding_source.py Unit tests for API base normalization + dimensions forwarding behavior.
tests/test_dashboard.py Dashboard API tests for templates exposure and embedding dim endpoint support.

@whatevertogo whatevertogo changed the title feat: add OpenAI-compatible embedding provider with Zhipu and Volcengine presets feat: add OpenAI-兼容 embedding provider 和智谱字节提供商 Mar 19, 2026
@whatevertogo whatevertogo changed the title feat: add OpenAI-兼容 embedding provider 和智谱字节提供商 feat: add OpenAI-兼容 embedding provider 和智谱字节ollama提供商 Mar 19, 2026
- Fix typo in docs: "yet,although" → "yet, although"
- Fix resource leak: store httpx.AsyncClient as self._http_client and close in terminate()
- Add timeout parsing with exception handling for invalid values
- Handle API base URL without scheme (e.g., "api.openai.com")
- Add test assertions to verify terminate() properly closes client
- Clarify that openai_embedding is kept for backward compatibility
- Explain dimensions forwarding behavior difference between the two providers
- Update hints in en-US, zh-CN, and ru-RU locales
- Explain that dimensions enables dimension reduction (not just forwarding)
- List services that support it: OpenAI, Zhipu, Volcengine
- Update all three locales: en-US, zh-CN, ru-RU
Replace hardcoded strings with i18n key paths to enable proper
translation in the frontend for send_dimensions_param field.
@whatevertogo whatevertogo changed the title feat: add OpenAI-兼容 embedding provider 和智谱字节ollama提供商 feat: 添加更多嵌入提供商 OpenAI-兼容 embedding provider 和智谱字节ollama提供商 Mar 20, 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:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

2 participants