104 lines
3.7 KiB
Python
104 lines
3.7 KiB
Python
# retoor <retoor@molodetz.nl>
|
|||
|
|
|
||
|
|
from devplacepy.services.openai_gateway.thinking import (
|
||
|
|
apply_thinking,
|
||
|
|
client_thinking_enabled,
|
||
|
|
thinking_dialect,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def test_dialect_from_upstream_url():
|
||
|
|
assert thinking_dialect("https://api.deepseek.com/chat/completions") == "deepseek"
|
||
|
|
assert thinking_dialect("https://openrouter.ai/api/v1/chat/completions") == "openrouter"
|
||
|
|
assert thinking_dialect("http://127.0.0.1:11434/api/chat") == "ollama"
|
||
|
|
assert thinking_dialect("http://ollama.local/v1/chat/completions") == "ollama"
|
||
|
|
assert thinking_dialect("https://unknown.example/v1/chat/completions") == "deepseek"
|
||
|
|
|
||
|
|
|
||
|
|
def test_client_intent_unspecified():
|
||
|
|
assert client_thinking_enabled({}) is None
|
||
|
|
assert client_thinking_enabled({"messages": []}) is None
|
||
|
|
assert client_thinking_enabled(None) is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_client_intent_disable():
|
||
|
|
assert client_thinking_enabled({"think": False}) is False
|
||
|
|
assert client_thinking_enabled({"think": "false"}) is False
|
||
|
|
assert client_thinking_enabled({"thinking": {"type": "disabled"}}) is False
|
||
|
|
assert client_thinking_enabled({"reasoning": {"effort": "none"}}) is False
|
||
|
|
assert client_thinking_enabled({"reasoning": {"enabled": False}}) is False
|
||
|
|
assert client_thinking_enabled({"enable_thinking": False}) is False
|
||
|
|
assert client_thinking_enabled(
|
||
|
|
{"chat_template_kwargs": {"enable_thinking": False}}
|
||
|
|
) is False
|
||
|
|
|
||
|
|
|
||
|
|
def test_client_intent_enable():
|
||
|
|
assert client_thinking_enabled({"think": True}) is True
|
||
|
|
assert client_thinking_enabled({"think": "high"}) is True
|
||
|
|
assert client_thinking_enabled({"thinking": {"type": "enabled"}}) is True
|
||
|
|
assert client_thinking_enabled({"reasoning": {"effort": "low"}}) is True
|
||
|
|
assert client_thinking_enabled({"reasoning_effort": "max"}) is True
|
||
|
|
|
||
|
|
|
||
|
|
def test_default_disables_deepseek_thinking():
|
||
|
|
payload = apply_thinking(
|
||
|
|
{"messages": [{"role": "user", "content": "hi"}]},
|
||
|
|
"https://api.deepseek.com/chat/completions",
|
||
|
|
)
|
||
|
|
assert payload["thinking"] == {"type": "disabled"}
|
||
|
|
assert "think" not in payload
|
||
|
|
assert "reasoning" not in payload
|
||
|
|
assert "reasoning_effort" not in payload
|
||
|
|
|
||
|
|
|
||
|
|
def test_default_disables_openrouter_and_ollama():
|
||
|
|
openrouter = apply_thinking({}, "https://openrouter.ai/api/v1/chat/completions")
|
||
|
|
assert openrouter["reasoning"] == {"effort": "none"}
|
||
|
|
assert "thinking" not in openrouter
|
||
|
|
ollama = apply_thinking({}, "http://127.0.0.1:11434/api/chat")
|
||
|
|
assert ollama["think"] is False
|
||
|
|
assert "thinking" not in ollama
|
||
|
|
|
||
|
|
|
||
|
|
def test_client_can_enable_thinking():
|
||
|
|
payload = apply_thinking(
|
||
|
|
{"think": True, "messages": []},
|
||
|
|
"https://api.deepseek.com/chat/completions",
|
||
|
|
)
|
||
|
|
assert payload["thinking"] == {"type": "enabled"}
|
||
|
|
assert "think" not in payload
|
||
|
|
|
||
|
|
|
||
|
|
def test_client_disable_beats_admin_default_on():
|
||
|
|
payload = apply_thinking(
|
||
|
|
{"thinking": {"type": "disabled"}},
|
||
|
|
"https://api.deepseek.com/chat/completions",
|
||
|
|
default_enabled=True,
|
||
|
|
)
|
||
|
|
assert payload["thinking"] == {"type": "disabled"}
|
||
|
|
|
||
|
|
|
||
|
|
def test_admin_default_on_when_client_silent():
|
||
|
|
payload = apply_thinking(
|
||
|
|
{"messages": []},
|
||
|
|
"https://api.deepseek.com/chat/completions",
|
||
|
|
default_enabled=True,
|
||
|
|
)
|
||
|
|
assert payload["thinking"] == {"type": "enabled"}
|
||
|
|
|
||
|
|
|
||
|
|
def test_strips_conflicting_client_fields():
|
||
|
|
payload = apply_thinking(
|
||
|
|
{
|
||
|
|
"think": False,
|
||
|
|
"reasoning_effort": "high",
|
||
|
|
"chat_template_kwargs": {"enable_thinking": True, "other": 1},
|
||
|
|
},
|
||
|
|
"https://api.deepseek.com/chat/completions",
|
||
|
|
)
|
||
|
|
assert payload["thinking"] == {"type": "disabled"}
|
||
|
|
assert "think" not in payload
|
||
|
|
assert "reasoning_effort" not in payload
|
||
|
|
assert payload["chat_template_kwargs"] == {"other": 1}
|