mirror of
https://github.com/lancedb/lancedb.git
synced 2026-08-18 20:18:37 +00:00
feat(python): added support for WatsonxReranker component (#3642)
## Summary Adds `WatsonxReranker` to the Python bindings, integrating the [IBM watsonx.ai text rerank API](https://cloud.ibm.com/docs/apis/watsonx-ai#text-rerank) via the `ibm_watsonx_ai` SDK (`pip install ibm-watsonx-ai`). ## Parameters | Parameter | Default | Description | |---|---|---| | `model_name` | `"cross-encoder/ms-marco-minilm-l-12-v2"` | Rerank model ID | | `column` | `"text"` | Table column used as document input | | `top_n` | `None` | Return only the top-n results | | `return_score` | `"relevance"` | `"relevance"` or `"all"` | | `api_key` | `None` | Falls back to `WATSONX_API_KEY` env var | | `project_id` | `None` | Falls back to `WATSONX_PROJECT_ID` env var — mutually exclusive with `space_id` | | `space_id` | `None` | Falls back to `WATSONX_SPACE_ID` env var — mutually exclusive with `project_id` | | `url` | `None` | Defaults to `https://us-south.ml.cloud.ibm.com` | | `truncate_input_tokens` | `None` | Token truncation limit | ## Usage ```python from lancedb.rerankers import WatsonxReranker # credentials from environment variables reranker = WatsonxReranker() # or passed explicitly reranker = WatsonxReranker( api_key="<key>", project_id="<project-id>", # or space_id="<space-id>" top_n=5, ) ``` ## Testing Integration test added in `test_rerankers.py`, skipped unless `WATSONX_API_KEY` and one of `WATSONX_PROJECT_ID` / `WATSONX_SPACE_ID` are set.
This commit is contained in:
@@ -23,6 +23,7 @@ from lancedb.rerankers import (
|
||||
AnswerdotaiRerankers,
|
||||
VoyageAIReranker,
|
||||
MRRReranker,
|
||||
WatsonxReranker,
|
||||
)
|
||||
from lancedb.table import LanceTable
|
||||
|
||||
@@ -727,3 +728,19 @@ def test_linear_combination_missing_fts_is_penalised():
|
||||
f"Document with FTS score (rowid 0, {scores[0]:.4f}) should beat "
|
||||
f"document with no FTS match (rowid 1, {scores[1]:.4f})"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
os.environ.get("WATSONX_API_KEY") is None
|
||||
or (
|
||||
os.environ.get("WATSONX_PROJECT_ID") is None
|
||||
and os.environ.get("WATSONX_SPACE_ID") is None
|
||||
),
|
||||
reason="WATSONX_API_KEY and one of WATSONX_PROJECT_ID / "
|
||||
"WATSONX_SPACE_ID must be set",
|
||||
)
|
||||
def test_watsonx_reranker(tmp_path):
|
||||
pytest.importorskip("ibm_watsonx_ai")
|
||||
table, schema = get_test_table(tmp_path)
|
||||
reranker = WatsonxReranker()
|
||||
_run_test_reranker(reranker, table, "single player experience", None, schema)
|
||||
|
||||
Reference in New Issue
Block a user