Skip to main content

Serverless Vector SDK from Upstash

Project description

Upstash Vector Python SDK

The Upstash Vector python client

Installation

Install a released version from pip:

pip3 install upstash-vector

Usage

In order to use this client, head out to Upstash Console and create a vector database. There, get the URL and the TOKEN from the dashboard.

Initialize the client

from upstash_vector import Index

index = Index(url=UPSTASH_VECTOR_REST_URL, token=UPSTASH_VECTOR_REST_TOKEN)

or alternatively, initialize from the environment

from upstash_vector import Index

index = Index.from_env()

Upsert Vectors

There are couple ways to upsert vectors. Feel free to use whichever one feels the most comfortable.

index.upsert(
    vectors=[
        ("id1", [0.1, 0.2], {"metadata_field": "metadata_value"}),
        ("id2", [0.3, 0.4]),
    ]
)
index.upsert(
    vectors=[
        {"id": "id3", "vector": [0.1, 0.2], "metadata": {"metadata_f": "metadata_v"}},
        {"id": "id4", "vector": [0.5, 0.6]},
    ]
)
from upstash_vector import Vector

index.upsert(
    vectors=[
        Vector(id="id5", vector=[1, 2], metadata={"metadata_f": "metadata_v"}),
        Vector(id="id6", vector=[6, 7]),
    ]
)

Query Index

query_vector = [0.6, 0.9]
top_k = 6
query_res = index.query(
    vector=query_vector,
    top_k=top_k,
    include_vectors=True,
    include_metadata=True,
)
# query_res is a list of vectors with scores:
# query_res[n].id: The identifier associated with the matching vector.
# query_res[n].score: A measure of similarity indicating how closely the vector matches the query vector.
# query_res[n].vector: The vector itself (included only if `include_vector` is set to `True`).
# query_res[n].metadata: Additional information or attributes linked to the matching vector.

Fetch Indexes

res = index.fetch(["id3", "id4"], include_vectors=True, include_metadata=True)
# res.vectors: A list containing information for each fetched vector, including `id`, `vector`, and `metadata`.

or, for singular fetch:

res = index.fetch("id1", include_vectors=True, include_metadata=True)

Range over Vectors - Scan the Index

# Scans the index 3 by 3, until all the indexes are traversed.
res = index.range(cursor="", limit=3, include_vectors=True, include_metadata=True)
while res.next_cursor != "":
    res = index.range(cursor=res.next_cursor, limit=3, include_vectors=True, include_metadata=True)

# res.nex_cursor: A cursor indicating the position to start the next range query. If "", there are no more results.
# res.vectors: A list containing information for each vector, including `id`, `vector`, and `metadata`.

Delete Vectors

res = index.delete(["id1", "id2"])
# res.deleted: An integer indicating how many vectors were deleted with the command.

or, for singular deletion:

res = index.delete("id1")

Reset the Index

# This will remove all the vectors that were upserted and index will be reset.
index.reset() 

Index Stats

stats = index.stats()
# stats.vector_count: total number of vectors in the index
# stats.pending_vector_count: total number of vectors waiting to be indexed
# stats.index_size: total size of the index on disk in bytes 

Contributing

Preparing the environment

This project uses Poetry for packaging and dependency management. Make sure you are able to create the poetry shell with relevant dependencies.

You will also need a vector database on Upstash.

poetry install 

Code Formatting

poetry run ruff format .

Running tests

To run all the tests, make sure the poetry virtual environment activated with all the necessary dependencies. Set the necessary environment variables and run:

poetry run pytest

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

upstash_vector-0.1.4.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

upstash_vector-0.1.4-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file upstash_vector-0.1.4.tar.gz.

File metadata

  • Download URL: upstash_vector-0.1.4.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1018-azure

File hashes

Hashes for upstash_vector-0.1.4.tar.gz
Algorithm Hash digest
SHA256 b752981c38626a364da3101b65a849d5ce448062929450f849fa55802e4e6133
MD5 e301945d70d79170f1b7c4d75441f0ee
BLAKE2b-256 9b0af67224d05cc5c86722a816ae4ce6d3dce14fb08f5566ee357ab96cd1d352

See more details on using hashes here.

Provenance

File details

Details for the file upstash_vector-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: upstash_vector-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1018-azure

File hashes

Hashes for upstash_vector-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e4db01d7fc0b498e0d865ecfb4c9fd70ebdd2597df07bf16761771e628e3ff2e
MD5 40a413a0db5414b9e510dd612913475a
BLAKE2b-256 a655e5295edced7374a7aca85b3b5ce427acdfef5c5a247c6c21a9eae8b1fc16

See more details on using hashes here.

Provenance

Supported by

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