summaryrefslogtreecommitdiff
path: root/src/ollama_web/routes/chat.py
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-06-24 18:11:10 +0900
committertanaka takashi <isazaka@gmail.com>2026-06-24 18:11:10 +0900
commitf7e2894856c10d8a1e2d429a7858d9f143c42404 (patch)
treefc916d63947acead39868968db26ecd80854579c /src/ollama_web/routes/chat.py
parent374ba05fe2a4022d95ed45322a13e9eef3ed59de (diff)
Web検索などのツール呼び出しによる応答不能を回避
Diffstat (limited to 'src/ollama_web/routes/chat.py')
-rw-r--r--src/ollama_web/routes/chat.py15
1 files changed, 15 insertions, 0 deletions
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