Skip to main content

Django client for Lantern

Project description

Django Client for Lantern

Install

pip install lantern-django

Basic usage

Create a migration to enable the extensions:

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

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

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

Get the nearest neighbors to a vector

from lantern_django import L2Distance

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

Get nearest neighbors to a text embedding

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

Get the distance

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

Get items within a certain distance

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

Add an index

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']
            )
        ]

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

lantern_django-0.0.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

lantern_django-0.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file lantern_django-0.0.1.tar.gz.

File metadata

  • Download URL: lantern_django-0.0.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for lantern_django-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ebe3ec98de7352354de2df78fb3ee14b1022ef6889063a0bf313b532569a862b
MD5 022095f714939db453388f72d50dd3d1
BLAKE2b-256 9f76549adc6eff1a529c96c649ef3f4002c364fc1fd8fe9867479d9c3b3e3896

See more details on using hashes here.

Provenance

The following attestation bundles were made for lantern_django-0.0.1.tar.gz:

Publisher: publish.yml on lanterndata/lantern-python

Attestations:

File details

Details for the file lantern_django-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for lantern_django-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d03dbbae2a3c2d91b9daf968abf8542700d9e90aa81665d6b4a6f24b1e248284
MD5 040384cbce48aa228034f631f9d77a64
BLAKE2b-256 c244725fd4047998a22b8253e6fdae8f689347b8857a97759d9beb4eda4249b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for lantern_django-0.0.1-py3-none-any.whl:

Publisher: publish.yml on lanterndata/lantern-python

Attestations:

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