mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 00:40:45 +08:00
fix(llm): DeepSeek strict tools via function.strict, not extra_body
Match DeepSeek Tool Calls docs: set strict:true on each tools[].function. Remove non-documented strict_tool_mode extra_body. Keep AIA_DEEPSEEK_STRICT_TOOL_MODE=0 to opt out. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
420abac9f1
commit
6ba3234a87
2 changed files with 60 additions and 74 deletions
|
|
@ -109,11 +109,13 @@ def _messages_contain_list_with_image(messages: list[dict[str, Any]]) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _deepseek_strict_tool_mode_enabled(*, base_url: str | None, model: str | None) -> bool:
|
def _deepseek_strict_tools_enabled(*, base_url: str | None, model: str | None) -> bool:
|
||||||
"""DeepSeek OpenAI-compat: optional ``strict_tool_mode`` in request body (via ``extra_body``).
|
"""Whether to set ``strict: true`` on each OpenAI-style ``tools[].function`` for DeepSeek.
|
||||||
|
|
||||||
Upstream documents this as reducing DSML-in-text tool output in favor of native ``tool_calls``.
|
DeepSeek documents strict tool output in
|
||||||
Opt out with ``AIA_DEEPSEEK_STRICT_TOOL_MODE=0`` when a proxy rejects unknown fields.
|
https://api-docs.deepseek.com/zh-cn/guides/tool_calls :
|
||||||
|
each ``function`` should include ``\"strict\": true`` (Beta; also needs ``/beta`` base URL
|
||||||
|
and JSON Schema rules on the provider side). Opt out with ``AIA_DEEPSEEK_STRICT_TOOL_MODE=0``.
|
||||||
"""
|
"""
|
||||||
raw = str(os.getenv("AIA_DEEPSEEK_STRICT_TOOL_MODE") or "").strip().lower()
|
raw = str(os.getenv("AIA_DEEPSEEK_STRICT_TOOL_MODE") or "").strip().lower()
|
||||||
if raw in ("0", "false", "no", "off"):
|
if raw in ("0", "false", "no", "off"):
|
||||||
|
|
@ -122,19 +124,28 @@ def _deepseek_strict_tool_mode_enabled(*, base_url: str | None, model: str | Non
|
||||||
return "deepseek" in hay
|
return "deepseek" in hay
|
||||||
|
|
||||||
|
|
||||||
def _merge_deepseek_strict_tool_mode_extra_body(
|
def _apply_deepseek_strict_tools(tools: list[dict[str, Any]] | None) -> list[dict[str, Any]] | None:
|
||||||
kwargs: dict[str, Any],
|
"""Return a shallow-copied tools list with ``function.strict: true`` on each function tool."""
|
||||||
*,
|
if not tools:
|
||||||
base_url: str | None,
|
return tools
|
||||||
model: str | None,
|
out: list[dict[str, Any]] = []
|
||||||
use_tools: bool,
|
for raw in tools:
|
||||||
) -> None:
|
if not isinstance(raw, dict):
|
||||||
if not use_tools or not _deepseek_strict_tool_mode_enabled(base_url=base_url, model=model):
|
out.append(raw) # type: ignore[arg-type]
|
||||||
return
|
continue
|
||||||
extra_body = kwargs.get("extra_body") if isinstance(kwargs.get("extra_body"), dict) else {}
|
if str(raw.get("type") or "").strip().lower() != "function":
|
||||||
extra_body = dict(extra_body)
|
out.append(dict(raw))
|
||||||
extra_body["strict_tool_mode"] = True
|
continue
|
||||||
kwargs["extra_body"] = extra_body
|
t = dict(raw)
|
||||||
|
fn = t.get("function")
|
||||||
|
if isinstance(fn, dict):
|
||||||
|
fn2 = dict(fn)
|
||||||
|
fn2["strict"] = True
|
||||||
|
t["function"] = fn2
|
||||||
|
else:
|
||||||
|
t["function"] = {"strict": True}
|
||||||
|
out.append(t)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def _should_proactively_downgrade_multimodal_messages(
|
def _should_proactively_downgrade_multimodal_messages(
|
||||||
|
|
@ -556,9 +567,10 @@ class OpenAIChatModel(ChatModel):
|
||||||
kwargs["tools"] = plan.tools_wired
|
kwargs["tools"] = plan.tools_wired
|
||||||
except Exception:
|
except Exception:
|
||||||
kwargs["tools"] = complete_openai_tools_wire_parameters(tools)
|
kwargs["tools"] = complete_openai_tools_wire_parameters(tools)
|
||||||
_merge_deepseek_strict_tool_mode_extra_body(
|
if use_tools and _deepseek_strict_tools_enabled(base_url=self.base_url, model=self.model):
|
||||||
kwargs, base_url=self.base_url, model=self.model, use_tools=use_tools
|
tw = kwargs.get("tools")
|
||||||
)
|
if isinstance(tw, list):
|
||||||
|
kwargs["tools"] = _apply_deepseek_strict_tools(tw)
|
||||||
try:
|
try:
|
||||||
return self._client.chat.completions.create(**kwargs)
|
return self._client.chat.completions.create(**kwargs)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|
@ -712,8 +724,8 @@ __all__ = [
|
||||||
"OpenAIChatModel",
|
"OpenAIChatModel",
|
||||||
"_likely_gemini_openai_compat_base_url",
|
"_likely_gemini_openai_compat_base_url",
|
||||||
"_model_id_suggests_gemini",
|
"_model_id_suggests_gemini",
|
||||||
"_deepseek_strict_tool_mode_enabled",
|
"_deepseek_strict_tools_enabled",
|
||||||
"_merge_deepseek_strict_tool_mode_extra_body",
|
"_apply_deepseek_strict_tools",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
"""DeepSeek OpenAI-compat: strict_tool_mode via extra_body."""
|
"""DeepSeek: per-function ``strict: true`` on tools (official Tool Calls / strict Beta)."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
@ -6,15 +6,15 @@ import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from svc.llm.transports.openai_chat_completions import (
|
from svc.llm.transports.openai_chat_completions import (
|
||||||
_deepseek_strict_tool_mode_enabled,
|
_apply_deepseek_strict_tools,
|
||||||
_merge_deepseek_strict_tool_mode_extra_body,
|
_deepseek_strict_tools_enabled,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class DeepseekStrictToolModeTests(unittest.TestCase):
|
class DeepseekStrictToolsTests(unittest.TestCase):
|
||||||
def test_enabled_when_deepseek_in_base_url(self) -> None:
|
def test_enabled_when_deepseek_in_base_url(self) -> None:
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
_deepseek_strict_tool_mode_enabled(
|
_deepseek_strict_tools_enabled(
|
||||||
base_url="https://api.deepseek.com/v1",
|
base_url="https://api.deepseek.com/v1",
|
||||||
model="deepseek-chat",
|
model="deepseek-chat",
|
||||||
)
|
)
|
||||||
|
|
@ -25,7 +25,7 @@ class DeepseekStrictToolModeTests(unittest.TestCase):
|
||||||
try:
|
try:
|
||||||
os.environ["AIA_DEEPSEEK_STRICT_TOOL_MODE"] = "0"
|
os.environ["AIA_DEEPSEEK_STRICT_TOOL_MODE"] = "0"
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
_deepseek_strict_tool_mode_enabled(
|
_deepseek_strict_tools_enabled(
|
||||||
base_url="https://api.deepseek.com/v1",
|
base_url="https://api.deepseek.com/v1",
|
||||||
model="x",
|
model="x",
|
||||||
)
|
)
|
||||||
|
|
@ -36,54 +36,28 @@ class DeepseekStrictToolModeTests(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
os.environ["AIA_DEEPSEEK_STRICT_TOOL_MODE"] = prev
|
os.environ["AIA_DEEPSEEK_STRICT_TOOL_MODE"] = prev
|
||||||
|
|
||||||
def test_merge_sets_extra_body_strict_tool_mode(self) -> None:
|
def test_apply_sets_strict_on_function_tools(self) -> None:
|
||||||
kwargs: dict = {"model": "m", "messages": [], "stream": False, "tools": [{"type": "function"}]}
|
tools = [
|
||||||
_merge_deepseek_strict_tool_mode_extra_body(
|
{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object", "properties": {}}}},
|
||||||
kwargs,
|
{"type": "not_function", "x": 1},
|
||||||
base_url="https://api.deepseek.com/v1",
|
]
|
||||||
model="deepseek-chat",
|
out = _apply_deepseek_strict_tools(tools)
|
||||||
use_tools=True,
|
self.assertIsNotNone(out)
|
||||||
)
|
assert out is not None
|
||||||
self.assertEqual(kwargs.get("extra_body"), {"strict_tool_mode": True})
|
self.assertTrue(out[0]["function"]["strict"])
|
||||||
|
self.assertEqual(out[1].get("type"), "not_function")
|
||||||
|
self.assertNotIn("strict", out[1])
|
||||||
|
# input not mutated
|
||||||
|
self.assertNotIn("strict", tools[0].get("function", {}))
|
||||||
|
|
||||||
def test_merge_preserves_existing_extra_body(self) -> None:
|
def test_apply_skips_non_deepseek_not_tested_here(self) -> None:
|
||||||
kwargs: dict = {
|
out = _apply_deepseek_strict_tools([])
|
||||||
"model": "m",
|
self.assertEqual(out, [])
|
||||||
"messages": [],
|
|
||||||
"stream": False,
|
|
||||||
"tools": [{"type": "function"}],
|
|
||||||
"extra_body": {"thinking": {"type": "disabled"}},
|
|
||||||
}
|
|
||||||
_merge_deepseek_strict_tool_mode_extra_body(
|
|
||||||
kwargs,
|
|
||||||
base_url="https://api.deepseek.com",
|
|
||||||
model="deepseek-reasoner",
|
|
||||||
use_tools=True,
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
kwargs["extra_body"],
|
|
||||||
{"thinking": {"type": "disabled"}, "strict_tool_mode": True},
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_merge_skips_without_tools(self) -> None:
|
def test_apply_handles_missing_function_dict(self) -> None:
|
||||||
kwargs: dict = {"model": "m", "messages": [], "stream": False}
|
out = _apply_deepseek_strict_tools([{"type": "function"}])
|
||||||
_merge_deepseek_strict_tool_mode_extra_body(
|
assert out is not None
|
||||||
kwargs,
|
self.assertEqual(out[0]["function"], {"strict": True})
|
||||||
base_url="https://api.deepseek.com/v1",
|
|
||||||
model="deepseek-chat",
|
|
||||||
use_tools=False,
|
|
||||||
)
|
|
||||||
self.assertNotIn("extra_body", kwargs)
|
|
||||||
|
|
||||||
def test_merge_skips_non_deepseek(self) -> None:
|
|
||||||
kwargs: dict = {"model": "m", "messages": [], "stream": False, "tools": [{"type": "function"}]}
|
|
||||||
_merge_deepseek_strict_tool_mode_extra_body(
|
|
||||||
kwargs,
|
|
||||||
base_url="https://api.openai.com/v1",
|
|
||||||
model="gpt-4o-mini",
|
|
||||||
use_tools=True,
|
|
||||||
)
|
|
||||||
self.assertNotIn("extra_body", kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue