Skip to main content

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

Python libraries

pip3 install opensearch-py

Usage

from src.opencone.opencone import OpenCone
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 = OpenCone(client=client)

# 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
titles = ["pp", "qq", "rr", "ss", "tt"]
tags = ["p1", "p2", "p3", "p4", "p5"]
vectors = []
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
print(oc.fetch(index_name="test", _id="id:1"))

# Delete vector
print(oc.delete(index_name="test", _id="id:1"))


# 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 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.1.tar.gz (34.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opencone-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file opencone-0.0.1.tar.gz.

File metadata

  • Download URL: opencone-0.0.1.tar.gz
  • Upload date:
  • Size: 34.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for opencone-0.0.1.tar.gz
Algorithm Hash digest
SHA256 873fd891f8f6fd3756ba2cf3022d8bc3bff990231663cdb669ee4eadd334bd81
MD5 528fd8453aa8181e0572b616ffe5fd93
BLAKE2b-256 70d60e03b89854b501c0def0d0377063d1e499aa4a2356a1f54a06f70fba2373

See more details on using hashes here.

File details

Details for the file opencone-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: opencone-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for opencone-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77897d424429b47e594d781ad3954c350e28d2e6ff341c375efe34c91e326e6b
MD5 c764a153a79be9a947ed7e434823f5e8
BLAKE2b-256 418a016fc531877ffee8cd1a3b17702f6c45a4efe292dca7c3e69888b8fe6aa6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page