affine-vectordb


Nameaffine-vectordb JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryA unified interface for vector databases
upload_time2024-11-09 14:15:50
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # affine

![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/ekorman/7fbb57e6d6a2c8b69617ddf141043b98/raw/affine-coverage.json)

Affine is a Python library for providing a uniform and structured interface to various backing vector databases and approximate nearest neighbor libraries. It allows simple dataclass-like objects to describe collections together with a high-level query syntax for doing filtered vector search.

For vector databases, it currently supports:

- qdrant
- weaviate
- pinecone

For local mode, the following approximate nearest neighbor libraries are supported:

- FAISS
- annoy
- pynndescent
- scikit-learn KDTree
- naive/NumPy

Note: this project is very similar to [vectordb-orm](https://github.com/piercefreeman/vectordb-orm), which looks to be no longer maintained.

## Installation

```bash
pip install affine-vectordb
# or `pip install "affine-vectordb[qdrant]"` for qdrant support
# `pip install "affine-vectordb[weaviate]"` for weaviate support
# `pip install "affine-vectordb[pinecone]"` for pinecone support
```

## Basic Usage

```python
from affine import Collection, Vector, Filter, Query

# Define a collection
class MyCollection(Collection):
    vec: Vector[3] # declare a 3-dimensional vector

    # support for additional fields for filtering
    a: int
    b: str

db = LocalEngine()

# Insert vectors
db.insert(MyCollection(vec=[0.1, 0.0, -0.5], a=1, b="foo"))
db.insert(MyCollection(vec=[1.3, 2.1, 3.6], a=2, b="bar"))
db.insert(MyCollection(vec=[-0.1, 0.2, 0.3], a=3, b="foo"))

# Query vectors
result: list[MyCollection] = (
    db.query(MyCollection)
    .filter(MyCollection.b == "foo")
    .similarity([2.8, 1.8, -4.5])
    .limit(1)
)
```

## Engines

A fundamental notion of _affine_ are `Engine` classes. All such classes conform to the same API for interchangeabillity (with the exception of a few engine-specific restrictions which are be mentioned below). There are two broad types of engines

1. `LocalEngine`: this does nearest neighbor search on the executing machine, and supports a variety of libraries for the backing nearest neighborsearch (these are called the _backend_ of the local engine).

2. Vector database engines: these are engines that connect to a vector database service, such as QDrant, Weaviate, or Pinecone.

### Vector Databases

The currently supported vector databases are:

| Database | Class                          | Constructor arguments                                                                                                                                                                                                                                                                                                                          | Notes                                                                                                    |
| -------- | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
| Qdrant   | `affine.engine.QdrantEngine`   | `host: str` hostname to use<br><br>`port: int` port to use                                                                                                                                                                                                                                                                                     | -                                                                                                        |
| Weaviate | `affine.engine.WeaviateEngine` | `host: str` hostname to use<br><br>`port: int` port to use                                                                                                                                                                                                                                                                                     | -                                                                                                        |
| Pinecone | `affine.engine.PineconeEngine` | `api_key: Union[str, None]` pinecone API key. if not provided, it will be read from the environment variable PINECONE_API_KEY.<br><br>`spec: Union[ServerlessSpec, PodSpec, None]` the PodSpec or ServerlessSpec object. If not provided, a`ServerlessSpec` will be created from the environment variables PINECONE_CLOUD and PINECONE_REGION. | the Pinecone engine has the restriction that every collection must contain exactly one vector attribute. |

### Approximate Nearest Neighbor Libraries

The `LocalEngine` class provides an interface for doing nearest neighbor search on the executing machine, supporting a variety of libraries for the backing nearest neighborsearch. Which one is specified by the `backend` argument to the constructor. For example, to use `annoy`:

```python
from affine.engine.local import LocalEngine, AnnoyBackend

db = LocalEngine(backend=AnnoyBackend(n_tress=10))
```

The options and settings for the various supported backends are as follows:

| Library             | Class                                    | Constructor arguments                                                    | Notes |
| ------------------- | ---------------------------------------- | ------------------------------------------------------------------------ | ----- |
| naive/numpy         | `affine.engine.local.NumPyBackend`       | -                                                                        | -     |
| scikit-learn KDTree | `affine.engine.local.KDTreeBackend`      | keyword arguments that get passed directly to `sklearn.neighbors.KDTree` | -     |
| annoy               | `affine.engine.local.AnnoyBackend`       | `n_trees: int` number of trees to use<br>`n_jobs: int` defaults to -1    | -     |
| FAISS               | `affine.engine.local.FAISSBackend`       | `index_factory_str: str`                                                 | -     |
| PyNNDescent         | `affine.engine.local.PyNNDescentBackend` | keyword arguments that get passed directly to `pynndescent.NNDescent`    | -     |

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "affine-vectordb",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/5b/69/59dcf805e7bcff6ea0f4c2185f43932133e245ae505f974307a8301af923/affine_vectordb-0.1.0.tar.gz",
    "platform": null,
    "description": "# affine\n\n![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/ekorman/7fbb57e6d6a2c8b69617ddf141043b98/raw/affine-coverage.json)\n\nAffine is a Python library for providing a uniform and structured interface to various backing vector databases and approximate nearest neighbor libraries. It allows simple dataclass-like objects to describe collections together with a high-level query syntax for doing filtered vector search.\n\nFor vector databases, it currently supports:\n\n- qdrant\n- weaviate\n- pinecone\n\nFor local mode, the following approximate nearest neighbor libraries are supported:\n\n- FAISS\n- annoy\n- pynndescent\n- scikit-learn KDTree\n- naive/NumPy\n\nNote: this project is very similar to [vectordb-orm](https://github.com/piercefreeman/vectordb-orm), which looks to be no longer maintained.\n\n## Installation\n\n```bash\npip install affine-vectordb\n# or `pip install \"affine-vectordb[qdrant]\"` for qdrant support\n# `pip install \"affine-vectordb[weaviate]\"` for weaviate support\n# `pip install \"affine-vectordb[pinecone]\"` for pinecone support\n```\n\n## Basic Usage\n\n```python\nfrom affine import Collection, Vector, Filter, Query\n\n# Define a collection\nclass MyCollection(Collection):\n    vec: Vector[3] # declare a 3-dimensional vector\n\n    # support for additional fields for filtering\n    a: int\n    b: str\n\ndb = LocalEngine()\n\n# Insert vectors\ndb.insert(MyCollection(vec=[0.1, 0.0, -0.5], a=1, b=\"foo\"))\ndb.insert(MyCollection(vec=[1.3, 2.1, 3.6], a=2, b=\"bar\"))\ndb.insert(MyCollection(vec=[-0.1, 0.2, 0.3], a=3, b=\"foo\"))\n\n# Query vectors\nresult: list[MyCollection] = (\n    db.query(MyCollection)\n    .filter(MyCollection.b == \"foo\")\n    .similarity([2.8, 1.8, -4.5])\n    .limit(1)\n)\n```\n\n## Engines\n\nA fundamental notion of _affine_ are `Engine` classes. All such classes conform to the same API for interchangeabillity (with the exception of a few engine-specific restrictions which are be mentioned below). There are two broad types of engines\n\n1. `LocalEngine`: this does nearest neighbor search on the executing machine, and supports a variety of libraries for the backing nearest neighborsearch (these are called the _backend_ of the local engine).\n\n2. Vector database engines: these are engines that connect to a vector database service, such as QDrant, Weaviate, or Pinecone.\n\n### Vector Databases\n\nThe currently supported vector databases are:\n\n| Database | Class                          | Constructor arguments                                                                                                                                                                                                                                                                                                                          | Notes                                                                                                    |\n| -------- | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |\n| Qdrant   | `affine.engine.QdrantEngine`   | `host: str` hostname to use<br><br>`port: int` port to use                                                                                                                                                                                                                                                                                     | -                                                                                                        |\n| Weaviate | `affine.engine.WeaviateEngine` | `host: str` hostname to use<br><br>`port: int` port to use                                                                                                                                                                                                                                                                                     | -                                                                                                        |\n| Pinecone | `affine.engine.PineconeEngine` | `api_key: Union[str, None]` pinecone API key. if not provided, it will be read from the environment variable PINECONE_API_KEY.<br><br>`spec: Union[ServerlessSpec, PodSpec, None]` the PodSpec or ServerlessSpec object. If not provided, a`ServerlessSpec` will be created from the environment variables PINECONE_CLOUD and PINECONE_REGION. | the Pinecone engine has the restriction that every collection must contain exactly one vector attribute. |\n\n### Approximate Nearest Neighbor Libraries\n\nThe `LocalEngine` class provides an interface for doing nearest neighbor search on the executing machine, supporting a variety of libraries for the backing nearest neighborsearch. Which one is specified by the `backend` argument to the constructor. For example, to use `annoy`:\n\n```python\nfrom affine.engine.local import LocalEngine, AnnoyBackend\n\ndb = LocalEngine(backend=AnnoyBackend(n_tress=10))\n```\n\nThe options and settings for the various supported backends are as follows:\n\n| Library             | Class                                    | Constructor arguments                                                    | Notes |\n| ------------------- | ---------------------------------------- | ------------------------------------------------------------------------ | ----- |\n| naive/numpy         | `affine.engine.local.NumPyBackend`       | -                                                                        | -     |\n| scikit-learn KDTree | `affine.engine.local.KDTreeBackend`      | keyword arguments that get passed directly to `sklearn.neighbors.KDTree` | -     |\n| annoy               | `affine.engine.local.AnnoyBackend`       | `n_trees: int` number of trees to use<br>`n_jobs: int` defaults to -1    | -     |\n| FAISS               | `affine.engine.local.FAISSBackend`       | `index_factory_str: str`                                                 | -     |\n| PyNNDescent         | `affine.engine.local.PyNNDescentBackend` | keyword arguments that get passed directly to `pynndescent.NNDescent`    | -     |\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A unified interface for vector databases",
    "version": "0.1.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f5c955c6def2ec286136690ad9744b4f9b33f609d53df74a1b434179a6e0f2af",
                "md5": "53d3b2296d6a2d74919936699f1ff9c3",
                "sha256": "3fd5ab649730c790ca83660e640de6827be456fc4522dd397f1feddf38d27afd"
            },
            "downloads": -1,
            "filename": "affine_vectordb-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "53d3b2296d6a2d74919936699f1ff9c3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 16141,
            "upload_time": "2024-11-09T14:15:48",
            "upload_time_iso_8601": "2024-11-09T14:15:48.335044Z",
            "url": "https://files.pythonhosted.org/packages/f5/c9/55c6def2ec286136690ad9744b4f9b33f609d53df74a1b434179a6e0f2af/affine_vectordb-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5b6959dcf805e7bcff6ea0f4c2185f43932133e245ae505f974307a8301af923",
                "md5": "853c7a3610973744cc42b317abb5ce09",
                "sha256": "c99c24e3ba7744c6c8e28452f0fe2fd739b7ced80ab3c37732d979f0239a5862"
            },
            "downloads": -1,
            "filename": "affine_vectordb-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "853c7a3610973744cc42b317abb5ce09",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 20930,
            "upload_time": "2024-11-09T14:15:50",
            "upload_time_iso_8601": "2024-11-09T14:15:50.206892Z",
            "url": "https://files.pythonhosted.org/packages/5b/69/59dcf805e7bcff6ea0f4c2185f43932133e245ae505f974307a8301af923/affine_vectordb-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-09 14:15:50",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "affine-vectordb"
}
        
Elapsed time: 1.11340s