A vector database wrapper on OpenSearch backend. [An alternative to Pinecone]
Project description
Opencone
A Pinecone alternative written on top of OpenSearch by simplifying features of OpenSearch
Requirements
- Install OpenSearch Backend
- or You can use a cloud hosted OpenSearch Backend
Python libraries
pip3 install opensearch-py
pip3 install opencone
Usage
Create an OpenconeClient
from opencone import OpenconeClient
import random
import numpy as np
import time
from opensearchpy import OpenSearch
host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
# Create an OpenSearch client
client = OpenSearch(
hosts=[{"host": host, "port": port}],
http_auth=auth,
use_ssl=True,
verify_certs=False,
ssl_assert_hostname=False,
ssl_show_warn=False
)
oc = OpenconeClient(client=client)
Create an Index
# Create an index
dimensions = 4096
t = time.time()
try:
oc.create_index(index_name="test", dimensions=dimensions)
except Exception as ex:
if "resource_already_exists_exception" in str(ex):
oc.delete_index(index_name="test")
oc.create_index(index_name="test", dimensions=dimensions)
else:
raise ex
print("Index created in ", time.time()-t)
Upsert Vectors
# Upsert vectors
# [(<id>, <embeddings>, <metadata>),...]
titles = ["pp", "qq", "rr", "ss", "tt"]
tags = ["p1", "p2", "p3", "p4", "p5"]
vectors = []
# Recommend to upsert 100 or less at a time
for i in range(100):
vectors.append((
"id:"+str(i),
np.random.randint(5, size=dimensions).tolist(),
{
"name": titles[random.randint(0, 4)],
"tags": tags[random.randint(0, 4):random.randint(0, 4)],
"no": random.randint(2000, 3000)
}
))
t = time.time()
oc.upsert(index_name="test", vectors=vectors)
print("Upsert time:", time.time()-t)
Fetch Vector
# Fetch vector
print(oc.fetch(index_name="test", _id="id:1"))
Delete vector
# Delete vector
print(oc.delete(index_name="test", _id="id:1"))
Search
# Search
filters = {
"no": {"$gte":2200, "$lte":2800},
"name": {"$eq": "pp"},
"tags": {"$in": ["p1", "t3"]}
}
t = time.time()
rs = oc.search(index_name="test", vector=(np.random.randint(5, size=dimensions)/1.1).tolist(), filters=filters, metadata=False, limit=10000)
print("Search time:", time.time()-t)
for each in rs:
print(each)
Delete an Index
# Delete index
oc.delete_index(index_name="test")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
opencone-0.0.4.tar.gz
(36.2 MB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opencone-0.0.4.tar.gz.
File metadata
- Download URL: opencone-0.0.4.tar.gz
- Upload date:
- Size: 36.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03041c557a801b013635ac6ca1ff201b0f18ae48065b3cce1dfbc790a557056a
|
|
| MD5 |
8259eaab6a11f356a9cff5bc12f831e0
|
|
| BLAKE2b-256 |
1f85727fbb8f4fd9bad27deafdf24f82bc352517f3acd36bd0d03f7fd95379de
|
File details
Details for the file opencone-0.0.4-py3-none-any.whl.
File metadata
- Download URL: opencone-0.0.4-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a59516c1384929614f98b74d6129394e52c6c6613bf7d34635cc12b01b6f381
|
|
| MD5 |
f15d38442ec400473b85bec1aa1e6c11
|
|
| BLAKE2b-256 |
129ef2652412ecba2371c74769f09ec3f86a47fe612663e91dbe09bc557a81ac
|