Use PaperlessTask for llmindex

This commit is contained in:
shamoon
2025-04-29 19:40:05 -07:00
parent 84e17535fc
commit 96daa5eb18
9 changed files with 82 additions and 48 deletions

View File

@@ -18,4 +18,5 @@ class Command(ProgressBarMixin, BaseCommand):
llmindex_index(
progress_bar_disable=self.no_progress_bar,
rebuild=options["command"] == "rebuild",
scheduled=False,
)

View File

@@ -0,0 +1,30 @@
# Generated by Django 5.1.8 on 2025-04-30 02:38
from django.db import migrations
from django.db import models
class Migration(migrations.Migration):
dependencies = [
("documents", "1065_workflowaction_assign_custom_fields_values"),
]
operations = [
migrations.AlterField(
model_name="paperlesstask",
name="task_name",
field=models.CharField(
choices=[
("consume_file", "Consume File"),
("train_classifier", "Train Classifier"),
("check_sanity", "Check Sanity"),
("index_optimize", "Index Optimize"),
("llmindex_update", "LLM Index Update"),
],
help_text="Name of the task that was run",
max_length=255,
null=True,
verbose_name="Task Name",
),
),
]

View File

@@ -543,6 +543,7 @@ class PaperlessTask(ModelWithOwner):
TRAIN_CLASSIFIER = ("train_classifier", _("Train Classifier"))
CHECK_SANITY = ("check_sanity", _("Check Sanity"))
INDEX_OPTIMIZE = ("index_optimize", _("Index Optimize"))
LLMINDEX_UPDATE = ("llmindex_update", _("LLM Index Update"))
task_id = models.CharField(
max_length=255,

View File

@@ -535,13 +535,29 @@ def check_scheduled_workflows():
@shared_task
def llmindex_index(*, progress_bar_disable=False, rebuild=False):
def llmindex_index(*, progress_bar_disable=True, rebuild=False, scheduled=True):
ai_config = AIConfig()
if ai_config.llm_index_enabled():
update_llm_index(
task = PaperlessTask.objects.create(
type=PaperlessTask.TaskType.SCHEDULED_TASK
if scheduled
else PaperlessTask.TaskType.MANUAL_TASK,
task_id=uuid.uuid4(),
task_name=PaperlessTask.TaskName.LLMINDEX_UPDATE,
status=states.STARTED,
date_created=timezone.now(),
date_started=timezone.now(),
)
from paperless_ai.indexing import update_llm_index
result = update_llm_index(
progress_bar_disable=progress_bar_disable,
rebuild=rebuild,
)
task.status = states.SUCCESS
task.result = result
task.date_done = timezone.now()
task.save(update_fields=["status", "result", "date_done"])
@shared_task
@@ -552,11 +568,3 @@ def update_document_in_llm_index(document):
@shared_task
def remove_document_from_llm_index(document):
llm_index_remove_document(document)
# TODO: schedule to run periodically
@shared_task
def rebuild_llm_index_task():
from paperless_ai.indexing import update_llm_index
update_llm_index(rebuild=True)