From 34a619a54fb0dddbce17e83ce0f94719940d4147 Mon Sep 17 00:00:00 2001 From: natoromano Date: Mon, 23 Feb 2026 16:34:59 +0100 Subject: [PATCH] chore: small fixes --- .../tests/tools/test_document_translate.py | 15 +++++++++++---- src/backend/chat/tools/document_translate.py | 19 +++++++++++++++---- .../configuration/llm/default.json | 2 +- .../chat/components/ToolInvocationItem.tsx | 18 +++++++----------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/backend/chat/tests/tools/test_document_translate.py b/src/backend/chat/tests/tools/test_document_translate.py index 4d640a1..5bb4c77 100644 --- a/src/backend/chat/tests/tools/test_document_translate.py +++ b/src/backend/chat/tests/tools/test_document_translate.py @@ -20,13 +20,13 @@ def fixture_translation_agent_config(settings): """Fixture to set used settings for agent configuration.""" settings.TRANSLATION_MAX_CHARS = 100_000 settings.LLM_CONFIGURATIONS = { - settings.LLM_SUMMARIZATION_MODEL_HRID: LLModel( + settings.LLM_DEFAULT_MODEL_HRID: LLModel( hrid="mistral-model", - model_name="mistral-7b-instruct-v0.1", - human_readable_name="Mistral 7B Instruct", + model_name="mistral-medium-2508", + human_readable_name="Mistral Medium 2508", profile=None, provider=LLMProvider( - hrid="mistral", + hrid="mistral-medium-2508", kind="mistral", base_url="https://api.mistral.ai/v1", api_key="testkey", @@ -71,6 +71,7 @@ async def test_document_translate_single_document(mocked_context, mock_translati mock_attachment.key = "test_doc.txt" mock_attachment.file_name = "test_doc.txt" mock_attachment.content_type = "text/plain" + mock_attachment.size = None mock_conversation = mock.Mock() mock_conversation.attachments = _mock_attachments_queryset(mock_attachment) @@ -103,6 +104,7 @@ async def test_document_translate_uses_last_document(mocked_context, mock_transl mock_attachment.key = "latest_doc.txt" mock_attachment.file_name = "latest_doc.txt" mock_attachment.content_type = "text/plain" + mock_attachment.size = None mock_conversation = mock.Mock() mock_conversation.attachments = _mock_attachments_queryset(mock_attachment) @@ -134,6 +136,7 @@ async def test_document_translate_with_custom_instructions(mocked_context, mock_ mock_attachment.key = "test.txt" mock_attachment.file_name = "test.txt" mock_attachment.content_type = "text/plain" + mock_attachment.size = None mock_conversation = mock.Mock() mock_conversation.attachments = _mock_attachments_queryset(mock_attachment) @@ -204,6 +207,7 @@ async def test_document_translate_error_reading_document(mocked_context, mock_tr mock_attachment.key = "test.txt" mock_attachment.file_name = "test.txt" mock_attachment.content_type = "text/plain" + mock_attachment.size = None mock_conversation = mock.Mock() mock_conversation.attachments = _mock_attachments_queryset(mock_attachment) @@ -227,6 +231,7 @@ async def test_document_translate_error_during_translation(mocked_context, mock_ mock_attachment.key = "test.txt" mock_attachment.file_name = "test.txt" mock_attachment.content_type = "text/plain" + mock_attachment.size = None mock_conversation = mock.Mock() mock_conversation.attachments = _mock_attachments_queryset(mock_attachment) @@ -259,6 +264,7 @@ async def test_document_translate_too_large(settings, mocked_context, mock_trans mock_attachment.key = "large_doc.txt" mock_attachment.file_name = "large_doc.txt" mock_attachment.content_type = "text/plain" + mock_attachment.size = None mock_conversation = mock.Mock() mock_conversation.attachments = _mock_attachments_queryset(mock_attachment) @@ -286,6 +292,7 @@ async def test_document_translate_empty_result(mocked_context, mock_translation_ mock_attachment.key = "test.txt" mock_attachment.file_name = "test.txt" mock_attachment.content_type = "text/plain" + mock_attachment.size = None mock_conversation = mock.Mock() mock_conversation.attachments = _mock_attachments_queryset(mock_attachment) diff --git a/src/backend/chat/tools/document_translate.py b/src/backend/chat/tools/document_translate.py index 97d44a2..c613401 100644 --- a/src/backend/chat/tools/document_translate.py +++ b/src/backend/chat/tools/document_translate.py @@ -24,7 +24,7 @@ async def document_translate( instructions: str | None = None, ) -> ToolReturn: """ - Translate the full content of uploaded documents into the specified target language. + Translate the full content of the last uploaded document into the specified target language. Preserve the original markdown formatting unless the instructions say otherwise. Return this translation directly to the user WITHOUT any modification or additional summarization. @@ -76,10 +76,21 @@ async def document_translate( "You must explain this to the user and ask them to provide documents." ) - doc_name, content = await read_document_content(last_attachment) - - # Check content size against the configured limit + # Check file size before reading to avoid loading oversized files into memory. + # Since UTF-8 encodes at least 1 byte per character, a file whose byte size + # already exceeds max_chars cannot possibly be within the character limit. max_chars = settings.TRANSLATION_MAX_CHARS + if last_attachment.size and last_attachment.size > max_chars: + raise ModelCannotRetry( + "The document is too large to translate. " + "You must explain this to the user, without providing numerical details. " + "Suggest them to reduce the document size by summarizing it or " + "by splitting it into smaller parts. " + "Also offer them to summarize the document in the target language instead, " + "which can be a good alternative to translation for large documents." + ) + + doc_name, content = await read_document_content(last_attachment) if len(content) > max_chars: raise ModelCannotRetry( diff --git a/src/backend/conversations/configuration/llm/default.json b/src/backend/conversations/configuration/llm/default.json index d77bc4b..a004952 100644 --- a/src/backend/conversations/configuration/llm/default.json +++ b/src/backend/conversations/configuration/llm/default.json @@ -37,7 +37,7 @@ "hrid": "default-provider", "base_url": "settings.AI_BASE_URL", "api_key": "settings.AI_API_KEY", - "kind": "openai" + "kind": "mistral" } ] } diff --git a/src/frontend/apps/conversations/src/features/chat/components/ToolInvocationItem.tsx b/src/frontend/apps/conversations/src/features/chat/components/ToolInvocationItem.tsx index 6719aa2..ae468a0 100644 --- a/src/frontend/apps/conversations/src/features/chat/components/ToolInvocationItem.tsx +++ b/src/frontend/apps/conversations/src/features/chat/components/ToolInvocationItem.tsx @@ -29,13 +29,15 @@ export const ToolInvocationItem: React.FC = ({ ?.documents; const documentIdentifiers: string[] = Array.isArray(documents) && - documents.every( - (doc): doc is { identifier: string } => - typeof doc === 'object' && doc !== null && 'identifier' in doc, - ) + documents.every( + (doc): doc is { identifier: string } => + typeof doc === 'object' && doc !== null && 'identifier' in doc, + ) ? documents.map((doc) => doc.identifier) : []; + const label = documentIdentifiers.length > 1 ? 'Extracting documents...' : 'Extracting document...'; + return ( = ({ > - {documentIdentifiers.length === 1 - ? t('Extracting document: {{documents}} ...', { - documents: documentIdentifiers[0], - }) - : t('Extracting documents: {{documents}} ...', { - documents: documentIdentifiers.join(', '), - })} + {t(label, { documents: documentIdentifiers.join(', ') })} );