Elffuss BraidHub

kikocis/lancedb public

main · 3.2 KiBRenderedRawPermalink
1# Jina Reranker
2
3This re-ranker uses the [Jina](https://jina.ai/reranker/) API to rerank the search results. You can use this re-ranker by passing `JinaReranker()` to the `rerank()` method. Note that you'll either need to set the `JINA_API_KEY` environment variable or pass the `api_key` argument to use this re-ranker.
4
5
6!!! note
7 Supported Query Types: Hybrid, Vector, FTS
8
9
10```python
11import os
12import lancedb
13from lancedb.embeddings import get_registry
14from lancedb.pydantic import LanceModel, Vector
15from lancedb.rerankers import JinaReranker
16
17os.environ['JINA_API_KEY'] = "jina_*"
18
19
20embedder = get_registry().get("jina").create()
21db = lancedb.connect("~/.lancedb")
22
23class Schema(LanceModel):
24 text: str = embedder.SourceField()
25 vector: Vector(embedder.ndims()) = embedder.VectorField()
26
27data = [
28 {"text": "hello world"},
29 {"text": "goodbye world"}
30 ]
31tbl = db.create_table("test", schema=Schema, mode="overwrite")
32tbl.add(data)
33reranker = JinaReranker(api_key="key")
34
35# Run vector search with a reranker
36result = tbl.search("hello").rerank(reranker=reranker).to_list()
37
38# Run FTS search with a reranker
39result = tbl.search("hello", query_type="fts").rerank(reranker=reranker).to_list()
40
41# Run hybrid search with a reranker
42tbl.create_fts_index("text", replace=True)
43result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
44
45```
46
47Accepted Arguments
48----------------
49| Argument | Type | Default | Description |
50| --- | --- | --- | --- |
51| `model_name` | `str` | `"jina-reranker-v2-base-multilingual"` | The name of the reranker model to use. You can find the list of available models in https://jina.ai/reranker/|
52| `column` | `str` | `"text"` | The name of the column to use as input to the cross encoder model. |
53| `top_n` | `str` | `None` | The number of results to return. If None, will return all results. |
54| `api_key` | `str` | `None` | The API key for the Jina API. If not provided, the `JINA_API_KEY` environment variable is used. |
55| `return_score` | str | `"relevance"` | Options are "relevance" or "all". The type of score to return. If "relevance", will return only the `_relevance_score. If "all" is supported, will return relevance score along with the vector and/or fts scores depending on query type |
56
57
58
59## Supported Scores for each query type
60You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type:
61
62### Hybrid Search
63|`return_score`| Status | Description |
64| --- | --- | --- |
65| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
66| `all` | ❌ Not Supported | Returns have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
67
68### Vector Search
69|`return_score`| Status | Description |
70| --- | --- | --- |
71| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
72| `all` | ✅ Supported | Returns have vector(`_distance`) along with Hybrid Search score(`_relevance_score`) |
73
74### FTS Search
75|`return_score`| Status | Description |
76| --- | --- | --- |
77| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
78| `all` | ✅ Supported | Returns have FTS(`score`) along with Hybrid Search score(`_relevance_score`) |