Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

annlite

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

annlite

Fast and Light Approximate Nearest Neighbor Search Library integrated with the Jina Ecosystem

  • 0.5.10
  • PyPI
  • Socket score

Maintainers
1




AnnLite logo: A fast and efficient ann libray

A fast embedded library for approximate nearest neighbor search

GitHub PyPI Codecov branch

What is AnnLite?

AnnLite is a lightweight and embeddable library for fast and filterable approximate nearest neighbor search (ANNS). It allows to search for nearest neighbors in a dataset of millions of points with a Pythonic API.

Highlighted features:

  • 🐥 Easy-to-use: a simple API is designed to be used with Python. It is easy to use and intuitive to set up to production.

  • 🐎 Fast: the library uses a highly optimized approximate nearest neighbor search algorithm (HNSW) to search for nearest neighbors.

  • 🔎 Filterable: the library allows you to search for nearest neighbors within a subset of the dataset.

  • 🍱 Integration: Smooth integration with neural search ecosystem including Jina and DocArray, so that users can easily expose search API with gRPC and/or HTTP.

The library is easy to install and use. It is designed to be used with Python.

Installation

To use AnnLite, you need to first install it. The easiest way to install AnnLite is using pip:

pip install -U annlite

or install from source:

python setup.py install

Quick start

Before you start, you need to know some experience about DocArray. AnnLite is designed to be used with DocArray, so you need to know how to use DocArray first.

For example, you can create a DocArray with 1000 random vectors with 128 dimensions:

from docarray import DocumentArray
import numpy as np

docs = DocumentArray.empty(1000)
docs.embeddings = np.random.random([1000, 128]).astype(np.float32)

Index

Then you can create an AnnIndexer to index the created docs and search for nearest neighbors:

from annlite import AnnLite

ann = AnnLite(128, metric='cosine', data_path="/tmp/annlite_data")
ann.index(docs)

Note that this will create a directory /tmp/annlite_data to persist the documents indexed. If this directory already exists, the index will be loaded from the directory. And if you want to create a new index, you can delete the directory first.

Then you can search for nearest neighbors for some query docs with ann.search():

query = DocumentArray.empty(5)
query.embeddings = np.random.random([5, 128]).astype(np.float32)

result = ann.search(query)

Then, you can inspect the retrieved docs for each query doc inside query matches:

for q in query:
    print(f'Query {q.id}')
    for k, m in enumerate(q.matches):
        print(f'{k}: {m.id} {m.scores["cosine"]}')
Query ddbae2073416527bad66ff186543eff8
0: 47dcf7f3fdbe3f0b8d73b87d2a1b266f {'value': 0.17575037}
1: 7f2cbb8a6c2a3ec7be024b750964f317 {'value': 0.17735684}
2: 2e7eed87f45a87d3c65c306256566abb {'value': 0.17917466}
Query dda90782f6514ebe4be4705054f74452
0: 6616eecba99bd10d9581d0d5092d59ce {'value': 0.14570713}
1: d4e3147fc430de1a57c9883615c252c6 {'value': 0.15338594}
2: 5c7b8b969d4381f405b8f07bc68f8148 {'value': 0.15743542}
...

Or shorten the loop as one-liner using the element & attribute selector:

print(query['@m', ('id', 'scores__cosine')])

Query

You can get specific document by its id:

doc = ann.get_doc_by_id('<doc_id>')

And you can also get the documents with limit and offset, which is useful for pagination:

docs = ann.get_docs(limit=10, offset=0)

Furthermore, you can also get the documents ordered by a specific column from the index:

docs = ann.get_docs(limit=10, offset=0, order_by='x', ascending=True)

Note: the order_by column must be one of the columns in the index.

Update

After you have indexed the docs, you can update the docs in the index by calling ann.update():

updated_docs = docs.sample(10)
updated_docs.embeddings = np.random.random([10, 128]).astype(np.float32)

ann.update(updated_docs)

Delete

And finally, you can delete the docs from the index by calling ann.delete():

to_delete = docs.sample(10)
ann.delete(to_delete)

Search with filters

To support search with filters, the annlite must be created with colums parameter, which is a series of fields you want to filter by. At the query time, the annlite will filter the dataset by providing conditions for certain fields.

import annlite

# the column schema: (name:str, dtype:type, create_index: bool)
ann = annlite.AnnLite(128, columns=[('price', float)], data_path="/tmp/annlite_data")

Then you can insert the docs, in which each doc has a field price with a float value contained in the tags:

import random

docs = DocumentArray.empty(1000)
docs = DocumentArray(
    [
        Document(id=f'{i}', tags={'price': random.random()})
        for i in range(1000)
    ]
)

docs.embeddings = np.random.random([1000, 128]).astype(np.float32)

ann.index(docs)

Then you can search for nearest neighbors with filtering conditions as:

query = DocumentArray.empty(5)
query.embeddings = np.random.random([5, 128]).astype(np.float32)

ann.search(query, filter={"price": {"$lte": 50}}, limit=10)
print(f'the result with filtering:')
for i, q in enumerate(query):
    print(f'query [{i}]:')
    for m in q.matches:
        print(f'\t{m.id} {m.scores["euclidean"].value} (price={m.tags["price"]})')

The conditions parameter is a dictionary of conditions. The key is the field name, and the value is a dictionary of conditions. The query language is the same as MongoDB Query Language. We currently support a subset of those selectors.

  • $eq - Equal to (number, string)
  • $ne - Not equal to (number, string)
  • $gt - Greater than (number)
  • $gte - Greater than or equal to (number)
  • $lt - Less than (number)
  • $lte - Less than or equal to (number)
  • $in - Included in an array
  • $nin - Not included in an array

The query will be performed on the field if the condition is satisfied. The following is an example of a query:

  1. A Nike shoes with white color

    {
      "brand": {"$eq": "Nike"},
      "category": {"$eq": "Shoes"},
      "color": {"$eq": "White"}
    }
    

    We also support boolean operators $or and $and:

    {
      "$and":
        {
          "brand": {"$eq": "Nike"},
          "category": {"$eq": "Shoes"},
          "color": {"$eq": "White"}
        }
    }
    
  2. A Nike shoes or price less than 100$:

    {
        "$or":
        {
        "brand": {"$eq": "Nike"},
        "price": {"$lte": 100}
        }
    }
    

Dump and Load

By default, the hnsw index is in memory. You can dump the index to data_path by calling .dump():


from annlite import AnnLite

ann = AnnLite(128, metric='cosine', data_path="/path/to/data_path")
ann.index(docs)
ann.dump()

And you can restore the hnsw index from data_path if it exists:

new_ann = AnnLite(128, metric='cosine', data_path="/path/to/data_path")

If you didn't dump the hnsw index, the index will be rebuilt from scratch. This will take a while.

Supported distance metrics

The annlite supports the following distance metrics:

Supported distances:
DistanceparameterEquation
Euclideaneuclideand = sqrt(sum((Ai-Bi)^2))
Inner productinner_productd = 1.0 - sum(Ai*Bi)
Cosine similaritycosined = 1.0 - sum(Ai*Bi) / sqrt(sum(Ai*Ai) * sum(Bi*Bi))

Note that inner product is not an actual metric. An element can be closer to some other element than to itself. That allows some speedup if you remove all elements that are not the closest to themselves from the index, e.g., inner_product([1.0, 1.0], [1.0. 1.0]) < inner_product([1.0, 1.0], [2.0, 2.0])

HNSW algorithm parameters

The HNSW algorithm has several parameters that can be tuned to improve the search performance.

Search parameters

  • ef_search - The size of the dynamic list for the nearest neighbors during search (default: 50). The larger the value, the more accurate the search results, but the slower the search speed. The ef_search must be larger than limit parameter in search(..., limit).

  • limit - The maximum number of results to return (default: 10).

Construction parameters

  • max_connection - The number of bi-directional links created for every new element during construction (default: 16). Reasonable range is from 2 to 100. Higher values works better for dataset with higher dimensionality and/or high recall. This parameter also affects the memory consumption during construction, which is roughly max_connection * 8-10 bytes per stored element.

    As an example for n_dim=4 random vectors optimal max_connection for search is somewhere around 6, while for high dimensional datasets, higher max_connection are required (e.g. M=48-64) for optimal performance at high recall. The range max_connection=12-48 is ok for the most of the use cases. When max_connection is changed one has to update the other parameters. Nonetheless, ef_search and ef_construction parameters can be roughly estimated by assuming that max_connection * ef_{construction} is a constant.

  • ef_construction: The size of the dynamic list for the nearest neighbors during construction (default: 200). Higher values give better accuracy, but increase construction time and memory consumption. At some point, increasing ef_construction does not give any more accuracy. To set ef_construction to a reasonable value, one can measure the recall: if the recall is lower than 0.9, then increase ef_construction and re-run the search.

To set the parameters, you can define them when creating the annlite:

from annlite import AnnLite

ann = AnnLite(128, columns=[('price', float)], data_path="/tmp/annlite_data", ef_construction=200, max_connection=16)

Benchmark

One can run executor/benchmark.py to get a quick performance overview.

Stored dataIndexing timeQuery size=1Query size=8Query size=64
100002.9700.0020.0130.100
10000076.4740.0110.0780.649
500000467.9360.0460.3562.823
10000001025.5060.0910.6955.778

Results with filtering can be generated from examples/benchmark_with_filtering.py. This script should produce a table similar to:

Stored data% same filterIndexing timeQuery size=1Query size=8Query size=64
1000052.8690.0040.0300.270
10000152.8690.0040.0350.294
10000203.5060.0050.0380.287
10000303.5060.0050.0440.356
10000503.5060.0080.0640.484
10000802.8690.0130.0980.910
100000575.9600.0180.1341.092
1000001575.9600.0260.2111.736
1000002078.4750.0340.2652.097
1000003078.4750.0440.3572.887
1000005078.4750.0680.5654.383
1000008075.9600.1110.8786.815
5000005497.7440.0690.5614.439
50000015497.7440.1341.0648.469
50000020440.1080.1521.1999.472
50000030440.1080.2121.65013.267
50000050440.1080.3282.63721.961
50000080497.7440.5804.60236.986
100000051052.3880.1311.0318.212
1000000151052.3880.2632.19116.643
100000020980.5980.3512.65921.193
100000030980.5980.4613.71329.794
100000050980.5980.7325.97547.356
1000000801052.3881.1519.25573.552

Note that:

  • query times presented are represented in seconds.
  • % same filter indicates the amount of data that verifies a filter in the database.
    • For example, if % same filter = 10 and Stored data = 1_000_000 then it means 100_000 example verify the filter.

Next steps

If you already have experience with Jina and DocArray, you can start using AnnLite right away.

Otherwise, you can check out this advanced tutorial to learn how to use AnnLite: here in practice.

🙋 FAQ

1. Why should I use AnnLite?

AnnLite is easy to use and intuitive to set up in production. It is also very fast and memory efficient, making it a great choice for approximate nearest neighbor search.

2. How do I use AnnLite with Jina?

We have implemented an executor for AnnLite that can be used with Jina.

from jina import Flow

with Flow().add(uses='jinahub://AnnLiteIndexer', uses_with={'n_dim': 128}) as f:
    f.post('/index', inputs=docs)
  1. Does AnnLite support search with filters?
Yes.

Documentation

You can find the documentation on Github and ReadTheDocs

🤝 Contribute and spread the word

We are also looking for contributors who want to help us improve: code, documentation, issues, feedback! Here is how you can get started:

  • Have a look through GitHub issues labeled "Good first issue".
  • Read our Contributor Covenant Code of Conduct
  • Open an issue or submit your pull request!

License

AnnLite is licensed under the Apache License 2.0.

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc