core/tools: search through a local tool where a hosted one is ignored

Only DeepSeek's Anthropic-compatible surface runs a hosted ``web_search``: the
Responses and chat-completions surfaces answer "I can't browse the web" with
``{"type": "web_search"}`` and with the server-tool shape alike, so a DeepSeek
provider that talks them had no search at all.

``config.routing.local_web_search`` decides per provider: a DeepSeek provider
whose ``provider_tools`` ask for ``web_search`` and whose convention is not
``anthropic`` gets the local tool (``tools/net/search.py``), which asks the
search-capable endpoint behind the scenes and hands the model one ``title —
url`` line per hit — read a page with ``fetch``, the bodies stay server-side.
DeepSeek's preset asks for no hosted tool by default, so this stays opt-in.
This commit is contained in:
2026-10-06 23:45:35 +08:00
parent e2ee937d8f
commit a0d39bba6d
5 changed files with 228 additions and 1 deletions
+54
View File
@@ -0,0 +1,54 @@
"""The routing decisions around a provider's API surface."""
from __future__ import annotations
from dataclasses import replace
from plyngent.config.models import DeepseekProvider
from plyngent.config.routing import (
EffectiveProvider,
deepseek_search_base_url,
local_web_search,
resolve_effective_provider,
wants_web_search,
)
SEARCH_TOOLS: list[dict[str, object]] = [{"type": "web_search"}]
def _effective(
*,
convention: str = "",
url: str = "",
tools: list[dict[str, object]] | None = None,
) -> EffectiveProvider:
provider = DeepseekProvider(access_key_or_token="sk-test", convention=convention, url=url)
effective = resolve_effective_provider(provider, model="deepseek-flash")
return effective if tools is None else replace(effective, provider_tools=tools)
def test_a_deepseek_provider_asks_for_no_hosted_tool_by_default() -> None:
# OpenAI's preset defaults ``provider_tools`` to web_search; DeepSeek's does
# not, so search stays opt-in there (``provider_tools = [{type = "web_search"}]``).
assert wants_web_search(_effective()) is False
assert wants_web_search(_effective(tools=SEARCH_TOOLS)) is True
def test_chat_and_responses_conventions_use_the_local_tool() -> None:
# Only the Anthropic surface hosts a search; the others ignore a hosted tool,
# so an asked-for web_search becomes plyngent's own tool there.
assert local_web_search(_effective(convention="", tools=SEARCH_TOOLS)) is True
assert local_web_search(_effective(convention="responses", tools=SEARCH_TOOLS)) is True
assert local_web_search(_effective(convention="anthropic", tools=SEARCH_TOOLS)) is False
def test_without_provider_tools_there_is_no_search() -> None:
assert local_web_search(_effective(convention="responses", tools=[])) is False
def test_search_base_url_prefers_an_anthropic_endpoint() -> None:
assert deepseek_search_base_url(_effective(convention="responses")) == "https://api.deepseek.com/anthropic"
assert (
deepseek_search_base_url(_effective(convention="responses", url="https://gateway.example/anthropic"))
== "https://gateway.example/anthropic"
)
+58
View File
@@ -0,0 +1,58 @@
"""The local ``web_search`` tool DeepSeek uses where its surface cannot host one."""
from __future__ import annotations
from typing import Any
import pytest
from plyngent.agent.tools import ToolTag
from plyngent.lmproto.deepseek.anthropic.search import SearchHit
from plyngent.tools.net.search import WEB_SEARCH_TOOL_NAME, build_web_search_tool
class _StubSearch:
"""Stands in for :class:`DeepseekSearchClient`."""
def __init__(self, hits: list[SearchHit]) -> None:
self.hits = hits
self.queries: list[tuple[str, str]] = []
async def search(self, query: str, *, model: str, **kwargs: object) -> list[SearchHit]:
self.queries.append((query, model))
return self.hits
def _definition(hits: list[SearchHit]) -> tuple[Any, _StubSearch]:
client = _StubSearch(hits)
return build_web_search_tool(client, model="deepseek-flash"), client
def test_tool_definition_shape() -> None:
definition, _ = _definition([])
assert definition.name == WEB_SEARCH_TOOL_NAME
assert definition.tags == ToolTag.LOCAL | ToolTag.READ_ONLY
assert "fetch" in definition.description
parameters = definition.parameters
assert parameters["properties"]["query"] == {"type": "string"}
assert parameters["required"] == ["query"]
@pytest.mark.asyncio
async def test_handler_returns_one_line_per_hit() -> None:
definition, client = _definition([SearchHit(title="DeepSeek-V4 preview", url="https://api-docs.deepseek.com/x")])
assert await definition.handler(query="DeepSeek-V4") == ("- DeepSeek-V4 preview — https://api-docs.deepseek.com/x")
assert client.queries == [("DeepSeek-V4", "deepseek-flash")]
@pytest.mark.asyncio
async def test_handler_caps_the_hits_and_names_an_untitled_page() -> None:
definition, _ = _definition([SearchHit(title="", url=f"https://example.com/{index}") for index in range(5)])
lines = (await definition.handler(query="anything", hits=2)).splitlines()
assert lines == ["- (untitled) — https://example.com/0", "- (untitled) — https://example.com/1"]
@pytest.mark.asyncio
async def test_handler_reports_an_empty_search() -> None:
definition, _ = _definition([])
assert "no results" in await definition.handler(query="nothing matches this")