mirror of
https://github.com/lancedb/lancedb.git
synced 2026-08-27 16:38:31 +00:00
5a1015ba72
`docs/src/python/python.md` is the whole Python API reference, but it is maintained by hand and had drifted from the public API. Anything not listed there simply doesn't get rendered, so a number of public, documented, tested APIs were invisible to users — most notably branch management, where `diff` and `merge` live. I audited every public symbol reachable from `lancedb` and its subpackages against the `:::` directives on the page. This adds the missing ones: - **Branching** — `Branches`, `AsyncBranches` (`list` / `create` / `checkout` / `delete` / `diff` / `merge`) - **Tables** — `TableStatistics` (returned by `Table.stats()`; the fragment-level stats classes were already listed) - **Full text queries** — `FullTextQuery`, `MatchQuery`, `PhraseQuery`, `BoostQuery`, `MultiMatchQuery`, `BooleanQuery`, `FullTextOperator`, `Occur` - **Querying** — `LanceEmptyQueryBuilder`, `LanceTakeQueryBuilder`, `AsyncTakeQuery` - **Indices** — `Fm` (the FM-index for substring search), `IndexConfig` - **Blobs** — `blob`, `BlobType`, `BlobFile` - **Namespaces** — `connect_namespace`, `connect_namespace_async`, and both namespace connection classes - **Remote config** — `TlsConfig`, `HeaderProvider`, `OAuthConfig`, `OAuthFlowType` - **Rerankers** — the `Reranker` base class plus `JinaReranker`, `RRFReranker`, `MRRReranker`, `AnswerdotaiRerankers`, `VoyageAIReranker`, `WatsonxReranker` (5 of 12 were listed) - **Embeddings** — `get_registry`, `register`, and the 14 embedding functions that were missing (3 of 17 were listed) - **PyTorch** — `StreamingDataset` and the permutation API it is built on - **Misc** — `Session`, `tokenize`, `FtsToken`, `pydantic.Vector`, `pydantic.MultiVector`, `instrument_lancedb_metrics`, and the two exception types It also repairs cross-references in docstrings that no longer resolve: links into guide pages that have since moved to lancedb.com (`querying-an-ann-index`, `experimental-full-text-search`), `lance.dataset` references with no inventory behind them, and the relative targets `[Table](Table)` and `[PyArrow Table](pyarrow.Table)`. Deliberately left out: concrete implementation classes reached through their abstract base (`LanceTable`, `LanceDBConnection`, `RemoteDBConnection`), query base classes already covered by `inherited_members: true`, and internal plumbing such as `FullTextSearchQuery` and `ColumnOrdering`. ## Testing The docs job only runs on pushes to `main`, so I built the site locally and compared against a build of `upstream/main`: every added entry resolves, and no symbol that was rendered before stopped being rendered when the four packages moved to automodule. `mkdocs build --strict` exits 0 on this branch, against 61 warnings on `main`. ## Also in this PR `lancedb.index`, `lancedb.embeddings`, `lancedb.remote` and `lancedb.rerankers` are now rendered by a single mkdocstrings directive each, driven by the module's `__all__`, rather than a hand-maintained list. These four are where most of the drift was, and `__all__` is harder to forget than a docs page. `lancedb.embeddings` had no `__all__`; without one mkdocstrings renders no members at all for a re-export package, so one is added. AGENTS.md gains a section on how the page is wired up and how to build the docs locally. Rendering all that code for the first time surfaced ~100 more build warnings, which would have made #3707 (turning on `mkdocs build --strict`) harder to land, so the warning backlog is cleared here too. 97 of the 158 warnings were one systematic false positive — griffe cannot see the generated `__init__` of a pydantic dataclass, so every documented parameter looks unknown — switched off via `warn_unknown_params`. The remaining 61 came from 15 docstrings with real bugs: prose trailing a `Parameters` section (we were rendering parameters called `The`, `you` and `To`), types dropped because numpydoc needs spaces around the colon, `num_partitions, default sqrt(num_rows)` parsing as a list of names and inventing a `default` parameter, and one parameter indented five spaces. `mkdocs build --strict` now exits 0. --- #3747 (the coverage test that keeps this from happening again) is stacked on this branch, so review it after this one. --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
178 lines
6.3 KiB
Python
178 lines
6.3 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
|
|
|
|
import os
|
|
from functools import cached_property
|
|
from typing import List, Optional, Union
|
|
|
|
import numpy as np
|
|
|
|
from lancedb.pydantic import PYDANTIC_VERSION
|
|
|
|
from ..util import attempt_import_or_raise
|
|
from .base import TextEmbeddingFunction
|
|
from .registry import register
|
|
from .utils import TEXT, api_key_not_found_help
|
|
|
|
EMBEDDING_BATCH_SIZE = 100
|
|
|
|
|
|
@register("gemini-text")
|
|
class GeminiText(TextEmbeddingFunction):
|
|
"""
|
|
An embedding function that uses Google's Gemini API. Requires GOOGLE_API_KEY to
|
|
be set.
|
|
|
|
https://ai.google.dev/gemini-api/docs/embeddings
|
|
|
|
Supports various tasks types:
|
|
| Task Type | Description |
|
|
|-------------------------|--------------------------------------------------------|
|
|
| "`retrieval_query`" | Specifies the given text is a query in a |
|
|
| | search/retrieval setting. |
|
|
| "`retrieval_document`" | Specifies the given text is a document in a |
|
|
| | search/retrieval setting. Using this task type |
|
|
| | requires a title but is automatically provided by |
|
|
| | Embeddings API |
|
|
| "`semantic_similarity`" | Specifies the given text will be used for Semantic |
|
|
| | Textual Similarity (STS). |
|
|
| "`classification`" | Specifies that the embeddings will be used for |
|
|
| | classification. |
|
|
| "`clustering`" | Specifies that the embeddings will be used for |
|
|
| | clustering. |
|
|
|
|
Note: The supported task types might change in the Gemini API, but as long as a
|
|
supported task type and its argument set is provided, those will be delegated
|
|
to the API calls.
|
|
|
|
Parameters
|
|
----------
|
|
name : str, default "gemini-embedding-001"
|
|
The name of the model to use. Supported models include:
|
|
- "gemini-embedding-001" (768 dimensions)
|
|
|
|
Note: The legacy "models/embedding-001" format is also supported but
|
|
"gemini-embedding-001" is recommended.
|
|
|
|
query_task_type : str, default "retrieval_query"
|
|
Sets the task type for the queries.
|
|
source_task_type : str, default "retrieval_document"
|
|
Sets the task type for ingestion.
|
|
|
|
Examples
|
|
--------
|
|
import lancedb
|
|
import pandas as pd
|
|
from lancedb.pydantic import LanceModel, Vector
|
|
from lancedb.embeddings import get_registry
|
|
|
|
model = get_registry().get("gemini-text").create()
|
|
|
|
class TextModel(LanceModel):
|
|
text: str = model.SourceField()
|
|
vector: Vector(model.ndims()) = model.VectorField()
|
|
|
|
df = pd.DataFrame({"text": ["hello world", "goodbye world"]})
|
|
db = lancedb.connect("~/.lancedb")
|
|
tbl = db.create_table("test", schema=TextModel, mode="overwrite")
|
|
|
|
tbl.add(df)
|
|
rs = tbl.search("hello").limit(1).to_pandas()
|
|
|
|
"""
|
|
|
|
name: str = "gemini-embedding-001"
|
|
dim: Optional[int] = None
|
|
query_task_type: str = "retrieval_query"
|
|
source_task_type: str = "retrieval_document"
|
|
|
|
if PYDANTIC_VERSION.major < 2: # Pydantic 1.x compat
|
|
|
|
class Config:
|
|
keep_untouched = (cached_property,)
|
|
else:
|
|
model_config = dict()
|
|
model_config["ignored_types"] = (cached_property,)
|
|
|
|
def ndims(self):
|
|
if self.dim:
|
|
return self.dim
|
|
# TODO: fix hardcoding
|
|
return 768
|
|
|
|
def compute_query_embeddings(self, query: str, *args, **kwargs) -> List[np.array]:
|
|
return self.compute_source_embeddings(query, task_type=self.query_task_type)
|
|
|
|
def compute_source_embeddings(self, texts: TEXT, *args, **kwargs) -> List[np.array]:
|
|
texts = self.sanitize_input(texts)
|
|
task_type = (
|
|
kwargs.get("task_type") or self.source_task_type
|
|
) # assume source task type if not passed by `compute_query_embeddings`
|
|
return self.generate_embeddings(texts, task_type=task_type)
|
|
|
|
def generate_embeddings(
|
|
self, texts: Union[List[str], np.ndarray], *args, **kwargs
|
|
) -> List[np.array]:
|
|
"""
|
|
Get the embeddings for the given texts
|
|
|
|
Parameters
|
|
----------
|
|
texts: list[str] or np.ndarray (of str)
|
|
The texts to embed
|
|
"""
|
|
from google.genai import types
|
|
|
|
task_type = kwargs.get("task_type")
|
|
|
|
# Build content objects for embed_content
|
|
contents = []
|
|
for text in texts:
|
|
if task_type == "retrieval_document":
|
|
# Provide a title for retrieval_document task
|
|
contents.append(
|
|
{"parts": [{"text": "Embedding of a document"}, {"text": text}]}
|
|
)
|
|
else:
|
|
contents.append({"parts": [{"text": text}]})
|
|
|
|
# Build config
|
|
config_kwargs = {"output_dimensionality": self.ndims()}
|
|
if task_type:
|
|
config_kwargs["task_type"] = task_type.upper() # API expects uppercase
|
|
|
|
config = types.EmbedContentConfig(**config_kwargs) if config_kwargs else None
|
|
|
|
# Call embed_content in groups of at most EMBEDDING_BATCH_SIZE docs at a time
|
|
embeddings = []
|
|
for i in range(0, len(contents), EMBEDDING_BATCH_SIZE):
|
|
chunk = contents[i : i + EMBEDDING_BATCH_SIZE]
|
|
response = self.client.models.embed_content(
|
|
model=self.name,
|
|
contents=chunk,
|
|
config=config,
|
|
)
|
|
embeddings.extend([np.array(e.values) for e in response.embeddings])
|
|
|
|
return embeddings
|
|
|
|
@cached_property
|
|
def client(self):
|
|
attempt_import_or_raise("google.genai", "google-genai")
|
|
|
|
if not os.environ.get("GOOGLE_API_KEY"):
|
|
api_key_not_found_help("google")
|
|
|
|
from google import genai as genai_module
|
|
from lancedb import __version__
|
|
|
|
return genai_module.Client(
|
|
api_key=os.environ.get("GOOGLE_API_KEY"),
|
|
http_options={
|
|
"headers": {
|
|
"x-goog-api-client": f"lancedb/{__version__}",
|
|
}
|
|
},
|
|
)
|