summaryrefslogtreecommitdiff
path: root/src/ollama_web/routes/chat.py
blob: 748b090b735a08a193363cfa1da8702551b3754b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"""SSE streaming chat endpoint."""

from __future__ import annotations

import functools
import html
import inspect
import json
import logging
from collections.abc import AsyncIterator
from datetime import datetime
from typing import Any

from ollama import Message
from ollama._types import Image as OllamaImage
from starlette.requests import Request
from starlette.responses import StreamingResponse

from ..i18n import t
from ..mcp import collect_mcp_tools, make_mcp_executor
from ..prompts import get_prompt
from ..sessions import SessionStore, is_valid_id
from ..settings_store import load_app_settings
from ..tools.fetch import pop_fetched_files
from ..tools.helper.image import resize_image
from ..tools.pdf import make_session_pdf_tool
from ..tools.registry import ToolArtifact, ToolRegistry, default_registry

logger = logging.getLogger("ollama_web.chat")


def _limit_text(text: str, limit: int) -> str:
    if len(text) <= limit:
        return text
    return text[:limit] + "\n…[truncated]"


def _compose_system_prompt(language: str, custom_prompt: str) -> str:
    """Combine the localized built-in tool prompt, current time, and user prompt."""
    tool_prompt = get_prompt("tool_system", lang=language)
    time_line = get_prompt("current_time", lang=language).format(
        time=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    )
    parts = [tool_prompt, time_line]
    if custom_prompt:
        parts.append(custom_prompt)
    return "\n\n".join(parts)


def _parse_messages(raw: list[dict[str, Any]]) -> list[Message]:
    """Convert raw JSON message dicts into ``ollama.Message`` objects."""
    out: list[Message] = []
    for m in raw:
        role = m.get("role", "user")
        content = _limit_text(str(m.get("content", "") or ""), 20000)
        name = m.get("name")
        images = m.get("images")
        kwargs: dict[str, Any] = {"role": role, "content": content}
        if name:
            kwargs["name"] = name
        if images:
            # ollama expects a sequence of ollama._types.Image objects.
            kwargs["images"] = [OllamaImage(value=img) for img in images]
        out.append(Message(**kwargs))
    return out


async def _build_registry(
    session_id: str | None, store: SessionStore | None = None
) -> ToolRegistry:
    """Return a registry with built-in tools and any configured MCP tools."""
    reg = default_registry()

    original = reg.get("search_and_fetch")
    if original is not None and session_id is not None:

        @functools.wraps(original)
        def wrapped(*args: Any, **kwargs: Any) -> Any:
            sig = inspect.signature(original)
            bound = sig.bind(*args, **kwargs)
            bound.arguments["session_id"] = session_id
            return original(*bound.args, **bound.kwargs)

        reg._tools["search_and_fetch"] = wrapped  # noqa: SLF001

    if session_id is not None and store is not None:

        def load_pdf_attachment(file_id: str) -> bytes | None:
            chat_file = store.get_file(session_id, file_id)
            if chat_file is None or chat_file.is_image:
                return None
            if chat_file.mime.lower() != "application/pdf" and not chat_file.name.lower().endswith(
                ".pdf"
            ):
                return None
            return store.get_file_data(session_id, file_id)

        reg._tools["pdf_to_text"] = make_session_pdf_tool(load_pdf_attachment)  # noqa: SLF001

    try:
        mcp_tools = await collect_mcp_tools()
    except Exception as exc:  # noqa: BLE001
        logger.warning("failed to collect MCP tools: %s", exc)
        mcp_tools = []

    tool_names = [t.get("function", {}).get("name") for t in mcp_tools]
    logger.info("collected %d MCP tool(s): %s", len(mcp_tools), tool_names)

    if mcp_tools:
        artifact_handler = None
        if session_id is not None and store is not None:

            def persist_artifact(full_name: str, artifact: ToolArtifact) -> None:
                if not artifact.for_user:
                    return
                chat_file = store.add_file(
                    session_id=session_id,
                    name=artifact.name,
                    data=artifact.data,
                    mime=artifact.mime,
                    text="",
                    source=full_name,
                )
                artifact.file_id = chat_file.id

            artifact_handler = persist_artifact

        executor = make_mcp_executor(artifact_handler=artifact_handler)
        for tool_def in mcp_tools:
            fn = tool_def.get("function", {})
            name = fn.get("name")
            if not name:
                continue
            try:
                reg.register_mcp_tool(name, tool_def, executor)
            except ValueError as exc:
                logger.warning("skipping duplicate MCP tool %s: %s", name, exc)

    return reg


def _attachment_context(
    store: SessionStore,
    session_id: str,
    file_ids: list[str],
    *,
    total_limit: int,
    language: str | None = None,
) -> str:
    if total_limit <= 0:
        return ""
    chat_files = []
    for file_id in file_ids:
        chat_file = store.get_file(session_id, file_id)
        if chat_file is not None and not chat_file.is_image and not chat_file.is_audio:
            chat_files.append(chat_file)
    if not chat_files:
        return ""

    parts: list[str] = []
    prefix = get_prompt("attachment_prefix", lang=language)
    paging_hint = get_prompt(
        "attachment_paging",
        lang=language,
        fallback=(
            "PDF previews may have unread content. Use pdf_to_text with the attachment URI "
            "and next_token until has_more is absent."
        ),
    )
    used = len(prefix) + len(paging_hint) + 4
    per_file = max(256, min(4000, (total_limit - used) // max(1, len(chat_files))))
    pdf_tool = make_session_pdf_tool(
        lambda file_id: (
            store.get_file_data(session_id, file_id)
            if store.get_file(session_id, file_id) is not None
            else None
        )
    )

    for chat_file in chat_files:
        source_uri = f"attachment:{chat_file.id}"
        if chat_file.mime.lower() == "application/pdf" or chat_file.name.lower().endswith(".pdf"):
            text = pdf_tool(source_uri, max_chars=per_file)
        else:
            text = _limit_text(chat_file.text or "", per_file)
        block = (
            f'<attached-file name="{html.escape(chat_file.name)}" id="{chat_file.id}" '
            f'source="{chat_file.source}" uri="{source_uri}">\n{text}\n</attached-file>'
        )
        if used + len(block) + 2 > total_limit:
            break
        parts.append(block)
        used += len(block) + 2
    if not parts:
        return ""
    return "\n\n".join([prefix, paging_hint, *parts])


def _has_image_attachments(store: SessionStore, session_id: str, file_ids: list[str]) -> bool:
    return any(
        (chat_file := store.get_file(session_id, file_id)) is not None and chat_file.is_image
        for file_id in file_ids
    )


def _has_audio_attachments(store: SessionStore, session_id: str, file_ids: list[str]) -> bool:
    return any(
        (chat_file := store.get_file(session_id, file_id)) is not None and chat_file.is_audio
        for file_id in file_ids
    )


def _collect_images(store: SessionStore, session_id: str, file_ids: list[str]) -> list[bytes]:
    images: list[bytes] = []
    for file_id in file_ids:
        chat_file = store.get_file(session_id, file_id)
        if chat_file is None or not chat_file.is_image:
            continue
        data = store.get_file_data(session_id, file_id)
        if data is None:
            continue
        try:
            resized = resize_image(data, name=chat_file.name, max_dimension=1024, quality=85)
            images.append(resized)
        except Exception:  # noqa: BLE001
            logger.warning(
                "failed to resize image %s, falling back to original bytes", chat_file.name
            )
            images.append(data)
    return images


def _token_count_from_done(
    done_event: dict[str, Any] | None, num_ctx: int
) -> dict[str, int] | None:
    if not done_event:
        return None
    prompt_eval_count = done_event.get("prompt_eval_count")
    eval_count = done_event.get("eval_count")
    if prompt_eval_count is None or eval_count is None:
        return None
    return {
        "prompt_eval_count": int(prompt_eval_count),
        "eval_count": int(eval_count),
        "num_ctx": num_ctx,
        "total_count": int(prompt_eval_count) + int(eval_count),
    }


async def _event_stream(
    request: Request,
    payload: dict[str, Any],
    session_id: str | None,
) -> AsyncIterator[bytes]:
    """Yield SSE events from the tool-augmented chat loop."""
    # Send a keepalive comment immediately so the browser knows the connection is alive.
    yield b":\n\n"
    async for chunk in _chat_event_stream(request, payload, session_id):
        data = json.dumps(chunk, ensure_ascii=False)
        yield f"data: {data}\n\n".encode()


async def _chat_event_stream(
    request: Request,
    payload: dict[str, Any],
    session_id: str | None,
) -> AsyncIterator[dict[str, Any]]:
    """Core chat loop yielding events. Exceptions are converted to SSE error events."""
    model = payload.get("model") or request.app.state.settings.default_model
    think = payload.get("think")
    runtime_settings = request.app.state.settings
    app_settings = load_app_settings(runtime_settings.data_dir, runtime_settings.language)
    language = str(app_settings["ui"]["language"])
    ollama_settings = app_settings["ollama"]
    ollama_options = dict(ollama_settings["options"])
    custom_prompt = str(ollama_settings["system_prompt"])
    system_prompt = _compose_system_prompt(language, custom_prompt)
    num_ctx_value = ollama_options.get("num_ctx")
    try:
        num_ctx = int(num_ctx_value) if num_ctx_value is not None else 0
    except (TypeError, ValueError):
        num_ctx = 0

    store: SessionStore = request.app.state.session_store
    file_ids: list[str] = [
        str(file_id) for file_id in payload.get("file_ids", []) if is_valid_id(str(file_id))
    ]
    user_content = ""
    assistant_text = ""
    assistant_file_ids: list[str] = []
    last_done_event: dict[str, Any] | None = None

    try:
        session_messages: list[Message] = []
        if session_id:
            session = store.get(session_id)
            if session is not None:
                session_messages = _parse_messages(session.get("messages", []))

        raw_messages = list(payload.get("messages", []))
        if raw_messages:
            user_content = _limit_text(
                str(raw_messages[-1].get("content", "") or ""),
                request.app.state.settings.max_message_chars,
            )
            raw_messages[-1]["content"] = user_content

        # An attachment-only submission still needs a user message to carry
        # either image bytes or the localized unsupported-vision notice.
        if file_ids and not raw_messages:
            raw_messages.append({"role": "user", "content": ""})

        host = request.app.state.settings.ollama_host

        from ollama_web import llm  # local import to avoid circular at module load

        capabilities = llm.get_model_capabilities(model, host=host)
        logger.info(
            "chat stream model=%s capabilities=%s",
            model,
            sorted(capabilities),
        )

        # Fallback: some cloud/proxy models support tools even though ollama
        # does not advertise the capability. Enable only tools; vision remains
        # opt-in because sending image bytes to an unknown model is not safe.
        if "tools" not in capabilities and ":cloud" in model:
            capabilities.add("tools")
            logger.info("enabled tools capability fallback for cloud model %s", model)

        attachment_ctx = ""
        if session_id and file_ids:
            attachment_budget = max(
                0,
                request.app.state.settings.max_message_chars - len(user_content) - 256,
            )
            attachment_ctx = _attachment_context(
                store,
                session_id,
                file_ids,
                total_limit=attachment_budget,
                language=language,
            )

        if attachment_ctx and user_content:
            # Inject attachment context into the latest user message.
            augmented = f"{user_content}\n\n{attachment_ctx}"
            if raw_messages:
                raw_messages[-1]["content"] = augmented
        elif attachment_ctx:
            raw_messages.append({"role": "user", "content": attachment_ctx})

        # Attach image bytes only when Ollama explicitly advertises vision.
        # Other models receive a localized fixed notice, never base64 text.
        if session_id and file_ids and raw_messages:
            notices: list[str] = []
            if _has_image_attachments(store, session_id, file_ids):
                if "vision" in capabilities:
                    images = _collect_images(store, session_id, file_ids)
                    if images:
                        raw_messages[-1]["images"] = images
                else:
                    notices.append(get_prompt("vision_unsupported_attachment", lang=language))
            if _has_audio_attachments(store, session_id, file_ids):
                notices.append(get_prompt("audio_unsupported_attachment", lang=language))
            if notices:
                current = str(raw_messages[-1].get("content", "") or "")
                raw_messages[-1]["content"] = f"{current}\n\n" + "\n\n".join(notices)

        messages = _parse_messages(raw_messages)
        if session_messages:
            messages = session_messages + messages

        # Prepend a system prompt to encourage efficient tool usage when tools
        # are available. Avoid duplicate injection if the session already has
        # a system message.
        if not any(getattr(m, "role", None) == "system" for m in messages):
            messages.insert(0, Message(role="system", content=system_prompt))

        registry = await _build_registry(session_id, store)

        total_image_bytes = sum(
            len(img.value) for img in (getattr(messages[-1], "images", None) or [])
        )
        logger.info(
            "chat stream model=%s session=%s messages=%d images=%d image_bytes=%d",
            model,
            session_id,
            len(messages),
            len(getattr(messages[-1], "images", []) or []),
            total_image_bytes,
        )

        async for event in llm.astream_chat_with_tools(
            messages,
            model,
            think=think,
            host=host,
            registry=registry,
            capabilities=capabilities,
            options=ollama_options,
            language=language,
        ):
            if event.get("type") == "delta":
                assistant_text = _limit_text(
                    assistant_text + event.get("content", ""),
                    request.app.state.settings.max_message_chars,
                )
            if event.get("type") == "done":
                event["num_ctx"] = num_ctx
                last_done_event = event
            if event.get("type") == "tool_end":
                for media in event.get("media", []):
                    file_id = str(media.get("id", ""))
                    if is_valid_id(file_id) and file_id not in assistant_file_ids:
                        assistant_file_ids.append(file_id)
            yield event

    except Exception as exc:  # noqa: BLE001
        logger.exception("chat stream failed")
        error_msg = f"Stream failed: {exc}"
        yield {"type": "error", "message": error_msg}
        # Persist the error as the assistant message so it survives reloads
        # and re-renders in the chat pane.
        assistant_text = (
            f"[ERROR] {error_msg}\n\n{t('errors.vision_model_required', lang=language)}"
        )

    finally:
        # Persist user and assistant messages to the session even when the
        # upstream call failed, so the UI keeps the user message.
        if session_id and (user_content or file_ids):
            store.add_message(
                session_id=session_id,
                role="user",
                content=user_content,
                attachments=file_ids,
            )
            if assistant_text or assistant_file_ids:
                token_count = _token_count_from_done(last_done_event, num_ctx)
                store.add_message(
                    session_id=session_id,
                    role="assistant",
                    content=assistant_text,
                    attachments=assistant_file_ids,
                    token_count=token_count,
                )

        # Persist fetched files from tools like search_and_fetch.
        if session_id:
            fetched = pop_fetched_files(session_id)
            for f in fetched:
                store.add_file(
                    session_id=session_id,
                    name=f["name"],
                    data=f["data"],
                    mime=f["mime"],
                    text=f["text"],
                    source="fetch",
                )

        # Always emit a terminal done event so the client closes cleanly.
        yield {"type": "done"}


async def chat(request: Request) -> StreamingResponse:
    """POST /api/chat: stream chat events as Server-Sent Events."""
    try:
        body = await request.body()
        if len(body) > request.app.state.settings.max_message_chars * 4:
            data = json.dumps(
                {"type": "error", "message": "Request body too large"},
                ensure_ascii=False,
            )
            return StreamingResponse(
                _single_event(data),
                media_type="text/event-stream",
                status_code=413,
            )
        payload = json.loads(body.decode("utf-8"))
    except Exception as exc:
        logger.error("failed to parse request json: %s", exc)
        data = json.dumps({"type": "error", "message": f"Invalid JSON: {exc}"}, ensure_ascii=False)
        return StreamingResponse(
            _single_event(data),
            media_type="text/event-stream",
        )

    session_id = payload.get("session_id")
    msg_count = len(payload.get("messages", []))
    logger.info(
        "chat request model=%s session=%s messages=%d",
        payload.get("model"),
        session_id,
        msg_count,
    )
    return StreamingResponse(
        _event_stream(request, payload, session_id),
        media_type="text/event-stream",
    )


async def _single_event(data: str) -> AsyncIterator[bytes]:
    """Yield a single SSE event and finish."""
    yield f"data: {data}\n\n".encode()