feat: add rerank provider for Text Embeddings Inference (TEI)#9274
Merged
Conversation
…ot into feat/tei-rerank-source
…ot into feat/tei-rerank-source
…ot into feat/tei-rerank-source
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
Contributor
There was a problem hiding this comment.
Hey - I've found 3 issues, and left some high level feedback:
- The
TEIRerankProvidercreates anaiohttp.ClientSessionin__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.clientis set toNone, butrerankonly checksself.client/self.client.closedand 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
Exceptioninstances 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Soulter
approved these changes
Jul 14, 2026
Takeoff0518
commented
Jul 14, 2026
Takeoff0518
left a comment
Contributor
Author
There was a problem hiding this comment.
All things done.
Contributor
Author
新的提交 b539b91 可以正常检索与召回。 |
This was referenced Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Text Embeddings Inference(TEI) 是由 HuggingFace 开源的一个专门用于部署和提供 embedding 和 reranking 模型的工具。
Closes #9266
TEI 的 OpenAPI
Modifications / 改动点
添加了 TEIRerankProvider 以及相关默认配置,使得框架可以使用 TEI rerank provider 提供 rerank 模型。
由于一个 TEI 容器实例在启动时就绑定了一个固定的模型,所以不需要再单独指定模型。
Screenshots or Test Results / 运行截图或测试结果
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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.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:
Enhancements: