From f7e2894856c10d8a1e2d429a7858d9f143c42404 Mon Sep 17 00:00:00 2001 From: tanaka takashi Date: Wed, 24 Jun 2026 18:11:10 +0900 Subject: Web検索などのツール呼び出しによる応答不能を回避 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ollama_web/routes/chat.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/ollama_web/routes') diff --git a/src/ollama_web/routes/chat.py b/src/ollama_web/routes/chat.py index 08b330a..eb7a555 100644 --- a/src/ollama_web/routes/chat.py +++ b/src/ollama_web/routes/chat.py @@ -22,6 +22,15 @@ from ..tools.registry import ToolRegistry, default_registry logger = logging.getLogger("ollama_web.chat") +# System prompt injected when tools are enabled. Encourages the model to use +# tools iteratively rather than firing many searches at once. +_TOOL_SYSTEM_PROMPT = ( + "あなたはWeb検索・スクレイピング・ファイル取得ツールを利用できます。" + "一度の回答で必要以上に多くの検索を同時に行わず、結果を確認してから次の調査を行ってください。" + "各ツール結果は要約されて提供されるため、重要なポイントを把握して活用してください。" +) + + def _parse_messages(raw: list[dict[str, Any]]) -> list[Message]: """Convert raw JSON message dicts into ``ollama.Message`` objects.""" out: list[Message] = [] @@ -155,6 +164,12 @@ async def _chat_event_stream( 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=_TOOL_SYSTEM_PROMPT)) + host = request.app.state.settings.ollama_host from ollama_web import llm # local import to avoid circular at module load -- cgit v1.2.3