diff --git a/src/paperless_ai/chat.py b/src/paperless_ai/chat.py index 10d2b96d8..7942321a6 100644 --- a/src/paperless_ai/chat.py +++ b/src/paperless_ai/chat.py @@ -85,6 +85,10 @@ def stream_chat_with_documents(query_str: str, documents: list[Document]): def _stream_chat_with_documents(query_str: str, documents: list[Document]): + if not documents: + yield CHAT_NO_CONTENT_MESSAGE + return + from llama_index.core.prompts import PromptTemplate from llama_index.core.query_engine import RetrieverQueryEngine from llama_index.core.response_synthesizers import get_response_synthesizer diff --git a/src/paperless_ai/tests/test_chat.py b/src/paperless_ai/tests/test_chat.py index 52e36b15a..bb32c62ae 100644 --- a/src/paperless_ai/tests/test_chat.py +++ b/src/paperless_ai/tests/test_chat.py @@ -176,6 +176,13 @@ def test_stream_chat_with_multiple_documents_retrieval(patch_embed_nodes) -> Non ) +def test_stream_chat_empty_document_list() -> None: + with patch("paperless_ai.chat.load_or_build_index") as mock_load_index: + output = list(stream_chat_with_documents("Any info?", [])) + mock_load_index.assert_not_called() + assert output == ["Sorry, I couldn't find any content to answer your question."] + + def test_stream_chat_no_matching_nodes() -> None: with ( patch("paperless_ai.chat.AIClient") as mock_client_cls,