test_qwen_runtime_options.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from __future__ import annotations
  2. from types import SimpleNamespace
  3. from typing import Any
  4. import pytest
  5. from agent.llm.qwen import create_qwen_llm_call
  6. @pytest.mark.asyncio
  7. async def test_qwen_factory_applies_timeout_and_non_thinking_mode(
  8. monkeypatch: pytest.MonkeyPatch,
  9. ) -> None:
  10. client_options: dict[str, Any] = {}
  11. request_options: dict[str, Any] = {}
  12. async def create(**kwargs: Any) -> Any:
  13. request_options.update(kwargs)
  14. return SimpleNamespace(
  15. choices=[
  16. SimpleNamespace(
  17. message=SimpleNamespace(content="ok", tool_calls=None),
  18. finish_reason="stop",
  19. )
  20. ],
  21. usage=SimpleNamespace(prompt_tokens=3, completion_tokens=2),
  22. )
  23. def fake_client(**kwargs: Any) -> Any:
  24. client_options.update(kwargs)
  25. return SimpleNamespace(chat=SimpleNamespace(completions=SimpleNamespace(create=create)))
  26. monkeypatch.setattr("agent.llm.qwen.AsyncOpenAI", fake_client)
  27. call = create_qwen_llm_call(
  28. api_key="secret",
  29. enable_thinking=False,
  30. request_timeout_seconds=45,
  31. max_retries=2,
  32. )
  33. result = await call(messages=[{"role": "user", "content": "hello"}])
  34. assert client_options["timeout"] == 45
  35. assert client_options["max_retries"] == 2
  36. assert request_options["extra_body"] == {"enable_thinking": False}
  37. assert result["content"] == "ok"
  38. @pytest.mark.asyncio
  39. async def test_qwen_factory_preserves_call_specific_extra_body(
  40. monkeypatch: pytest.MonkeyPatch,
  41. ) -> None:
  42. request_options: dict[str, Any] = {}
  43. async def create(**kwargs: Any) -> Any:
  44. request_options.update(kwargs)
  45. return SimpleNamespace(
  46. choices=[
  47. SimpleNamespace(
  48. message=SimpleNamespace(content="ok", tool_calls=None),
  49. finish_reason="stop",
  50. )
  51. ],
  52. usage=SimpleNamespace(prompt_tokens=1, completion_tokens=1),
  53. )
  54. monkeypatch.setattr(
  55. "agent.llm.qwen.AsyncOpenAI",
  56. lambda **_kwargs: SimpleNamespace(
  57. chat=SimpleNamespace(completions=SimpleNamespace(create=create))
  58. ),
  59. )
  60. call = create_qwen_llm_call(api_key="secret", enable_thinking=False)
  61. await call(
  62. messages=[{"role": "user", "content": "hello"}],
  63. extra_body={"enable_thinking": True, "custom": "kept"},
  64. )
  65. assert request_options["extra_body"] == {"enable_thinking": True, "custom": "kept"}