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
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.3.tar.gz (36.2 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.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: opencone-0.0.3.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

Hashes for opencone-0.0.3.tar.gz
Algorithm Hash digest
SHA256 cad997b66be1b0105551416478ad7b58abfa9a7dbcf92cdc04990c42f3789b32
MD5 7aeb4ea15504cc5bf4f1b42c964b4c04
BLAKE2b-256 e096b4ff77e84f55724a5692886d43505189a3da97d5ae778bd8314f32bd05b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: opencone-0.0.3-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

Hashes for opencone-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bd82c439e1562c5778297d25cc83d27e54e9d07f8475a7f65670fab2bb99f2da
MD5 ec00c44acd9022e8281b04cad714ae5a
BLAKE2b-256 7858ea3ad6bc775f66eb05471378ed66e9724bc5931eb44fefa6a14cb6535b39

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