sie-lancedb 0.1.10__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sie_lancedb-0.1.10/.gitignore +259 -0
- sie_lancedb-0.1.10/PKG-INFO +12 -0
- sie_lancedb-0.1.10/pyproject.toml +32 -0
- sie_lancedb-0.1.10/src/sie_lancedb/__init__.py +70 -0
- sie_lancedb-0.1.10/src/sie_lancedb/embeddings.py +276 -0
- sie_lancedb-0.1.10/src/sie_lancedb/extractors.py +238 -0
- sie_lancedb-0.1.10/src/sie_lancedb/rerankers.py +181 -0
- sie_lancedb-0.1.10/tests/__init__.py +0 -0
- sie_lancedb-0.1.10/tests/conftest.py +206 -0
- sie_lancedb-0.1.10/tests/test_embeddings.py +328 -0
- sie_lancedb-0.1.10/tests/test_extractors.py +249 -0
- sie_lancedb-0.1.10/tests/test_integration.py +201 -0
- sie_lancedb-0.1.10/tests/test_rerankers.py +186 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.venv
|
|
140
|
+
env/
|
|
141
|
+
venv/
|
|
142
|
+
ENV/
|
|
143
|
+
env.bak/
|
|
144
|
+
venv.bak/
|
|
145
|
+
|
|
146
|
+
# Spyder project settings
|
|
147
|
+
.spyderproject
|
|
148
|
+
.spyproject
|
|
149
|
+
|
|
150
|
+
# Rope project settings
|
|
151
|
+
.ropeproject
|
|
152
|
+
|
|
153
|
+
# mkdocs documentation
|
|
154
|
+
/site
|
|
155
|
+
|
|
156
|
+
# Pyre type checker
|
|
157
|
+
.pyre/
|
|
158
|
+
|
|
159
|
+
# pytype static type analyzer
|
|
160
|
+
.pytype/
|
|
161
|
+
|
|
162
|
+
# Cython debug symbols
|
|
163
|
+
cython_debug/
|
|
164
|
+
|
|
165
|
+
# PyCharm
|
|
166
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
167
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
168
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
169
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
170
|
+
#.idea/
|
|
171
|
+
|
|
172
|
+
# Abstra
|
|
173
|
+
# Abstra is an AI-powered process automation framework.
|
|
174
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
175
|
+
# Learn more at https://abstra.io/docs
|
|
176
|
+
.abstra/
|
|
177
|
+
|
|
178
|
+
# Visual Studio Code
|
|
179
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
180
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
181
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
182
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
183
|
+
# .vscode/
|
|
184
|
+
|
|
185
|
+
# Ruff stuff:
|
|
186
|
+
.ruff_cache/
|
|
187
|
+
|
|
188
|
+
# PyPI configuration file
|
|
189
|
+
.pypirc
|
|
190
|
+
|
|
191
|
+
# Cursor
|
|
192
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
193
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
194
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
195
|
+
.cursorignore
|
|
196
|
+
.cursorindexingignore
|
|
197
|
+
|
|
198
|
+
# vcscode
|
|
199
|
+
.vscode
|
|
200
|
+
|
|
201
|
+
# Marimo
|
|
202
|
+
marimo/_static/
|
|
203
|
+
marimo/_lsp/
|
|
204
|
+
__marimo__/
|
|
205
|
+
|
|
206
|
+
# SIE specific
|
|
207
|
+
# Model weights cache
|
|
208
|
+
.cache/
|
|
209
|
+
*.safetensors
|
|
210
|
+
*.bin
|
|
211
|
+
|
|
212
|
+
# Secrets (never commit)
|
|
213
|
+
*.pem
|
|
214
|
+
*.key
|
|
215
|
+
credentials.json
|
|
216
|
+
*-key.json
|
|
217
|
+
|
|
218
|
+
# Terraform
|
|
219
|
+
# Local .terraform directories (cached providers/modules)
|
|
220
|
+
**/.terraform/*
|
|
221
|
+
.terraform.lock*
|
|
222
|
+
# State files (contain sensitive data)
|
|
223
|
+
*.tfstate
|
|
224
|
+
*.tfstate.*
|
|
225
|
+
# Crash log files
|
|
226
|
+
crash.log
|
|
227
|
+
crash.*.log
|
|
228
|
+
# Override files (local developer overrides)
|
|
229
|
+
override.tf
|
|
230
|
+
override.tf.json
|
|
231
|
+
*_override.tf
|
|
232
|
+
*_override.tf.json
|
|
233
|
+
# tfvars files may contain secrets
|
|
234
|
+
*.tfvars
|
|
235
|
+
*.tfvars.json
|
|
236
|
+
# Keep .terraform.lock.hcl for reproducibility (provider versions)
|
|
237
|
+
|
|
238
|
+
# Node.js
|
|
239
|
+
node_modules/
|
|
240
|
+
|
|
241
|
+
# OS
|
|
242
|
+
.DS_Store
|
|
243
|
+
Thumbs.db
|
|
244
|
+
|
|
245
|
+
# VIM
|
|
246
|
+
*.swp
|
|
247
|
+
|
|
248
|
+
# kilocode
|
|
249
|
+
.kilo/
|
|
250
|
+
|
|
251
|
+
# Worktree metadata
|
|
252
|
+
.base-branch
|
|
253
|
+
|
|
254
|
+
# Temporary files
|
|
255
|
+
tmp/
|
|
256
|
+
.tmp/
|
|
257
|
+
.local/
|
|
258
|
+
|
|
259
|
+
.requirements-modal.txt
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sie-lancedb
|
|
3
|
+
Version: 0.1.10
|
|
4
|
+
Summary: SIE integration for LanceDB
|
|
5
|
+
Requires-Python: >=3.12
|
|
6
|
+
Requires-Dist: lancedb>=0.17
|
|
7
|
+
Requires-Dist: pyarrow>=14.0
|
|
8
|
+
Requires-Dist: pylance>=0.20
|
|
9
|
+
Requires-Dist: sie-sdk>=0.1.0
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
|
|
12
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "sie-lancedb"
|
|
3
|
+
version = "0.1.10"
|
|
4
|
+
description = "SIE integration for LanceDB"
|
|
5
|
+
requires-python = ">=3.12"
|
|
6
|
+
dependencies = [
|
|
7
|
+
"sie-sdk>=0.1.0",
|
|
8
|
+
"lancedb>=0.17",
|
|
9
|
+
"pylance>=0.20",
|
|
10
|
+
"pyarrow>=14.0",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
[project.optional-dependencies]
|
|
14
|
+
dev = [
|
|
15
|
+
"pytest>=8.0",
|
|
16
|
+
"pytest-asyncio>=0.24",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["hatchling"]
|
|
21
|
+
build-backend = "hatchling.build"
|
|
22
|
+
|
|
23
|
+
[tool.hatch.build.targets.wheel]
|
|
24
|
+
packages = ["src/sie_lancedb"]
|
|
25
|
+
|
|
26
|
+
[tool.pytest.ini_options]
|
|
27
|
+
testpaths = ["tests"]
|
|
28
|
+
asyncio_mode = "auto"
|
|
29
|
+
markers = [
|
|
30
|
+
"integration: marks tests as integration tests (require running SIE server and LanceDB)",
|
|
31
|
+
]
|
|
32
|
+
addopts = "-v --tb=short -m 'not integration'"
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""SIE integration for LanceDB.
|
|
2
|
+
|
|
3
|
+
This package provides LanceDB embedding functions, rerankers, and extractors
|
|
4
|
+
that use SIE for inference:
|
|
5
|
+
|
|
6
|
+
Embedding Functions (registered in LanceDB's embedding function registry):
|
|
7
|
+
- SIEEmbeddingFunction: Dense embeddings via the "sie" registry name
|
|
8
|
+
- SIEMultiVectorEmbeddingFunction: ColBERT/ColPali multi-vector via "sie-multivector"
|
|
9
|
+
|
|
10
|
+
Rerankers:
|
|
11
|
+
- SIEReranker: Cross-encoder reranking for hybrid search pipelines
|
|
12
|
+
|
|
13
|
+
Extractors:
|
|
14
|
+
- SIEExtractor: Entity extraction with table enrichment support
|
|
15
|
+
|
|
16
|
+
Example usage with embedding function registry:
|
|
17
|
+
|
|
18
|
+
import lancedb
|
|
19
|
+
from lancedb.embeddings import get_registry
|
|
20
|
+
from lancedb.pydantic import LanceModel, Vector
|
|
21
|
+
import sie_lancedb # registers "sie" and "sie-multivector"
|
|
22
|
+
|
|
23
|
+
sie = get_registry().get("sie").create(model="BAAI/bge-m3")
|
|
24
|
+
|
|
25
|
+
class Documents(LanceModel):
|
|
26
|
+
text: str = sie.SourceField()
|
|
27
|
+
vector: Vector(sie.ndims()) = sie.VectorField()
|
|
28
|
+
|
|
29
|
+
db = lancedb.connect("~/.lancedb")
|
|
30
|
+
table = db.create_table("docs", schema=Documents)
|
|
31
|
+
table.add([{"text": "hello world"}]) # auto-embeds
|
|
32
|
+
results = table.search("hello").limit(5).to_list() # auto-embeds query
|
|
33
|
+
|
|
34
|
+
Example usage with reranker:
|
|
35
|
+
|
|
36
|
+
from sie_lancedb import SIEReranker
|
|
37
|
+
|
|
38
|
+
table.create_fts_index("text")
|
|
39
|
+
results = (
|
|
40
|
+
table.search("hello", query_type="hybrid")
|
|
41
|
+
.rerank(SIEReranker(model="jinaai/jina-reranker-v2-base-multilingual"))
|
|
42
|
+
.limit(10)
|
|
43
|
+
.to_list()
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
Example usage with extractor:
|
|
47
|
+
|
|
48
|
+
from sie_lancedb import SIEExtractor
|
|
49
|
+
|
|
50
|
+
extractor = SIEExtractor(model="urchade/gliner_multi-v2.1")
|
|
51
|
+
extractor.enrich_table(
|
|
52
|
+
table,
|
|
53
|
+
source_column="text",
|
|
54
|
+
target_column="entities",
|
|
55
|
+
labels=["person", "organization", "location"],
|
|
56
|
+
id_column="id",
|
|
57
|
+
)
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
from sie_lancedb.embeddings import SIEEmbeddingFunction, SIEMultiVectorEmbeddingFunction
|
|
61
|
+
from sie_lancedb.extractors import ENTITY_STRUCT, SIEExtractor
|
|
62
|
+
from sie_lancedb.rerankers import SIEReranker
|
|
63
|
+
|
|
64
|
+
__all__ = [
|
|
65
|
+
"ENTITY_STRUCT",
|
|
66
|
+
"SIEEmbeddingFunction",
|
|
67
|
+
"SIEExtractor",
|
|
68
|
+
"SIEMultiVectorEmbeddingFunction",
|
|
69
|
+
"SIEReranker",
|
|
70
|
+
]
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"""SIE embedding functions for LanceDB.
|
|
2
|
+
|
|
3
|
+
Provides LanceDB-native embedding functions registered in the global registry:
|
|
4
|
+
- SIEEmbeddingFunction: Dense text embeddings via "sie"
|
|
5
|
+
- SIEMultiVectorEmbeddingFunction: ColBERT/ColPali multi-vector via "sie-multivector"
|
|
6
|
+
|
|
7
|
+
These integrate with LanceDB's auto-embedding system: embeddings are computed
|
|
8
|
+
automatically on table.add() and table.search() when the schema uses
|
|
9
|
+
SourceField/VectorField annotations.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
from lancedb.embeddings import EmbeddingFunction, TextEmbeddingFunction, register
|
|
18
|
+
from pydantic import PrivateAttr
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@register("sie")
|
|
22
|
+
class SIEEmbeddingFunction(TextEmbeddingFunction):
|
|
23
|
+
"""Dense text embeddings via SIE, registered as "sie" in LanceDB.
|
|
24
|
+
|
|
25
|
+
Wraps SIEClient.encode() with output_types=["dense"] to implement
|
|
26
|
+
LanceDB's TextEmbeddingFunction interface. Embeddings are computed
|
|
27
|
+
automatically when using SourceField/VectorField annotations.
|
|
28
|
+
|
|
29
|
+
Example:
|
|
30
|
+
>>> from lancedb.embeddings import get_registry
|
|
31
|
+
>>> from lancedb.pydantic import LanceModel, Vector
|
|
32
|
+
>>> import sie_lancedb # registers "sie"
|
|
33
|
+
>>>
|
|
34
|
+
>>> sie = (
|
|
35
|
+
... get_registry()
|
|
36
|
+
... .get("sie")
|
|
37
|
+
... .create(
|
|
38
|
+
... model="BAAI/bge-m3",
|
|
39
|
+
... base_url="http://localhost:8080",
|
|
40
|
+
... )
|
|
41
|
+
... )
|
|
42
|
+
>>>
|
|
43
|
+
>>> class Documents(LanceModel):
|
|
44
|
+
... text: str = sie.SourceField()
|
|
45
|
+
... vector: Vector(sie.ndims()) = sie.VectorField()
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
base_url: URL of the SIE server.
|
|
49
|
+
model: Model name/ID to use for encoding.
|
|
50
|
+
instruction: Optional instruction prefix for embedding (model-dependent).
|
|
51
|
+
output_dtype: Output dtype: "float32" (default), "float16", "int8", "binary".
|
|
52
|
+
options: Runtime options dict passed to the model adapter.
|
|
53
|
+
gpu: Target GPU type for routing (e.g., "l4", "a100-80gb").
|
|
54
|
+
timeout_s: Request timeout in seconds.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
base_url: str = "http://localhost:8080"
|
|
58
|
+
model: str = "BAAI/bge-m3"
|
|
59
|
+
instruction: str | None = None
|
|
60
|
+
output_dtype: str | None = None
|
|
61
|
+
options: dict[str, Any] | None = None
|
|
62
|
+
gpu: str | None = None
|
|
63
|
+
timeout_s: float = 180.0
|
|
64
|
+
|
|
65
|
+
_sie_client: Any = PrivateAttr(default=None)
|
|
66
|
+
_ndims: int | None = PrivateAttr(default=None)
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def client(self) -> Any:
|
|
70
|
+
"""Lazily initialize the SIE client."""
|
|
71
|
+
if self._sie_client is None:
|
|
72
|
+
from sie_sdk import SIEClient
|
|
73
|
+
|
|
74
|
+
self._sie_client = SIEClient(
|
|
75
|
+
self.base_url,
|
|
76
|
+
timeout_s=self.timeout_s,
|
|
77
|
+
gpu=self.gpu,
|
|
78
|
+
options=self.options,
|
|
79
|
+
)
|
|
80
|
+
return self._sie_client
|
|
81
|
+
|
|
82
|
+
def ndims(self) -> int:
|
|
83
|
+
"""Return embedding dimensionality.
|
|
84
|
+
|
|
85
|
+
Queries ``GET /v1/models/{model}`` on first call (lightweight,
|
|
86
|
+
reads from model config — no model loading or inference).
|
|
87
|
+
Cached after the first call per LanceDB's base class convention.
|
|
88
|
+
"""
|
|
89
|
+
if self._ndims is None:
|
|
90
|
+
info = self.client.get_model(self.model)
|
|
91
|
+
dims = info.get("dims") or {}
|
|
92
|
+
if "dense" not in dims:
|
|
93
|
+
msg = f"Model '{self.model}' does not support dense embeddings"
|
|
94
|
+
raise ValueError(msg)
|
|
95
|
+
self._ndims = dims["dense"]
|
|
96
|
+
return self._ndims
|
|
97
|
+
|
|
98
|
+
def generate_embeddings(
|
|
99
|
+
self,
|
|
100
|
+
texts: list[str] | np.ndarray,
|
|
101
|
+
) -> list[np.ndarray | None]:
|
|
102
|
+
"""Generate dense embeddings for a list of texts.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
texts: Texts to embed.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
List of embedding arrays, one per text.
|
|
109
|
+
"""
|
|
110
|
+
if not len(texts):
|
|
111
|
+
return []
|
|
112
|
+
|
|
113
|
+
from sie_sdk.types import Item
|
|
114
|
+
|
|
115
|
+
items = [Item(text=t) for t in texts]
|
|
116
|
+
results = self.client.encode(
|
|
117
|
+
self.model,
|
|
118
|
+
items,
|
|
119
|
+
output_types=["dense"],
|
|
120
|
+
instruction=self.instruction,
|
|
121
|
+
output_dtype=self.output_dtype,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
return [self._extract_dense(result) for result in results]
|
|
125
|
+
|
|
126
|
+
def _extract_dense(self, result: Any) -> list[float]:
|
|
127
|
+
"""Extract dense embedding from encode result."""
|
|
128
|
+
dense = result.get("dense") if isinstance(result, dict) else getattr(result, "dense", None)
|
|
129
|
+
if dense is None:
|
|
130
|
+
msg = "Encode result missing dense embedding"
|
|
131
|
+
raise ValueError(msg)
|
|
132
|
+
return dense.tolist() if hasattr(dense, "tolist") else list(dense)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@register("sie-multivector")
|
|
136
|
+
class SIEMultiVectorEmbeddingFunction(EmbeddingFunction):
|
|
137
|
+
"""Multi-vector embeddings via SIE, registered as "sie-multivector" in LanceDB.
|
|
138
|
+
|
|
139
|
+
Wraps SIEClient.encode() with output_types=["multivector"] for ColBERT
|
|
140
|
+
and ColPali models. Works with LanceDB's MultiVector type and native
|
|
141
|
+
MaxSim scoring.
|
|
142
|
+
|
|
143
|
+
Example:
|
|
144
|
+
>>> from lancedb.embeddings import get_registry
|
|
145
|
+
>>> from lancedb.pydantic import LanceModel, MultiVector
|
|
146
|
+
>>> import sie_lancedb # registers "sie-multivector"
|
|
147
|
+
>>>
|
|
148
|
+
>>> sie_colbert = (
|
|
149
|
+
... get_registry()
|
|
150
|
+
... .get("sie-multivector")
|
|
151
|
+
... .create(
|
|
152
|
+
... model="jinaai/jina-colbert-v2",
|
|
153
|
+
... base_url="http://localhost:8080",
|
|
154
|
+
... )
|
|
155
|
+
... )
|
|
156
|
+
>>>
|
|
157
|
+
>>> class Documents(LanceModel):
|
|
158
|
+
... text: str = sie_colbert.SourceField()
|
|
159
|
+
... vector: MultiVector(sie_colbert.ndims()) = sie_colbert.VectorField()
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
base_url: URL of the SIE server.
|
|
163
|
+
model: Multi-vector model name/ID (e.g., ColBERT, ColPali).
|
|
164
|
+
instruction: Optional instruction prefix for embedding.
|
|
165
|
+
options: Runtime options dict passed to the model adapter.
|
|
166
|
+
gpu: Target GPU type for routing (e.g., "l4", "a100-80gb").
|
|
167
|
+
timeout_s: Request timeout in seconds.
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
base_url: str = "http://localhost:8080"
|
|
171
|
+
model: str = "jinaai/jina-colbert-v2"
|
|
172
|
+
instruction: str | None = None
|
|
173
|
+
options: dict[str, Any] | None = None
|
|
174
|
+
gpu: str | None = None
|
|
175
|
+
timeout_s: float = 180.0
|
|
176
|
+
|
|
177
|
+
_sie_client: Any = PrivateAttr(default=None)
|
|
178
|
+
_ndims: int | None = PrivateAttr(default=None)
|
|
179
|
+
|
|
180
|
+
@property
|
|
181
|
+
def client(self) -> Any:
|
|
182
|
+
"""Lazily initialize the SIE client."""
|
|
183
|
+
if self._sie_client is None:
|
|
184
|
+
from sie_sdk import SIEClient
|
|
185
|
+
|
|
186
|
+
self._sie_client = SIEClient(
|
|
187
|
+
self.base_url,
|
|
188
|
+
timeout_s=self.timeout_s,
|
|
189
|
+
gpu=self.gpu,
|
|
190
|
+
options=self.options,
|
|
191
|
+
)
|
|
192
|
+
return self._sie_client
|
|
193
|
+
|
|
194
|
+
def ndims(self) -> int:
|
|
195
|
+
"""Return per-token embedding dimensionality.
|
|
196
|
+
|
|
197
|
+
Queries ``GET /v1/models/{model}`` on first call (lightweight,
|
|
198
|
+
reads from model config — no model loading or inference).
|
|
199
|
+
Cached after the first call per LanceDB's base class convention.
|
|
200
|
+
"""
|
|
201
|
+
if self._ndims is None:
|
|
202
|
+
info = self.client.get_model(self.model)
|
|
203
|
+
dims = info.get("dims") or {}
|
|
204
|
+
if "multivector" not in dims:
|
|
205
|
+
msg = f"Model '{self.model}' does not support multivector embeddings"
|
|
206
|
+
raise ValueError(msg)
|
|
207
|
+
self._ndims = dims["multivector"]
|
|
208
|
+
return self._ndims
|
|
209
|
+
|
|
210
|
+
def compute_source_embeddings(
|
|
211
|
+
self,
|
|
212
|
+
*args: Any,
|
|
213
|
+
**kwargs: Any, # noqa: ARG002
|
|
214
|
+
) -> list[list[list[float]] | None]:
|
|
215
|
+
"""Compute multi-vector embeddings for source documents.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
*args: First positional arg is texts (str, list, pa.Array, etc.).
|
|
219
|
+
|
|
220
|
+
Returns:
|
|
221
|
+
List of 2D embeddings (tokens x dims), one per document.
|
|
222
|
+
"""
|
|
223
|
+
texts = self.sanitize_input(args[0])
|
|
224
|
+
|
|
225
|
+
from sie_sdk.types import Item
|
|
226
|
+
|
|
227
|
+
items = [Item(text=t) for t in texts]
|
|
228
|
+
results = self.client.encode(
|
|
229
|
+
self.model,
|
|
230
|
+
items,
|
|
231
|
+
output_types=["multivector"],
|
|
232
|
+
instruction=self.instruction,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
return [self._extract_multivector(result) for result in results]
|
|
236
|
+
|
|
237
|
+
def compute_query_embeddings(
|
|
238
|
+
self,
|
|
239
|
+
*args: Any,
|
|
240
|
+
**kwargs: Any, # noqa: ARG002
|
|
241
|
+
) -> list[list[list[float]] | None]:
|
|
242
|
+
"""Compute multi-vector embeddings for queries.
|
|
243
|
+
|
|
244
|
+
Passes is_query=True for models that differentiate query/document encoding.
|
|
245
|
+
|
|
246
|
+
Args:
|
|
247
|
+
*args: First positional arg is query text(s).
|
|
248
|
+
|
|
249
|
+
Returns:
|
|
250
|
+
List of 2D embeddings (tokens x dims), one per query.
|
|
251
|
+
"""
|
|
252
|
+
query = args[0]
|
|
253
|
+
if isinstance(query, str):
|
|
254
|
+
query = [query]
|
|
255
|
+
texts = self.sanitize_input(query)
|
|
256
|
+
|
|
257
|
+
from sie_sdk.types import Item
|
|
258
|
+
|
|
259
|
+
items = [Item(text=t) for t in texts]
|
|
260
|
+
results = self.client.encode(
|
|
261
|
+
self.model,
|
|
262
|
+
items,
|
|
263
|
+
output_types=["multivector"],
|
|
264
|
+
instruction=self.instruction,
|
|
265
|
+
options={"is_query": True},
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
return [self._extract_multivector(result) for result in results]
|
|
269
|
+
|
|
270
|
+
def _extract_multivector(self, result: Any) -> list[list[float]]:
|
|
271
|
+
"""Extract multi-vector embedding from encode result."""
|
|
272
|
+
mv = result.get("multivector") if isinstance(result, dict) else getattr(result, "multivector", None)
|
|
273
|
+
if mv is None:
|
|
274
|
+
msg = "Encode result missing multivector embedding"
|
|
275
|
+
raise ValueError(msg)
|
|
276
|
+
return mv.tolist() if hasattr(mv, "tolist") else list(mv)
|