Skip to content

Commit

Permalink
Merge pull request #85 from weaviate/jose/update_readme_config
Browse files Browse the repository at this point in the history
Add examples of configuration for WCD clusters.
  • Loading branch information
jfrancoa authored Oct 30, 2024
2 parents 4b9ff0b + 8131f26 commit 3f366ed
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ lint:
test:
pytest test/unittests

all: format lint test build

build-all: build build-check

all: format lint test build-all
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ weaviate-cli query data --collection movies --search-type hybrid --query "action

## Configuration

Weaviate CLI allows you to configure your cluster endpoints and parameters through a configuration file. By default, the CLI looks for a
Weaviate CLI allows you to configure your cluster endpoints and parameters through a configuration file. By default, the CLI looks for a
configuration file at `~/.config/weaviate/config.json`. If this file does not exist, it will be created with the following default values:

```json
Expand Down Expand Up @@ -80,14 +80,27 @@ The configuration file should be a JSON file with the following structure:
}
```

If you are using a remote Weaviate instance, you can use the `weaviate-cli` command to authenticate with your Weaviate instance.
Here you can see an example on how the configuration file should look like if you are connecting to a WCD cluster:

```json
{
"host": "thisisaninventedcluster.url.s3.us-west3.prov.weaviate.cloud",
"auth": {
"type": "api_key",
"api_key": "jfeRFsdfRfSasgsDoNOtTrYToUsErRQwqqdZfghasd"
}
}
```

## Requirements

- Python 3.8+
- Python 3.9+
- Weaviate instance (local or remote)

## Documentation

For detailed documentation and examples, visit our [Documentation](https://weaviate.io/developers/weaviate).
Detailed documentation will be added soon.

## Community & Support

Expand All @@ -97,13 +110,9 @@ For detailed documentation and examples, visit our [Documentation](https://weavi

## Contributing

We welcome contributions! Please see our [Contributing Guidelines](https://github.com/weaviate/weaviate-cli/blob/main/CONTRIBUTING.md) for
We welcome contributions! Please see our [Contributing Guidelines](https://github.com/weaviate/weaviate-cli/blob/main/CONTRIBUTING.md) for
details.

## License

BSD-3-Clause License




97 changes: 50 additions & 47 deletions weaviate_cli/managers/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
from weaviate import WeaviateClient
from weaviate.classes.query import MetadataQuery
from weaviate.collections.classes.tenants import TenantActivityStatus
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional, Union, Any
import weaviate.classes.config as wvc
from weaviate.collections import Collection
from datetime import datetime, timedelta
import sys
import importlib.resources as resources
from pathlib import Path

PROPERTY_NAME_MAPPING = {"release_date": "releaseDate"}


class DataManager:
def __init__(self, client: WeaviateClient):
Expand All @@ -32,59 +34,60 @@ def __import_json(
properties: List[wvc.Property] = collection.config.get().properties

try:
# Different approach based on Python version
if sys.version_info >= (3, 9):
# Python 3.9+ approach
with (
resources.files("weaviate_cli.datasets")
.joinpath(file_name)
.open("r") as f
):
data = json.load(f)
else:
# Python 3.8 approach
with resources.open_text("weaviate_cli.datasets", file_name) as f:
data = json.load(f)

cl_collection: Collection = collection.with_consistency_level(cl)
with cl_collection.batch.dynamic() as batch:
for obj in data[:num_objects] if num_objects else data:
added_obj = {}
for prop in properties:
if prop.name == "release_date":
prop.name = "releaseDate"
if prop.name in obj:
if prop.data_type == wvc.DataType.NUMBER:
added_obj[prop.name] = float(obj[prop.name])
elif prop.data_type == wvc.DataType.DATE:
date = datetime.strptime(obj[prop.name], "%Y-%m-%d")
added_obj[prop.name] = date.strftime(
"%Y-%m-%dT%H:%M:%SZ"
with (
resources.files("weaviate_cli.datasets")
.joinpath(file_name)
.open("r") as f
):
data = json.load(f)

cl_collection: Collection = collection.with_consistency_level(cl)
with cl_collection.batch.dynamic() as batch:
for obj in data[:num_objects] if num_objects else data:
added_obj = {}
for prop in properties:
prop_name = PROPERTY_NAME_MAPPING.get(prop.name, prop.name)
if prop_name in obj:
added_obj[prop_name] = self.__convert_property_value(
obj[prop_name], prop.data_type
)
else:
added_obj[prop.name] = obj[prop.name]
batch.add_object(properties=added_obj)
counter += 1

if cl_collection.batch.failed_objects:
for failed_object in cl_collection.batch.failed_objects:
print(
f"Failed to add object with UUID {failed_object.original_uuid}: {failed_object.message}"
)
return -1

expected: int = len(data[:num_objects]) if num_objects else len(data)
assert (
counter == expected
), f"Expected {expected} objects, but added {counter} objects."

batch.add_object(properties=added_obj)
counter += 1

if cl_collection.batch.failed_objects:
for failed_object in cl_collection.batch.failed_objects:
print(
f"Failed to add object with UUID {failed_object.original_uuid}: "
f"{failed_object.message}"
)
return -1

expected: int = len(data[:num_objects]) if num_objects else len(data)
assert (
counter == expected
), f"Expected {expected} objects, but added {counter} objects."

except json.JSONDecodeError as e:
print(f"Error decoding JSON file: {str(e)}")
return -1
except FileNotFoundError as e:
print(f"Dataset file not found: {str(e)}")
return -1
except Exception as e:
print(f"Error loading data file: {str(e)}")
print(f"Unexpected error loading data file: {str(e)}")
return -1

print(f"Finished processing {counter} objects.")
return counter

def __convert_property_value(self, value: Any, data_type: wvc.DataType) -> Any:
if data_type == wvc.DataType.NUMBER:
return float(value)
elif data_type == wvc.DataType.DATE:
date = datetime.strptime(value, "%Y-%m-%d")
return date.strftime("%Y-%m-%dT%H:%M:%SZ")
return value

def __generate_data_object(
self, limit: int, is_update: bool = False
) -> Union[List[Dict], Dict]:
Expand Down

0 comments on commit 3f366ed

Please sign in to comment.