mirror of
https://github.com/lancedb/lancedb.git
synced 2025-12-23 13:29:57 +00:00
Compare commits
20 Commits
python-v0.
...
python-v0.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1090c311e8 | ||
|
|
e767cbb374 | ||
|
|
3d7c48feca | ||
|
|
08d62550bb | ||
|
|
b272408b05 | ||
|
|
46ffa87cd4 | ||
|
|
cd9fc37b95 | ||
|
|
431f94e564 | ||
|
|
c1a7d65473 | ||
|
|
1e5ccb1614 | ||
|
|
2e7ab373dc | ||
|
|
c7fbc4aaee | ||
|
|
7e023c1ef2 | ||
|
|
1d0dd9a8b8 | ||
|
|
deb947ddbd | ||
|
|
b039765d50 | ||
|
|
d155e82723 | ||
|
|
5d8c91256c | ||
|
|
44c03ebef3 | ||
|
|
8ea06fe7f3 |
3
.github/workflows/cargo-publish.yml
vendored
3
.github/workflows/cargo-publish.yml
vendored
@@ -8,6 +8,9 @@ env:
|
|||||||
# This env var is used by Swatinem/rust-cache@v2 for the cache
|
# This env var is used by Swatinem/rust-cache@v2 for the cache
|
||||||
# key, so we set it to make sure it is always consistent.
|
# key, so we set it to make sure it is always consistent.
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
|
# Up-to-date compilers needed for fp16kernels.
|
||||||
|
CC: gcc-12
|
||||||
|
CXX: g++-12
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|||||||
6
.github/workflows/pypi-publish.yml
vendored
6
.github/workflows/pypi-publish.yml
vendored
@@ -6,6 +6,8 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
linux:
|
linux:
|
||||||
|
# Only runs on tags that matches the python-make-release action
|
||||||
|
if: startsWith(github.ref, 'refs/tags/python-v')
|
||||||
name: Python ${{ matrix.config.platform }} manylinux${{ matrix.config.manylinux }}
|
name: Python ${{ matrix.config.platform }} manylinux${{ matrix.config.manylinux }}
|
||||||
timeout-minutes: 60
|
timeout-minutes: 60
|
||||||
strategy:
|
strategy:
|
||||||
@@ -44,6 +46,8 @@ jobs:
|
|||||||
token: ${{ secrets.LANCEDB_PYPI_API_TOKEN }}
|
token: ${{ secrets.LANCEDB_PYPI_API_TOKEN }}
|
||||||
repo: "pypi"
|
repo: "pypi"
|
||||||
mac:
|
mac:
|
||||||
|
# Only runs on tags that matches the python-make-release action
|
||||||
|
if: startsWith(github.ref, 'refs/tags/python-v')
|
||||||
timeout-minutes: 60
|
timeout-minutes: 60
|
||||||
runs-on: ${{ matrix.config.runner }}
|
runs-on: ${{ matrix.config.runner }}
|
||||||
strategy:
|
strategy:
|
||||||
@@ -76,6 +80,8 @@ jobs:
|
|||||||
token: ${{ secrets.LANCEDB_PYPI_API_TOKEN }}
|
token: ${{ secrets.LANCEDB_PYPI_API_TOKEN }}
|
||||||
repo: "pypi"
|
repo: "pypi"
|
||||||
windows:
|
windows:
|
||||||
|
# Only runs on tags that matches the python-make-release action
|
||||||
|
if: startsWith(github.ref, 'refs/tags/python-v')
|
||||||
timeout-minutes: 60
|
timeout-minutes: 60
|
||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
strategy:
|
strategy:
|
||||||
|
|||||||
24
Cargo.toml
24
Cargo.toml
@@ -14,19 +14,19 @@ keywords = ["lancedb", "lance", "database", "vector", "search"]
|
|||||||
categories = ["database-implementations"]
|
categories = ["database-implementations"]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
lance = { "version" = "=0.10.10", "features" = ["dynamodb"] }
|
lance = { "version" = "=0.10.16", "features" = ["dynamodb"] }
|
||||||
lance-index = { "version" = "=0.10.10" }
|
lance-index = { "version" = "=0.10.16" }
|
||||||
lance-linalg = { "version" = "=0.10.10" }
|
lance-linalg = { "version" = "=0.10.16" }
|
||||||
lance-testing = { "version" = "=0.10.10" }
|
lance-testing = { "version" = "=0.10.16" }
|
||||||
# Note that this one does not include pyarrow
|
# Note that this one does not include pyarrow
|
||||||
arrow = { version = "50.0", optional = false }
|
arrow = { version = "51.0", optional = false }
|
||||||
arrow-array = "50.0"
|
arrow-array = "51.0"
|
||||||
arrow-data = "50.0"
|
arrow-data = "51.0"
|
||||||
arrow-ipc = "50.0"
|
arrow-ipc = "51.0"
|
||||||
arrow-ord = "50.0"
|
arrow-ord = "51.0"
|
||||||
arrow-schema = "50.0"
|
arrow-schema = "51.0"
|
||||||
arrow-arith = "50.0"
|
arrow-arith = "51.0"
|
||||||
arrow-cast = "50.0"
|
arrow-cast = "51.0"
|
||||||
async-trait = "0"
|
async-trait = "0"
|
||||||
chrono = "0.4.35"
|
chrono = "0.4.35"
|
||||||
half = { "version" = "=2.3.1", default-features = false, features = [
|
half = { "version" = "=2.3.1", default-features = false, features = [
|
||||||
|
|||||||
@@ -57,16 +57,6 @@ plugins:
|
|||||||
- https://arrow.apache.org/docs/objects.inv
|
- https://arrow.apache.org/docs/objects.inv
|
||||||
- https://pandas.pydata.org/docs/objects.inv
|
- https://pandas.pydata.org/docs/objects.inv
|
||||||
- mkdocs-jupyter
|
- mkdocs-jupyter
|
||||||
- ultralytics:
|
|
||||||
verbose: True
|
|
||||||
enabled: True
|
|
||||||
default_image: "assets/lancedb_and_lance.png" # Default image for all pages
|
|
||||||
add_image: True # Automatically add meta image
|
|
||||||
add_keywords: True # Add page keywords in the header tag
|
|
||||||
add_share_buttons: True # Add social share buttons
|
|
||||||
add_authors: False # Display page authors
|
|
||||||
add_desc: False
|
|
||||||
add_dates: False
|
|
||||||
|
|
||||||
markdown_extensions:
|
markdown_extensions:
|
||||||
- admonition
|
- admonition
|
||||||
@@ -104,6 +94,14 @@ nav:
|
|||||||
- Overview: hybrid_search/hybrid_search.md
|
- Overview: hybrid_search/hybrid_search.md
|
||||||
- Comparing Rerankers: hybrid_search/eval.md
|
- Comparing Rerankers: hybrid_search/eval.md
|
||||||
- Airbnb financial data example: notebooks/hybrid_search.ipynb
|
- Airbnb financial data example: notebooks/hybrid_search.ipynb
|
||||||
|
- Reranking:
|
||||||
|
- Quickstart: reranking/index.md
|
||||||
|
- Cohere Reranker: reranking/cohere.md
|
||||||
|
- Linear Combination Reranker: reranking/linear_combination.md
|
||||||
|
- Cross Encoder Reranker: reranking/cross_encoder.md
|
||||||
|
- ColBERT Reranker: reranking/colbert.md
|
||||||
|
- OpenAI Reranker: reranking/openai.md
|
||||||
|
- Building Custom Rerankers: reranking/custom_reranker.md
|
||||||
- Filtering: sql.md
|
- Filtering: sql.md
|
||||||
- Versioning & Reproducibility: notebooks/reproducibility.ipynb
|
- Versioning & Reproducibility: notebooks/reproducibility.ipynb
|
||||||
- Configuring Storage: guides/storage.md
|
- Configuring Storage: guides/storage.md
|
||||||
@@ -120,9 +118,10 @@ nav:
|
|||||||
- Pandas and PyArrow: python/pandas_and_pyarrow.md
|
- Pandas and PyArrow: python/pandas_and_pyarrow.md
|
||||||
- Polars: python/polars_arrow.md
|
- Polars: python/polars_arrow.md
|
||||||
- DuckDB: python/duckdb.md
|
- DuckDB: python/duckdb.md
|
||||||
- LangChain 🔗: https://python.langchain.com/en/latest/modules/indexes/vectorstores/examples/lancedb.html
|
- LangChain:
|
||||||
- LangChain JS/TS 🔗: https://js.langchain.com/docs/modules/data_connection/vectorstores/integrations/lancedb
|
- LangChain 🔗: https://python.langchain.com/docs/integrations/vectorstores/lancedb/
|
||||||
- LlamaIndex 🦙: https://gpt-index.readthedocs.io/en/latest/examples/vector_stores/LanceDBIndexDemo.html
|
- LangChain JS/TS 🔗: https://js.langchain.com/docs/integrations/vectorstores/lancedb
|
||||||
|
- LlamaIndex 🦙: https://docs.llamaindex.ai/en/stable/examples/vector_stores/LanceDBIndexDemo/
|
||||||
- Pydantic: python/pydantic.md
|
- Pydantic: python/pydantic.md
|
||||||
- Voxel51: integrations/voxel51.md
|
- Voxel51: integrations/voxel51.md
|
||||||
- PromptTools: integrations/prompttools.md
|
- PromptTools: integrations/prompttools.md
|
||||||
@@ -170,6 +169,14 @@ nav:
|
|||||||
- Overview: hybrid_search/hybrid_search.md
|
- Overview: hybrid_search/hybrid_search.md
|
||||||
- Comparing Rerankers: hybrid_search/eval.md
|
- Comparing Rerankers: hybrid_search/eval.md
|
||||||
- Airbnb financial data example: notebooks/hybrid_search.ipynb
|
- Airbnb financial data example: notebooks/hybrid_search.ipynb
|
||||||
|
- Reranking:
|
||||||
|
- Quickstart: reranking/index.md
|
||||||
|
- Cohere Reranker: reranking/cohere.md
|
||||||
|
- Linear Combination Reranker: reranking/linear_combination.md
|
||||||
|
- Cross Encoder Reranker: reranking/cross_encoder.md
|
||||||
|
- ColBERT Reranker: reranking/colbert.md
|
||||||
|
- OpenAI Reranker: reranking/openai.md
|
||||||
|
- Building Custom Rerankers: reranking/custom_reranker.md
|
||||||
- Filtering: sql.md
|
- Filtering: sql.md
|
||||||
- Versioning & Reproducibility: notebooks/reproducibility.ipynb
|
- Versioning & Reproducibility: notebooks/reproducibility.ipynb
|
||||||
- Configuring Storage: guides/storage.md
|
- Configuring Storage: guides/storage.md
|
||||||
@@ -186,8 +193,8 @@ nav:
|
|||||||
- Pandas and PyArrow: python/pandas_and_pyarrow.md
|
- Pandas and PyArrow: python/pandas_and_pyarrow.md
|
||||||
- Polars: python/polars_arrow.md
|
- Polars: python/polars_arrow.md
|
||||||
- DuckDB: python/duckdb.md
|
- DuckDB: python/duckdb.md
|
||||||
- LangChain 🦜️🔗↗: https://python.langchain.com/en/latest/modules/indexes/vectorstores/examples/lancedb.html
|
- LangChain 🦜️🔗↗: https://python.langchain.com/docs/integrations/vectorstores/lancedb
|
||||||
- LangChain.js 🦜️🔗↗: https://js.langchain.com/docs/modules/data_connection/vectorstores/integrations/lancedb
|
- LangChain.js 🦜️🔗↗: https://js.langchain.com/docs/integrations/vectorstores/lancedb
|
||||||
- LlamaIndex 🦙↗: https://gpt-index.readthedocs.io/en/latest/examples/vector_stores/LanceDBIndexDemo.html
|
- LlamaIndex 🦙↗: https://gpt-index.readthedocs.io/en/latest/examples/vector_stores/LanceDBIndexDemo.html
|
||||||
- Pydantic: python/pydantic.md
|
- Pydantic: python/pydantic.md
|
||||||
- Voxel51: integrations/voxel51.md
|
- Voxel51: integrations/voxel51.md
|
||||||
|
|||||||
@@ -2,5 +2,4 @@ mkdocs==1.5.3
|
|||||||
mkdocs-jupyter==0.24.1
|
mkdocs-jupyter==0.24.1
|
||||||
mkdocs-material==9.5.3
|
mkdocs-material==9.5.3
|
||||||
mkdocstrings[python]==0.20.0
|
mkdocstrings[python]==0.20.0
|
||||||
pydantic
|
pydantic
|
||||||
mkdocs-ultralytics-plugin==0.0.44
|
|
||||||
@@ -154,9 +154,12 @@ Allows you to set parameters when registering a `sentence-transformers` object.
|
|||||||
!!! note "BAAI Embeddings example"
|
!!! note "BAAI Embeddings example"
|
||||||
Here is an example that uses BAAI embedding model from the HuggingFace Hub [supported models](https://huggingface.co/models?library=sentence-transformers)
|
Here is an example that uses BAAI embedding model from the HuggingFace Hub [supported models](https://huggingface.co/models?library=sentence-transformers)
|
||||||
```python
|
```python
|
||||||
|
import lancedb
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
|
||||||
db = lancedb.connect("/tmp/db")
|
db = lancedb.connect("/tmp/db")
|
||||||
registry = EmbeddingFunctionRegistry.get_instance()
|
model = get_registry().get("sentence-transformers").create(name="BAAI/bge-small-en-v1.5", device="cpu")
|
||||||
model = registry.get("sentence-transformers").create(name="BAAI/bge-small-en-v1.5", device="cpu")
|
|
||||||
|
|
||||||
class Words(LanceModel):
|
class Words(LanceModel):
|
||||||
text: str = model.SourceField()
|
text: str = model.SourceField()
|
||||||
@@ -165,7 +168,7 @@ Allows you to set parameters when registering a `sentence-transformers` object.
|
|||||||
table = db.create_table("words", schema=Words)
|
table = db.create_table("words", schema=Words)
|
||||||
table.add(
|
table.add(
|
||||||
[
|
[
|
||||||
{"text": "hello world"}
|
{"text": "hello world"},
|
||||||
{"text": "goodbye world"}
|
{"text": "goodbye world"}
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@@ -213,18 +216,21 @@ LanceDB registers the OpenAI embeddings function in the registry by default, as
|
|||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
import lancedb
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
|
||||||
db = lancedb.connect("/tmp/db")
|
db = lancedb.connect("/tmp/db")
|
||||||
registry = EmbeddingFunctionRegistry.get_instance()
|
func = get_registry().get("openai").create(name="text-embedding-ada-002")
|
||||||
func = registry.get("openai").create()
|
|
||||||
|
|
||||||
class Words(LanceModel):
|
class Words(LanceModel):
|
||||||
text: str = func.SourceField()
|
text: str = func.SourceField()
|
||||||
vector: Vector(func.ndims()) = func.VectorField()
|
vector: Vector(func.ndims()) = func.VectorField()
|
||||||
|
|
||||||
table = db.create_table("words", schema=Words)
|
table = db.create_table("words", schema=Words, mode="overwrite")
|
||||||
table.add(
|
table.add(
|
||||||
[
|
[
|
||||||
{"text": "hello world"}
|
{"text": "hello world"},
|
||||||
{"text": "goodbye world"}
|
{"text": "goodbye world"}
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@@ -353,6 +359,10 @@ Supported parameters (to be passed in `create` method) are:
|
|||||||
Usage Example:
|
Usage Example:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
import lancedb
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
|
||||||
model = get_registry().get("bedrock-text").create()
|
model = get_registry().get("bedrock-text").create()
|
||||||
|
|
||||||
class TextModel(LanceModel):
|
class TextModel(LanceModel):
|
||||||
@@ -387,10 +397,12 @@ This embedding function supports ingesting images as both bytes and urls. You ca
|
|||||||
LanceDB supports ingesting images directly from accessible links.
|
LanceDB supports ingesting images directly from accessible links.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
import lancedb
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
|
||||||
db = lancedb.connect(tmp_path)
|
db = lancedb.connect(tmp_path)
|
||||||
registry = EmbeddingFunctionRegistry.get_instance()
|
func = get_registry.get("open-clip").create()
|
||||||
func = registry.get("open-clip").create()
|
|
||||||
|
|
||||||
class Images(LanceModel):
|
class Images(LanceModel):
|
||||||
label: str
|
label: str
|
||||||
@@ -465,9 +477,12 @@ This function is registered as `imagebind` and supports Audio, Video and Text mo
|
|||||||
Below is an example demonstrating how the API works:
|
Below is an example demonstrating how the API works:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
import lancedb
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
|
||||||
db = lancedb.connect(tmp_path)
|
db = lancedb.connect(tmp_path)
|
||||||
registry = EmbeddingFunctionRegistry.get_instance()
|
func = get_registry.get("imagebind").create()
|
||||||
func = registry.get("imagebind").create()
|
|
||||||
|
|
||||||
class ImageBindModel(LanceModel):
|
class ImageBindModel(LanceModel):
|
||||||
text: str
|
text: str
|
||||||
|
|||||||
@@ -11,4 +11,64 @@ LanceDB supports 3 methods of working with embeddings.
|
|||||||
that extends the default embedding functions.
|
that extends the default embedding functions.
|
||||||
|
|
||||||
For python users, there is also a legacy [with_embeddings API](./legacy.md).
|
For python users, there is also a legacy [with_embeddings API](./legacy.md).
|
||||||
It is retained for compatibility and will be removed in a future version.
|
It is retained for compatibility and will be removed in a future version.
|
||||||
|
|
||||||
|
## Quickstart
|
||||||
|
|
||||||
|
To get started with embeddings, you can use the built-in embedding functions.
|
||||||
|
|
||||||
|
### OpenAI Embedding function
|
||||||
|
LanceDB registers the OpenAI embeddings function in the registry as `openai`. You can pass any supported model name to the `create`. By default it uses `"text-embedding-ada-002"`.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import lancedb
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
|
||||||
|
db = lancedb.connect("/tmp/db")
|
||||||
|
func = get_registry().get("openai").create(name="text-embedding-ada-002")
|
||||||
|
|
||||||
|
class Words(LanceModel):
|
||||||
|
text: str = func.SourceField()
|
||||||
|
vector: Vector(func.ndims()) = func.VectorField()
|
||||||
|
|
||||||
|
table = db.create_table("words", schema=Words, mode="overwrite")
|
||||||
|
table.add(
|
||||||
|
[
|
||||||
|
{"text": "hello world"},
|
||||||
|
{"text": "goodbye world"}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
query = "greetings"
|
||||||
|
actual = table.search(query).limit(1).to_pydantic(Words)[0]
|
||||||
|
print(actual.text)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sentence Transformers Embedding function
|
||||||
|
LanceDB registers the Sentence Transformers embeddings function in the registry as `sentence-transformers`. You can pass any supported model name to the `create`. By default it uses `"sentence-transformers/paraphrase-MiniLM-L6-v2"`.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import lancedb
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
|
||||||
|
db = lancedb.connect("/tmp/db")
|
||||||
|
model = get_registry().get("sentence-transformers").create(name="BAAI/bge-small-en-v1.5", device="cpu")
|
||||||
|
|
||||||
|
class Words(LanceModel):
|
||||||
|
text: str = model.SourceField()
|
||||||
|
vector: Vector(model.ndims()) = model.VectorField()
|
||||||
|
|
||||||
|
table = db.create_table("words", schema=Words)
|
||||||
|
table.add(
|
||||||
|
[
|
||||||
|
{"text": "hello world"},
|
||||||
|
{"text": "goodbye world"}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
query = "greetings"
|
||||||
|
actual = table.search(query).limit(1).to_pydantic(Words)[0]
|
||||||
|
print(actual.text)
|
||||||
|
```
|
||||||
@@ -24,7 +24,8 @@ data = [
|
|||||||
table = db.create_table("pd_table", data=data)
|
table = db.create_table("pd_table", data=data)
|
||||||
```
|
```
|
||||||
|
|
||||||
To query the table, first call `to_lance` to convert the table to a "dataset", which is an object that can be queried by DuckDB. Then all you need to do is reference that dataset by the same name in your SQL query.
|
The `to_lance` method converts the LanceDB table to a `LanceDataset`, which is accessible to DuckDB through the Arrow compatibility layer.
|
||||||
|
To query the resulting Lance dataset in DuckDB, all you need to do is reference the dataset by the same name in your SQL query.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import duckdb
|
import duckdb
|
||||||
|
|||||||
75
docs/src/reranking/cohere.md
Normal file
75
docs/src/reranking/cohere.md
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
# Cohere Reranker
|
||||||
|
|
||||||
|
This re-ranker uses the [Cohere](https://cohere.ai/) API to rerank the search results. You can use this re-ranker by passing `CohereReranker()` to the `rerank()` method. Note that you'll either need to set the `COHERE_API_KEY` environment variable or pass the `api_key` argument to use this re-ranker.
|
||||||
|
|
||||||
|
|
||||||
|
!!! note
|
||||||
|
Supported Query Types: Hybrid, Vector, FTS
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
import numpy
|
||||||
|
import lancedb
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.rerankers import CohereReranker
|
||||||
|
|
||||||
|
embedder = get_registry().get("sentence-transformers").create()
|
||||||
|
db = lancedb.connect("~/.lancedb")
|
||||||
|
|
||||||
|
class Schema(LanceModel):
|
||||||
|
text: str = embedder.SourceField()
|
||||||
|
vector: Vector(embedder.ndims()) = embedder.VectorField()
|
||||||
|
|
||||||
|
data = [
|
||||||
|
{"text": "hello world"},
|
||||||
|
{"text": "goodbye world"}
|
||||||
|
]
|
||||||
|
tbl = db.create_table("test", schema=Schema, mode="overwrite")
|
||||||
|
tbl.add(data)
|
||||||
|
reranker = CohereReranker(api_key="key")
|
||||||
|
|
||||||
|
# Run vector search with a reranker
|
||||||
|
result = tbl.search("hello").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
# Run FTS search with a reranker
|
||||||
|
result = tbl.search("hello", query_type="fts").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
# Run hybrid search with a reranker
|
||||||
|
tbl.create_fts_index("text", replace=True)
|
||||||
|
result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Accepted Arguments
|
||||||
|
----------------
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `model_name` | `str` | `"rerank-english-v2.0"` | The name of the reranker model to use. Available cohere models are: rerank-english-v2.0, rerank-multilingual-v2.0 |
|
||||||
|
| `column` | `str` | `"text"` | The name of the column to use as input to the cross encoder model. |
|
||||||
|
| `top_n` | `str` | `None` | The number of results to return. If None, will return all results. |
|
||||||
|
| `api_key` | `str` | `None` | The API key for the Cohere API. If not provided, the `COHERE_API_KEY` environment variable is used. |
|
||||||
|
| `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 |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Supported Scores for each query type
|
||||||
|
You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type:
|
||||||
|
|
||||||
|
### Hybrid Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ❌ Not Supported | Returns have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
|
|
||||||
|
### Vector Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have vector(`_distance`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
|
|
||||||
|
### FTS Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
71
docs/src/reranking/colbert.md
Normal file
71
docs/src/reranking/colbert.md
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
# ColBERT Reranker
|
||||||
|
|
||||||
|
This re-ranker uses ColBERT model to rerank the search results. You can use this re-ranker by passing `ColbertReranker()` to the `rerank()` method.
|
||||||
|
!!! note
|
||||||
|
Supported Query Types: Hybrid, Vector, FTS
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
import numpy
|
||||||
|
import lancedb
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.rerankers import ColbertReranker
|
||||||
|
|
||||||
|
embedder = get_registry().get("sentence-transformers").create()
|
||||||
|
db = lancedb.connect("~/.lancedb")
|
||||||
|
|
||||||
|
class Schema(LanceModel):
|
||||||
|
text: str = embedder.SourceField()
|
||||||
|
vector: Vector(embedder.ndims()) = embedder.VectorField()
|
||||||
|
|
||||||
|
data = [
|
||||||
|
{"text": "hello world"},
|
||||||
|
{"text": "goodbye world"}
|
||||||
|
]
|
||||||
|
tbl = db.create_table("test", schema=Schema, mode="overwrite")
|
||||||
|
tbl.add(data)
|
||||||
|
reranker = ColbertReranker()
|
||||||
|
|
||||||
|
# Run vector search with a reranker
|
||||||
|
result = tbl.search("hello").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
# Run FTS search with a reranker
|
||||||
|
result = tbl.search("hello", query_type="fts").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
# Run hybrid search with a reranker
|
||||||
|
tbl.create_fts_index("text", replace=True)
|
||||||
|
result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Accepted Arguments
|
||||||
|
----------------
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `model_name` | `str` | `"colbert-ir/colbertv2.0"` | The name of the reranker model to use.|
|
||||||
|
| `column` | `str` | `"text"` | The name of the column to use as input to the cross encoder model. |
|
||||||
|
| `device` | `str` | `None` | The device to use for the cross encoder model. If None, will use "cuda" if available, otherwise "cpu". |
|
||||||
|
| `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 |
|
||||||
|
|
||||||
|
|
||||||
|
## Supported Scores for each query type
|
||||||
|
You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type:
|
||||||
|
|
||||||
|
### Hybrid Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ❌ Not Supported | Returns have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
|
|
||||||
|
### Vector Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have vector(`_distance`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
|
|
||||||
|
### FTS Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
70
docs/src/reranking/cross_encoder.md
Normal file
70
docs/src/reranking/cross_encoder.md
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
# Cross Encoder Reranker
|
||||||
|
|
||||||
|
This re-ranker uses Cross Encoder models from sentence-transformers to rerank the search results. You can use this re-ranker by passing `CrossEncoderReranker()` to the `rerank()` method.
|
||||||
|
!!! note
|
||||||
|
Supported Query Types: Hybrid, Vector, FTS
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
import numpy
|
||||||
|
import lancedb
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.rerankers import CrossEncoderReranker
|
||||||
|
|
||||||
|
embedder = get_registry().get("sentence-transformers").create()
|
||||||
|
db = lancedb.connect("~/.lancedb")
|
||||||
|
|
||||||
|
class Schema(LanceModel):
|
||||||
|
text: str = embedder.SourceField()
|
||||||
|
vector: Vector(embedder.ndims()) = embedder.VectorField()
|
||||||
|
|
||||||
|
data = [
|
||||||
|
{"text": "hello world"},
|
||||||
|
{"text": "goodbye world"}
|
||||||
|
]
|
||||||
|
tbl = db.create_table("test", schema=Schema, mode="overwrite")
|
||||||
|
tbl.add(data)
|
||||||
|
reranker = CrossEncoderReranker()
|
||||||
|
|
||||||
|
# Run vector search with a reranker
|
||||||
|
result = tbl.search("hello").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
# Run FTS search with a reranker
|
||||||
|
result = tbl.search("hello", query_type="fts").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
# Run hybrid search with a reranker
|
||||||
|
tbl.create_fts_index("text", replace=True)
|
||||||
|
result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Accepted Arguments
|
||||||
|
----------------
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `model_name` | `str` | `""cross-encoder/ms-marco-TinyBERT-L-6"` | The name of the reranker model to use.|
|
||||||
|
| `column` | `str` | `"text"` | The name of the column to use as input to the cross encoder model. |
|
||||||
|
| `device` | `str` | `None` | The device to use for the cross encoder model. If None, will use "cuda" if available, otherwise "cpu". |
|
||||||
|
| `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 |
|
||||||
|
|
||||||
|
## Supported Scores for each query type
|
||||||
|
You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type:
|
||||||
|
|
||||||
|
### Hybrid Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ❌ Not Supported | Returns have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
|
|
||||||
|
### Vector Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have vector(`_distance`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
|
|
||||||
|
### FTS Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
88
docs/src/reranking/custom_reranker.md
Normal file
88
docs/src/reranking/custom_reranker.md
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
## Building Custom Rerankers
|
||||||
|
You can build your own custom reranker by subclassing the `Reranker` class and implementing the `rerank_hybrid()` method. Optionally, you can also implement the `rerank_vector()` and `rerank_fts()` methods if you want to support reranking for vector and FTS search separately.
|
||||||
|
Here's an example of a custom reranker that combines the results of semantic and full-text search using a linear combination of the scores.
|
||||||
|
|
||||||
|
The `Reranker` base interface comes with a `merge_results()` method that can be used to combine the results of semantic and full-text search. This is a vanilla merging algorithm that simply concatenates the results and removes the duplicates without taking the scores into consideration. It only keeps the first copy of the row encountered. This works well in cases that don't require the scores of semantic and full-text search to combine the results. If you want to use the scores or want to support `return_score="all"`, you'll need to implement your own merging algorithm.
|
||||||
|
|
||||||
|
```python
|
||||||
|
|
||||||
|
from lancedb.rerankers import Reranker
|
||||||
|
import pyarrow as pa
|
||||||
|
|
||||||
|
class MyReranker(Reranker):
|
||||||
|
def __init__(self, param1, param2, ..., return_score="relevance"):
|
||||||
|
super().__init__(return_score)
|
||||||
|
self.param1 = param1
|
||||||
|
self.param2 = param2
|
||||||
|
|
||||||
|
def rerank_hybrid(self, query: str, vector_results: pa.Table, fts_results: pa.Table):
|
||||||
|
# Use the built-in merging function
|
||||||
|
combined_result = self.merge_results(vector_results, fts_results)
|
||||||
|
|
||||||
|
# Do something with the combined results
|
||||||
|
# ...
|
||||||
|
|
||||||
|
# Return the combined results
|
||||||
|
return combined_result
|
||||||
|
|
||||||
|
def rerank_vector(self, query: str, vector_results: pa.Table):
|
||||||
|
# Do something with the vector results
|
||||||
|
# ...
|
||||||
|
|
||||||
|
# Return the vector results
|
||||||
|
return vector_results
|
||||||
|
|
||||||
|
def rerank_fts(self, query: str, fts_results: pa.Table):
|
||||||
|
# Do something with the FTS results
|
||||||
|
# ...
|
||||||
|
|
||||||
|
# Return the FTS results
|
||||||
|
return fts_results
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example of a Custom Reranker
|
||||||
|
For the sake of simplicity let's build custom reranker that just enchances the Cohere Reranker by accepting a filter query, and accept other CohereReranker params as kwags.
|
||||||
|
|
||||||
|
```python
|
||||||
|
|
||||||
|
from typing import List, Union
|
||||||
|
import pandas as pd
|
||||||
|
from lancedb.rerankers import CohereReranker
|
||||||
|
|
||||||
|
class ModifiedCohereReranker(CohereReranker):
|
||||||
|
def __init__(self, filters: Union[str, List[str]], **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
filters = filters if isinstance(filters, list) else [filters]
|
||||||
|
self.filters = filters
|
||||||
|
|
||||||
|
def rerank_hybrid(self, query: str, vector_results: pa.Table, fts_results: pa.Table)-> pa.Table:
|
||||||
|
combined_result = super().rerank_hybrid(query, vector_results, fts_results)
|
||||||
|
df = combined_result.to_pandas()
|
||||||
|
for filter in self.filters:
|
||||||
|
df = df.query("not text.str.contains(@filter)")
|
||||||
|
|
||||||
|
return pa.Table.from_pandas(df)
|
||||||
|
|
||||||
|
def rerank_vector(self, query: str, vector_results: pa.Table)-> pa.Table:
|
||||||
|
vector_results = super().rerank_vector(query, vector_results)
|
||||||
|
df = vector_results.to_pandas()
|
||||||
|
for filter in self.filters:
|
||||||
|
df = df.query("not text.str.contains(@filter)")
|
||||||
|
|
||||||
|
return pa.Table.from_pandas(df)
|
||||||
|
|
||||||
|
def rerank_fts(self, query: str, fts_results: pa.Table)-> pa.Table:
|
||||||
|
fts_results = super().rerank_fts(query, fts_results)
|
||||||
|
df = fts_results.to_pandas()
|
||||||
|
for filter in self.filters:
|
||||||
|
df = df.query("not text.str.contains(@filter)")
|
||||||
|
|
||||||
|
return pa.Table.from_pandas(df)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
!!! tip
|
||||||
|
The `vector_results` and `fts_results` are pyarrow tables. Lean more about pyarrow tables [here](https://arrow.apache.org/docs/python). It can be convered to other data types like pandas dataframe, pydict, pylist etc.
|
||||||
|
|
||||||
|
For example, You can convert them to pandas dataframes using `to_pandas()` method and perform any operations you want. After you are done, you can convert the dataframe back to pyarrow table using `pa.Table.from_pandas()` method and return it.
|
||||||
60
docs/src/reranking/index.md
Normal file
60
docs/src/reranking/index.md
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
Reranking is the process of reordering a list of items based on some criteria. In the context of search, reranking is used to reorder the search results returned by a search engine based on some criteria. This can be useful when the initial ranking of the search results is not satisfactory or when the user has provided additional information that can be used to improve the ranking of the search results.
|
||||||
|
|
||||||
|
LanceDB comes with some built-in rerankers. Some of the rerankers that are available in LanceDB are:
|
||||||
|
|
||||||
|
| Reranker | Description | Supported Query Types |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `LinearCombinationReranker` | Reranks search results based on a linear combination of FTS and vector search scores | Hybrid |
|
||||||
|
| `CohereReranker` | Uses cohere Reranker API to rerank results | Vector, FTS, Hybrid |
|
||||||
|
| `CrossEncoderReranker` | Uses a cross-encoder model to rerank search results | Vector, FTS, Hybrid |
|
||||||
|
| `ColbertReranker` | Uses a colbert model to rerank search results | Vector, FTS, Hybrid |
|
||||||
|
| `OpenaiReranker`(Experimental) | Uses OpenAI's chat model to rerank search results | Vector, FTS, Hybrid |
|
||||||
|
|
||||||
|
|
||||||
|
## Using a Reranker
|
||||||
|
Using rerankers is optional for vector and FTS. However, for hybrid search, rerankers are required. To use a reranker, you need to create an instance of the reranker and pass it to the `rerank` method of the query builder.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import numpy
|
||||||
|
import lancedb
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.rerankers import CohereReranker
|
||||||
|
|
||||||
|
embedder = get_registry().get("sentence-transformers").create()
|
||||||
|
db = lancedb.connect("~/.lancedb")
|
||||||
|
|
||||||
|
class Schema(LanceModel):
|
||||||
|
text: str = embedder.SourceField()
|
||||||
|
vector: Vector(embedder.ndims()) = embedder.VectorField()
|
||||||
|
|
||||||
|
data = [
|
||||||
|
{"text": "hello world"},
|
||||||
|
{"text": "goodbye world"}
|
||||||
|
]
|
||||||
|
tbl = db.create_table("test", data)
|
||||||
|
reranker = CohereReranker(api_key="your_api_key")
|
||||||
|
|
||||||
|
# Run vector search with a reranker
|
||||||
|
result = tbl.query("hello").rerank(reranker).to_list()
|
||||||
|
|
||||||
|
# Run FTS search with a reranker
|
||||||
|
result = tbl.query("hello", query_type="fts").rerank(reranker).to_list()
|
||||||
|
|
||||||
|
# Run hybrid search with a reranker
|
||||||
|
tbl.create_fts_index("text")
|
||||||
|
result = tbl.query("hello", query_type="hybrid").rerank(reranker).to_list()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Available Rerankers
|
||||||
|
LanceDB comes with some built-in rerankers. Here are some of the rerankers that are available in LanceDB:
|
||||||
|
|
||||||
|
- [Cohere Reranker](./cohere.md)
|
||||||
|
- [Cross Encoder Reranker](./cross_encoder.md)
|
||||||
|
- [ColBERT Reranker](./colbert.md)
|
||||||
|
- [OpenAI Reranker](./openai.md)
|
||||||
|
- [Linear Combination Reranker](./linear_combination.md)
|
||||||
|
|
||||||
|
## Creating Custom Rerankers
|
||||||
|
|
||||||
|
LanceDB also you to create custom rerankers by extending the base `Reranker` class. The custom reranker should implement the `rerank` method that takes a list of search results and returns a reranked list of search results. This is covered in more detail in the [Creating Custom Rerankers](./custom_reranker.md) section.
|
||||||
52
docs/src/reranking/linear_combination.md
Normal file
52
docs/src/reranking/linear_combination.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# Linear Combination Reranker
|
||||||
|
|
||||||
|
This is the default re-ranker used by LanceDB hybrid search. It combines the results of semantic and full-text search using a linear combination of the scores. The weights for the linear combination can be specified. It defaults to 0.7, i.e, 70% weight for semantic search and 30% weight for full-text search.
|
||||||
|
|
||||||
|
!!! note
|
||||||
|
Supported Query Types: Hybrid
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
import numpy
|
||||||
|
import lancedb
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.rerankers import LinearCombinationReranker
|
||||||
|
|
||||||
|
embedder = get_registry().get("sentence-transformers").create()
|
||||||
|
db = lancedb.connect("~/.lancedb")
|
||||||
|
|
||||||
|
class Schema(LanceModel):
|
||||||
|
text: str = embedder.SourceField()
|
||||||
|
vector: Vector(embedder.ndims()) = embedder.VectorField()
|
||||||
|
|
||||||
|
data = [
|
||||||
|
{"text": "hello world"},
|
||||||
|
{"text": "goodbye world"}
|
||||||
|
]
|
||||||
|
tbl = db.create_table("test", schema=Schema, mode="overwrite")
|
||||||
|
tbl.add(data)
|
||||||
|
reranker = LinearCombinationReranker()
|
||||||
|
|
||||||
|
# Run hybrid search with a reranker
|
||||||
|
tbl.create_fts_index("text", replace=True)
|
||||||
|
result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Accepted Arguments
|
||||||
|
----------------
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `weight` | `float` | `0.7` | The weight to use for the semantic search score. The weight for the full-text search score is `1 - weights`. |
|
||||||
|
| `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", will return all scores from the vector and FTS search along with the relevance score. |
|
||||||
|
|
||||||
|
|
||||||
|
## Supported Scores for each query type
|
||||||
|
You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type:
|
||||||
|
|
||||||
|
### Hybrid Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_distance`) |
|
||||||
73
docs/src/reranking/openai.md
Normal file
73
docs/src/reranking/openai.md
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
# OpenAI Reranker (Experimental)
|
||||||
|
|
||||||
|
This re-ranker uses OpenAI chat model to rerank the search results. You can use this re-ranker by passing `OpenAI()` to the `rerank()` method.
|
||||||
|
!!! note
|
||||||
|
Supported Query Types: Hybrid, Vector, FTS
|
||||||
|
|
||||||
|
!!! warning
|
||||||
|
This re-ranker is experimental. OpenAI doesn't have a dedicated reranking model, so we are using the chat model for reranking.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import numpy
|
||||||
|
import lancedb
|
||||||
|
from lancedb.embeddings import get_registry
|
||||||
|
from lancedb.pydantic import LanceModel, Vector
|
||||||
|
from lancedb.rerankers import OpenaiReranker
|
||||||
|
|
||||||
|
embedder = get_registry().get("sentence-transformers").create()
|
||||||
|
db = lancedb.connect("~/.lancedb")
|
||||||
|
|
||||||
|
class Schema(LanceModel):
|
||||||
|
text: str = embedder.SourceField()
|
||||||
|
vector: Vector(embedder.ndims()) = embedder.VectorField()
|
||||||
|
|
||||||
|
data = [
|
||||||
|
{"text": "hello world"},
|
||||||
|
{"text": "goodbye world"}
|
||||||
|
]
|
||||||
|
tbl = db.create_table("test", schema=Schema, mode="overwrite")
|
||||||
|
tbl.add(data)
|
||||||
|
reranker = OpenaiReranker()
|
||||||
|
|
||||||
|
# Run vector search with a reranker
|
||||||
|
result = tbl.search("hello").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
# Run FTS search with a reranker
|
||||||
|
result = tbl.search("hello", query_type="fts").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
# Run hybrid search with a reranker
|
||||||
|
tbl.create_fts_index("text", replace=True)
|
||||||
|
result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Accepted Arguments
|
||||||
|
----------------
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `model_name` | `str` | `"gpt-4-turbo-preview"` | The name of the reranker model to use.|
|
||||||
|
| `column` | `str` | `"text"` | The name of the column to use as input to the cross encoder model. |
|
||||||
|
| `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 |
|
||||||
|
| `api_key` | str | `None` | The API key to use. If None, will use the OPENAI_API_KEY environment variable.
|
||||||
|
|
||||||
|
|
||||||
|
## Supported Scores for each query type
|
||||||
|
You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type:
|
||||||
|
|
||||||
|
### Hybrid Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ❌ Not Supported | Returns have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
|
|
||||||
|
### Vector Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have vector(`_distance`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
|
|
||||||
|
### FTS Search
|
||||||
|
|`return_score`| Status | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `relevance` | ✅ Supported | Returns only have the `_relevance_score` column |
|
||||||
|
| `all` | ✅ Supported | Returns have FTS(`score`) along with Hybrid Search score(`_relevance_score`) |
|
||||||
@@ -15,6 +15,7 @@ excluded_globs = [
|
|||||||
"../src/ann_indexes.md",
|
"../src/ann_indexes.md",
|
||||||
"../src/basic.md",
|
"../src/basic.md",
|
||||||
"../src/hybrid_search/hybrid_search.md",
|
"../src/hybrid_search/hybrid_search.md",
|
||||||
|
"../src/reranking/*.md",
|
||||||
]
|
]
|
||||||
|
|
||||||
python_prefix = "py"
|
python_prefix = "py"
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ export interface CreateTableOptions<T> {
|
|||||||
/**
|
/**
|
||||||
* Connect to a LanceDB instance at the given URI.
|
* Connect to a LanceDB instance at the given URI.
|
||||||
*
|
*
|
||||||
* Accpeted formats:
|
* Accepted formats:
|
||||||
*
|
*
|
||||||
* - `/path/to/database` - local database
|
* - `/path/to/database` - local database
|
||||||
* - `s3://bucket/path/to/database` or `gs://bucket/path/to/database` - database on cloud storage
|
* - `s3://bucket/path/to/database` or `gs://bucket/path/to/database` - database on cloud storage
|
||||||
|
|||||||
@@ -140,6 +140,9 @@ export class RemoteConnection implements Connection {
|
|||||||
schema = nameOrOpts.schema
|
schema = nameOrOpts.schema
|
||||||
embeddings = nameOrOpts.embeddingFunction
|
embeddings = nameOrOpts.embeddingFunction
|
||||||
tableName = nameOrOpts.name
|
tableName = nameOrOpts.name
|
||||||
|
if (data === undefined) {
|
||||||
|
data = nameOrOpts.data
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer: Buffer
|
let buffer: Buffer
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { Table as ArrowTable, Schema } from "apache-arrow";
|
|||||||
/**
|
/**
|
||||||
* Connect to a LanceDB instance at the given URI.
|
* Connect to a LanceDB instance at the given URI.
|
||||||
*
|
*
|
||||||
* Accpeted formats:
|
* Accepted formats:
|
||||||
*
|
*
|
||||||
* - `/path/to/database` - local database
|
* - `/path/to/database` - local database
|
||||||
* - `s3://bucket/path/to/database` or `gs://bucket/path/to/database` - database on cloud storage
|
* - `s3://bucket/path/to/database` or `gs://bucket/path/to/database` - database on cloud storage
|
||||||
@@ -77,6 +77,18 @@ export interface OpenTableOptions {
|
|||||||
* The available options are described at https://lancedb.github.io/lancedb/guides/storage/
|
* The available options are described at https://lancedb.github.io/lancedb/guides/storage/
|
||||||
*/
|
*/
|
||||||
storageOptions?: Record<string, string>;
|
storageOptions?: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* Set the size of the index cache, specified as a number of entries
|
||||||
|
*
|
||||||
|
* The exact meaning of an "entry" will depend on the type of index:
|
||||||
|
* - IVF: there is one entry for each IVF partition
|
||||||
|
* - BTREE: there is one entry for the entire index
|
||||||
|
*
|
||||||
|
* This cache applies to the entire opened table, across all indices.
|
||||||
|
* Setting this value higher will increase performance on larger datasets
|
||||||
|
* at the expense of more RAM
|
||||||
|
*/
|
||||||
|
indexCacheSize?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TableNamesOptions {
|
export interface TableNamesOptions {
|
||||||
@@ -160,6 +172,7 @@ export class Connection {
|
|||||||
const innerTable = await this.inner.openTable(
|
const innerTable = await this.inner.openTable(
|
||||||
name,
|
name,
|
||||||
cleanseStorageOptions(options?.storageOptions),
|
cleanseStorageOptions(options?.storageOptions),
|
||||||
|
options?.indexCacheSize,
|
||||||
);
|
);
|
||||||
return new Table(innerTable);
|
return new Table(innerTable);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -176,6 +176,7 @@ impl Connection {
|
|||||||
&self,
|
&self,
|
||||||
name: String,
|
name: String,
|
||||||
storage_options: Option<HashMap<String, String>>,
|
storage_options: Option<HashMap<String, String>>,
|
||||||
|
index_cache_size: Option<u32>,
|
||||||
) -> napi::Result<Table> {
|
) -> napi::Result<Table> {
|
||||||
let mut builder = self.get_inner()?.open_table(&name);
|
let mut builder = self.get_inner()?.open_table(&name);
|
||||||
if let Some(storage_options) = storage_options {
|
if let Some(storage_options) = storage_options {
|
||||||
@@ -183,6 +184,9 @@ impl Connection {
|
|||||||
builder = builder.storage_option(key, value);
|
builder = builder.storage_option(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(index_cache_size) = index_cache_size {
|
||||||
|
builder = builder.index_cache_size(index_cache_size);
|
||||||
|
}
|
||||||
let tbl = builder
|
let tbl = builder
|
||||||
.execute()
|
.execute()
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
[bumpversion]
|
[bumpversion]
|
||||||
current_version = 0.6.8
|
current_version = 0.6.11
|
||||||
commit = True
|
commit = True
|
||||||
message = [python] Bump version: {current_version} → {new_version}
|
message = [python] Bump version: {current_version} → {new_version}
|
||||||
tag = True
|
tag = True
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ name = "_lancedb"
|
|||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
arrow = { version = "50.0.0", features = ["pyarrow"] }
|
arrow = { version = "51.0.0", features = ["pyarrow"] }
|
||||||
lancedb = { path = "../rust/lancedb" }
|
lancedb = { path = "../rust/lancedb" }
|
||||||
env_logger = "0.10"
|
env_logger = "0.10"
|
||||||
pyo3 = { version = "0.20", features = ["extension-module", "abi3-py38"] }
|
pyo3 = { version = "0.20", features = ["extension-module", "abi3-py38"] }
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "lancedb"
|
name = "lancedb"
|
||||||
version = "0.6.8"
|
version = "0.6.11"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"deprecation",
|
"deprecation",
|
||||||
"pylance==0.10.10",
|
"pylance==0.10.12",
|
||||||
"ratelimiter~=1.0",
|
"ratelimiter~=1.0",
|
||||||
"requests>=2.31.0",
|
"requests>=2.31.0",
|
||||||
"retry>=0.9.2",
|
"retry>=0.9.2",
|
||||||
@@ -65,7 +65,6 @@ docs = [
|
|||||||
"mkdocs-jupyter",
|
"mkdocs-jupyter",
|
||||||
"mkdocs-material",
|
"mkdocs-material",
|
||||||
"mkdocstrings[python]",
|
"mkdocstrings[python]",
|
||||||
"mkdocs-ultralytics-plugin==0.0.44",
|
|
||||||
]
|
]
|
||||||
clip = ["torch", "pillow", "open-clip"]
|
clip = ["torch", "pillow", "open-clip"]
|
||||||
embeddings = [
|
embeddings = [
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ def connect(
|
|||||||
|
|
||||||
>>> db = lancedb.connect("s3://my-bucket/lancedb")
|
>>> db = lancedb.connect("s3://my-bucket/lancedb")
|
||||||
|
|
||||||
Connect to LancdDB cloud:
|
Connect to LanceDB cloud:
|
||||||
|
|
||||||
>>> db = lancedb.connect("db://my_database", api_key="ldb_...")
|
>>> db = lancedb.connect("db://my_database", api_key="ldb_...")
|
||||||
|
|
||||||
|
|||||||
@@ -224,13 +224,23 @@ class DBConnection(EnforceOverrides):
|
|||||||
def __getitem__(self, name: str) -> LanceTable:
|
def __getitem__(self, name: str) -> LanceTable:
|
||||||
return self.open_table(name)
|
return self.open_table(name)
|
||||||
|
|
||||||
def open_table(self, name: str) -> Table:
|
def open_table(self, name: str, *, index_cache_size: Optional[int] = None) -> Table:
|
||||||
"""Open a Lance Table in the database.
|
"""Open a Lance Table in the database.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
name: str
|
name: str
|
||||||
The name of the table.
|
The name of the table.
|
||||||
|
index_cache_size: int, default 256
|
||||||
|
Set the size of the index cache, specified as a number of entries
|
||||||
|
|
||||||
|
The exact meaning of an "entry" will depend on the type of index:
|
||||||
|
* IVF - there is one entry for each IVF partition
|
||||||
|
* BTREE - there is one entry for the entire index
|
||||||
|
|
||||||
|
This cache applies to the entire opened table, across all indices.
|
||||||
|
Setting this value higher will increase performance on larger datasets
|
||||||
|
at the expense of more RAM
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
@@ -248,6 +258,18 @@ class DBConnection(EnforceOverrides):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def rename_table(self, cur_name: str, new_name: str):
|
||||||
|
"""Rename a table in the database.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
cur_name: str
|
||||||
|
The current name of the table.
|
||||||
|
new_name: str
|
||||||
|
The new name of the table.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
def drop_database(self):
|
def drop_database(self):
|
||||||
"""
|
"""
|
||||||
Drop database
|
Drop database
|
||||||
@@ -407,7 +429,9 @@ class LanceDBConnection(DBConnection):
|
|||||||
return tbl
|
return tbl
|
||||||
|
|
||||||
@override
|
@override
|
||||||
def open_table(self, name: str) -> LanceTable:
|
def open_table(
|
||||||
|
self, name: str, *, index_cache_size: Optional[int] = None
|
||||||
|
) -> LanceTable:
|
||||||
"""Open a table in the database.
|
"""Open a table in the database.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@@ -419,7 +443,7 @@ class LanceDBConnection(DBConnection):
|
|||||||
-------
|
-------
|
||||||
A LanceTable object representing the table.
|
A LanceTable object representing the table.
|
||||||
"""
|
"""
|
||||||
return LanceTable.open(self, name)
|
return LanceTable.open(self, name, index_cache_size=index_cache_size)
|
||||||
|
|
||||||
@override
|
@override
|
||||||
def drop_table(self, name: str, ignore_missing: bool = False):
|
def drop_table(self, name: str, ignore_missing: bool = False):
|
||||||
@@ -751,7 +775,10 @@ class AsyncConnection(object):
|
|||||||
return AsyncTable(new_table)
|
return AsyncTable(new_table)
|
||||||
|
|
||||||
async def open_table(
|
async def open_table(
|
||||||
self, name: str, storage_options: Optional[Dict[str, str]] = None
|
self,
|
||||||
|
name: str,
|
||||||
|
storage_options: Optional[Dict[str, str]] = None,
|
||||||
|
index_cache_size: Optional[int] = None,
|
||||||
) -> Table:
|
) -> Table:
|
||||||
"""Open a Lance Table in the database.
|
"""Open a Lance Table in the database.
|
||||||
|
|
||||||
@@ -764,12 +791,22 @@ class AsyncConnection(object):
|
|||||||
connection will be inherited by the table, but can be overridden here.
|
connection will be inherited by the table, but can be overridden here.
|
||||||
See available options at
|
See available options at
|
||||||
https://lancedb.github.io/lancedb/guides/storage/
|
https://lancedb.github.io/lancedb/guides/storage/
|
||||||
|
index_cache_size: int, default 256
|
||||||
|
Set the size of the index cache, specified as a number of entries
|
||||||
|
|
||||||
|
The exact meaning of an "entry" will depend on the type of index:
|
||||||
|
* IVF - there is one entry for each IVF partition
|
||||||
|
* BTREE - there is one entry for the entire index
|
||||||
|
|
||||||
|
This cache applies to the entire opened table, across all indices.
|
||||||
|
Setting this value higher will increase performance on larger datasets
|
||||||
|
at the expense of more RAM
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
A LanceTable object representing the table.
|
A LanceTable object representing the table.
|
||||||
"""
|
"""
|
||||||
table = await self._inner.open_table(name, storage_options)
|
table = await self._inner.open_table(name, storage_options, index_cache_size)
|
||||||
return AsyncTable(table)
|
return AsyncTable(table)
|
||||||
|
|
||||||
async def drop_table(self, name: str):
|
async def drop_table(self, name: str):
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ class RemoteDBConnection(DBConnection):
|
|||||||
yield item
|
yield item
|
||||||
|
|
||||||
@override
|
@override
|
||||||
def open_table(self, name: str) -> Table:
|
def open_table(self, name: str, *, index_cache_size: Optional[int] = None) -> Table:
|
||||||
"""Open a Lance Table in the database.
|
"""Open a Lance Table in the database.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@@ -110,6 +110,12 @@ class RemoteDBConnection(DBConnection):
|
|||||||
|
|
||||||
self._client.mount_retry_adapter_for_table(name)
|
self._client.mount_retry_adapter_for_table(name)
|
||||||
|
|
||||||
|
if index_cache_size is not None:
|
||||||
|
logging.info(
|
||||||
|
"index_cache_size is ignored in LanceDb Cloud"
|
||||||
|
" (there is no local cache to configure)"
|
||||||
|
)
|
||||||
|
|
||||||
# check if table exists
|
# check if table exists
|
||||||
if self._table_cache.get(name) is None:
|
if self._table_cache.get(name) is None:
|
||||||
self._client.post(f"/v1/table/{name}/describe/")
|
self._client.post(f"/v1/table/{name}/describe/")
|
||||||
@@ -281,6 +287,24 @@ class RemoteDBConnection(DBConnection):
|
|||||||
)
|
)
|
||||||
self._table_cache.pop(name)
|
self._table_cache.pop(name)
|
||||||
|
|
||||||
|
@override
|
||||||
|
def rename_table(self, cur_name: str, new_name: str):
|
||||||
|
"""Rename a table in the database.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
cur_name: str
|
||||||
|
The current name of the table.
|
||||||
|
new_name: str
|
||||||
|
The new name of the table.
|
||||||
|
"""
|
||||||
|
self._client.post(
|
||||||
|
f"/v1/table/{cur_name}/rename/",
|
||||||
|
json={"new_table_name": new_name},
|
||||||
|
)
|
||||||
|
self._table_cache.pop(cur_name)
|
||||||
|
self._table_cache[new_name] = True
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
"""Close the connection to the database."""
|
"""Close the connection to the database."""
|
||||||
self._client.close()
|
self._client.close()
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class RemoteTable(Table):
|
|||||||
return resp
|
return resp
|
||||||
|
|
||||||
def index_stats(self, index_uuid: str):
|
def index_stats(self, index_uuid: str):
|
||||||
"""List all the indices on the table"""
|
"""List all the stats of a specified index"""
|
||||||
resp = self._conn._client.post(
|
resp = self._conn._client.post(
|
||||||
f"/v1/table/{self._name}/index/{index_uuid}/stats/"
|
f"/v1/table/{self._name}/index/{index_uuid}/stats/"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import semver
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
@@ -42,6 +43,14 @@ class CohereReranker(Reranker):
|
|||||||
@cached_property
|
@cached_property
|
||||||
def _client(self):
|
def _client(self):
|
||||||
cohere = attempt_import_or_raise("cohere")
|
cohere = attempt_import_or_raise("cohere")
|
||||||
|
# ensure version is at least 0.5.0
|
||||||
|
if (
|
||||||
|
hasattr(cohere, "__version__")
|
||||||
|
and semver.compare(cohere.__version__, "5.0.0") < 0
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
f"cohere version must be at least 0.5.0, found {cohere.__version__}"
|
||||||
|
)
|
||||||
if os.environ.get("COHERE_API_KEY") is None and self.api_key is None:
|
if os.environ.get("COHERE_API_KEY") is None and self.api_key is None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"COHERE_API_KEY not set. Either set it in your environment or \
|
"COHERE_API_KEY not set. Either set it in your environment or \
|
||||||
@@ -51,11 +60,14 @@ class CohereReranker(Reranker):
|
|||||||
|
|
||||||
def _rerank(self, result_set: pa.Table, query: str):
|
def _rerank(self, result_set: pa.Table, query: str):
|
||||||
docs = result_set[self.column].to_pylist()
|
docs = result_set[self.column].to_pylist()
|
||||||
results = self._client.rerank(
|
response = self._client.rerank(
|
||||||
query=query,
|
query=query,
|
||||||
documents=docs,
|
documents=docs,
|
||||||
top_n=self.top_n,
|
top_n=self.top_n,
|
||||||
model=self.model_name,
|
model=self.model_name,
|
||||||
|
)
|
||||||
|
results = (
|
||||||
|
response.results
|
||||||
) # returns list (text, idx, relevance) attributes sorted descending by score
|
) # returns list (text, idx, relevance) attributes sorted descending by score
|
||||||
indices, scores = list(
|
indices, scores = list(
|
||||||
zip(*[(result.index, result.relevance_score) for result in results])
|
zip(*[(result.index, result.relevance_score) for result in results])
|
||||||
|
|||||||
@@ -806,6 +806,7 @@ class _LanceLatestDatasetRef(_LanceDatasetRef):
|
|||||||
"""Reference to the latest version of a LanceDataset."""
|
"""Reference to the latest version of a LanceDataset."""
|
||||||
|
|
||||||
uri: str
|
uri: str
|
||||||
|
index_cache_size: Optional[int] = None
|
||||||
read_consistency_interval: Optional[timedelta] = None
|
read_consistency_interval: Optional[timedelta] = None
|
||||||
last_consistency_check: Optional[float] = None
|
last_consistency_check: Optional[float] = None
|
||||||
_dataset: Optional[LanceDataset] = None
|
_dataset: Optional[LanceDataset] = None
|
||||||
@@ -813,7 +814,9 @@ class _LanceLatestDatasetRef(_LanceDatasetRef):
|
|||||||
@property
|
@property
|
||||||
def dataset(self) -> LanceDataset:
|
def dataset(self) -> LanceDataset:
|
||||||
if not self._dataset:
|
if not self._dataset:
|
||||||
self._dataset = lance.dataset(self.uri)
|
self._dataset = lance.dataset(
|
||||||
|
self.uri, index_cache_size=self.index_cache_size
|
||||||
|
)
|
||||||
self.last_consistency_check = time.monotonic()
|
self.last_consistency_check = time.monotonic()
|
||||||
elif self.read_consistency_interval is not None:
|
elif self.read_consistency_interval is not None:
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
@@ -842,12 +845,15 @@ class _LanceLatestDatasetRef(_LanceDatasetRef):
|
|||||||
class _LanceTimeTravelRef(_LanceDatasetRef):
|
class _LanceTimeTravelRef(_LanceDatasetRef):
|
||||||
uri: str
|
uri: str
|
||||||
version: int
|
version: int
|
||||||
|
index_cache_size: Optional[int] = None
|
||||||
_dataset: Optional[LanceDataset] = None
|
_dataset: Optional[LanceDataset] = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dataset(self) -> LanceDataset:
|
def dataset(self) -> LanceDataset:
|
||||||
if not self._dataset:
|
if not self._dataset:
|
||||||
self._dataset = lance.dataset(self.uri, version=self.version)
|
self._dataset = lance.dataset(
|
||||||
|
self.uri, version=self.version, index_cache_size=self.index_cache_size
|
||||||
|
)
|
||||||
return self._dataset
|
return self._dataset
|
||||||
|
|
||||||
@dataset.setter
|
@dataset.setter
|
||||||
@@ -884,6 +890,8 @@ class LanceTable(Table):
|
|||||||
connection: "LanceDBConnection",
|
connection: "LanceDBConnection",
|
||||||
name: str,
|
name: str,
|
||||||
version: Optional[int] = None,
|
version: Optional[int] = None,
|
||||||
|
*,
|
||||||
|
index_cache_size: Optional[int] = None,
|
||||||
):
|
):
|
||||||
self._conn = connection
|
self._conn = connection
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -892,11 +900,13 @@ class LanceTable(Table):
|
|||||||
self._ref = _LanceTimeTravelRef(
|
self._ref = _LanceTimeTravelRef(
|
||||||
uri=self._dataset_uri,
|
uri=self._dataset_uri,
|
||||||
version=version,
|
version=version,
|
||||||
|
index_cache_size=index_cache_size,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self._ref = _LanceLatestDatasetRef(
|
self._ref = _LanceLatestDatasetRef(
|
||||||
uri=self._dataset_uri,
|
uri=self._dataset_uri,
|
||||||
read_consistency_interval=connection.read_consistency_interval,
|
read_consistency_interval=connection.read_consistency_interval,
|
||||||
|
index_cache_size=index_cache_size,
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -368,6 +368,15 @@ async def test_create_exist_ok_async(tmp_path):
|
|||||||
# await db.create_table("test", schema=bad_schema, exist_ok=True)
|
# await db.create_table("test", schema=bad_schema, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def test_open_table_sync(tmp_path):
|
||||||
|
db = lancedb.connect(tmp_path)
|
||||||
|
db.create_table("test", data=[{"id": 0}])
|
||||||
|
assert db.open_table("test").count_rows() == 1
|
||||||
|
assert db.open_table("test", index_cache_size=0).count_rows() == 1
|
||||||
|
with pytest.raises(FileNotFoundError, match="does not exist"):
|
||||||
|
db.open_table("does_not_exist")
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_open_table(tmp_path):
|
async def test_open_table(tmp_path):
|
||||||
db = await lancedb.connect_async(tmp_path)
|
db = await lancedb.connect_async(tmp_path)
|
||||||
@@ -397,6 +406,10 @@ async def test_open_table(tmp_path):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# No way to verify this yet, but at least make sure we
|
||||||
|
# can pass the parameter
|
||||||
|
await db.open_table("test", index_cache_size=0)
|
||||||
|
|
||||||
with pytest.raises(ValueError, match="was not found"):
|
with pytest.raises(ValueError, match="was not found"):
|
||||||
await db.open_table("does_not_exist")
|
await db.open_table("does_not_exist")
|
||||||
|
|
||||||
|
|||||||
@@ -134,17 +134,21 @@ impl Connection {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pyo3(signature = (name, storage_options = None))]
|
#[pyo3(signature = (name, storage_options = None, index_cache_size = None))]
|
||||||
pub fn open_table(
|
pub fn open_table(
|
||||||
self_: PyRef<'_, Self>,
|
self_: PyRef<'_, Self>,
|
||||||
name: String,
|
name: String,
|
||||||
storage_options: Option<HashMap<String, String>>,
|
storage_options: Option<HashMap<String, String>>,
|
||||||
|
index_cache_size: Option<u32>,
|
||||||
) -> PyResult<&PyAny> {
|
) -> PyResult<&PyAny> {
|
||||||
let inner = self_.get_inner()?.clone();
|
let inner = self_.get_inner()?.clone();
|
||||||
let mut builder = inner.open_table(name);
|
let mut builder = inner.open_table(name);
|
||||||
if let Some(storage_options) = storage_options {
|
if let Some(storage_options) = storage_options {
|
||||||
builder = builder.storage_options(storage_options);
|
builder = builder.storage_options(storage_options);
|
||||||
}
|
}
|
||||||
|
if let Some(index_cache_size) = index_cache_size {
|
||||||
|
builder = builder.index_cache_size(index_cache_size);
|
||||||
|
}
|
||||||
future_into_py(self_.py(), async move {
|
future_into_py(self_.py(), async move {
|
||||||
let table = builder.execute().await.infer_error()?;
|
let table = builder.execute().await.infer_error()?;
|
||||||
Ok(Table::new(table))
|
Ok(Table::new(table))
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ aws-sdk-kms = { version = "1.0" }
|
|||||||
aws-config = { version = "1.0" }
|
aws-config = { version = "1.0" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["remote"]
|
default = []
|
||||||
remote = ["dep:reqwest"]
|
remote = ["dep:reqwest"]
|
||||||
fp16kernels = ["lance-linalg/fp16kernels"]
|
fp16kernels = ["lance-linalg/fp16kernels"]
|
||||||
s3-test = []
|
s3-test = []
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ use crate::table::{NativeTable, WriteOptions};
|
|||||||
use crate::utils::validate_table_name;
|
use crate::utils::validate_table_name;
|
||||||
use crate::Table;
|
use crate::Table;
|
||||||
|
|
||||||
|
#[cfg(feature = "remote")]
|
||||||
|
use log::warn;
|
||||||
|
|
||||||
pub const LANCE_FILE_EXTENSION: &str = "lance";
|
pub const LANCE_FILE_EXTENSION: &str = "lance";
|
||||||
|
|
||||||
pub type TableBuilderCallback = Box<dyn FnOnce(OpenTableBuilder) -> OpenTableBuilder + Send>;
|
pub type TableBuilderCallback = Box<dyn FnOnce(OpenTableBuilder) -> OpenTableBuilder + Send>;
|
||||||
@@ -579,6 +582,7 @@ impl ConnectBuilder {
|
|||||||
let api_key = self.api_key.ok_or_else(|| Error::InvalidInput {
|
let api_key = self.api_key.ok_or_else(|| Error::InvalidInput {
|
||||||
message: "An api_key is required when connecting to LanceDb Cloud".to_string(),
|
message: "An api_key is required when connecting to LanceDb Cloud".to_string(),
|
||||||
})?;
|
})?;
|
||||||
|
warn!("The rust implementation of the remote client is not yet ready for use.");
|
||||||
let internal = Arc::new(crate::remote::db::RemoteDatabase::try_new(
|
let internal = Arc::new(crate::remote::db::RemoteDatabase::try_new(
|
||||||
&self.uri,
|
&self.uri,
|
||||||
&api_key,
|
&api_key,
|
||||||
@@ -909,12 +913,23 @@ impl ConnectionInternal for Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Some ReadParams are exposed in the OpenTableBuilder, but we also
|
||||||
|
// let the user provide their own ReadParams.
|
||||||
|
//
|
||||||
|
// If we have a user provided ReadParams use that
|
||||||
|
// If we don't then start with the default ReadParams and customize it with
|
||||||
|
// the options from the OpenTableBuilder
|
||||||
|
let read_params = options.lance_read_params.unwrap_or_else(|| ReadParams {
|
||||||
|
index_cache_size: options.index_cache_size as usize,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
|
||||||
let native_table = Arc::new(
|
let native_table = Arc::new(
|
||||||
NativeTable::open_with_params(
|
NativeTable::open_with_params(
|
||||||
&table_uri,
|
&table_uri,
|
||||||
&options.name,
|
&options.name,
|
||||||
self.store_wrapper.clone(),
|
self.store_wrapper.clone(),
|
||||||
options.lance_read_params,
|
Some(read_params),
|
||||||
self.read_consistency_interval,
|
self.read_consistency_interval,
|
||||||
)
|
)
|
||||||
.await?,
|
.await?,
|
||||||
@@ -1032,7 +1047,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
#[ignore = "this can't pass due to https://github.com/lancedb/lancedb/issues/1019, enable it after the bug fixed"]
|
|
||||||
async fn test_open_table() {
|
async fn test_open_table() {
|
||||||
let tmp_dir = tempdir().unwrap();
|
let tmp_dir = tempdir().unwrap();
|
||||||
let uri = tmp_dir.path().to_str().unwrap();
|
let uri = tmp_dir.path().to_str().unwrap();
|
||||||
|
|||||||
@@ -46,10 +46,18 @@ impl VectorIndex {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct VectorIndexMetadata {
|
||||||
|
pub metric_type: String,
|
||||||
|
pub index_type: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct VectorIndexStatistics {
|
pub struct VectorIndexStatistics {
|
||||||
pub num_indexed_rows: usize,
|
pub num_indexed_rows: usize,
|
||||||
pub num_unindexed_rows: usize,
|
pub num_unindexed_rows: usize,
|
||||||
|
pub index_type: String,
|
||||||
|
pub indices: Vec<VectorIndexMetadata>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builder for an IVF PQ index.
|
/// Builder for an IVF PQ index.
|
||||||
|
|||||||
@@ -350,8 +350,16 @@ mod test {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_e2e() {
|
async fn test_e2e() {
|
||||||
let dir1 = tempfile::tempdir().unwrap().into_path();
|
let dir1 = tempfile::tempdir()
|
||||||
let dir2 = tempfile::tempdir().unwrap().into_path();
|
.unwrap()
|
||||||
|
.into_path()
|
||||||
|
.canonicalize()
|
||||||
|
.unwrap();
|
||||||
|
let dir2 = tempfile::tempdir()
|
||||||
|
.unwrap()
|
||||||
|
.into_path()
|
||||||
|
.canonicalize()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let secondary_store = LocalFileSystem::new_with_prefix(dir2.to_str().unwrap()).unwrap();
|
let secondary_store = LocalFileSystem::new_with_prefix(dir2.to_str().unwrap()).unwrap();
|
||||||
let object_store_wrapper = Arc::new(MirroringObjectStoreWrapper {
|
let object_store_wrapper = Arc::new(MirroringObjectStoreWrapper {
|
||||||
|
|||||||
@@ -34,6 +34,16 @@
|
|||||||
//! cargo install lancedb
|
//! cargo install lancedb
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
//! ## Crate Features
|
||||||
|
//!
|
||||||
|
//! ### Experimental Features
|
||||||
|
//!
|
||||||
|
//! These features are not enabled by default. They are experimental or in-development features that
|
||||||
|
//! are not yet ready to be released.
|
||||||
|
//!
|
||||||
|
//! - `remote` - Enable remote client to connect to LanceDB cloud. This is not yet fully implemented
|
||||||
|
//! and should not be enabled.
|
||||||
|
//!
|
||||||
//! ### Quick Start
|
//! ### Quick Start
|
||||||
//!
|
//!
|
||||||
//! #### Connect to a database.
|
//! #### Connect to a database.
|
||||||
|
|||||||
@@ -1061,6 +1061,26 @@ impl NativeTable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_index_type(&self, index_uuid: &str) -> Result<Option<String>> {
|
||||||
|
match self.load_index_stats(index_uuid).await? {
|
||||||
|
Some(stats) => Ok(Some(stats.index_type)),
|
||||||
|
None => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_distance_type(&self, index_uuid: &str) -> Result<Option<String>> {
|
||||||
|
match self.load_index_stats(index_uuid).await? {
|
||||||
|
Some(stats) => Ok(Some(
|
||||||
|
stats
|
||||||
|
.indices
|
||||||
|
.iter()
|
||||||
|
.map(|i| i.metric_type.clone())
|
||||||
|
.collect(),
|
||||||
|
)),
|
||||||
|
None => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn load_indices(&self) -> Result<Vec<VectorIndex>> {
|
pub async fn load_indices(&self) -> Result<Vec<VectorIndex>> {
|
||||||
let dataset = self.dataset.get().await?;
|
let dataset = self.dataset.get().await?;
|
||||||
let (indices, mf) = futures::try_join!(dataset.load_indices(), dataset.latest_manifest())?;
|
let (indices, mf) = futures::try_join!(dataset.load_indices(), dataset.latest_manifest())?;
|
||||||
|
|||||||
Reference in New Issue
Block a user