mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-18 02:22:20 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
179307d57a | ||
|
|
a690885a8a |
No files matched your search
+2
-3
@@ -151,9 +151,8 @@ typing = [
|
|||||||
required-version = ">=0.9.0"
|
required-version = ">=0.9.0"
|
||||||
prerelease = "disallow"
|
prerelease = "disallow"
|
||||||
environments = [
|
environments = [
|
||||||
"sys_platform == 'darwin' and platform_machine == 'arm64' and platform_python_implementation == 'CPython'",
|
"sys_platform == 'darwin'",
|
||||||
"sys_platform == 'linux' and platform_machine == 'aarch64' and platform_python_implementation == 'CPython'",
|
"sys_platform == 'linux'",
|
||||||
"sys_platform == 'linux' and platform_machine == 'x86_64' and platform_python_implementation == 'CPython'",
|
|
||||||
]
|
]
|
||||||
package = false
|
package = false
|
||||||
[tool.uv.sources]
|
[tool.uv.sources]
|
||||||
|
|||||||
+2
-2
@@ -4151,14 +4151,14 @@
|
|||||||
<source>Ask a question about this document...</source>
|
<source>Ask a question about this document...</source>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/chat/chat/chat.component.ts</context>
|
<context context-type="sourcefile">src/app/components/chat/chat/chat.component.ts</context>
|
||||||
<context context-type="linenumber">87</context>
|
<context context-type="linenumber">83</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="224049052163028286" datatype="html">
|
<trans-unit id="224049052163028286" datatype="html">
|
||||||
<source>Ask a question about a document...</source>
|
<source>Ask a question about a document...</source>
|
||||||
<context-group purpose="location">
|
<context-group purpose="location">
|
||||||
<context context-type="sourcefile">src/app/components/chat/chat/chat.component.ts</context>
|
<context context-type="sourcefile">src/app/components/chat/chat/chat.component.ts</context>
|
||||||
<context context-type="linenumber">88</context>
|
<context context-type="linenumber">84</context>
|
||||||
</context-group>
|
</context-group>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="8700121026680200191" datatype="html">
|
<trans-unit id="8700121026680200191" datatype="html">
|
||||||
|
|||||||
@@ -188,62 +188,6 @@ describe('ChatComponent', () => {
|
|||||||
expect(component.loading()).toBe(false)
|
expect(component.loading()).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should reveal a complete answer within one second', () => {
|
|
||||||
component.input.set('Hello')
|
|
||||||
component.sendMessage()
|
|
||||||
|
|
||||||
const answer = 'A'.repeat(660)
|
|
||||||
mockStream$.next(answer)
|
|
||||||
mockStream$.complete()
|
|
||||||
|
|
||||||
expect(component.messages()[1].content.length).toBeGreaterThan(0)
|
|
||||||
expect(component.messages()[1].content.length).toBeLessThan(answer.length)
|
|
||||||
expect(component.loading()).toBe(true)
|
|
||||||
|
|
||||||
jest.advanceTimersByTime(800)
|
|
||||||
expect(component.messages()[1].content).toBe(answer)
|
|
||||||
expect(component.loading()).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should give a long answer more time to reveal', () => {
|
|
||||||
component.input.set('Hello')
|
|
||||||
component.sendMessage()
|
|
||||||
|
|
||||||
const answer = 'A'.repeat(3000)
|
|
||||||
mockStream$.next(answer)
|
|
||||||
mockStream$.complete()
|
|
||||||
|
|
||||||
jest.advanceTimersByTime(800)
|
|
||||||
expect(component.messages()[1].content.length).toBeLessThan(answer.length)
|
|
||||||
expect(component.loading()).toBe(true)
|
|
||||||
|
|
||||||
jest.advanceTimersByTime(2500)
|
|
||||||
expect(component.messages()[1].content).toBe(answer)
|
|
||||||
expect(component.loading()).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should reveal fast incoming chunks without making them pop in', () => {
|
|
||||||
component.input.set('Hello')
|
|
||||||
component.sendMessage()
|
|
||||||
|
|
||||||
let answer = ''
|
|
||||||
for (let i = 0; i < 20; i++) {
|
|
||||||
answer += 'A'.repeat(100)
|
|
||||||
const visibleBefore = component.messages()[1].content.length
|
|
||||||
mockStream$.next(answer)
|
|
||||||
jest.advanceTimersByTime(10)
|
|
||||||
expect(
|
|
||||||
component.messages()[1].content.length - visibleBefore
|
|
||||||
).toBeLessThan(100)
|
|
||||||
}
|
|
||||||
expect(component.messages()[1].content.length).toBeLessThan(answer.length)
|
|
||||||
|
|
||||||
mockStream$.complete()
|
|
||||||
jest.advanceTimersByTime(5200)
|
|
||||||
expect(component.messages()[1].content).toBe(answer)
|
|
||||||
expect(component.loading()).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should show a slow response message while waiting', () => {
|
it('should show a slow response message while waiting', () => {
|
||||||
component.input.set('Hello')
|
component.input.set('Hello')
|
||||||
component.sendMessage()
|
component.sendMessage()
|
||||||
|
|||||||
@@ -21,9 +21,6 @@ import {
|
|||||||
import { pngxPopperOptions } from 'src/app/utils/popper-options'
|
import { pngxPopperOptions } from 'src/app/utils/popper-options'
|
||||||
|
|
||||||
const SLOW_RESPONSE_DELAY_MS = 8000
|
const SLOW_RESPONSE_DELAY_MS = 8000
|
||||||
const TYPEWRITER_MIN_DURATION_MS = 750
|
|
||||||
const TYPEWRITER_MAX_DURATION_MS = 3000
|
|
||||||
const TYPEWRITER_INTERVAL_MS = 16
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'pngx-chat',
|
selector: 'pngx-chat',
|
||||||
@@ -76,7 +73,6 @@ export class ChatComponent implements OnInit {
|
|||||||
private typewriterBuffer: string[] = []
|
private typewriterBuffer: string[] = []
|
||||||
private typewriterActive = false
|
private typewriterActive = false
|
||||||
private typewriterTimeout: ReturnType<typeof setTimeout>
|
private typewriterTimeout: ReturnType<typeof setTimeout>
|
||||||
private typewriterDeadline = 0
|
|
||||||
private streamSubscription: Subscription
|
private streamSubscription: Subscription
|
||||||
private streamingMessage: ChatMessage
|
private streamingMessage: ChatMessage
|
||||||
private streamFinished = false
|
private streamFinished = false
|
||||||
@@ -138,7 +134,7 @@ export class ChatComponent implements OnInit {
|
|||||||
if (nextResponse.content.length < lastVisibleContent.length) {
|
if (nextResponse.content.length < lastVisibleContent.length) {
|
||||||
this.resetTypewriter(assistantMessage, nextResponse.content)
|
this.resetTypewriter(assistantMessage, nextResponse.content)
|
||||||
lastVisibleContent = nextResponse.content
|
lastVisibleContent = nextResponse.content
|
||||||
} else if (nextResponse.content.length > lastVisibleContent.length) {
|
} else {
|
||||||
const visibleDelta = nextResponse.content.substring(
|
const visibleDelta = nextResponse.content.substring(
|
||||||
lastVisibleContent.length
|
lastVisibleContent.length
|
||||||
)
|
)
|
||||||
@@ -218,12 +214,6 @@ export class ChatComponent implements OnInit {
|
|||||||
if (!chunk) return
|
if (!chunk) return
|
||||||
|
|
||||||
this.typewriterBuffer.push(...chunk.split(''))
|
this.typewriterBuffer.push(...chunk.split(''))
|
||||||
// Longer bursts need more time to remain readable as the chat scrolls.
|
|
||||||
const revealDuration = Math.min(
|
|
||||||
TYPEWRITER_MAX_DURATION_MS,
|
|
||||||
Math.max(TYPEWRITER_MIN_DURATION_MS, this.typewriterBuffer.length)
|
|
||||||
)
|
|
||||||
this.typewriterDeadline = Date.now() + revealDuration
|
|
||||||
|
|
||||||
if (!this.typewriterActive) {
|
if (!this.typewriterActive) {
|
||||||
this.typewriterActive = true
|
this.typewriterActive = true
|
||||||
@@ -232,29 +222,19 @@ export class ChatComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
playTypewriter(message: ChatMessage): void {
|
playTypewriter(message: ChatMessage): void {
|
||||||
// Spread the queued text across the updates left before the deadline
|
|
||||||
const remainingUpdates = Math.max(
|
|
||||||
1,
|
|
||||||
Math.ceil((this.typewriterDeadline - Date.now()) / TYPEWRITER_INTERVAL_MS)
|
|
||||||
)
|
|
||||||
const charactersToShow = Math.ceil(
|
|
||||||
this.typewriterBuffer.length / remainingUpdates
|
|
||||||
)
|
|
||||||
message.content += this.typewriterBuffer
|
|
||||||
.splice(0, charactersToShow)
|
|
||||||
.join('')
|
|
||||||
this.notifyMessagesChanged()
|
|
||||||
this.scrollToBottom()
|
|
||||||
|
|
||||||
if (this.typewriterBuffer.length === 0) {
|
if (this.typewriterBuffer.length === 0) {
|
||||||
this.typewriterActive = false
|
this.typewriterActive = false
|
||||||
if (this.streamFinished) this.finishStreaming(message)
|
if (this.streamFinished) this.finishStreaming(message)
|
||||||
} else {
|
return
|
||||||
this.typewriterTimeout = setTimeout(
|
|
||||||
() => this.playTypewriter(message),
|
|
||||||
TYPEWRITER_INTERVAL_MS
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const nextChar = this.typewriterBuffer.shift()
|
||||||
|
message.content += nextChar
|
||||||
|
this.notifyMessagesChanged()
|
||||||
|
this.scrollToBottom()
|
||||||
|
|
||||||
|
// 10ms per character
|
||||||
|
this.typewriterTimeout = setTimeout(() => this.playTypewriter(message), 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
private notifyMessagesChanged(): void {
|
private notifyMessagesChanged(): void {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"""Queued document bulk operations."""
|
"""Queued document bulk operations."""
|
||||||
|
|
||||||
from contextlib import nullcontext
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from auditlog.context import set_actor
|
from auditlog.context import set_actor
|
||||||
@@ -113,8 +112,6 @@ def execute_bulk_operation(
|
|||||||
if operation == "run_workflow" and not user.has_perm("documents.view_workflow"):
|
if operation == "run_workflow" and not user.has_perm("documents.view_workflow"):
|
||||||
raise PermissionDenied("Insufficient permissions to run this workflow")
|
raise PermissionDenied("Insufficient permissions to run this workflow")
|
||||||
|
|
||||||
actor_context = set_actor(user) if settings.AUDIT_LOG_ENABLED else nullcontext()
|
|
||||||
|
|
||||||
if operation not in MODIFIED_FIELDS:
|
if operation not in MODIFIED_FIELDS:
|
||||||
execution_parameters = parameters.copy()
|
execution_parameters = parameters.copy()
|
||||||
if operation in USER_OPERATIONS:
|
if operation in USER_OPERATIONS:
|
||||||
@@ -122,7 +119,7 @@ def execute_bulk_operation(
|
|||||||
if operation in TRIGGER_SOURCE_OPERATIONS:
|
if operation in TRIGGER_SOURCE_OPERATIONS:
|
||||||
execution_parameters["trigger_source"] = trigger_source
|
execution_parameters["trigger_source"] = trigger_source
|
||||||
modified_field = LEGACY_MODIFIED_FIELDS.get(operation)
|
modified_field = LEGACY_MODIFIED_FIELDS.get(operation)
|
||||||
with actor_context:
|
with set_actor(user):
|
||||||
old_values = (
|
old_values = (
|
||||||
snapshot_field(document_ids, modified_field)
|
snapshot_field(document_ids, modified_field)
|
||||||
if settings.AUDIT_LOG_ENABLED and modified_field
|
if settings.AUDIT_LOG_ENABLED and modified_field
|
||||||
@@ -149,7 +146,7 @@ def execute_bulk_operation(
|
|||||||
modified_field = MODIFIED_FIELDS[operation]
|
modified_field = MODIFIED_FIELDS[operation]
|
||||||
# Keep metadata writes and their audit entries together. Follow-up processing
|
# Keep metadata writes and their audit entries together. Follow-up processing
|
||||||
# runs after commit, within this same tracked task.
|
# runs after commit, within this same tracked task.
|
||||||
with actor_context, transaction.atomic():
|
with set_actor(user), transaction.atomic():
|
||||||
old_values = (
|
old_values = (
|
||||||
snapshot_field(document_ids, modified_field)
|
snapshot_field(document_ids, modified_field)
|
||||||
if settings.AUDIT_LOG_ENABLED and modified_field
|
if settings.AUDIT_LOG_ENABLED and modified_field
|
||||||
|
|||||||
@@ -9,12 +9,11 @@ from rest_framework import status
|
|||||||
from rest_framework.test import APIClient
|
from rest_framework.test import APIClient
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
from documents.views import _iterate_in_sync_thread
|
|
||||||
from paperless_testing.factories import UserFactory
|
from paperless_testing.factories import UserFactory
|
||||||
from paperless_testing.permissions import grant_global
|
from paperless_testing.permissions import grant_global
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Generator
|
from collections.abc import AsyncIterator
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from pytest_mock import MockerFixture
|
from pytest_mock import MockerFixture
|
||||||
@@ -59,6 +58,11 @@ class TestChatStreamingViewInputValidation(APITestCase):
|
|||||||
reaches the Django request the middleware sees.
|
reaches the Django request the middleware sees.
|
||||||
"""
|
"""
|
||||||
chunks = [f"token{i} " for i in range(40)]
|
chunks = [f"token{i} " for i in range(40)]
|
||||||
|
|
||||||
|
async def answer() -> AsyncIterator[str]:
|
||||||
|
for chunk in chunks:
|
||||||
|
yield chunk
|
||||||
|
|
||||||
with (
|
with (
|
||||||
mock.patch(
|
mock.patch(
|
||||||
"documents.views.AIConfig",
|
"documents.views.AIConfig",
|
||||||
@@ -66,7 +70,7 @@ class TestChatStreamingViewInputValidation(APITestCase):
|
|||||||
),
|
),
|
||||||
mock.patch(
|
mock.patch(
|
||||||
"paperless_ai.chat.stream_chat_with_documents",
|
"paperless_ai.chat.stream_chat_with_documents",
|
||||||
return_value=(c for c in chunks),
|
return_value=answer(),
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
resp = self.client.post(
|
resp = self.client.post(
|
||||||
@@ -163,74 +167,3 @@ class TestChatStreamingViewUnrestrictedFlag:
|
|||||||
assert (
|
assert (
|
||||||
mocked_stream_chat.call_args.kwargs["unrestricted"] is expected_unrestricted
|
mocked_stream_chat.call_args.kwargs["unrestricted"] is expected_unrestricted
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestIterateInSyncThread:
|
|
||||||
@pytest.fixture
|
|
||||||
def events(self) -> list[str]:
|
|
||||||
return []
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def chunks(self, events: list[str]) -> Generator[str, None, None]:
|
|
||||||
def generate() -> Generator[str, None, None]:
|
|
||||||
try:
|
|
||||||
for chunk in ("first", "second"):
|
|
||||||
events.append(f"produced {chunk}")
|
|
||||||
yield chunk
|
|
||||||
finally:
|
|
||||||
events.append("closed")
|
|
||||||
|
|
||||||
return generate()
|
|
||||||
|
|
||||||
def test_yields_each_chunk_before_pulling_the_next(
|
|
||||||
self,
|
|
||||||
chunks: Generator[str, None, None],
|
|
||||||
events: list[str],
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- A sync generator wrapped for async iteration
|
|
||||||
WHEN:
|
|
||||||
- The wrapper is consumed
|
|
||||||
THEN:
|
|
||||||
- Each chunk is delivered before the next one is generated,
|
|
||||||
rather than the whole generator being drained up front
|
|
||||||
"""
|
|
||||||
|
|
||||||
async def consume() -> None:
|
|
||||||
async for chunk in _iterate_in_sync_thread(chunks):
|
|
||||||
events.append(f"received {chunk}")
|
|
||||||
|
|
||||||
async_to_sync(consume)()
|
|
||||||
|
|
||||||
assert events == [
|
|
||||||
"produced first",
|
|
||||||
"received first",
|
|
||||||
"produced second",
|
|
||||||
"received second",
|
|
||||||
"closed",
|
|
||||||
]
|
|
||||||
|
|
||||||
def test_closing_early_closes_the_source_generator(
|
|
||||||
self,
|
|
||||||
chunks: Generator[str, None, None],
|
|
||||||
events: list[str],
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- A sync generator wrapped for async iteration
|
|
||||||
WHEN:
|
|
||||||
- The consumer closes the wrapper after the first chunk, as
|
|
||||||
happens when the client disconnects
|
|
||||||
THEN:
|
|
||||||
- The source generator is closed, so its cleanup runs
|
|
||||||
"""
|
|
||||||
|
|
||||||
async def consume_one() -> None:
|
|
||||||
wrapper = _iterate_in_sync_thread(chunks)
|
|
||||||
await anext(wrapper)
|
|
||||||
await wrapper.aclose()
|
|
||||||
|
|
||||||
async_to_sync(consume_one)()
|
|
||||||
|
|
||||||
assert events == ["produced first", "closed"]
|
|
||||||
@@ -123,41 +123,6 @@ def test_document_action_runs_with_user_in_worker(bulk_setup):
|
|||||||
assert result == {"operation": "rotate", "document_count": 1}
|
assert result == {"operation": "rotate", "document_count": 1}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
|
||||||
def test_delete_task_without_audit_log(bulk_setup, settings):
|
|
||||||
user, _, doc, _ = bulk_setup
|
|
||||||
settings.AUDIT_LOG_ENABLED = False
|
|
||||||
with mock.patch(
|
|
||||||
"documents.bulk_operations.set_actor",
|
|
||||||
side_effect=AssertionError("audit context must not be entered"),
|
|
||||||
):
|
|
||||||
result = execute_bulk_operation("delete", [doc.pk], {}, user.pk)
|
|
||||||
assert result == {"operation": "delete", "document_count": 1}
|
|
||||||
assert not Document.objects.filter(pk=doc.pk).exists()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
|
||||||
def test_metadata_task_without_audit_log(bulk_setup, settings):
|
|
||||||
user, _, doc, correspondent = bulk_setup
|
|
||||||
settings.AUDIT_LOG_ENABLED = False
|
|
||||||
with (
|
|
||||||
mock.patch(
|
|
||||||
"documents.bulk_operations.set_actor",
|
|
||||||
side_effect=AssertionError("audit context must not be entered"),
|
|
||||||
),
|
|
||||||
mock.patch("documents.bulk_operations.bulk_update_documents"),
|
|
||||||
):
|
|
||||||
result = execute_bulk_operation(
|
|
||||||
"set_correspondent",
|
|
||||||
[doc.pk],
|
|
||||||
{"correspondent": correspondent.pk},
|
|
||||||
user.pk,
|
|
||||||
)
|
|
||||||
doc.refresh_from_db()
|
|
||||||
assert doc.correspondent_id == correspondent.pk
|
|
||||||
assert result == {"operation": "set_correspondent", "document_count": 1}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_document_action_queue_contains_only_serializable_parameters(bulk_setup):
|
def test_document_action_queue_contains_only_serializable_parameters(bulk_setup):
|
||||||
user, client, doc, _ = bulk_setup
|
user, client, doc, _ = bulk_setup
|
||||||
|
|||||||
+5
-28
@@ -7,9 +7,7 @@ import tempfile
|
|||||||
import zipfile
|
import zipfile
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from collections.abc import AsyncIterator
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from collections.abc import Generator
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
@@ -29,7 +27,6 @@ from urllib.parse import urlparse
|
|||||||
import httpx
|
import httpx
|
||||||
import magic
|
import magic
|
||||||
import pathvalidate
|
import pathvalidate
|
||||||
from asgiref.sync import sync_to_async
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import Group
|
from django.contrib.auth.models import Group
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
@@ -2419,24 +2416,6 @@ class DocumentViewSet(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _iterate_in_sync_thread(
|
|
||||||
chunks: Generator[str, None, None],
|
|
||||||
) -> AsyncIterator[str]:
|
|
||||||
"""Serve a blocking sync generator to an ASGI server chunk by chunk.
|
|
||||||
|
|
||||||
Under ASGI, Django drains a sync iterator with ``sync_to_async(list)``
|
|
||||||
before sending anything. Pull each chunk in the request's sync thread
|
|
||||||
instead, where the generator's ORM access belongs.
|
|
||||||
"""
|
|
||||||
sentinel = object() # StopIteration can't cross an asyncio Future
|
|
||||||
next_chunk = sync_to_async(next)
|
|
||||||
try:
|
|
||||||
while (chunk := await next_chunk(chunks, sentinel)) is not sentinel:
|
|
||||||
yield chunk
|
|
||||||
finally:
|
|
||||||
await sync_to_async(chunks.close)()
|
|
||||||
|
|
||||||
|
|
||||||
class ChatStreamingSerializer(serializers.Serializer[dict[str, Any]]):
|
class ChatStreamingSerializer(serializers.Serializer[dict[str, Any]]):
|
||||||
q = serializers.CharField(required=True, max_length=4000)
|
q = serializers.CharField(required=True, max_length=4000)
|
||||||
document_id = serializers.IntegerField(required=False, allow_null=True)
|
document_id = serializers.IntegerField(required=False, allow_null=True)
|
||||||
@@ -2496,13 +2475,11 @@ class ChatStreamingView(GenericAPIView[Any]):
|
|||||||
)
|
)
|
||||||
|
|
||||||
return StreamingHttpResponse(
|
return StreamingHttpResponse(
|
||||||
_iterate_in_sync_thread(
|
stream_chat_with_documents(
|
||||||
stream_chat_with_documents(
|
query_str=question,
|
||||||
query_str=question,
|
documents=documents,
|
||||||
documents=documents,
|
unrestricted=unrestricted,
|
||||||
unrestricted=unrestricted,
|
output_language=output_language,
|
||||||
output_language=output_language,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
content_type="text/event-stream",
|
content_type="text/event-stream",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ msgid ""
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: paperless-ngx\n"
|
"Project-Id-Version: paperless-ngx\n"
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2026-11-17 20:48+0000\n"
|
"POT-Creation-Date: 2026-11-17 00:24+0000\n"
|
||||||
"PO-Revision-Date: 2022-02-17 04:17\n"
|
"PO-Revision-Date: 2022-02-17 04:17\n"
|
||||||
"Last-Translator: \n"
|
"Last-Translator: \n"
|
||||||
"Language-Team: English\n"
|
"Language-Team: English\n"
|
||||||
@@ -1669,8 +1669,8 @@ msgid "workflow runs"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:483 documents/serialisers.py:844
|
#: documents/serialisers.py:483 documents/serialisers.py:844
|
||||||
#: documents/serialisers.py:2893 documents/views.py:359 documents/views.py:625
|
#: documents/serialisers.py:2893 documents/views.py:356 documents/views.py:622
|
||||||
#: documents/views.py:2783 paperless_mail/serialisers.py:145
|
#: documents/views.py:2761 paperless_mail/serialisers.py:145
|
||||||
msgid "Insufficient permissions."
|
msgid "Insufficient permissions."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -1710,7 +1710,7 @@ msgstr ""
|
|||||||
msgid "Duplicate document identifiers are not allowed."
|
msgid "Duplicate document identifiers are not allowed."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/serialisers.py:2979 documents/views.py:4770
|
#: documents/serialisers.py:2979 documents/views.py:4748
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Documents not found: %(ids)s"
|
msgid "Documents not found: %(ids)s"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@@ -1961,40 +1961,40 @@ msgstr ""
|
|||||||
msgid ", "
|
msgid ", "
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:314
|
#: documents/views.py:311
|
||||||
msgid "Specify only one of text, title_search, query, or more_like_id."
|
msgid "Specify only one of text, title_search, query, or more_like_id."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:352 documents/views.py:2780
|
#: documents/views.py:349 documents/views.py:2758
|
||||||
msgid "Invalid more_like_id"
|
msgid "Invalid more_like_id"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:1684
|
#: documents/views.py:1681
|
||||||
msgid "Invalid AI configuration."
|
msgid "Invalid AI configuration."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:1695
|
#: documents/views.py:1692
|
||||||
msgid "AI backend request timed out."
|
msgid "AI backend request timed out."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:1707
|
#: documents/views.py:1704
|
||||||
msgid "AI backend rejected the request. Check logs for details."
|
msgid "AI backend rejected the request. Check logs for details."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:4786
|
#: documents/views.py:4764
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Insufficient permissions to share document %(id)s."
|
msgid "Insufficient permissions to share document %(id)s."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:4833
|
#: documents/views.py:4811
|
||||||
msgid "Bundle is already being processed."
|
msgid "Bundle is already being processed."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:4899
|
#: documents/views.py:4877
|
||||||
msgid "The share link bundle is still being prepared. Please try again later."
|
msgid "The share link bundle is still being prepared. Please try again later."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: documents/views.py:4913
|
#: documents/views.py:4891
|
||||||
msgid "The share link bundle is unavailable."
|
msgid "The share link bundle is unavailable."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import socket
|
import socket
|
||||||
from collections.abc import Collection
|
from collections.abc import Collection
|
||||||
@@ -175,7 +176,9 @@ class PinnedHostAsyncHTTPTransport(httpx.AsyncHTTPTransport):
|
|||||||
self.allow_internal = allow_internal
|
self.allow_internal = allow_internal
|
||||||
|
|
||||||
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
|
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
|
||||||
request = _rewrite_request_to_pinned_ip(
|
# Resolving the host blocks, so keep it off the event loop
|
||||||
|
request = await asyncio.to_thread(
|
||||||
|
_rewrite_request_to_pinned_ip,
|
||||||
request,
|
request,
|
||||||
allow_internal=self.allow_internal,
|
allow_internal=self.allow_internal,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from paperless.network import PinnedHostAsyncHTTPTransport
|
||||||
from paperless.network import PinnedHostHTTPTransport
|
from paperless.network import PinnedHostHTTPTransport
|
||||||
from paperless.network import is_public_ip
|
from paperless.network import is_public_ip
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pytest_mock import MockerFixture
|
||||||
|
|
||||||
|
|
||||||
def test_pinned_host_transport_blocks_internal_rebinding():
|
def test_pinned_host_transport_blocks_internal_rebinding():
|
||||||
transport = PinnedHostHTTPTransport(allow_internal=False)
|
transport = PinnedHostHTTPTransport(allow_internal=False)
|
||||||
@@ -51,6 +59,58 @@ def test_pinned_host_transport_rewrites_to_vetted_ip():
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
class TestPinnedHostAsyncHTTPTransport:
|
||||||
|
def test_blocks_internal_rebinding(self, mocker: MockerFixture) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A hostname that resolves to a loopback address
|
||||||
|
WHEN:
|
||||||
|
- The async transport sends a request to it
|
||||||
|
THEN:
|
||||||
|
- The request is refused as a non-public address
|
||||||
|
"""
|
||||||
|
mocker.patch(
|
||||||
|
"paperless.network.resolve_hostname_ips",
|
||||||
|
return_value=["127.0.0.1"],
|
||||||
|
)
|
||||||
|
transport = PinnedHostAsyncHTTPTransport(allow_internal=False)
|
||||||
|
request = httpx.Request("GET", "http://example.com/test")
|
||||||
|
|
||||||
|
with pytest.raises(httpx.ConnectError, match="non-public address"):
|
||||||
|
asyncio.run(transport.handle_async_request(request))
|
||||||
|
|
||||||
|
def test_rewrites_to_vetted_ip(self, mocker: MockerFixture) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A hostname that resolves to a public address
|
||||||
|
WHEN:
|
||||||
|
- The async transport sends a request to it
|
||||||
|
THEN:
|
||||||
|
- The connection goes to that address, keeping the original Host
|
||||||
|
header and TLS SNI hostname
|
||||||
|
"""
|
||||||
|
mocker.patch(
|
||||||
|
"paperless.network.resolve_hostname_ips",
|
||||||
|
return_value=["93.184.216.34"],
|
||||||
|
)
|
||||||
|
sent = mocker.patch.object(
|
||||||
|
httpx.AsyncHTTPTransport,
|
||||||
|
"handle_async_request",
|
||||||
|
autospec=True,
|
||||||
|
side_effect=lambda _self, req: httpx.Response(200, request=req),
|
||||||
|
)
|
||||||
|
transport = PinnedHostAsyncHTTPTransport(allow_internal=False)
|
||||||
|
request = httpx.Request("GET", "https://example.com:8443/test")
|
||||||
|
|
||||||
|
response = asyncio.run(transport.handle_async_request(request))
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
rewritten = sent.call_args.args[1]
|
||||||
|
assert str(rewritten.url) == "https://93.184.216.34:8443/test"
|
||||||
|
assert rewritten.headers["Host"] == "example.com:8443"
|
||||||
|
assert rewritten.extensions["sni_hostname"] == "example.com"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"address",
|
"address",
|
||||||
[
|
[
|
||||||
|
|||||||
+57
-29
@@ -1,7 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import sys
|
from collections.abc import AsyncIterator
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
|
from django.db import connections
|
||||||
from django.db.models import QuerySet
|
from django.db.models import QuerySet
|
||||||
|
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
@@ -17,6 +20,10 @@ from paperless_ai.prompts.context import ChatQaPromptContext
|
|||||||
from paperless_ai.prompts.context import ChatRefinePromptContext
|
from paperless_ai.prompts.context import ChatRefinePromptContext
|
||||||
from paperless_ai.prompts.render import render_prompt
|
from paperless_ai.prompts.render import render_prompt
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from llama_index.core.response_synthesizers import BaseSynthesizer
|
||||||
|
from llama_index.core.schema import NodeWithScore
|
||||||
|
|
||||||
logger = logging.getLogger("paperless_ai.chat")
|
logger = logging.getLogger("paperless_ai.chat")
|
||||||
|
|
||||||
CHAT_METADATA_DELIMITER = "\n\n__PAPERLESS_CHAT_METADATA__"
|
CHAT_METADATA_DELIMITER = "\n\n__PAPERLESS_CHAT_METADATA__"
|
||||||
@@ -103,35 +110,67 @@ def _format_chat_metadata_trailer(references: list[dict[str, int | str]]) -> str
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def stream_chat_with_documents(
|
async def stream_chat_with_documents(
|
||||||
query_str: str,
|
query_str: str,
|
||||||
documents: QuerySet[Document],
|
documents: QuerySet[Document],
|
||||||
*,
|
*,
|
||||||
unrestricted: bool = False,
|
unrestricted: bool = False,
|
||||||
output_language: str | None = None,
|
output_language: str | None = None,
|
||||||
):
|
) -> AsyncIterator[str]:
|
||||||
try:
|
try:
|
||||||
yield from _stream_chat_with_documents(
|
async for chunk in _stream_chat_with_documents(
|
||||||
query_str,
|
query_str,
|
||||||
documents,
|
documents,
|
||||||
unrestricted=unrestricted,
|
unrestricted=unrestricted,
|
||||||
output_language=output_language,
|
output_language=output_language,
|
||||||
)
|
):
|
||||||
|
yield chunk
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception("Failed to stream document chat response: %s", e)
|
logger.exception("Failed to stream document chat response: %s", e)
|
||||||
yield CHAT_ERROR_MESSAGE
|
yield CHAT_ERROR_MESSAGE
|
||||||
|
|
||||||
|
|
||||||
def _stream_chat_with_documents(
|
async def _stream_chat_with_documents(
|
||||||
query_str: str,
|
query_str: str,
|
||||||
documents: QuerySet[Document],
|
documents: QuerySet[Document],
|
||||||
*,
|
*,
|
||||||
unrestricted: bool = False,
|
unrestricted: bool = False,
|
||||||
output_language: str | None = None,
|
output_language: str | None = None,
|
||||||
):
|
) -> AsyncIterator[str]:
|
||||||
if not documents.exists():
|
prepared = await sync_to_async(_prepare_chat)(
|
||||||
|
query_str,
|
||||||
|
documents,
|
||||||
|
unrestricted=unrestricted,
|
||||||
|
output_language=output_language,
|
||||||
|
)
|
||||||
|
if prepared is None:
|
||||||
yield CHAT_NO_CONTENT_MESSAGE
|
yield CHAT_NO_CONTENT_MESSAGE
|
||||||
return
|
return
|
||||||
|
response_synthesizer, top_nodes, references = prepared
|
||||||
|
|
||||||
|
logger.debug("Document chat query: %s", query_str)
|
||||||
|
response_stream = await response_synthesizer.asynthesize(query_str, top_nodes)
|
||||||
|
async for chunk in response_stream.async_response_gen():
|
||||||
|
yield chunk
|
||||||
|
|
||||||
|
if references:
|
||||||
|
yield _format_chat_metadata_trailer(references)
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare_chat(
|
||||||
|
query_str: str,
|
||||||
|
documents: QuerySet[Document],
|
||||||
|
*,
|
||||||
|
unrestricted: bool,
|
||||||
|
output_language: str | None,
|
||||||
|
) -> tuple["BaseSynthesizer", list["NodeWithScore"], list[dict[str, int | str]]] | None:
|
||||||
|
"""Everything before the answer streams, all of it blocking: ORM queries,
|
||||||
|
the index read lock and its thread-bound sqlite connection, the query
|
||||||
|
embedding, and building the LLM client. Returns None when there is no
|
||||||
|
content to answer from.
|
||||||
|
"""
|
||||||
|
if not documents.exists():
|
||||||
|
return None
|
||||||
|
|
||||||
from llama_index.core.prompts import PromptTemplate
|
from llama_index.core.prompts import PromptTemplate
|
||||||
from llama_index.core.response_synthesizers import get_response_synthesizer
|
from llama_index.core.response_synthesizers import get_response_synthesizer
|
||||||
@@ -150,8 +189,6 @@ def _stream_chat_with_documents(
|
|||||||
|
|
||||||
# Hold the shared read lock only while reading the index, so the compaction
|
# Hold the shared read lock only while reading the index, so the compaction
|
||||||
# swap cannot replace the database file while this connection is open.
|
# swap cannot replace the database file while this connection is open.
|
||||||
# Everything after retrieval works on the nodes already in memory, so a slow
|
|
||||||
# LLM stream never holds the lock.
|
|
||||||
with read_store() as store:
|
with read_store() as store:
|
||||||
if permitted_ids is not None:
|
if permitted_ids is not None:
|
||||||
filters = _permitted_documents_filter(store, permitted_ids)
|
filters = _permitted_documents_filter(store, permitted_ids)
|
||||||
@@ -179,35 +216,26 @@ def _stream_chat_with_documents(
|
|||||||
|
|
||||||
if not top_nodes:
|
if not top_nodes:
|
||||||
logger.warning("No nodes found for the given documents.")
|
logger.warning("No nodes found for the given documents.")
|
||||||
yield CHAT_NO_CONTENT_MESSAGE
|
return None
|
||||||
return
|
|
||||||
|
|
||||||
client = AIClient()
|
|
||||||
|
|
||||||
references = _get_document_references(documents, top_nodes)
|
references = _get_document_references(documents, top_nodes)
|
||||||
|
|
||||||
prompt_template = PromptTemplate(template=_build_chat_prompt(output_language))
|
client = AIClient()
|
||||||
refine_template = PromptTemplate(template=_build_refine_prompt(output_language))
|
|
||||||
response_synthesizer = get_response_synthesizer(
|
response_synthesizer = get_response_synthesizer(
|
||||||
llm=client.llm,
|
llm=client.llm,
|
||||||
prompt_helper=get_rag_prompt_helper(
|
prompt_helper=get_rag_prompt_helper(
|
||||||
chunk_size=config.llm_embedding_chunk_size,
|
chunk_size=config.llm_embedding_chunk_size,
|
||||||
context_size=config.llm_context_size,
|
context_size=config.llm_context_size,
|
||||||
),
|
),
|
||||||
text_qa_template=prompt_template,
|
text_qa_template=PromptTemplate(template=_build_chat_prompt(output_language)),
|
||||||
refine_template=refine_template,
|
refine_template=PromptTemplate(
|
||||||
|
template=_build_refine_prompt(output_language),
|
||||||
|
),
|
||||||
streaming=True,
|
streaming=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.debug("Document chat query: %s", query_str)
|
# No ORM access happens while the answer streams, so hand the pooled DB
|
||||||
# Release the pooled DB connection for the slow streaming LLM response
|
# connection back now rather than pin it for the whole stream. See #12976.
|
||||||
# so it is not pinned for the whole stream; see paperless_ai.db and
|
connections.close_all()
|
||||||
# #12976.
|
|
||||||
with db_connection_released():
|
|
||||||
response_stream = response_synthesizer.synthesize(query_str, top_nodes)
|
|
||||||
for chunk in response_stream.response_gen:
|
|
||||||
yield chunk
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
if references:
|
return response_synthesizer, top_nodes, references
|
||||||
yield _format_chat_metadata_trailer(references)
|
|
||||||
@@ -4,10 +4,12 @@ import json
|
|||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from asgiref.sync import async_to_sync
|
||||||
from django.db.models.signals import post_init
|
from django.db.models.signals import post_init
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from llama_index.core import settings as llama_settings
|
from llama_index.core import settings as llama_settings
|
||||||
@@ -26,6 +28,7 @@ from paperless_ai.chat import stream_chat_with_documents
|
|||||||
from paperless_testing.factories import DocumentFactory
|
from paperless_testing.factories import DocumentFactory
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import AsyncIterator
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -66,6 +69,26 @@ def assert_chat_output(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def collect(chunks: AsyncIterator[str]) -> list[str]:
|
||||||
|
"""Drain the chat stream from sync test code. Its blocking setup then runs
|
||||||
|
on the test's own thread, inside the test's DB transaction."""
|
||||||
|
|
||||||
|
async def drain() -> list[str]:
|
||||||
|
return [chunk async for chunk in chunks]
|
||||||
|
|
||||||
|
return async_to_sync(drain)()
|
||||||
|
|
||||||
|
|
||||||
|
def fake_response_stream(chunks: list[str]) -> MagicMock:
|
||||||
|
"""An AsyncStreamingResponse stand-in yielding ``chunks``."""
|
||||||
|
|
||||||
|
async def response_gen() -> AsyncIterator[str]:
|
||||||
|
for chunk in chunks:
|
||||||
|
yield chunk
|
||||||
|
|
||||||
|
return MagicMock(async_response_gen=response_gen)
|
||||||
|
|
||||||
|
|
||||||
def _fake_documents_queryset(pks: list[int]) -> MagicMock:
|
def _fake_documents_queryset(pks: list[int]) -> MagicMock:
|
||||||
qs = MagicMock()
|
qs = MagicMock()
|
||||||
qs.exists.return_value = bool(pks)
|
qs.exists.return_value = bool(pks)
|
||||||
@@ -170,19 +193,19 @@ def test_stream_chat_with_one_document_retrieval(
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
mock_response_stream = MagicMock()
|
|
||||||
mock_response_stream.response_gen = iter(["chunk1", "chunk2"])
|
|
||||||
mock_synthesizer = mock_get_response_synthesizer.return_value
|
mock_synthesizer = mock_get_response_synthesizer.return_value
|
||||||
mock_synthesizer.synthesize.return_value = mock_response_stream
|
mock_synthesizer.asynthesize = AsyncMock(
|
||||||
|
return_value=fake_response_stream(["chunk1", "chunk2"]),
|
||||||
|
)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"llama_index.core.retrievers.VectorIndexRetriever",
|
"llama_index.core.retrievers.VectorIndexRetriever",
|
||||||
return_value=mock_retriever_instance,
|
return_value=mock_retriever_instance,
|
||||||
):
|
):
|
||||||
output = list(stream_chat_with_documents("What is this?", documents))
|
output = collect(stream_chat_with_documents("What is this?", documents))
|
||||||
|
|
||||||
mock_retriever_instance.retrieve.assert_called_once_with("What is this?")
|
mock_retriever_instance.retrieve.assert_called_once_with("What is this?")
|
||||||
mock_synthesizer.synthesize.assert_called_once_with(
|
mock_synthesizer.asynthesize.assert_awaited_once_with(
|
||||||
"What is this?",
|
"What is this?",
|
||||||
mock_retriever_instance.retrieve.return_value,
|
mock_retriever_instance.retrieve.return_value,
|
||||||
)
|
)
|
||||||
@@ -236,20 +259,19 @@ def test_stream_chat_with_multiple_documents_retrieval(patch_embed_nodes) -> Non
|
|||||||
MagicMock(metadata={"document_id": str(doc2.pk), "title": "Document 2"}),
|
MagicMock(metadata={"document_id": str(doc2.pk), "title": "Document 2"}),
|
||||||
]
|
]
|
||||||
|
|
||||||
mock_response_stream = MagicMock()
|
|
||||||
mock_response_stream.response_gen = iter(["chunk1", "chunk2"])
|
|
||||||
|
|
||||||
mock_synthesizer = mock_get_response_synthesizer.return_value
|
mock_synthesizer = mock_get_response_synthesizer.return_value
|
||||||
mock_synthesizer.synthesize.return_value = mock_response_stream
|
mock_synthesizer.asynthesize = AsyncMock(
|
||||||
|
return_value=fake_response_stream(["chunk1", "chunk2"]),
|
||||||
|
)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"llama_index.core.retrievers.VectorIndexRetriever",
|
"llama_index.core.retrievers.VectorIndexRetriever",
|
||||||
return_value=mock_retriever_instance,
|
return_value=mock_retriever_instance,
|
||||||
):
|
):
|
||||||
output = list(stream_chat_with_documents("What's up?", documents))
|
output = collect(stream_chat_with_documents("What's up?", documents))
|
||||||
|
|
||||||
mock_retriever_instance.retrieve.assert_called_once_with("What's up?")
|
mock_retriever_instance.retrieve.assert_called_once_with("What's up?")
|
||||||
mock_synthesizer.synthesize.assert_called_once_with(
|
mock_synthesizer.asynthesize.assert_awaited_once_with(
|
||||||
"What's up?",
|
"What's up?",
|
||||||
mock_retriever_instance.retrieve.return_value,
|
mock_retriever_instance.retrieve.return_value,
|
||||||
)
|
)
|
||||||
@@ -266,7 +288,9 @@ def test_stream_chat_with_multiple_documents_retrieval(patch_embed_nodes) -> Non
|
|||||||
|
|
||||||
def test_stream_chat_empty_document_list() -> None:
|
def test_stream_chat_empty_document_list() -> None:
|
||||||
with patch("paperless_ai.chat.load_or_build_index") as mock_load_index:
|
with patch("paperless_ai.chat.load_or_build_index") as mock_load_index:
|
||||||
output = list(stream_chat_with_documents("Any info?", Document.objects.none()))
|
output = collect(
|
||||||
|
stream_chat_with_documents("Any info?", Document.objects.none()),
|
||||||
|
)
|
||||||
mock_load_index.assert_not_called()
|
mock_load_index.assert_not_called()
|
||||||
assert output == ["Sorry, I couldn't find any content to answer your question."]
|
assert output == ["Sorry, I couldn't find any content to answer your question."]
|
||||||
|
|
||||||
@@ -286,7 +310,7 @@ def test_stream_chat_no_matching_nodes() -> None:
|
|||||||
mock_index.vector_store.get_nodes.return_value = []
|
mock_index.vector_store.get_nodes.return_value = []
|
||||||
mock_load_index.return_value = mock_index
|
mock_load_index.return_value = mock_index
|
||||||
|
|
||||||
output = list(
|
output = collect(
|
||||||
stream_chat_with_documents("Any info?", _fake_documents_queryset([1])),
|
stream_chat_with_documents("Any info?", _fake_documents_queryset([1])),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -317,7 +341,7 @@ def test_stream_chat_unexpected_failure_returns_generic_error(caplog) -> None:
|
|||||||
)
|
)
|
||||||
mock_retriever_cls.return_value = mock_retriever
|
mock_retriever_cls.return_value = mock_retriever
|
||||||
|
|
||||||
output = list(
|
output = collect(
|
||||||
stream_chat_with_documents("Any info?", _fake_documents_queryset([1])),
|
stream_chat_with_documents("Any info?", _fake_documents_queryset([1])),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -367,7 +391,7 @@ class TestStreamChatRetrieval:
|
|||||||
) -> None:
|
) -> None:
|
||||||
doc = DocumentFactory.create(content="hello world")
|
doc = DocumentFactory.create(content="hello world")
|
||||||
# Nothing indexed for this document yet.
|
# Nothing indexed for this document yet.
|
||||||
out = list(
|
out = collect(
|
||||||
chat.stream_chat_with_documents(
|
chat.stream_chat_with_documents(
|
||||||
"question?",
|
"question?",
|
||||||
Document.objects.filter(pk=doc.pk),
|
Document.objects.filter(pk=doc.pk),
|
||||||
@@ -390,7 +414,7 @@ class TestStreamChatRetrieval:
|
|||||||
indexing.llm_index_add_or_update_document(included)
|
indexing.llm_index_add_or_update_document(included)
|
||||||
indexing.llm_index_add_or_update_document(excluded)
|
indexing.llm_index_add_or_update_document(excluded)
|
||||||
|
|
||||||
list(
|
collect(
|
||||||
chat.stream_chat_with_documents(
|
chat.stream_chat_with_documents(
|
||||||
"question?",
|
"question?",
|
||||||
Document.objects.filter(pk=included.pk),
|
Document.objects.filter(pk=included.pk),
|
||||||
@@ -419,7 +443,7 @@ class TestStreamChatRetrieval:
|
|||||||
document = DocumentFactory.create(content="indexed document content")
|
document = DocumentFactory.create(content="indexed document content")
|
||||||
indexing.llm_index_add_or_update_document(document)
|
indexing.llm_index_add_or_update_document(document)
|
||||||
|
|
||||||
list(
|
collect(
|
||||||
chat.stream_chat_with_documents(
|
chat.stream_chat_with_documents(
|
||||||
"question?",
|
"question?",
|
||||||
Document.objects.filter(pk=document.pk),
|
Document.objects.filter(pk=document.pk),
|
||||||
@@ -455,7 +479,7 @@ class TestStreamChatRetrieval:
|
|||||||
deleted_at=timezone.now(),
|
deleted_at=timezone.now(),
|
||||||
)
|
)
|
||||||
|
|
||||||
list(
|
collect(
|
||||||
chat.stream_chat_with_documents(
|
chat.stream_chat_with_documents(
|
||||||
"question?",
|
"question?",
|
||||||
Document.objects.filter(pk=kept.pk),
|
Document.objects.filter(pk=kept.pk),
|
||||||
@@ -487,7 +511,7 @@ class TestStreamChatRetrieval:
|
|||||||
indexing.llm_index_add_or_update_document(doc)
|
indexing.llm_index_add_or_update_document(doc)
|
||||||
permitted = Document.objects.filter(pk__in=[docs[0].pk, docs[1].pk])
|
permitted = Document.objects.filter(pk__in=[docs[0].pk, docs[1].pk])
|
||||||
|
|
||||||
list(chat.stream_chat_with_documents("question?", permitted))
|
collect(chat.stream_chat_with_documents("question?", permitted))
|
||||||
|
|
||||||
metadata_filter = captured_filters[0].filters[0]
|
metadata_filter = captured_filters[0].filters[0]
|
||||||
assert metadata_filter.operator == FilterOperator.NIN
|
assert metadata_filter.operator == FilterOperator.NIN
|
||||||
@@ -520,16 +544,21 @@ class TestStreamChatRetrieval:
|
|||||||
synthesizer = mocker.patch(
|
synthesizer = mocker.patch(
|
||||||
"llama_index.core.response_synthesizers.get_response_synthesizer",
|
"llama_index.core.response_synthesizers.get_response_synthesizer",
|
||||||
).return_value
|
).return_value
|
||||||
synthesizer.synthesize.return_value.response_gen = iter(["chunk"])
|
synthesizer.asynthesize = AsyncMock(
|
||||||
|
return_value=fake_response_stream(["chunk"]),
|
||||||
|
)
|
||||||
|
|
||||||
list(
|
collect(
|
||||||
chat.stream_chat_with_documents(
|
chat.stream_chat_with_documents(
|
||||||
"question?",
|
"question?",
|
||||||
Document.objects.filter(pk=permitted_doc.pk),
|
Document.objects.filter(pk=permitted_doc.pk),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
synthesizer.synthesize.assert_called_once_with("question?", [permitted_node])
|
synthesizer.asynthesize.assert_awaited_once_with(
|
||||||
|
"question?",
|
||||||
|
[permitted_node],
|
||||||
|
)
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_get_document_references_only_queries_referenced_documents(
|
def test_get_document_references_only_queries_referenced_documents(
|
||||||
@@ -612,9 +641,9 @@ class TestChatReadLock:
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
def synthesize(*_args: object) -> object:
|
def asynthesize(*_args: object) -> MagicMock:
|
||||||
events.append("synthesize")
|
events.append("synthesize")
|
||||||
return mocker.MagicMock(response_gen=iter(["chunk"]))
|
return fake_response_stream(["chunk"])
|
||||||
|
|
||||||
mocker.patch("paperless_ai.chat.read_store", fake_read_store)
|
mocker.patch("paperless_ai.chat.read_store", fake_read_store)
|
||||||
mocker.patch("paperless_ai.chat.AIClient")
|
mocker.patch("paperless_ai.chat.AIClient")
|
||||||
@@ -625,9 +654,9 @@ class TestChatReadLock:
|
|||||||
)
|
)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
"llama_index.core.response_synthesizers.get_response_synthesizer",
|
"llama_index.core.response_synthesizers.get_response_synthesizer",
|
||||||
).return_value.synthesize.side_effect = synthesize
|
).return_value.asynthesize = AsyncMock(side_effect=asynthesize)
|
||||||
|
|
||||||
list(
|
collect(
|
||||||
stream_chat_with_documents(
|
stream_chat_with_documents(
|
||||||
"What is this?",
|
"What is this?",
|
||||||
Document.objects.filter(pk=document.pk),
|
Document.objects.filter(pk=document.pk),
|
||||||
|
|||||||
Reference in new issue
Block a user