mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-15 22:13:23 +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
|
|
@ -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
|
||||
|
||||
|
|
@ -6,15 +6,15 @@ import os
|
|||
import unittest
|
||||
|
||||
from svc.llm.transports.openai_chat_completions import (
|
||||
_deepseek_strict_tool_mode_enabled,
|
||||
_merge_deepseek_strict_tool_mode_extra_body,
|
||||
_apply_deepseek_strict_tools,
|
||||
_deepseek_strict_tools_enabled,
|
||||
)
|
||||
|
||||
|
||||
class DeepseekStrictToolModeTests(unittest.TestCase):
|
||||
class DeepseekStrictToolsTests(unittest.TestCase):
|
||||
def test_enabled_when_deepseek_in_base_url(self) -> None:
|
||||
self.assertTrue(
|
||||
_deepseek_strict_tool_mode_enabled(
|
||||
_deepseek_strict_tools_enabled(
|
||||
base_url="https://api.deepseek.com/v1",
|
||||
model="deepseek-chat",
|
||||
)
|
||||
|
|
@ -25,7 +25,7 @@ class DeepseekStrictToolModeTests(unittest.TestCase):
|
|||
try:
|
||||
os.environ["AIA_DEEPSEEK_STRICT_TOOL_MODE"] = "0"
|
||||
self.assertFalse(
|
||||
_deepseek_strict_tool_mode_enabled(
|
||||
_deepseek_strict_tools_enabled(
|
||||
base_url="https://api.deepseek.com/v1",
|
||||
model="x",
|
||||
)
|
||||
|
|
@ -36,54 +36,28 @@ class DeepseekStrictToolModeTests(unittest.TestCase):
|
|||
else:
|
||||
os.environ["AIA_DEEPSEEK_STRICT_TOOL_MODE"] = prev
|
||||
|
||||
def test_merge_sets_extra_body_strict_tool_mode(self) -> None:
|
||||
kwargs: dict = {"model": "m", "messages": [], "stream": False, "tools": [{"type": "function"}]}
|
||||
_merge_deepseek_strict_tool_mode_extra_body(
|
||||
kwargs,
|
||||
base_url="https://api.deepseek.com/v1",
|
||||
model="deepseek-chat",
|
||||
use_tools=True,
|
||||
)
|
||||
self.assertEqual(kwargs.get("extra_body"), {"strict_tool_mode": True})
|
||||
def test_apply_sets_strict_on_function_tools(self) -> None:
|
||||
tools = [
|
||||
{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object", "properties": {}}}},
|
||||
{"type": "not_function", "x": 1},
|
||||
]
|
||||
out = _apply_deepseek_strict_tools(tools)
|
||||
self.assertIsNotNone(out)
|
||||
assert out is not None
|
||||
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:
|
||||
kwargs: dict = {
|
||||
"model": "m",
|
||||
"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_apply_skips_non_deepseek_not_tested_here(self) -> None:
|
||||
out = _apply_deepseek_strict_tools([])
|
||||
self.assertEqual(out, [])
|
||||
|
||||
def test_merge_skips_without_tools(self) -> None:
|
||||
kwargs: dict = {"model": "m", "messages": [], "stream": False}
|
||||
_merge_deepseek_strict_tool_mode_extra_body(
|
||||
kwargs,
|
||||
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)
|
||||
def test_apply_handles_missing_function_dict(self) -> None:
|
||||
out = _apply_deepseek_strict_tools([{"type": "function"}])
|
||||
assert out is not None
|
||||
self.assertEqual(out[0]["function"], {"strict": True})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue