Database / pgvector basics Interview Questions
How do you use pgvector with Django?
The pgvector Python package includes a Django integration that provides a VectorField model field, enabling vector storage and similarity search within Django ORM queries.
# pip install pgvector django psycopg2-binary # settings.py - make sure django uses PostgreSQL: DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "mydb", "USER": "user", "PASSWORD": "pass", "HOST": "localhost", } } # models.py from django.db import models from pgvector.django import VectorField, HnswIndex class Document(models.Model): content = models.TextField() embedding = VectorField(dimensions=1536) # pgvector field class Meta: indexes = [ HnswIndex( name="document_embedding_hnsw", fields=["embedding"], m=16, ef_construction=64, opclasses=["vector_cosine_ops"], ) ] # Migration: generates CREATE EXTENSION vector + table # python manage.py makemigrations && python manage.py migrate # views.py / management commands: from pgvector.django import CosineDistance # Insert Document.objects.create(content="Example", embedding=[0.1, 0.2, ...]) # Query: 5 most similar documents query_vector = get_embedding("search query") results = Document.objects.annotate( distance=CosineDistance("embedding", query_vector) ).order_by("distance")[:5] for doc in results: print(doc.content, doc.distance)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
