|
| 1 | +from types import SimpleNamespace |
| 2 | +from unittest.mock import AsyncMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from hindsight_api.engine.providers.openai_compatible_llm import OpenAICompatibleLLM |
| 7 | + |
| 8 | + |
| 9 | +def _make_minimax(extra_body=None) -> OpenAICompatibleLLM: |
| 10 | + return OpenAICompatibleLLM( |
| 11 | + provider="minimax", |
| 12 | + api_key="test-key", |
| 13 | + base_url="", |
| 14 | + model="MiniMax-M3", |
| 15 | + extra_body=extra_body, |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def _text_response(content: str = "ok"): |
| 20 | + return SimpleNamespace( |
| 21 | + error=None, |
| 22 | + usage=None, |
| 23 | + choices=[ |
| 24 | + SimpleNamespace( |
| 25 | + finish_reason="stop", |
| 26 | + message=SimpleNamespace(content=content, tool_calls=None, refusal=None), |
| 27 | + ) |
| 28 | + ], |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def _tool_response(): |
| 33 | + tool_call = SimpleNamespace( |
| 34 | + id="call_minimax_123", |
| 35 | + function=SimpleNamespace(name="recall", arguments='{"query": "Project Rin"}'), |
| 36 | + ) |
| 37 | + return SimpleNamespace( |
| 38 | + error=None, |
| 39 | + usage=None, |
| 40 | + choices=[ |
| 41 | + SimpleNamespace( |
| 42 | + finish_reason="tool_calls", |
| 43 | + message=SimpleNamespace(content=None, tool_calls=[tool_call], refusal=None), |
| 44 | + ) |
| 45 | + ], |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_minimax_call_disables_thinking_by_default(): |
| 51 | + llm = _make_minimax() |
| 52 | + llm._client.chat.completions.create = AsyncMock(return_value=_text_response()) |
| 53 | + |
| 54 | + with patch("hindsight_api.engine.providers.openai_compatible_llm.get_metrics_collector"): |
| 55 | + await llm.call(messages=[{"role": "user", "content": "hi"}], max_retries=0) |
| 56 | + |
| 57 | + assert llm._client.chat.completions.create.call_args.kwargs["extra_body"] == {"thinking": {"type": "disabled"}} |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_minimax_call_preserves_configured_thinking_extra_body(): |
| 62 | + llm = _make_minimax(extra_body={"thinking": {"type": "enabled"}, "reasoning_split": True}) |
| 63 | + llm._client.chat.completions.create = AsyncMock(return_value=_text_response()) |
| 64 | + |
| 65 | + with patch("hindsight_api.engine.providers.openai_compatible_llm.get_metrics_collector"): |
| 66 | + await llm.call(messages=[{"role": "user", "content": "hi"}], max_retries=0) |
| 67 | + |
| 68 | + assert llm._client.chat.completions.create.call_args.kwargs["extra_body"] == { |
| 69 | + "thinking": {"type": "enabled"}, |
| 70 | + "reasoning_split": True, |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_minimax_tool_call_disables_thinking_by_default(): |
| 76 | + llm = _make_minimax() |
| 77 | + llm._client.chat.completions.create = AsyncMock(return_value=_tool_response()) |
| 78 | + |
| 79 | + with patch("hindsight_api.engine.providers.openai_compatible_llm.get_metrics_collector"): |
| 80 | + await llm.call_with_tools( |
| 81 | + messages=[{"role": "user", "content": "Search memory."}], |
| 82 | + tools=[ |
| 83 | + { |
| 84 | + "type": "function", |
| 85 | + "function": { |
| 86 | + "name": "recall", |
| 87 | + "description": "Recall memories", |
| 88 | + "parameters": { |
| 89 | + "type": "object", |
| 90 | + "properties": {"query": {"type": "string"}}, |
| 91 | + "required": ["query"], |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | + ], |
| 96 | + max_retries=0, |
| 97 | + ) |
| 98 | + |
| 99 | + assert llm._client.chat.completions.create.call_args.kwargs["extra_body"] == {"thinking": {"type": "disabled"}} |
0 commit comments