Skip to content

feat: 添加更多嵌入提供商,优化现有embeddingurl处理逻辑,添加ollama embedding提供商#6642

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

feat: 添加更多嵌入提供商,优化现有embeddingurl处理逻辑,添加ollama embedding提供商#6642
whatevertogo wants to merge 11 commits intoAstrBotDevs:masterfrom
whatevertogo:feat/more-embedding-providers

Conversation

@whatevertogo
Copy link
Copy Markdown
Contributor

@whatevertogo whatevertogo commented Mar 19, 2026

Fixes #6638
Fix #3790
Fix #4226
Fix #3909

一、PR 概述

在现有 openai_embedding 适配器基础上,新增智谱、火山引擎、Ollama 三个 Embedding 预设配置,并改进 URL 处理逻辑以正确兼容带路径的 API 地址。


二、背景 / 动机

  • 问题现象:之前 AstrBot 只支持原生 OpenAI Embedding 和 Gemini Embedding 两种嵌入服务,用户无法直接使用智谱、火山引擎、Ollama 等兼容 OpenAI API 的嵌入服务。
  • 影响范围:知识库、记忆搜索等依赖 embedding 的功能。
  • 原有 openai_embedding 的局限:URL 处理逻辑基于简单的 endswith("/v1") / endswith("/v4") 后缀匹配,无法正确处理智谱 /api/paas/v4、火山 /api/v3 等带路径的地址(火山会被错误追加 /v1)。同时 proxy 的 httpx.AsyncClient 未在 terminate() 中关闭。

三、改动内容

  • 改进 OpenAIEmbeddingProvider 的 URL 处理:使用 urlsplit 解析 URL,只在纯域名(无路径)时自动追加 /v1,带路径的地址保持原样
  • 修复资源泄漏terminate() 中关闭 proxy 的 _http_client
  • 新增三个 Embedding 预设(复用 openai_embedding 类型,无需新增适配器):
    • 智谱 Embedding — embedding-3https://open.bigmodel.cn/api/paas/v4
    • 火山引擎 Embedding — doubao-embedding-visionhttps://ark.cn-beijing.volces.com/api/v3
    • Ollama Embedding — embeddinggemma,本地 http://127.0.0.1:11434
  • 更新 Dashboard hint 处理providerHint 改为动态匹配所有 embedding 类型的 hint key,避免显示原始 i18n key
  • 更新中/英/俄三语 i18n:添加新预设的 hint,更新 openai_embedding 的提示说明
  • 更新中英文知识库文档

改动类型:

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

四、实现说明

  • 核心思路:不新增独立的 provider 类型,而是复用现有 openai_embedding 适配器,通过预设配置(默认 embedding_api_baseembedding_model)实现不同服务的快捷接入。
  • 关键设计/权衡
    • 改进现有适配器的 URL 处理逻辑(urlsplit),而非引入新的 openai_compatible_embedding 类型,避免代码冗余
    • 预设使用 openai_embedding 类型,所有预设共享 openai_embeddingembedding_api_base 字段提示
    • dimensions 参数仍然通过现有的 embedding_dimensions 字段自动透传,不需要额外的 send_dimensions_param 开关
  • 主要涉及文件/模块
    • astrbot/core/provider/sources/openai_embedding_source.py — 改进 URL 处理 + 修复资源泄漏
    • astrbot/core/config/default.py — 新增三个预设配置
    • dashboard/src/components/shared/AstrBotConfig.vue — 改进 hint 处理逻辑
    • dashboard/src/i18n/locales/*/features/config-metadata.json — 三语 i18n
    • docs/*/use/knowledge-base.md — 文档更新

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

  • 之前是:只能选择 OpenAI Embedding 或 Gemini Embedding,且 openai_embedding 对带路径的 URL 处理不正确
  • 现在是:可额外选择智谱 Embedding、火山引擎 Embedding、Ollama Embedding 预设;openai_embedding 的 URL 处理更加智能
  • 兼容性影响:无 Breaking Changes。已有的 openai_embedding 配置继续正常工作。纯域名的 URL 仍会自动追加 /v1(与之前行为一致)。

六、测试与验证

自测结果:

  • 已本地验证通过
  • 已跑单测(tests/test_dashboard.py — 22 passed)

测试命令:

python -m pytest tests/test_dashboard.py -v

七、Checklist

  • 代码风格符合项目规范
  • 已添加必要的注释
  • 已更新相关文档(中/英知识库文档)
  • 已添加/更新测试用例
  • 无 Breaking Changes
  • 已关联相关 Issue

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
Copy Markdown
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
Copy Markdown
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.

Comment thread tests/test_openai_compatible_embedding_source.py Outdated
Comment thread docs/en/use/knowledge-base.md Outdated
Copy link
Copy Markdown
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 中,涉及资源管理和配置解析的健壮性。具体的建议已在代码注释中提出,希望能帮助您进一步完善代码。

Comment thread astrbot/core/provider/sources/openai_compatible_embedding_source.py Outdated
Comment thread astrbot/core/provider/sources/openai_compatible_embedding_source.py Outdated
Copy link
Copy Markdown
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.

Comment thread astrbot/core/provider/manager.py Outdated
Comment thread docs/en/use/knowledge-base.md Outdated
@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
@Sizolity
Copy link
Copy Markdown

为什么没有合并

@whatevertogo whatevertogo reopened this Mar 29, 2026
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Mar 29, 2026
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Mar 30, 2026
@whatevertogo whatevertogo changed the title feat: 添加更多嵌入提供商 OpenAI-兼容 embedding provider 和智谱字节ollama提供商 feat: 添加更多嵌入提供商,优化现有embeddingurl处理逻辑,添加ollama embedding提供商 Mar 30, 2026
@Sizolity
Copy link
Copy Markdown

我觉得引入Ollama API 可以很大程度上改善性能以及使用体验
当前使用OpenAI兼容ollama API 存在一些问题:

  • API 调用问题 : 流式输出具有明显的问题 , 链接超时后会出现消息仍被收集并显示的情况

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:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

3 participants