-
Notifications
You must be signed in to change notification settings - Fork 24
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
add update example #364
Open
wxyucs
wants to merge
1
commit into
main
Choose a base branch
from
add-update-example
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
add update example #364
Changes from all commits
Commits
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,136 @@ | ||
|
||
// Copyright 2024-present the vsag project | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <vsag/vsag.h> | ||
|
||
#include <iostream> | ||
|
||
int | ||
main(int argc, char** argv) { | ||
/******************* Prepare Base Dataset *****************/ | ||
int64_t num_vectors = 10000; | ||
int64_t dim = 64; | ||
std::vector<int64_t> ids(num_vectors); | ||
std::vector<float> vectors(num_vectors * dim); | ||
std::mt19937 rng(47); | ||
std::uniform_real_distribution<float> distrib_real; | ||
for (int64_t i = 0; i < num_vectors; ++i) { | ||
ids[i] = i; | ||
} | ||
for (int64_t i = 0; i < dim * num_vectors; ++i) { | ||
vectors[i] = distrib_real(rng); | ||
} | ||
auto base = vsag::Dataset::Make(); | ||
base->NumElements(num_vectors) | ||
->Dim(dim) | ||
->Ids(ids.data()) | ||
->Float32Vectors(vectors.data()) | ||
->Owner(false); | ||
|
||
/******************* Create HNSW Index *****************/ | ||
auto hnsw_build_paramesters = R"( | ||
{ | ||
"dtype": "float32", | ||
"metric_type": "l2", | ||
"dim": 64, | ||
"hnsw": { | ||
"max_degree": 16, | ||
"ef_construction": 100 | ||
} | ||
} | ||
)"; | ||
auto index = vsag::Factory::CreateIndex("hnsw", hnsw_build_paramesters).value(); | ||
|
||
/******************* Build HNSW Index *****************/ | ||
if (auto build_result = index->Build(base); build_result.has_value()) { | ||
std::cout << "After Build(), Index Hnsw contains: " << index->GetNumElements() << std::endl; | ||
} else { | ||
std::cerr << "Failed to build index: " << build_result.error().message << std::endl; | ||
exit(-1); | ||
} | ||
|
||
/******************* Update Vector in Index *****************/ | ||
int64_t update_id = 9527; | ||
std::vector<float> new_vector(dim); | ||
for (int64_t i = 0; i < dim; ++i) { | ||
new_vector[i] = distrib_real(rng); | ||
} | ||
auto update_dataset = vsag::Dataset::Make(); | ||
update_dataset->NumElements(1) | ||
->Dim(dim) | ||
->Ids(&update_id) | ||
->Float32Vectors(new_vector.data()) | ||
->Owner(false); | ||
|
||
// try to update determines by the distance between the new vector and the old vector | ||
if (auto update_vector = index->UpdateVector(update_id, update_dataset, false); | ||
not update_vector.has_value()) { /* update returns an error */ | ||
std::cerr << "update vector failed: " << update_vector.error().message << std::endl; | ||
abort(); | ||
} else if (*update_vector) { /* updated, new vector is near to the old vector */ | ||
std::cout << "updated, new vector is near to the old vector" << std::endl; | ||
} else { /* not update, new vector is far away from the old vector */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: default is in place update ? |
||
std::cout << "not update, new vector is far away from the old vector" << std::endl; | ||
|
||
// not good to update in-place, choose to delete and insert | ||
if (auto remove = index->Remove(update_id); | ||
not remove.has_value()) { /* remove returns an error */ | ||
std::cerr << "delete vector failed: " << remove.error().message << std::endl; | ||
abort(); | ||
} else if (not *remove) { /* id not exists, should NOT happend in this example */ | ||
std::cerr << "example error" << std::endl; | ||
abort(); | ||
} else { /* delete vector success */ | ||
std::cout << "delete old vector" << std::endl; | ||
if (auto add = index->Add(update_dataset); | ||
not add.has_value()) { /* add returns an error */ | ||
std::cout << "insert vector failed: " << add.error().message << std::endl; | ||
abort(); | ||
} else if ( | ||
not add->empty()) { /* not insert, id is already exist in index, shoud NOT happen in this example */ | ||
std::cerr << "example error" << std::endl; | ||
abort(); | ||
} else { | ||
std::cout << "insert new vector" << std::endl; | ||
} | ||
} | ||
} | ||
|
||
/******************* Search and Print Results *****************/ | ||
auto topk = 10; | ||
auto query_vector = new float[dim]; | ||
for (uint64_t i = 0; i < dim; ++i) { | ||
query_vector[i] = distrib_real(rng); | ||
} | ||
auto query = vsag::Dataset::Make(); | ||
query->NumElements(1)->Dim(dim)->Float32Vectors(query_vector)->Owner(false); | ||
auto search_parameters = R"( | ||
{ | ||
"hnsw": { | ||
"ef_search": 100 | ||
} | ||
} | ||
)"; | ||
if (auto knn_search = index->KnnSearch(query, topk, search_parameters); | ||
not knn_search.has_value()) { | ||
std::cerr << "search knn failed: " << knn_search.error().message << std::endl; | ||
abort(); | ||
} else { | ||
auto result = *knn_search; | ||
for (int64_t i = 0; i < result->GetDim(); ++i) { | ||
std::cout << result->GetIds()[i] << " " << result->GetDistances()[i] << std::endl; | ||
} | ||
} | ||
} |
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.
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.
update_vector -> update_status ?