forked from phoenix-oss/llama-stack-mirror
# What does this PR do? This PR introduces support for keyword based FTS5 search with BM25 relevance scoring. It makes changes to the existing EmbeddingIndex base class in order to support a search_mode and query_str parameter, that can be used for keyword based search implementations. [//]: # (If resolving an issue, uncomment and update the line below) [//]: # (Closes #[issue-number]) ## Test Plan run ``` pytest llama_stack/providers/tests/vector_io/test_sqlite_vec.py -v -s --tb=short --disable-warnings --asyncio-mode=auto ``` Output: ``` pytest llama_stack/providers/tests/vector_io/test_sqlite_vec.py -v -s --tb=short --disable-warnings --asyncio-mode=auto /Users/vnarsing/miniconda3/envs/stack-client/lib/python3.10/site-packages/pytest_asyncio/plugin.py:207: PytestDeprecationWarning: The configuration option "asyncio_default_fixture_loop_scope" is unset. The event loop scope for asynchronous fixtures will default to the fixture caching scope. Future versions of pytest-asyncio will default the loop scope for asynchronous fixtures to function scope. Set the default fixture loop scope explicitly in order to avoid unexpected behavior in the future. Valid fixture loop scopes are: "function", "class", "module", "package", "session" warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET)) ====================================================== test session starts ======================================================= platform darwin -- Python 3.10.16, pytest-8.3.4, pluggy-1.5.0 -- /Users/vnarsing/miniconda3/envs/stack-client/bin/python cachedir: .pytest_cache metadata: {'Python': '3.10.16', 'Platform': 'macOS-14.7.4-arm64-arm-64bit', 'Packages': {'pytest': '8.3.4', 'pluggy': '1.5.0'}, 'Plugins': {'html': '4.1.1', 'metadata': '3.1.1', 'asyncio': '0.25.3', 'anyio': '4.8.0'}} rootdir: /Users/vnarsing/go/src/github/meta-llama/llama-stack configfile: pyproject.toml plugins: html-4.1.1, metadata-3.1.1, asyncio-0.25.3, anyio-4.8.0 asyncio: mode=auto, asyncio_default_fixture_loop_scope=None collected 7 items llama_stack/providers/tests/vector_io/test_sqlite_vec.py::test_add_chunks PASSED llama_stack/providers/tests/vector_io/test_sqlite_vec.py::test_query_chunks_vector PASSED llama_stack/providers/tests/vector_io/test_sqlite_vec.py::test_query_chunks_fts PASSED llama_stack/providers/tests/vector_io/test_sqlite_vec.py::test_chunk_id_conflict PASSED llama_stack/providers/tests/vector_io/test_sqlite_vec.py::test_register_vector_db PASSED llama_stack/providers/tests/vector_io/test_sqlite_vec.py::test_unregister_vector_db PASSED llama_stack/providers/tests/vector_io/test_sqlite_vec.py::test_generate_chunk_id PASSED ``` For reference, with the implementation, the fts table looks like below: ``` Chunk ID: 9fbc39ce-c729-64a2-260f-c5ec9bb2a33e, Content: Sentence 0 from document 0 Chunk ID: 94062914-3e23-44cf-1e50-9e25821ba882, Content: Sentence 1 from document 0 Chunk ID: e6cfd559-4641-33ba-6ce1-7038226495eb, Content: Sentence 2 from document 0 Chunk ID: 1383af9b-f1f0-f417-4de5-65fe9456cc20, Content: Sentence 3 from document 0 Chunk ID: 2db19b1a-de14-353b-f4e1-085e8463361c, Content: Sentence 4 from document 0 Chunk ID: 9faf986a-f028-7714-068a-1c795e8f2598, Content: Sentence 5 from document 0 Chunk ID: ef593ead-5a4a-392f-7ad8-471a50f033e8, Content: Sentence 6 from document 0 Chunk ID: e161950f-021f-7300-4d05-3166738b94cf, Content: Sentence 7 from document 0 Chunk ID: 90610fc4-67c1-e740-f043-709c5978867a, Content: Sentence 8 from document 0 Chunk ID: 97712879-6fff-98ad-0558-e9f42e6b81d3, Content: Sentence 9 from document 0 Chunk ID: aea70411-51df-61ba-d2f0-cb2b5972c210, Content: Sentence 0 from document 1 Chunk ID: b678a463-7b84-92b8-abb2-27e9a1977e3c, Content: Sentence 1 from document 1 Chunk ID: 27bd63da-909c-1606-a109-75bdb9479882, Content: Sentence 2 from document 1 Chunk ID: a2ad49ad-f9be-5372-e0c7-7b0221d0b53e, Content: Sentence 3 from document 1 Chunk ID: cac53bcd-1965-082a-c0f4-ceee7323fc70, Content: Sentence 4 from document 1 ``` Query results: Result 1: Sentence 5 from document 0 Result 2: Sentence 5 from document 1 Result 3: Sentence 5 from document 2 [//]: # (## Documentation) --------- Signed-off-by: Varsha Prasad Narsing <varshaprasad96@gmail.com>
197 lines
6.8 KiB
Python
197 lines
6.8 KiB
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# All rights reserved.
|
|
#
|
|
# This source code is licensed under the terms described in the LICENSE file in
|
|
# the root directory of this source tree.
|
|
import json
|
|
import logging
|
|
from typing import Any
|
|
|
|
import weaviate
|
|
import weaviate.classes as wvc
|
|
from numpy.typing import NDArray
|
|
from weaviate.classes.init import Auth
|
|
from weaviate.classes.query import Filter
|
|
|
|
from llama_stack.apis.common.content_types import InterleavedContent
|
|
from llama_stack.apis.vector_dbs import VectorDB
|
|
from llama_stack.apis.vector_io import Chunk, QueryChunksResponse, VectorIO
|
|
from llama_stack.distribution.request_headers import NeedsRequestProviderData
|
|
from llama_stack.providers.datatypes import Api, VectorDBsProtocolPrivate
|
|
from llama_stack.providers.utils.memory.vector_store import (
|
|
EmbeddingIndex,
|
|
VectorDBWithIndex,
|
|
)
|
|
|
|
from .config import WeaviateRequestProviderData, WeaviateVectorIOConfig
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class WeaviateIndex(EmbeddingIndex):
|
|
def __init__(self, client: weaviate.Client, collection_name: str):
|
|
self.client = client
|
|
self.collection_name = collection_name
|
|
|
|
async def add_chunks(self, chunks: list[Chunk], embeddings: NDArray):
|
|
assert len(chunks) == len(embeddings), (
|
|
f"Chunk length {len(chunks)} does not match embedding length {len(embeddings)}"
|
|
)
|
|
|
|
data_objects = []
|
|
for i, chunk in enumerate(chunks):
|
|
data_objects.append(
|
|
wvc.data.DataObject(
|
|
properties={
|
|
"chunk_content": chunk.model_dump_json(),
|
|
},
|
|
vector=embeddings[i].tolist(),
|
|
)
|
|
)
|
|
|
|
# Inserting chunks into a prespecified Weaviate collection
|
|
collection = self.client.collections.get(self.collection_name)
|
|
|
|
# TODO: make this async friendly
|
|
collection.data.insert_many(data_objects)
|
|
|
|
async def query_vector(self, embedding: NDArray, k: int, score_threshold: float) -> QueryChunksResponse:
|
|
collection = self.client.collections.get(self.collection_name)
|
|
|
|
results = collection.query.near_vector(
|
|
near_vector=embedding.tolist(),
|
|
limit=k,
|
|
return_metadata=wvc.query.MetadataQuery(distance=True),
|
|
)
|
|
|
|
chunks = []
|
|
scores = []
|
|
for doc in results.objects:
|
|
chunk_json = doc.properties["chunk_content"]
|
|
try:
|
|
chunk_dict = json.loads(chunk_json)
|
|
chunk = Chunk(**chunk_dict)
|
|
except Exception:
|
|
log.exception(f"Failed to parse document: {chunk_json}")
|
|
continue
|
|
|
|
chunks.append(chunk)
|
|
scores.append(1.0 / doc.metadata.distance)
|
|
|
|
return QueryChunksResponse(chunks=chunks, scores=scores)
|
|
|
|
async def delete(self, chunk_ids: list[str]) -> None:
|
|
collection = self.client.collections.get(self.collection_name)
|
|
collection.data.delete_many(where=Filter.by_property("id").contains_any(chunk_ids))
|
|
|
|
async def query_keyword(
|
|
self,
|
|
query_string: str,
|
|
k: int,
|
|
score_threshold: float,
|
|
) -> QueryChunksResponse:
|
|
raise NotImplementedError("Keyword search is not supported in Weaviate")
|
|
|
|
|
|
class WeaviateVectorIOAdapter(
|
|
VectorIO,
|
|
NeedsRequestProviderData,
|
|
VectorDBsProtocolPrivate,
|
|
):
|
|
def __init__(self, config: WeaviateVectorIOConfig, inference_api: Api.inference) -> None:
|
|
self.config = config
|
|
self.inference_api = inference_api
|
|
self.client_cache = {}
|
|
self.cache = {}
|
|
|
|
def _get_client(self) -> weaviate.Client:
|
|
provider_data = self.get_request_provider_data()
|
|
assert provider_data is not None, "Request provider data must be set"
|
|
assert isinstance(provider_data, WeaviateRequestProviderData)
|
|
|
|
key = f"{provider_data.weaviate_cluster_url}::{provider_data.weaviate_api_key}"
|
|
if key in self.client_cache:
|
|
return self.client_cache[key]
|
|
|
|
client = weaviate.connect_to_weaviate_cloud(
|
|
cluster_url=provider_data.weaviate_cluster_url,
|
|
auth_credentials=Auth.api_key(provider_data.weaviate_api_key),
|
|
)
|
|
self.client_cache[key] = client
|
|
return client
|
|
|
|
async def initialize(self) -> None:
|
|
pass
|
|
|
|
async def shutdown(self) -> None:
|
|
for client in self.client_cache.values():
|
|
client.close()
|
|
|
|
async def register_vector_db(
|
|
self,
|
|
vector_db: VectorDB,
|
|
) -> None:
|
|
client = self._get_client()
|
|
|
|
# Create collection if it doesn't exist
|
|
if not client.collections.exists(vector_db.identifier):
|
|
client.collections.create(
|
|
name=vector_db.identifier,
|
|
vectorizer_config=wvc.config.Configure.Vectorizer.none(),
|
|
properties=[
|
|
wvc.config.Property(
|
|
name="chunk_content",
|
|
data_type=wvc.config.DataType.TEXT,
|
|
),
|
|
],
|
|
)
|
|
|
|
self.cache[vector_db.identifier] = VectorDBWithIndex(
|
|
vector_db,
|
|
WeaviateIndex(client=client, collection_name=vector_db.identifier),
|
|
self.inference_api,
|
|
)
|
|
|
|
async def _get_and_cache_vector_db_index(self, vector_db_id: str) -> VectorDBWithIndex | None:
|
|
if vector_db_id in self.cache:
|
|
return self.cache[vector_db_id]
|
|
|
|
vector_db = await self.vector_db_store.get_vector_db(vector_db_id)
|
|
if not vector_db:
|
|
raise ValueError(f"Vector DB {vector_db_id} not found")
|
|
|
|
client = self._get_client()
|
|
if not client.collections.exists(vector_db.identifier):
|
|
raise ValueError(f"Collection with name `{vector_db.identifier}` not found")
|
|
|
|
index = VectorDBWithIndex(
|
|
vector_db=vector_db,
|
|
index=WeaviateIndex(client=client, collection_name=vector_db.identifier),
|
|
inference_api=self.inference_api,
|
|
)
|
|
self.cache[vector_db_id] = index
|
|
return index
|
|
|
|
async def insert_chunks(
|
|
self,
|
|
vector_db_id: str,
|
|
chunks: list[Chunk],
|
|
ttl_seconds: int | None = None,
|
|
) -> None:
|
|
index = await self._get_and_cache_vector_db_index(vector_db_id)
|
|
if not index:
|
|
raise ValueError(f"Vector DB {vector_db_id} not found")
|
|
|
|
await index.insert_chunks(chunks)
|
|
|
|
async def query_chunks(
|
|
self,
|
|
vector_db_id: str,
|
|
query: InterleavedContent,
|
|
params: dict[str, Any] | None = None,
|
|
) -> QueryChunksResponse:
|
|
index = await self._get_and_cache_vector_db_index(vector_db_id)
|
|
if not index:
|
|
raise ValueError(f"Vector DB {vector_db_id} not found")
|
|
|
|
return await index.query_chunks(query, params)
|