-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
testing: Weaviate impl #88
Open
emekaokoli19
wants to merge
6
commits into
AI-Northstar-Tech:main
Choose a base branch
from
emekaokoli19:weaviate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e2a592
testing
emekaokoli19 fa5f40b
changes
emekaokoli19 4e18e48
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ad6920c
fixing some things
dhruv-anand-aintech 3f48bdd
Merge branch 'main' into pr/emekaokoli19/88
dhruv-anand-aintech 8c311b7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 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,122 @@ | ||
import os | ||
import weaviate | ||
from tqdm import tqdm | ||
from vdf_io.import_vdf.vdf_import_cls import ImportVDB | ||
from vdf_io.names import DBNames | ||
from vdf_io.constants import INT_MAX, DEFAULT_BATCH_SIZE | ||
from vdf_io.weaviate_util import prompt_for_creds | ||
|
||
# Set these environment variables | ||
URL = os.getenv("YOUR_WCS_URL") | ||
APIKEY = os.getenv("YOUR_WCS_API_KEY") | ||
|
||
|
||
class ImportWeaviate(ImportVDB): | ||
DB_NAME_SLUG = DBNames.WEAVIATE | ||
|
||
@classmethod | ||
def make_parser(cls, subparsers): | ||
parser_weaviate = subparsers.add_parser( | ||
cls.DB_NAME_SLUG, help="Import data into Weaviate" | ||
) | ||
|
||
parser_weaviate.add_argument("--url", type=str, help="URL of Weaviate instance") | ||
parser_weaviate.add_argument("--api_key", type=str, help="Weaviate API key") | ||
parser_weaviate.add_argument( | ||
"--connection-type", | ||
type=str, | ||
choices=["local", "cloud"], | ||
default="cloud", | ||
help="Type of connection to Weaviate (local or cloud)", | ||
) | ||
parser_weaviate.add_argument( | ||
"--batch_size", | ||
type=int, | ||
help="batch size for fetching", | ||
default=DEFAULT_BATCH_SIZE, | ||
) | ||
|
||
@classmethod | ||
def import_vdb(cls, args): | ||
prompt_for_creds(args) | ||
weaviate_import = ImportWeaviate(args) | ||
weaviate_import.upsert_data() | ||
return weaviate_import | ||
|
||
def __init__(self, args): | ||
super().__init__(args) | ||
if self.args["connection_type"] == "local": | ||
self.client = weaviate.connect_to_local() | ||
else: | ||
self.client = weaviate.connect_to_wcs( | ||
cluster_url=self.args["url"], | ||
auth_credentials=weaviate.auth.AuthApiKey(self.args["api_key"]), | ||
headers={"X-OpenAI-Api-key": self.args.get("openai_api_key", "")}, | ||
skip_init_checks=True, | ||
) | ||
|
||
def upsert_data(self): | ||
max_hit = False | ||
total_imported_count = 0 | ||
|
||
# Iterate over the indexes and import the data | ||
for index_name, index_meta in tqdm( | ||
self.vdf_meta["indexes"].items(), desc="Importing indexes" | ||
): | ||
tqdm.write(f"Importing data for index '{index_name}'") | ||
for namespace_meta in index_meta: | ||
self.set_dims(namespace_meta, index_name) | ||
|
||
# Create or get the index | ||
index_name = self.create_new_name( | ||
index_name, self.client.collections.list_all().keys() | ||
) | ||
|
||
# Load data from the Parquet files | ||
data_path = namespace_meta["data_path"] | ||
final_data_path = self.get_final_data_path(data_path) | ||
parquet_files = self.get_parquet_files(final_data_path) | ||
|
||
vectors = {} | ||
metadata = {} | ||
vector_column_names, vector_column_name = self.get_vector_column_name( | ||
index_name, namespace_meta | ||
) | ||
|
||
for file in tqdm(parquet_files, desc="Loading data from parquet files"): | ||
file_path = os.path.join(final_data_path, file) | ||
df = self.read_parquet_progress(file_path) | ||
|
||
if len(vectors) > (self.args.get("max_num_rows") or INT_MAX): | ||
max_hit = True | ||
break | ||
if len(vectors) + len(df) > (self.args.get("max_num_rows") or INT_MAX): | ||
df = df.head( | ||
(self.args.get("max_num_rows") or INT_MAX) - len(vectors) | ||
) | ||
max_hit = True | ||
self.update_vectors(vectors, vector_column_name, df) | ||
self.update_metadata(metadata, vector_column_names, df) | ||
if max_hit: | ||
break | ||
|
||
tqdm.write( | ||
f"Loaded {len(vectors)} vectors from {len(parquet_files)} parquet files" | ||
) | ||
|
||
# Upsert the vectors and metadata to the Weaviate index in batches | ||
BATCH_SIZE = self.args.get("batch_size") | ||
|
||
with self.client.batch.fixed_size(batch_size=BATCH_SIZE) as batch: | ||
for _, vector in vectors.items(): | ||
batch.add_object( | ||
vector=vector, | ||
collection=index_name, | ||
# TODO: Find way to add Metadata | ||
) | ||
total_imported_count += 1 | ||
|
||
tqdm.write( | ||
f"Data import completed successfully. Imported {total_imported_count} vectors" | ||
) | ||
self.args["imported_count"] = total_imported_count |
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,31 @@ | ||
from vdf_io.util import set_arg_from_input, set_arg_from_password | ||
|
||
|
||
def prompt_for_creds(args): | ||
set_arg_from_input( | ||
args, | ||
"connection_type", | ||
"Enter 'local' or 'cloud' for connection types: ", | ||
choices=["local", "cloud"], | ||
) | ||
if args["connection_type"] == "cloud": | ||
set_arg_from_input( | ||
args, | ||
"url", | ||
"Enter the URL of Weaviate instance: ", | ||
str, | ||
env_var="WEAVIATE_URL", | ||
) | ||
set_arg_from_password( | ||
args, | ||
"api_key", | ||
"Enter the Weaviate API key: ", | ||
"WEAVIATE_API_KEY", | ||
) | ||
|
||
set_arg_from_password( | ||
args, | ||
"api_key", | ||
"Enter the Weaviate API key: ", | ||
"WEAVIATE_API_KEY", | ||
) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
connection_type
argument is used here but it is not defined in the argument parser for the import script. This will cause an error when trying to accessself.args["connection_type"]
.To fix this, add the
connection_type
argument to the parser in themake_parser
method: