|
| 1 | +# Copyright(C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +"""Tests for LiteLLM provider.""" |
| 4 | + |
| 5 | +import sys |
| 6 | +import types |
| 7 | +from unittest.mock import MagicMock |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +def _stub_litellm(): |
| 13 | + """Install a stub litellm module so tests run without the real package.""" |
| 14 | + fake = types.ModuleType("litellm") |
| 15 | + fake.completion = MagicMock(name="litellm.completion") |
| 16 | + fake.embedding = MagicMock(name="litellm.embedding") |
| 17 | + fake.drop_params = False |
| 18 | + sys.modules["litellm"] = fake |
| 19 | + return fake |
| 20 | + |
| 21 | + |
| 22 | +class TestLiteLLMProviderName: |
| 23 | + def test_provider_name(self): |
| 24 | + fake = _stub_litellm() |
| 25 | + from gaia.llm.providers.litellm import LiteLLMProvider |
| 26 | + |
| 27 | + provider = LiteLLMProvider(api_key="test-key", model="gpt-4o") |
| 28 | + assert provider.provider_name == "LiteLLM" |
| 29 | + del sys.modules["litellm"] |
| 30 | + |
| 31 | + |
| 32 | +class TestLiteLLMFactory: |
| 33 | + def test_create_client_litellm(self): |
| 34 | + _stub_litellm() |
| 35 | + from gaia.llm import create_client |
| 36 | + |
| 37 | + client = create_client("litellm", api_key="test-key") |
| 38 | + assert client.provider_name == "LiteLLM" |
| 39 | + del sys.modules["litellm"] |
| 40 | + |
| 41 | + def test_create_client_litellm_case_insensitive(self): |
| 42 | + _stub_litellm() |
| 43 | + from gaia.llm import create_client |
| 44 | + |
| 45 | + client = create_client("LITELLM", api_key="test-key") |
| 46 | + assert client.provider_name == "LiteLLM" |
| 47 | + del sys.modules["litellm"] |
| 48 | + |
| 49 | + |
| 50 | +class TestLiteLLMChat: |
| 51 | + def test_chat_calls_litellm_completion(self): |
| 52 | + fake = _stub_litellm() |
| 53 | + fake.completion.return_value = MagicMock( |
| 54 | + choices=[MagicMock(message=MagicMock(content="Hello!"))] |
| 55 | + ) |
| 56 | + from gaia.llm.providers.litellm import LiteLLMProvider |
| 57 | + |
| 58 | + provider = LiteLLMProvider(api_key="sk-test", model="gpt-4o") |
| 59 | + result = provider.chat([{"role": "user", "content": "Hi"}]) |
| 60 | + |
| 61 | + assert result == "Hello!" |
| 62 | + fake.completion.assert_called_once() |
| 63 | + call_kwargs = fake.completion.call_args |
| 64 | + assert call_kwargs.kwargs["model"] == "gpt-4o" |
| 65 | + assert call_kwargs.kwargs["drop_params"] is True |
| 66 | + assert call_kwargs.kwargs["api_key"] == "sk-test" |
| 67 | + del sys.modules["litellm"] |
| 68 | + |
| 69 | + def test_chat_prepends_system_prompt(self): |
| 70 | + fake = _stub_litellm() |
| 71 | + fake.completion.return_value = MagicMock( |
| 72 | + choices=[MagicMock(message=MagicMock(content="OK"))] |
| 73 | + ) |
| 74 | + from gaia.llm.providers.litellm import LiteLLMProvider |
| 75 | + |
| 76 | + provider = LiteLLMProvider( |
| 77 | + api_key="sk-test", model="gpt-4o", system_prompt="You are helpful." |
| 78 | + ) |
| 79 | + provider.chat([{"role": "user", "content": "Hi"}]) |
| 80 | + |
| 81 | + messages = fake.completion.call_args.kwargs["messages"] |
| 82 | + assert messages[0]["role"] == "system" |
| 83 | + assert messages[0]["content"] == "You are helpful." |
| 84 | + del sys.modules["litellm"] |
| 85 | + |
| 86 | + def test_chat_omits_api_key_when_not_set(self): |
| 87 | + fake = _stub_litellm() |
| 88 | + fake.completion.return_value = MagicMock( |
| 89 | + choices=[MagicMock(message=MagicMock(content="OK"))] |
| 90 | + ) |
| 91 | + from gaia.llm.providers.litellm import LiteLLMProvider |
| 92 | + |
| 93 | + provider = LiteLLMProvider(model="gpt-4o") |
| 94 | + provider.chat([{"role": "user", "content": "Hi"}]) |
| 95 | + |
| 96 | + assert "api_key" not in fake.completion.call_args.kwargs |
| 97 | + del sys.modules["litellm"] |
| 98 | + |
| 99 | + def test_chat_uses_override_model(self): |
| 100 | + fake = _stub_litellm() |
| 101 | + fake.completion.return_value = MagicMock( |
| 102 | + choices=[MagicMock(message=MagicMock(content="OK"))] |
| 103 | + ) |
| 104 | + from gaia.llm.providers.litellm import LiteLLMProvider |
| 105 | + |
| 106 | + provider = LiteLLMProvider(model="gpt-4o") |
| 107 | + provider.chat( |
| 108 | + [{"role": "user", "content": "Hi"}], |
| 109 | + model="anthropic/claude-sonnet-4-6", |
| 110 | + ) |
| 111 | + |
| 112 | + assert ( |
| 113 | + fake.completion.call_args.kwargs["model"] == "anthropic/claude-sonnet-4-6" |
| 114 | + ) |
| 115 | + del sys.modules["litellm"] |
| 116 | + |
| 117 | + |
| 118 | +class TestLiteLLMGenerate: |
| 119 | + def test_generate_delegates_to_chat(self): |
| 120 | + fake = _stub_litellm() |
| 121 | + fake.completion.return_value = MagicMock( |
| 122 | + choices=[MagicMock(message=MagicMock(content="4"))] |
| 123 | + ) |
| 124 | + from gaia.llm.providers.litellm import LiteLLMProvider |
| 125 | + |
| 126 | + provider = LiteLLMProvider(model="gpt-4o") |
| 127 | + result = provider.generate("What is 2+2?") |
| 128 | + |
| 129 | + assert result == "4" |
| 130 | + messages = fake.completion.call_args.kwargs["messages"] |
| 131 | + assert messages[0] == {"role": "user", "content": "What is 2+2?"} |
| 132 | + del sys.modules["litellm"] |
| 133 | + |
| 134 | + |
| 135 | +class TestLiteLLMNotSupported: |
| 136 | + def test_vision_raises_not_supported(self): |
| 137 | + _stub_litellm() |
| 138 | + from gaia.llm import NotSupportedError |
| 139 | + from gaia.llm.providers.litellm import LiteLLMProvider |
| 140 | + |
| 141 | + provider = LiteLLMProvider(model="gpt-4o") |
| 142 | + with pytest.raises(NotSupportedError) as exc: |
| 143 | + provider.vision([b"image"], "describe this") |
| 144 | + assert "LiteLLM" in str(exc.value) |
| 145 | + del sys.modules["litellm"] |
| 146 | + |
| 147 | + def test_load_model_raises_not_supported(self): |
| 148 | + _stub_litellm() |
| 149 | + from gaia.llm import NotSupportedError |
| 150 | + from gaia.llm.providers.litellm import LiteLLMProvider |
| 151 | + |
| 152 | + provider = LiteLLMProvider(model="gpt-4o") |
| 153 | + with pytest.raises(NotSupportedError): |
| 154 | + provider.load_model("some-model") |
| 155 | + del sys.modules["litellm"] |
0 commit comments