-
-
Notifications
You must be signed in to change notification settings - Fork 398
feat(storage): add vector and analytics buckets support #1318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d4db1c0
feat(storage): add vector bucket support
o-santi 458cf33
chore: add sync version, add `vectors` method to storage client
o-santi fa5633f
chore: format makefile
o-santi 3a0f26f
feat(storage): add analytics, read correct field from response
o-santi bf9384f
fix(storage): fix some more issues
o-santi 8b6e3cb
fix(storage): fix more stuff
o-santi 31bf56d
fix(storage): also implement sync
o-santi 30616c3
fix(storage): make analytics endpoints work
o-santi 5a1c19a
fix(storage): remove print
o-santi 252cc61
fix(storage): make field name snake_case
o-santi 3816be3
chore(storage): use class keyword to ignore extra fields
o-santi 845b9f3
fix(storage): misc fixes to serialization of basemodels
o-santi a452b24
fix(storage): return models instead of contained fields
o-santi be4d570
fix(storage): run build-sync
o-santi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| from typing import List, Optional | ||
|
|
||
| from httpx import QueryParams | ||
|
|
||
| from ..types import ( | ||
| AnalyticsBucket, | ||
| AnalyticsBucketDeleteResponse, | ||
| AnalyticsBucketsParser, | ||
| SortColumn, | ||
| SortOrder, | ||
| ) | ||
| from .request import AsyncRequestBuilder | ||
|
|
||
|
|
||
| class AsyncStorageAnalyticsClient: | ||
| def __init__(self, request: AsyncRequestBuilder) -> None: | ||
| self._request = request | ||
|
|
||
| async def create(self, bucket_name: str) -> AnalyticsBucket: | ||
| body = {"name": bucket_name} | ||
| data = await self._request.send(http_method="POST", path=["bucket"], body=body) | ||
| return AnalyticsBucket.model_validate_json(data.content) | ||
|
|
||
| async def list( | ||
| self, | ||
| limit: Optional[int] = None, | ||
| offset: Optional[int] = None, | ||
| sort_column: Optional[SortColumn] = None, | ||
| sort_order: Optional[SortOrder] = None, | ||
| search: Optional[str] = None, | ||
| ) -> List[AnalyticsBucket]: | ||
| params = dict( | ||
| limit=limit, | ||
| offset=offset, | ||
| sort_column=sort_column, | ||
| sort_order=sort_order, | ||
| search=search, | ||
| ) | ||
| filtered_params = QueryParams( | ||
| **{k: v for k, v in params.items() if v is not None} | ||
| ) | ||
| data = await self._request.send( | ||
| http_method="GET", path=["bucket"], query_params=filtered_params | ||
| ) | ||
| print(data.content) | ||
| return AnalyticsBucketsParser.validate_json(data.content) | ||
|
|
||
| async def delete(self, bucket_name: str) -> AnalyticsBucketDeleteResponse: | ||
| data = await self._request.send( | ||
| http_method="DELETE", path=["bucket", bucket_name] | ||
| ) | ||
| return AnalyticsBucketDeleteResponse.model_validate_json(data.content) | ||
This file contains hidden or 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 hidden or 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,47 @@ | ||
| from typing import Optional | ||
|
|
||
| from httpx import AsyncClient, Headers, HTTPStatusError, QueryParams, Response | ||
| from pydantic import ValidationError | ||
| from yarl import URL | ||
|
|
||
| from ..exceptions import StorageApiError, VectorBucketErrorMessage | ||
| from ..types import JSON, RequestMethod | ||
|
|
||
|
|
||
| class AsyncRequestBuilder: | ||
| def __init__(self, session: AsyncClient, base_url: URL, headers: Headers) -> None: | ||
| self._session = session | ||
| self._base_url = base_url | ||
| self.headers = headers | ||
|
|
||
| async def send( | ||
| self, | ||
| http_method: RequestMethod, | ||
| path: list[str], | ||
| body: JSON = None, | ||
| query_params: Optional[QueryParams] = None, | ||
| ) -> Response: | ||
| response = await self._session.request( | ||
| method=http_method, | ||
| json=body, | ||
| url=str(self._base_url.joinpath(*path)), | ||
| headers=self.headers, | ||
| params=query_params or QueryParams(), | ||
| ) | ||
| try: | ||
| response.raise_for_status() | ||
| return response | ||
| except HTTPStatusError as exc: | ||
| try: | ||
| error = VectorBucketErrorMessage.model_validate_json(response.content) | ||
| raise StorageApiError( | ||
| message=error.message, | ||
| code=error.code or "400", | ||
| status=error.statusCode, | ||
| ) from exc | ||
| except ValidationError as exc: | ||
| raise StorageApiError( | ||
| message=f"The request failed, but could not parse error message response:'{response.text}'", | ||
| code="LibraryError", | ||
| status=response.status_code, | ||
| ) from exc |
This file contains hidden or 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,213 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import List, Optional | ||
|
|
||
| from httpx import AsyncClient, Headers | ||
| from yarl import URL | ||
|
|
||
| from ..exceptions import StorageApiError, VectorBucketException | ||
| from ..types import ( | ||
| JSON, | ||
| DistanceMetric, | ||
| GetVectorBucketResponse, | ||
| GetVectorIndexResponse, | ||
| GetVectorsResponse, | ||
| ListVectorBucketsResponse, | ||
| ListVectorIndexesResponse, | ||
| ListVectorsResponse, | ||
| MetadataConfiguration, | ||
| QueryVectorsResponse, | ||
| VectorBucket, | ||
| VectorData, | ||
| VectorFilter, | ||
| VectorIndex, | ||
| VectorMatch, | ||
| VectorObject, | ||
| ) | ||
| from .request import AsyncRequestBuilder | ||
|
|
||
|
|
||
| # used to not send non-required values as `null` | ||
| # for they cannot be null | ||
| def remove_none(**kwargs: JSON) -> JSON: | ||
| return {key: val for key, val in kwargs.items() if val is not None} | ||
|
|
||
|
|
||
| class AsyncVectorBucketScope: | ||
| def __init__(self, request: AsyncRequestBuilder, bucket_name: str) -> None: | ||
| self._request = request | ||
| self._bucket_name = bucket_name | ||
|
|
||
| def with_metadata(self, **data: JSON) -> JSON: | ||
| return remove_none(vectorBucketName=self._bucket_name, **data) | ||
|
|
||
| async def create_index( | ||
| self, | ||
| index_name: str, | ||
| dimension: int, | ||
| distance_metric: DistanceMetric, | ||
| data_type: str, | ||
| metadata: Optional[MetadataConfiguration] = None, | ||
| ) -> None: | ||
| body = self.with_metadata( | ||
| indexName=index_name, | ||
| dimension=dimension, | ||
| distanceMetric=distance_metric, | ||
| dataType=data_type, | ||
| metadataConfiguration=dict(metadata) if metadata else None, | ||
| ) | ||
| await self._request.send(http_method="POST", path=["CreateIndex"], body=body) | ||
|
|
||
| async def get_index(self, index_name: str) -> Optional[VectorIndex]: | ||
| body = self.with_metadata(indexName=index_name) | ||
| try: | ||
| data = await self._request.send( | ||
| http_method="POST", path=["GetIndex"], body=body | ||
| ) | ||
| return GetVectorIndexResponse.model_validate_json(data.content).index | ||
| except StorageApiError: | ||
| return None | ||
|
|
||
| async def list_indexes( | ||
| self, | ||
| next_token: Optional[str] = None, | ||
| max_results: Optional[int] = None, | ||
| prefix: Optional[str] = None, | ||
| ) -> ListVectorIndexesResponse: | ||
| body = self.with_metadata( | ||
| next_token=next_token, max_results=max_results, prefix=prefix | ||
| ) | ||
| data = await self._request.send( | ||
| http_method="POST", path=["ListIndexes"], body=body | ||
| ) | ||
| return ListVectorIndexesResponse.model_validate_json(data.content) | ||
|
|
||
| async def delete_index(self, index_name: str) -> None: | ||
| body = self.with_metadata(indexName=index_name) | ||
| await self._request.send(http_method="POST", path=["DeleteIndex"], body=body) | ||
|
|
||
| def index(self, index_name: str) -> AsyncVectorIndexScope: | ||
| return AsyncVectorIndexScope(self._request, self._bucket_name, index_name) | ||
|
|
||
|
|
||
| class AsyncVectorIndexScope: | ||
| def __init__( | ||
| self, request: AsyncRequestBuilder, bucket_name: str, index_name: str | ||
| ) -> None: | ||
| self._request = request | ||
| self._bucket_name = bucket_name | ||
| self._index_name = index_name | ||
|
|
||
| def with_metadata(self, **data: JSON) -> JSON: | ||
| return remove_none( | ||
| vectorBucketName=self._bucket_name, | ||
| indexName=self._index_name, | ||
| **data, | ||
| ) | ||
|
|
||
| async def put(self, vectors: List[VectorObject]) -> None: | ||
| body = self.with_metadata(vectors=[v.as_json() for v in vectors]) | ||
| await self._request.send(http_method="POST", path=["PutVectors"], body=body) | ||
|
|
||
| async def get( | ||
| self, *keys: str, return_data: bool = True, return_metadata: bool = True | ||
| ) -> List[VectorMatch]: | ||
| body = self.with_metadata( | ||
| keys=keys, returnData=return_data, returnMetadata=return_metadata | ||
| ) | ||
| data = await self._request.send( | ||
| http_method="POST", path=["GetVectors"], body=body | ||
| ) | ||
| return GetVectorsResponse.model_validate_json(data.content).vectors | ||
|
|
||
| async def list( | ||
| self, | ||
| max_results: Optional[int] = None, | ||
| next_token: Optional[str] = None, | ||
| return_data: bool = True, | ||
| return_metadata: bool = True, | ||
| segment_count: Optional[int] = None, | ||
| segment_index: Optional[int] = None, | ||
| ) -> ListVectorsResponse: | ||
| body = self.with_metadata( | ||
| maxResults=max_results, | ||
| nextToken=next_token, | ||
| returnData=return_data, | ||
| returnMetadata=return_metadata, | ||
| segmentCount=segment_count, | ||
| segmentIndex=segment_index, | ||
| ) | ||
| data = await self._request.send( | ||
| http_method="POST", path=["ListVectors"], body=body | ||
| ) | ||
| return ListVectorsResponse.model_validate_json(data.content) | ||
|
|
||
| async def query( | ||
| self, | ||
| query_vector: VectorData, | ||
| topK: Optional[int] = None, | ||
| filter: Optional[VectorFilter] = None, | ||
| return_distance: bool = True, | ||
| return_metadata: bool = True, | ||
| ) -> QueryVectorsResponse: | ||
| body = self.with_metadata( | ||
| queryVector=dict(query_vector), | ||
| topK=topK, | ||
| filter=filter, | ||
| returnDistance=return_distance, | ||
| returnMetadata=return_metadata, | ||
| ) | ||
| data = await self._request.send( | ||
| http_method="POST", path=["QueryVectors"], body=body | ||
| ) | ||
| return QueryVectorsResponse.model_validate_json(data.content) | ||
|
|
||
| async def delete(self, keys: List[str]) -> None: | ||
| if 1 < len(keys) or len(keys) > 500: | ||
| raise VectorBucketException("Keys batch size must be between 1 and 500.") | ||
| body = self.with_metadata(keys=keys) | ||
| await self._request.send(http_method="POST", path=["DeleteVectors"], body=body) | ||
|
|
||
|
|
||
| class AsyncStorageVectorsClient: | ||
| def __init__(self, url: URL, headers: Headers, session: AsyncClient) -> None: | ||
| self._request = AsyncRequestBuilder(session, base_url=URL(url), headers=headers) | ||
|
|
||
| def from_(self, bucket_name: str) -> AsyncVectorBucketScope: | ||
| return AsyncVectorBucketScope(self._request, bucket_name) | ||
|
|
||
| async def create_bucket(self, bucket_name: str) -> None: | ||
| body = {"vectorBucketName": bucket_name} | ||
| await self._request.send( | ||
| http_method="POST", path=["CreateVectorBucket"], body=body | ||
| ) | ||
|
|
||
| async def get_bucket(self, bucket_name: str) -> Optional[VectorBucket]: | ||
| body = {"vectorBucketName": bucket_name} | ||
| try: | ||
| data = await self._request.send( | ||
| http_method="POST", path=["GetVectorBucket"], body=body | ||
| ) | ||
| return GetVectorBucketResponse.model_validate_json( | ||
| data.content | ||
| ).vectorBucket | ||
| except StorageApiError: | ||
| return None | ||
|
|
||
| async def list_buckets( | ||
| self, | ||
| prefix: Optional[str] = None, | ||
| max_results: Optional[int] = None, | ||
| next_token: Optional[str] = None, | ||
| ) -> ListVectorBucketsResponse: | ||
| body = remove_none(prefix=prefix, maxResults=max_results, nextToken=next_token) | ||
| data = await self._request.send( | ||
| http_method="POST", path=["ListVectorBuckets"], body=body | ||
| ) | ||
| return ListVectorBucketsResponse.model_validate_json(data.content) | ||
|
|
||
| async def delete_bucket(self, bucket_name: str) -> None: | ||
| body = {"vectorBucketName": bucket_name} | ||
| await self._request.send( | ||
| http_method="POST", path=["DeleteVectorBucket"], body=body | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.