-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
154 additions
and
165 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
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 |
---|---|---|
@@ -1,55 +1,45 @@ | ||
import uuid | ||
from typing import List, Optional | ||
|
||
from weaviate import Client | ||
from weaviate import WeaviateClient | ||
from weaviate.connect import ConnectionParams | ||
from weaviate.classes.data import DataObject | ||
|
||
from engine.base_client.upload import BaseUploader | ||
from engine.clients.weaviate.config import WEAVIATE_CLASS_NAME, WEAVIATE_DEFAULT_PORT | ||
|
||
|
||
class WeaviateUploader(BaseUploader): | ||
client = None | ||
client: WeaviateClient = None | ||
upload_params = {} | ||
collection = None | ||
|
||
@classmethod | ||
def init_client(cls, host, distance, connection_params, upload_params): | ||
url = f"http://{host}:{connection_params.get('port', WEAVIATE_DEFAULT_PORT)}" | ||
cls.client = Client(url, **connection_params) | ||
|
||
cls.client = WeaviateClient( | ||
ConnectionParams.from_url(url, 50051), skip_init_checks=True | ||
) | ||
cls.client.connect() | ||
cls.upload_params = upload_params | ||
cls.connection_params = connection_params | ||
|
||
@staticmethod | ||
def _update_geo_data(data_object): | ||
keys = data_object.keys() | ||
for key in keys: | ||
if isinstance(data_object[key], dict): | ||
if lat := data_object[key].get("lat", None): | ||
data_object[key]["latitude"] = lat | ||
if lon := data_object[key].get("lon", None): | ||
data_object[key]["longitude"] = lon | ||
|
||
return data_object | ||
cls.collection = cls.client.collections.get( | ||
WEAVIATE_CLASS_NAME, skip_argument_validation=True | ||
) | ||
|
||
@classmethod | ||
def upload_batch( | ||
cls, ids: List[int], vectors: List[list], metadata: List[Optional[dict]] | ||
): | ||
# Weaviate introduced the batch_size, so it can handle built-in client's | ||
# multi-threading. That should make the upload faster. | ||
cls.client.batch.configure( | ||
batch_size=100, | ||
timeout_retries=3, | ||
) | ||
objects = [] | ||
for i in range(len(ids)): | ||
id = uuid.UUID(int=ids[i]) | ||
property = metadata[i] or {} | ||
objects.append(DataObject(properties=property, vector=vectors[i], uuid=id)) | ||
if len(objects) > 0: | ||
cls.collection.data.insert_many(objects) | ||
|
||
with cls.client.batch as batch: | ||
for id_, vector, data_object in zip(ids, vectors, metadata): | ||
data_object = cls._update_geo_data(data_object or {}) | ||
batch.add_data_object( | ||
data_object=data_object, | ||
class_name=WEAVIATE_CLASS_NAME, | ||
uuid=uuid.UUID(int=id_).hex, | ||
vector=vector, | ||
) | ||
|
||
batch.create_objects() | ||
@classmethod | ||
def delete_client(cls): | ||
if cls.client is not None: | ||
cls.client.close() |
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
Oops, something went wrong.