Skip to content

feat: add rerank provider for Text Embeddings Inference (TEI)#9274

Merged
Soulter merged 7 commits into
AstrBotDevs:masterfrom
Takeoff0518:feat/tei-rerank-source
Jul 15, 2026
Merged

feat: add rerank provider for Text Embeddings Inference (TEI)#9274
Soulter merged 7 commits into
AstrBotDevs:masterfrom
Takeoff0518:feat/tei-rerank-source

Conversation

@Takeoff0518

@Takeoff0518 Takeoff0518 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Text Embeddings Inference(TEI) 是由 HuggingFace 开源的一个专门用于部署和提供 embedding 和 reranking 模型的工具。

Closes #9266

TEI 的 OpenAPI

Modifications / 改动点

添加了 TEIRerankProvider 以及相关默认配置,使得框架可以使用 TEI rerank provider 提供 rerank 模型。

由于一个 TEI 容器实例在启动时就绑定了一个固定的模型,所以不需要再单独指定模型。

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

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

0ef9b329e589c2ed7bc3e2ef41d8a10a image 53e45fef2094a9bc3033149e1a51924b 47279a23ca9c1c58976dd57dfbc3ce61 d15689eaacde38604a39f00bc92c8aea

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 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.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Add HuggingFace Text Embeddings Inference (TEI) as a rerank provider and wire it into the provider system and dashboard configuration.

New Features:

  • Introduce TEIRerankProvider to integrate HuggingFace TEI reranking into the provider framework.
  • Add default configuration and UI metadata for the TEI rerank provider, including truncation, score format, and return-text options.

Enhancements:

  • Register the TEI rerank provider in the dynamic provider manager and expose a HuggingFace icon in the dashboard for visual identification.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Jul 14, 2026

@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 found 3 issues, and left some high level feedback:

  • The TEIRerankProvider creates an aiohttp.ClientSession in __init__; consider lazy-initializing the session in an async method (or ensuring this matches the existing pattern) to avoid potential event loop issues when constructing providers synchronously.
  • After terminate, self.client is set to None, but rerank only checks self.client/self.client.closed and does not recreate the session; you may want to add logic to reinitialize the client or clearly prevent further use after termination.
  • Error handling currently raises generic Exception instances with user-facing messages; using more specific exception types (or a provider-specific error class) would make upstream error handling and debugging more precise.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `TEIRerankProvider` creates an `aiohttp.ClientSession` in `__init__`; consider lazy-initializing the session in an async method (or ensuring this matches the existing pattern) to avoid potential event loop issues when constructing providers synchronously.
- After `terminate`, `self.client` is set to `None`, but `rerank` only checks `self.client`/`self.client.closed` and does not recreate the session; you may want to add logic to reinitialize the client or clearly prevent further use after termination.
- Error handling currently raises generic `Exception` instances with user-facing messages; using more specific exception types (or a provider-specific error class) would make upstream error handling and debugging more precise.

## Individual Comments

### Comment 1
<location path="astrbot/core/provider/sources/tei_rerank_source.py" line_range="26-27" />
<code_context>
+        self.base_url = provider_config.get("rerank_api_base", "http://127.0.0.1:8080").rstrip("/")
+        self.timeout = provider_config.get("timeout", 20)
+        self.truncate = provider_config.get("tei_rerank_truncate", False)
+        self.truncation_direction = provider_config.get(
+            "tei_rerank_truncation_direction", "Right"
+        )
+        self.raw_scores = provider_config.get("tei_rerank_raw_scores", False)
</code_context>
<issue_to_address>
**issue (bug_risk):** Normalize or validate truncation_direction to match TEI API expectations.

TEI’s `/rerank` API expects lowercase values like `"left"` / `"right"`, but this defaults to `"Right"` and forwards it unchanged. To avoid request failures or ignored params, normalize the value (e.g., `self.truncation_direction.lower()`) and/or validate it against the allowed options before sending the request.
</issue_to_address>

### Comment 2
<location path="astrbot/core/provider/sources/tei_rerank_source.py" line_range="46-48" />
<code_context>
+        documents: list[str],
+        top_n: int | None = None,
+    ) -> list[RerankResult]:
+        if not self.client or self.client.closed:
+            logger.error("[TEI Rerank] Client session is not initialized or closed")
+            return []
+        if not documents:
+            logger.warning("[TEI Rerank] Document list is empty, returning empty results")
</code_context>
<issue_to_address>
**issue (bug_risk):** Returning an empty list when the client session is closed can silently hide real errors.

When `self.client` is `None` or closed, we log an error but still return an empty list. This is indistinguishable from a legitimate "no results" response and can obscure session/connection failures. It would be better to either recreate the client here or raise an exception so callers can detect and handle the failure explicitly.
</issue_to_address>

### Comment 3
<location path="astrbot/core/provider/sources/tei_rerank_source.py" line_range="55-57" />
<code_context>
+        if not query.strip():
+            logger.warning("[TEI Rerank] Query is empty, returning empty results")
+            return []
+        payload: dict = {
+            "query": query,
+            "texts": documents,
+        }
+
</code_context>
<issue_to_address>
**suggestion (performance):** Consider passing top_n to TEI so the server can limit results for performance.

Currently TEI returns all reranked documents and you truncate with `results = results[:top_n]`. If `/rerank` supports a `top_n` parameter, consider adding it to `payload` when provided so the service can do the truncation, reducing both work and response size.

Suggested implementation:

```python
        if not query.strip():
            logger.warning("[TEI Rerank] Query is empty, returning empty results")
            return []

        payload: dict = {
            "query": query,
            "texts": documents,
        }

        if top_n is not None:
            payload["top_n"] = top_n

```

1. Ensure the method signature includes a `top_n: int | None = None` (or equivalent) parameter if it does not already.
2. Verify that wherever this method is called, `top_n` is passed appropriately.
3. Confirm that the HTTP request to the TEI `/rerank` endpoint uses the `payload` dictionary defined here as its body.
</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 astrbot/core/provider/sources/tei_rerank_source.py Outdated
Comment thread astrbot/core/provider/sources/tei_rerank_source.py
Comment thread astrbot/core/provider/sources/tei_rerank_source.py
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 14, 2026
@Takeoff0518
Takeoff0518 requested a review from Soulter July 14, 2026 02:35

@Takeoff0518 Takeoff0518 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

All things done.

Comment thread astrbot/core/provider/sources/tei_rerank_source.py
Comment thread astrbot/core/provider/sources/tei_rerank_source.py
Comment thread astrbot/core/provider/sources/tei_rerank_source.py Outdated
@Takeoff0518

Copy link
Copy Markdown
Contributor Author
image

新的提交 b539b91 可以正常检索与召回。

@Soulter
Soulter merged commit 148d8cc into AstrBotDevs:master Jul 15, 2026
21 checks passed
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. 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.

[Feature] add rerank provider for Text Embeddings Inference (TEI)

2 participants