mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-06-03 20:29:45 +00:00
Enhancement: try to respect language for AI suggestions (#12894)
This commit is contained in:
@@ -25,6 +25,7 @@ from documents.models import DocumentType
|
||||
from documents.models import ShareLink
|
||||
from documents.models import StoragePath
|
||||
from documents.models import Tag
|
||||
from documents.models import UiSettings
|
||||
from documents.signals.handlers import update_llm_suggestions_cache
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from documents.tests.utils import read_streaming_response
|
||||
@@ -319,6 +320,10 @@ class TestAISuggestions(DirectoriesMixin, TestCase):
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.json(), {"tags": ["tag1", "tag2"]})
|
||||
mock_get_cache.assert_called_once_with(
|
||||
self.document.pk,
|
||||
backend="mock_backend",
|
||||
)
|
||||
mock_refresh_cache.assert_called_once_with(self.document.pk)
|
||||
|
||||
@patch("documents.views.get_ai_document_classification")
|
||||
@@ -359,6 +364,49 @@ class TestAISuggestions(DirectoriesMixin, TestCase):
|
||||
"dates": ["2023-01-01"],
|
||||
},
|
||||
)
|
||||
mock_get_ai_classification.assert_called_once_with(
|
||||
self.document,
|
||||
self.user,
|
||||
None,
|
||||
)
|
||||
|
||||
@patch("documents.views.get_ai_document_classification")
|
||||
@override_settings(
|
||||
AI_ENABLED=True,
|
||||
LLM_BACKEND="mock_backend",
|
||||
)
|
||||
def test_ai_suggestions_uses_user_display_language(
|
||||
self,
|
||||
mock_get_ai_classification,
|
||||
) -> None:
|
||||
UiSettings.objects.create(user=self.user, settings={"language": "de-de"})
|
||||
mock_get_ai_classification.return_value = {
|
||||
"title": "KI Title",
|
||||
"tags": [],
|
||||
"correspondents": [],
|
||||
"document_types": [],
|
||||
"storage_paths": [],
|
||||
"dates": [],
|
||||
}
|
||||
|
||||
self.client.force_login(user=self.user)
|
||||
response = self.client.get(
|
||||
f"/api/documents/{self.document.pk}/ai_suggestions/",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
mock_get_ai_classification.assert_called_once_with(
|
||||
self.document,
|
||||
self.user,
|
||||
"de-de",
|
||||
)
|
||||
self.assertEqual(
|
||||
get_llm_suggestion_cache(
|
||||
self.document.pk,
|
||||
backend="mock_backend:de-de",
|
||||
).suggestions["title"],
|
||||
"KI Title",
|
||||
)
|
||||
|
||||
@patch("documents.views.get_ai_document_classification")
|
||||
@override_settings(
|
||||
|
||||
+19
-3
@@ -1469,9 +1469,21 @@ class DocumentViewSet(
|
||||
if not ai_config.ai_enabled:
|
||||
return HttpResponseBadRequest("AI is required for this feature")
|
||||
|
||||
output_language = None
|
||||
if hasattr(request.user, "ui_settings") and isinstance(
|
||||
request.user.ui_settings.settings,
|
||||
dict,
|
||||
):
|
||||
output_language = request.user.ui_settings.settings.get("language") or None
|
||||
llm_cache_backend = (
|
||||
f"{ai_config.llm_backend}:{output_language}"
|
||||
if output_language
|
||||
else ai_config.llm_backend
|
||||
)
|
||||
|
||||
cached_llm_suggestions = get_llm_suggestion_cache(
|
||||
doc.pk,
|
||||
backend=ai_config.llm_backend,
|
||||
backend=llm_cache_backend,
|
||||
)
|
||||
|
||||
if cached_llm_suggestions:
|
||||
@@ -1479,7 +1491,11 @@ class DocumentViewSet(
|
||||
return Response(cached_llm_suggestions.suggestions)
|
||||
|
||||
try:
|
||||
llm_suggestions = get_ai_document_classification(doc, request.user)
|
||||
llm_suggestions = get_ai_document_classification(
|
||||
doc,
|
||||
request.user,
|
||||
output_language,
|
||||
)
|
||||
except ValueError as exc:
|
||||
logger.exception(
|
||||
"Invalid AI configuration while generating suggestions for "
|
||||
@@ -1532,7 +1548,7 @@ class DocumentViewSet(
|
||||
"dates": llm_suggestions.get("dates", []),
|
||||
}
|
||||
|
||||
set_llm_suggestions_cache(doc.pk, resp_data, backend=ai_config.llm_backend)
|
||||
set_llm_suggestions_cache(doc.pk, resp_data, backend=llm_cache_backend)
|
||||
|
||||
return Response(resp_data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user