-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Astra DB vector store implementation
- Loading branch information
Showing
7 changed files
with
376 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ numpy | |
pypi | ||
nbformat | ||
semversioner | ||
astrapy | ||
|
||
# Library Methods | ||
iterrows | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Copyright (c) 2024 Microsoft Corporation. | ||
# Licensed under the MIT License | ||
|
||
"""The Astra DB vector store implementation package.""" | ||
|
||
import json | ||
from typing import Any | ||
from typing_extensions import override | ||
|
||
from astrapy import DataAPIClient | ||
|
||
from graphrag.model.types import TextEmbedder | ||
|
||
from .base import BaseVectorStore, VectorStoreDocument, VectorStoreSearchResult | ||
|
||
|
||
class AstraDB(BaseVectorStore): | ||
"""The Astra DB vector storage implementation.""" | ||
|
||
@override | ||
def connect(self, | ||
*, | ||
token: str | None = None, | ||
api_endpoint: str | None = None, | ||
namespace: str | None = None, | ||
**kwargs: Any) -> Any: | ||
self.db_connection = DataAPIClient().get_database( | ||
api_endpoint=api_endpoint, | ||
token=token, | ||
namespace=namespace, | ||
) | ||
self.document_collection = self.db_connection.get_collection( | ||
self.collection_name, | ||
namespace=namespace | ||
) | ||
|
||
@override | ||
def load_documents( | ||
self, documents: list[VectorStoreDocument], overwrite: bool = True | ||
) -> None: | ||
if overwrite: | ||
self.document_collection.drop() | ||
|
||
if not documents: | ||
return | ||
|
||
self.db_connection.create_collection( | ||
name=self.collection_name, | ||
dimension=len(documents[0].vector), | ||
check_exists=False, | ||
) | ||
|
||
batch = [ | ||
{ | ||
"content": doc.text, | ||
"_id": doc.id, | ||
"$vector": doc.vector, | ||
"metadata": json.dumps(doc.attributes), | ||
} | ||
for doc in documents | ||
if doc.vector is not None | ||
] | ||
|
||
if batch and len(batch) > 0: | ||
self.document_collection.insert_many(batch) | ||
|
||
@override | ||
def filter_by_id(self, include_ids: list[str] | list[int]) -> Any: | ||
if include_ids is None or len(include_ids) == 0: | ||
self.query_filter = {} | ||
else: | ||
self.query_filter = {"_id": {"$in": include_ids}} | ||
return self.query_filter | ||
|
||
@override | ||
def similarity_search_by_vector( | ||
self, query_embedding: list[float], k: int = 10, **kwargs: Any | ||
) -> list[VectorStoreSearchResult]: | ||
response = self.document_collection.find( | ||
filter=self.query_filter or {}, | ||
projection={ | ||
"_id": True, | ||
"content": True, | ||
"metadata": True, | ||
"$vector": True, | ||
}, | ||
limit=k, | ||
include_similarity=True, | ||
sort={"$vector": query_embedding}, | ||
) | ||
return [ | ||
VectorStoreSearchResult( | ||
document=VectorStoreDocument( | ||
id=doc["_id"], | ||
text=doc["content"], | ||
vector=doc["$vector"], | ||
attributes=doc["metadata"], | ||
), | ||
score=doc["$similarity"], | ||
) | ||
for doc in response | ||
] | ||
|
||
@override | ||
def similarity_search_by_text( | ||
self, text: str, text_embedder: TextEmbedder, k: int = 10, **kwargs: Any | ||
) -> list[VectorStoreSearchResult]: | ||
query_embedding = text_embedder(text) | ||
if query_embedding: | ||
return self.similarity_search_by_vector( | ||
query_embedding=query_embedding, k=k | ||
) | ||
return [] | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.