Signals & Auto-Indexing¶
Django RAGKit includes built-in Django signals that eliminate manual vectorization workflows. Whenever knowledge entries are created or updated, their vector representations are automatically computed and indexed.
Implementation Details¶
The signal receivers are registered in django_ragkit/signals.py and connected when the Django app initializes.
1. Automatic Creation (post_save)¶
When a new QuestionAnswer instance is saved for the first time:
@receiver(post_save, sender=QuestionAnswer)
def create_embedding(sender, instance, created, **kwargs):
if created:
qa_create_and_save_embed(instance)
- Checks if
createdisTrue. - Sends
instance.questionto the configured embedding provider. - Inserts a new row in
QAEmbeddinglinked to thisQuestionAnswer. - The PostgreSQL HNSW index automatically incorporates the new vector.
2. Automatic Updates (pre_save)¶
When an existing QuestionAnswer instance is modified:
@receiver(pre_save, sender=QuestionAnswer)
def update_embedding(sender, instance, **kwargs):
if not instance.pk:
return
old = QuestionAnswer.objects.get(pk=instance.pk)
if old.question != instance.question:
vector = qa_create_embed(instance.question)
QAEmbedding.objects.filter(QA_foreign_key=instance).update(vector=vector)
- Inspects the previous database state using
instance.pk. - Compares
old.questionagainst the incominginstance.question. - If the question string was changed:
- Recalculates the vector embedding.
- Updates the associated
QAEmbeddingrow in-place. - If only the
answerwas modified, the embedding calculation is skipped, saving network bandwidth and API costs.
App Initialization¶
To ensure signals are properly registered, django_ragkit/apps.py imports them in the ready() lifecycle method: