Metadata-Version: 2.4
Name: alayalite-dev
Version: 0.1.0a1
Summary: AlayaLite Python extension module
Home-page: https://github.com/AlayaDB-AI/AlayaLite
Author: AlayaDB.AI
Author-email: "AlayaDB.AI" <dev@alayadb.ai>
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
        
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pandas>=1.3.3
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Dynamic: author
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-python

# AlayaLite Python Interfaces 

This page introduces the Python interfaces in AlayaLite, which include two layers:

**Collection**: Manages raw data and its relationship with indexes. Users can build their knowledge base by integrating our *Collection* into other tools like [Open WebUI](https://github.com/open-webui/open-webui) and [LangChain](https://github.com/langchain-ai/langchain), or building their own RAG application following [our tutorial](../examples/rag/README.md).

**Index**: Handles search, insert, and delete operations for indexes. Researchers and developers can modify it to enhance algorithms or mechanisms. We support ANN-Benchmark evaluation in [here](./alayalite).

<p align="center">
    <img src="./.assets/user-protrait.svg" width=800 alt="AlayaDB Logo"></a>
</p>

In particular, collections and indexes are managed by **Client**. 
Next, we will introduce **Client**, **Collection** and **Index** in detail.

## Client
The Client provides a convenient interface for creating, retrieving, saving, and deleting collections and indices from disk.

### Initialize a client
You can initialize a client in following ways:
```python
# Initialize the client without a URL, you can process data in memory
client = Client()

# Initialize the client with a URL to load data from disk
client = Client(url='/path/to/your/directory')
```
* `list_collections()`: Returns a list of all collection names currently managed by the client.
* `list_indices()`: Returns a list of all index names currently managed by the client.

### Get a collection or index
Get a collection object or an index object
```python
# Get a collection by name
collection = client.get_collection(name='your_collection_name')

# Get an index by name
index = client.get_index(name='your_index_name')
```
* `get_collection(name: str)`: Retrieves a collection by name.
* `get_index(name: str)`: Retrieves an index by name.

### Create a collection or index
Creating a collection or an index:
```python
 Create a new collection
new_collection = client.create_collection(name='new_collection_name')

# Create a new index
new_index = client.create_index(name='new_index_name', index_type='your_index_type', data_type='your_data_type')
```

* `create_collection(name: str, **kwargs)`: Creates a new collection with the given name and parameters. Raises a RuntimeError if a collection or index with the same name already exists.
* `create_index(name: str, **kwargs)`: Creates a new index with the given name and parameters. Raises a RuntimeError if a collection or index with the same name already exists.

### Get or create a collection or index
We also provide interfaces for "getting or creating" interface:
```python
# Get or create a collection
collection = client.get_or_create_collection(name='your_collection_name')

# Get or create an index
index = client.get_or_create_index(name='your_index_name')
```

### Delete a collection 
In addition, you can deleting a collection or an index by following ways:
```python
# Delete a collection
client.delete_collection(collection_name='your_collection_name', delete_on_disk=True)

# Delete an index
client.delete_index(index_name='your_index_name', delete_on_disk=True)
```

* `delete_collection(collection_name: str, delete_on_disk: bool)`: Deletes a collection by name you maintained in memory. Optionally, it can also delete the collection from disk. Raises a RuntimeError if the collection does not exist.
* `delete_index(index_name: str, delete_on_disk: bool)`: Deletes an index by name you maintained in memory. Optionally, it can also delete the index from disk. Raises a RuntimeError if the index does not exist.

### Reset the client
Or you may want to delete everything
```python
client.reset()
```
* The `reset()` method clears all collections and indices from the client, removing all loaded data from memory. Then you can use the name to create a new one.

### Save the collection or index
When you want to save all collections and indices for persistence, you can use the following interface:
```python
# Save a collection
client.save_collection(collection_name='your_collection_name')

# Save an index
client.save_index(index_name='your_index_name')
```

* `save_collection(collection_name: str)`: Saves a collection to disk. The collection schema will be stored in a JSON file. Raises a RuntimeError if the client is not initialized with a URL or the collection does not exist.
* `save_index(index_name: str)`: Saves an index to disk. The index schema will be stored in a JSON file. Raises a RuntimeError if the client is not initialized with a URL or the index does not exist.

## Collection

The Collection class is a fundamental component for managing a set of documents along with their associated metadata and embeddings.

### Initialize a collection
You can initialize a Collection instance like this:
```python
from alayalite import Collection

collection = Collection(name='your_collection_name')
```
The `__init__` method creates an empty structure to hold the collection's data, including raw document and embeddings.

### Insert to a collection
To add new documents to the collection, you can use the insert method:
```python
items = [(1, 'document1', [1.0, 2.0, 3.0], {'category': 'A'}), (2, 'document2', [4.0, 5.0, 6.0], {'category': 'B'})]

collection.insert(items)
```
* `insert(items: List[tuple])`: Inserts a list of tuples, where each tuple contains an id, a document, an embedding, and metadata. 

### Upsert to a collection
If you want to insert new items or update existing ones if ids already exist, the upsert method is available:
```python
items = [(1, 'updated_document1', [1.1, 2.1, 3.1], {'category': 'A'}), (3, 'document3', [7.0, 8.0, 9.0], {'category': 'C'})]

collection.upsert(items)
```
* `upsert(items: List[tuple])`: Checks if an item with a given id already exists. If it does, the existing item is updated. If not, a new item is inserted.

### Query a batch of vectors to a collection
For retrieving data based on vector similarity, the `batch_query` method can be used:
```python
vectors = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
limit = 5
ef_search = 100
num_threads = 1

results = collection.batch_query(vectors, limit, ef_search, num_threads)
```
* `batch_query(vectors: list[list[float | int]], limit: int, ef_search: int = 100, num_threads: int = 1)`: Queries the index using a batch of vectors. `ef_search` and `num_thread` will be used forwarded as the parameters of search process.

### Filter query in a collection
If you want to filter data based on metadata, you can use the filter_query method:
```python
filter = {'category': ['A', 'B'], 'status': 1}

filtered_results = collection.filter_query(filter, 10)
```
* `filter_query(filter: dict, limit: Optional[int] = None)`: Filters the collection based on the given metadata conditions. 

### Delete by id
To remove documents from the collection by their id, you can use the delete_by_id method:

```python
ids = [1, 2]

collection.delete_by_id(ids)
```
* `delete_by_id(ids: List[int])`: Removes the documents with the specified ids.

### Delete by filtering metadata
If you want to delete items based on metadata filters, the `delete_by_filter` method is available:

```python
filter = {'category': 'A'}

collection.delete_by_filter(filter)
```
* `delete_by_filter(filter: dict)`: Deletes items from the collection that match the given metadata filter.

### Save a collection
To save the collection to disk for future use, you can use the save method:
```python
url = '/path/to/save/collection'

collection.save(url)
```
* `save(url: str)`: Saves the collection's data, including the DataFrame and mapping structures, to the specified directory. It also saves the index schema in a JSON file. The collection data is pickled and saved in a collection.pkl file.

### Load a collection
To load a previously saved collection from disk, you can use the load class method:
```python
url = '/path/where/collection/is/stored'
name = 'your_collection_name'

loaded_collection = Collection.load(url, name)
```

* `load(url: str, name: str)`: Loads a collection from the specified directory.

## Index

The Index class offers a python interface for handling vectors at the lowest level.

### Initialize an index
You can initialize a new Index instance in the following way:
```python
from your_module import Index, IndexParams

# Initialize with default name and default parameters
index = Index()

# Initialize with a custom name and custom parameters
params = IndexParams()
index = Index(name='your_index_name', params=params)
```
* The `__init__` method takes an optional name (default is "default") and params (an instance of IndexParams). The index is not fully initialized at this point; it's a late - initialization process. In particular, the dimension, data type, et al. is not known until it is fitted or the first vector is inserted. In addition, you can set the parameter to set the metric type or restrict the index type, data type, et al. 

### Build an index
To build the index with a set of vectors, use the fit method:
```python
import numpy as np
vectors = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
ef_construction = 100
num_threads = 1

index.fit(vectors, ef_construction, num_threads)
```
* `fit(vectors: VectorLikeBatch, ef_construction: int = 100, num_threads: int = 1)`: Constructs the index using the provided 2D array of vectors. The `ef_construction` parameter controls the accuracy of index construction, and `num_threads` specifies the number of threads to use during construction. The index can only be fitted once.

### Insert a vector into the index
To insert a new vector into the index, use the insert method:
```python
vector = np.array([7.0, 8.0, 9.0])
ef = 100

inserted_id = index.insert(vector, ef)
```
* `insert(vectors: VectorLike, ef: int = 100)`: Inserts a 1D vector into the index. The `ef` parameter controls the retrieval accuracy. It returns the assigned ID of the inserted vector.


### Remove a vector from the index
To remove a vector from the index by its ID, use the remove method:
```python
id_to_remove = 0
index.remove(id_to_remove)
```
* `remove(id: int)`: Removes the vector with the specified ID from the index.

### ANN Search of a single query
For single - query searches, use the search method:
```python
query = np.array([1.0, 2.0, 3.0])
topk = 2
ef_search = 100

neighbors = index.search(query, topk, ef_search)
```
* `search(query: VectorLike, topk: int, ef_search: int = 100)`: Performs an ANN search for the given 1D query vector. It returns the top-k nearest neighbors.

### ANN Search of a batch of queries
For batch searches, use the batch_search method:
```python
queries = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
topk = 2
ef_search = 100
num_threads = 1

batch_neighbors = index.batch_search(queries, topk, ef_search, num_threads)
```
* `batch_search(queries: VectorLikeBatch, topk: int, ef_search: int = 100, num_threads: int = 1)`: Performs a batch search for multiple query vectors. It returns the top-k nearest neighbors for each query.

### Get the vector data by ID
To get the vector data associated with a given ID, use the `get_data_by_id` method:
```python
id = 0
vector_data = index.get_data_by_id(id)
```
* `get_data_by_id(id: int)`: Retrieves the vector data corresponding to the given ID.

### Get the dimensionality of the vectors
To get the dimensionality of the vectors stored in the index, use the get_dim method:
```python
dim = index.get_dim()
```
* `get_dim()`: Returns the dimension of the indexed vectors.

### Save an index
To save the index to disk, use the save method:
```python
url = '/path/to/save/index'
index_metadata = index.save(url)
```
* `save(url: str)`: Saves the index to the specified directory. It returns a dictionary containing metadata about the saved index.

### Load an index
To load an existing index from disk, use the load class method:
```python
url = '/path/where/index/is/stored'
name = 'your_index_name'
loaded_index = Index.load(url, name)
```
* `load(url: str, name: str)`: Loads an index from the specified directory. It returns an instance of the Index class with the loaded index data.

<!-- 

## Installation

### Local Compilation and Installation


To compile and install the Alaya Lite PY package locally, use the provided script as follows:
```bash
conda env create --file=environment.yaml
conda activate alayalite
bash build_support/pyinstall.sh
```

> The script performs the following steps:
>
> ```Bash
> # Locally compile the python package
> python -m build
> 
> # Install the compiled package
> pip install pyalaya/AlayaDBLite/AlayaDBLite-*.whl --force-reinstall
> ```


### 3. Training the Dataset

```Python
max_nbrs       = 32
fit_thread_num = 96

client.fit(base_data, max_nbrs, fit_thread_num)
```

> ```Python
> def fit(
>      self,
>      vectors: VectorLikeBatch,
>      M: int,
>      num_threads: int = _defaults.NUM_THREADS,
>      R: int = _defaults.R,
>      L: int = _defaults.L,
>      index_path: str = _defaults.INDEX_PATH,
>      index_prefix: str = _defaults.INDEX_PREFIX,
>  ) -> None
>  """
>  fit the index with the given vectors
>  :param vectors: 2D array of vectors to fit the index
>  :param M: maximum number of neighbors
>  :param num_threads: number of threads to use
>  :param R: the maximum out-degree of the underlay graph, and half of R is the maximum out-degree of the overlay graph.
>  :param L: The size of the dynamic candidate list for the construction phase.
>  :param index_path: path to store the index
>  :param index_prefix: prefix of the index file
> 
>  :return: None
>  """
> ``` -->
<!-- 
### 4. Searching

Perform batch and single searches as follows:

```Python
topk          = 10
ef            = 100
search_thread = 32

# batch search 
results = client.batch_search(query_data, topk, ef, search_thread)

# single search
result  = client.search(query_data[0], topk, ef)
```

> ```Python
> def batch_search(
>      self,
>      queries: VectorLikeBatch,
>      topk: int,
>      ef_search: int = _defaults.EF_SEARCH,
>      num_threads: int = _defaults.NUM_THREADS,
>  ) -> VectorLikeBatch:
>  """
>  search for the topk nearest neighbors for a batch of queries
>  :param queries: 2D array of queries
>  :param topk: number of nearest neighbors to search for
>  :param ef_search: the number of candidate to evaluate during the search
>  :param num_threads: number of threads to use
> 
>  :return: 2D array of nearest neighbors for each query
>  """
> 
> def search(
>      self, 
>      query: VectorLike, 
>      topk: int, 
>      ef_search: int = _defaults.EF_SEARCH
>  ) -> VectorLike:
>  """
>  search for the topk nearest neighbors for a single query
>  :param query: 1D array of query
>  :param topk: number of nearest neighbors to search for
>  :param ef_search: the number of candidate to evaluate during the search
> 
>  :return: 1D array of nearest neighbors for the query
>  """
> ```

## Conducting ANN-Benchmark Tests

### Setup for Benchmarking

Clone the ANN-Benchmarks repository and set up the environment:

```bash
git clone git@github.com:erikbern/ann-benchmarks.git
# Ensure the `pyalaya/AlayaDBLite` is copied to the `ann_benchmarks/algorithms` directory
```
-->





<!-- 
Install necessary dependencies:

```Bash
pip install -r requirements.txt
```

Initialize the AlayaDBLite Docker environment:

```
python install.py --algorithm AlayaDBLite
```

### Running Benchmarks

Execute benchmarks for specified datasets:

```
python run.py --algorithm AlayaDBLite --dataset DATASET_NAME
# Replace DATASET_NAME with e.g., gist-960-euclidean
```

### Plotting the Results

Adjust permissions and generate result plots:

```
sudo chmod 777 -R results && python plot.py --dataset DATASET_NAME
```  -->
