lantern-django


Namelantern-django JSON
Version 0.0.1 PyPI version JSON
download
home_pagehttps://github.com/lanterndata/lantern-python
SummaryDjango client for Lantern
upload_time2024-11-06 11:58:31
maintainerNone
docs_urlNone
authorDi Qi
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Django Client for Lantern

## Install

```sh
pip install lantern-django
```

## Basic usage

Create a migration to enable the extensions:

```python
from django.db import migrations
from lantern_django import LanternExtension, LanternExtrasExtension

class Migration(migrations.Migration):
    operations = [
        LanternExtension(),
        LanternExtrasExtension(),
    ]
```

Add a `REAL[]` field to your model

```python
from django.db import models
from django.contrib.postgres.fields import ArrayField
from lantern_django import RealField

class Item(models.Model):
    embedding = ArrayField(RealField(), size=3, null=True)
```

Insert a vector

```python
item = Item(embedding=[1, 2, 3])
item.save()
```

Get the nearest neighbors to a vector

```python
from lantern_django import L2Distance

Item.objects.order_by(L2Distance('embedding', [3, 1, 2]))[:5]
```

Get nearest neighbors to a text embedding

```python
distance = L2Distance('embedding', TextEmbedding(
            'BAAI/bge-small-en', 'hello'))
        results = Item.objects.annotate(distance=distance).order_by('distance')[:5]
```

Get the distance

```python
Item.objects.annotate(distance=L2Distance('embedding', [3, 1, 2]))
```

Get items within a certain distance

```python
Item.objects.alias(distance=L2Distance('embedding', [3, 1, 2])).filter(distance__lt=5)
```

Add an index

```python
from lantern_django import HnswIndex

class Item(models.Model):
    class Meta:
        indexes = [
            HnswIndex(
                name='hnsw_idx',
                fields=['embedding'],
                m=16,
                ef=64,
                ef_construction=64,
                dim=384,
                opclasses=['dist_l2sq_ops']
            )
        ]
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/lanterndata/lantern-python",
    "name": "lantern-django",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Di Qi",
    "author_email": "di@lantern.dev",
    "download_url": "https://files.pythonhosted.org/packages/9f/76/549adc6eff1a529c96c649ef3f4002c364fc1fd8fe9867479d9c3b3e3896/lantern_django-0.0.1.tar.gz",
    "platform": null,
    "description": "# Django Client for Lantern\n\n## Install\n\n```sh\npip install lantern-django\n```\n\n## Basic usage\n\nCreate a migration to enable the extensions:\n\n```python\nfrom django.db import migrations\nfrom lantern_django import LanternExtension, LanternExtrasExtension\n\nclass Migration(migrations.Migration):\n    operations = [\n        LanternExtension(),\n        LanternExtrasExtension(),\n    ]\n```\n\nAdd a `REAL[]` field to your model\n\n```python\nfrom django.db import models\nfrom django.contrib.postgres.fields import ArrayField\nfrom lantern_django import RealField\n\nclass Item(models.Model):\n    embedding = ArrayField(RealField(), size=3, null=True)\n```\n\nInsert a vector\n\n```python\nitem = Item(embedding=[1, 2, 3])\nitem.save()\n```\n\nGet the nearest neighbors to a vector\n\n```python\nfrom lantern_django import L2Distance\n\nItem.objects.order_by(L2Distance('embedding', [3, 1, 2]))[:5]\n```\n\nGet nearest neighbors to a text embedding\n\n```python\ndistance = L2Distance('embedding', TextEmbedding(\n            'BAAI/bge-small-en', 'hello'))\n        results = Item.objects.annotate(distance=distance).order_by('distance')[:5]\n```\n\nGet the distance\n\n```python\nItem.objects.annotate(distance=L2Distance('embedding', [3, 1, 2]))\n```\n\nGet items within a certain distance\n\n```python\nItem.objects.alias(distance=L2Distance('embedding', [3, 1, 2])).filter(distance__lt=5)\n```\n\nAdd an index\n\n```python\nfrom lantern_django import HnswIndex\n\nclass Item(models.Model):\n    class Meta:\n        indexes = [\n            HnswIndex(\n                name='hnsw_idx',\n                fields=['embedding'],\n                m=16,\n                ef=64,\n                ef_construction=64,\n                dim=384,\n                opclasses=['dist_l2sq_ops']\n            )\n        ]\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Django client for Lantern",
    "version": "0.0.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/lanterndata/lantern-python/issues",
        "Homepage": "https://github.com/lanterndata/lantern-python"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c244725fd4047998a22b8253e6fdae8f689347b8857a97759d9beb4eda4249b3",
                "md5": "040384cbce48aa228034f631f9d77a64",
                "sha256": "d03dbbae2a3c2d91b9daf968abf8542700d9e90aa81665d6b4a6f24b1e248284"
            },
            "downloads": -1,
            "filename": "lantern_django-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "040384cbce48aa228034f631f9d77a64",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 4512,
            "upload_time": "2024-11-06T11:58:29",
            "upload_time_iso_8601": "2024-11-06T11:58:29.713126Z",
            "url": "https://files.pythonhosted.org/packages/c2/44/725fd4047998a22b8253e6fdae8f689347b8857a97759d9beb4eda4249b3/lantern_django-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9f76549adc6eff1a529c96c649ef3f4002c364fc1fd8fe9867479d9c3b3e3896",
                "md5": "022095f714939db453388f72d50dd3d1",
                "sha256": "ebe3ec98de7352354de2df78fb3ee14b1022ef6889063a0bf313b532569a862b"
            },
            "downloads": -1,
            "filename": "lantern_django-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "022095f714939db453388f72d50dd3d1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 3673,
            "upload_time": "2024-11-06T11:58:31",
            "upload_time_iso_8601": "2024-11-06T11:58:31.499990Z",
            "url": "https://files.pythonhosted.org/packages/9f/76/549adc6eff1a529c96c649ef3f4002c364fc1fd8fe9867479d9c3b3e3896/lantern_django-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-06 11:58:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lanterndata",
    "github_project": "lantern-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "lantern-django"
}
        
Elapsed time: 0.33815s