-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from the-alex-b/0.3.0
0.3.0
- Loading branch information
Showing
3 changed files
with
61 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,53 @@ | ||
# from locus import Index, Vector, Config | ||
# import numpy as np | ||
# from pprint import pprint | ||
import numpy as np | ||
from locusdb import Config, Index, Vector | ||
import cProfile | ||
|
||
# index = Index(dimensions=10) | ||
|
||
# embedding = np.float32(np.random.random((1, 10))) | ||
# structured_data = {"a": 1} | ||
# vector = Vector(embedding=embedding, data=structured_data) | ||
def profile_performance(): | ||
num_of_elements = 1000 | ||
|
||
# create a new configuration | ||
config = Config( | ||
max_elements=num_of_elements, | ||
ef_construction=200, | ||
M=16, | ||
dim=128, | ||
space="cosine", | ||
storage_location="index.db", | ||
) | ||
|
||
# index.add_vector(vector) | ||
# create a new index instance | ||
index = Index(dimensions=config.dim, config=config) | ||
|
||
import numpy as np | ||
# create some random vectors | ||
vectors = [] | ||
for i in range(num_of_elements): | ||
embedding = np.random.randn(config.dim) | ||
data = {"id": i, "message": f"test message {i}"} | ||
vector = Vector(embedding=embedding, data=data) | ||
vectors.append(vector) | ||
|
||
# add the vectors to the index | ||
for vector in vectors: | ||
index.add_vector(vector, persist_on_disk=False) # persisting is very expensive | ||
|
||
# retrieve the closest vectors to a query embedding | ||
query_embedding = np.random.randn(config.dim) | ||
results = index.retrieve(query_embedding, number_of_results=3) | ||
|
||
print(f"Matches: {results}") | ||
print(f"Items in index: {index.count}") | ||
|
||
# store the index on disk | ||
index.persist_on_disk() | ||
|
||
# load the index from disk | ||
new_index = Index.from_file(config.storage_location) | ||
|
||
# retrieve the closest vectors to a query embedding | ||
# query_embedding = np.random.randn(config.dim) | ||
# results = new_index.retrieve(query_embedding, number_of_results=3) | ||
# print(results) | ||
|
||
from locusdb import Config, Index, Vector | ||
|
||
# create a new configuration | ||
config = Config( | ||
max_elements=1000, | ||
ef_construction=200, | ||
M=16, | ||
dim=128, | ||
space="cosine", | ||
storage_location="index.db", | ||
) | ||
|
||
# create a new index instance | ||
index = Index(dimensions=config.dim, config=config) | ||
|
||
# create some random vectors | ||
vectors = [] | ||
for i in range(10): | ||
embedding = np.random.randn(config.dim) | ||
data = {"id": i, "message": f"test message {i}"} | ||
vector = Vector(embedding=embedding, data=data) | ||
vectors.append(vector) | ||
|
||
# add the vectors to the index | ||
for vector in vectors: | ||
index.add_vector(vector) | ||
|
||
# retrieve the closest vectors to a query embedding | ||
query_embedding = np.random.randn(config.dim) | ||
results = index.retrieve(query_embedding, number_of_results=3) | ||
print(results) | ||
|
||
# store the index on disk | ||
index._store_on_disk() | ||
|
||
# load the index from disk | ||
new_index = Index.from_file(config.storage_location) | ||
cProfile.run("profile_performance()") |