|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | +from pytest_mock import MockerFixture |
| 4 | + |
| 5 | +from ..conftest import try_import |
| 6 | + |
| 7 | +with try_import() as imports_successful: |
| 8 | + from openai import AsyncOpenAI |
| 9 | + |
| 10 | + from pydantic_ai.profiles.openai import OpenAIJsonSchemaTransformer, OpenAIModelProfile |
| 11 | + from pydantic_ai.providers.litellm import LiteLLMProvider |
| 12 | + |
| 13 | +pytestmark = [ |
| 14 | + pytest.mark.skipif(not imports_successful(), reason='OpenAI client not installed'), |
| 15 | + pytest.mark.anyio, |
| 16 | +] |
| 17 | + |
| 18 | + |
| 19 | +def test_init_with_api_config(): |
| 20 | + provider = LiteLLMProvider(api_key='test-key', api_base='https://custom.litellm.com/v1') |
| 21 | + assert provider.base_url == 'https://custom.litellm.com/v1/' |
| 22 | + assert provider.client.api_key == 'test-key' |
| 23 | + |
| 24 | + |
| 25 | +def test_init_without_api_key(): |
| 26 | + provider = LiteLLMProvider() |
| 27 | + assert provider.name == 'litellm' |
| 28 | + assert provider.client.api_key == 'litellm-placeholder' |
| 29 | + |
| 30 | + |
| 31 | +def test_init_with_openai_client(): |
| 32 | + openai_client = AsyncOpenAI(api_key='custom-key', base_url='https://custom.openai.com/v1') |
| 33 | + provider = LiteLLMProvider(openai_client=openai_client) |
| 34 | + assert provider.client == openai_client |
| 35 | + assert provider.base_url == 'https://custom.openai.com/v1/' |
| 36 | + |
| 37 | + |
| 38 | +def test_model_profile_returns_openai_compatible_profile(mocker: MockerFixture): |
| 39 | + provider = LiteLLMProvider(api_key='test-key') |
| 40 | + |
| 41 | + # Create a proper mock profile object that can be updated |
| 42 | + from dataclasses import dataclass |
| 43 | + |
| 44 | + @dataclass |
| 45 | + class MockProfile: |
| 46 | + max_tokens: int = 4096 |
| 47 | + supports_streaming: bool = True |
| 48 | + |
| 49 | + mock_profile = MockProfile() |
| 50 | + mock_openai_profile = mocker.patch('pydantic_ai.providers.litellm.openai_model_profile', return_value=mock_profile) |
| 51 | + |
| 52 | + profile = provider.model_profile('gpt-3.5-turbo') |
| 53 | + |
| 54 | + # Verify openai_model_profile was called with the model name |
| 55 | + mock_openai_profile.assert_called_once_with('gpt-3.5-turbo') |
| 56 | + |
| 57 | + # Verify the returned profile is an OpenAIModelProfile with OpenAIJsonSchemaTransformer |
| 58 | + assert isinstance(profile, OpenAIModelProfile) |
| 59 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 60 | + |
| 61 | + |
| 62 | +def test_model_profile_with_different_models(mocker: MockerFixture): |
| 63 | + provider = LiteLLMProvider(api_key='test-key') |
| 64 | + |
| 65 | + # Create mocks for all profile functions |
| 66 | + from dataclasses import dataclass |
| 67 | + |
| 68 | + @dataclass |
| 69 | + class MockProfile: |
| 70 | + max_tokens: int = 4096 |
| 71 | + supports_streaming: bool = True |
| 72 | + |
| 73 | + # Mock all profile functions |
| 74 | + mock_profiles = { |
| 75 | + 'openai': mocker.patch('pydantic_ai.providers.litellm.openai_model_profile', return_value=MockProfile()), |
| 76 | + 'anthropic': mocker.patch('pydantic_ai.providers.litellm.anthropic_model_profile', return_value=MockProfile()), |
| 77 | + 'google': mocker.patch('pydantic_ai.providers.litellm.google_model_profile', return_value=MockProfile()), |
| 78 | + 'meta': mocker.patch('pydantic_ai.providers.litellm.meta_model_profile', return_value=MockProfile()), |
| 79 | + 'mistral': mocker.patch('pydantic_ai.providers.litellm.mistral_model_profile', return_value=MockProfile()), |
| 80 | + 'cohere': mocker.patch('pydantic_ai.providers.litellm.cohere_model_profile', return_value=MockProfile()), |
| 81 | + 'amazon': mocker.patch('pydantic_ai.providers.litellm.amazon_model_profile', return_value=MockProfile()), |
| 82 | + 'deepseek': mocker.patch('pydantic_ai.providers.litellm.deepseek_model_profile', return_value=MockProfile()), |
| 83 | + 'groq': mocker.patch('pydantic_ai.providers.litellm.groq_model_profile', return_value=MockProfile()), |
| 84 | + 'grok': mocker.patch('pydantic_ai.providers.litellm.grok_model_profile', return_value=MockProfile()), |
| 85 | + 'moonshotai': mocker.patch( |
| 86 | + 'pydantic_ai.providers.litellm.moonshotai_model_profile', return_value=MockProfile() |
| 87 | + ), |
| 88 | + 'qwen': mocker.patch('pydantic_ai.providers.litellm.qwen_model_profile', return_value=MockProfile()), |
| 89 | + } |
| 90 | + |
| 91 | + # Test models without provider prefix (should use openai profile) |
| 92 | + models_without_prefix = ['gpt-4', 'claude-3-sonnet', 'gemini-pro', 'llama2-70b'] |
| 93 | + |
| 94 | + for model in models_without_prefix: |
| 95 | + profile = provider.model_profile(model) |
| 96 | + assert isinstance(profile, OpenAIModelProfile) |
| 97 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 98 | + |
| 99 | + # Verify openai_model_profile was called for each model without prefix |
| 100 | + assert mock_profiles['openai'].call_count == len(models_without_prefix) |
| 101 | + |
| 102 | + # Reset all call counts |
| 103 | + for mock in mock_profiles.values(): |
| 104 | + mock.reset_mock() |
| 105 | + |
| 106 | + # Test all provider prefixes |
| 107 | + test_cases = [ |
| 108 | + ('anthropic/claude-3-haiku', 'anthropic', 'claude-3-haiku'), |
| 109 | + ('openai/gpt-4-turbo', 'openai', 'gpt-4-turbo'), |
| 110 | + ('google/gemini-1.5-pro', 'google', 'gemini-1.5-pro'), |
| 111 | + ('mistralai/mistral-large', 'mistral', 'mistral-large'), |
| 112 | + ('mistral/mistral-7b', 'mistral', 'mistral-7b'), |
| 113 | + ('cohere/command-r', 'cohere', 'command-r'), |
| 114 | + ('amazon/titan-text', 'amazon', 'titan-text'), |
| 115 | + ('bedrock/claude-v2', 'amazon', 'claude-v2'), |
| 116 | + ('meta-llama/llama-3-8b', 'meta', 'llama-3-8b'), |
| 117 | + ('meta/llama-2-70b', 'meta', 'llama-2-70b'), |
| 118 | + ('groq/llama3-70b', 'groq', 'llama3-70b'), |
| 119 | + ('deepseek/deepseek-coder', 'deepseek', 'deepseek-coder'), |
| 120 | + ('moonshotai/moonshot-v1', 'moonshotai', 'moonshot-v1'), |
| 121 | + ('x-ai/grok-beta', 'grok', 'grok-beta'), |
| 122 | + ('qwen/qwen-72b', 'qwen', 'qwen-72b'), |
| 123 | + ] |
| 124 | + |
| 125 | + for model_name, expected_profile, expected_suffix in test_cases: |
| 126 | + profile = provider.model_profile(model_name) |
| 127 | + assert isinstance(profile, OpenAIModelProfile) |
| 128 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 129 | + # Verify the correct profile function was called with the correct suffix |
| 130 | + mock_profiles[expected_profile].assert_called_with(expected_suffix) |
| 131 | + mock_profiles[expected_profile].reset_mock() |
| 132 | + |
| 133 | + # Test unknown provider prefix (should fall back to openai) |
| 134 | + provider.model_profile('unknown-provider/some-model') |
| 135 | + mock_profiles['openai'].assert_called_once_with('unknown-provider/some-model') |
| 136 | + |
| 137 | + |
| 138 | +async def test_cached_http_client_usage(mocker: MockerFixture): |
| 139 | + # Create a real AsyncClient for the mock |
| 140 | + async with httpx.AsyncClient() as mock_cached_client: |
| 141 | + mock_cached_http_client_func = mocker.patch( |
| 142 | + 'pydantic_ai.providers.litellm.cached_async_http_client', return_value=mock_cached_client |
| 143 | + ) |
| 144 | + |
| 145 | + provider = LiteLLMProvider(api_key='test-key') |
| 146 | + |
| 147 | + # Verify cached_async_http_client was called with 'litellm' provider |
| 148 | + mock_cached_http_client_func.assert_called_once_with(provider='litellm') |
| 149 | + |
| 150 | + # Verify the client was created |
| 151 | + assert isinstance(provider.client, AsyncOpenAI) |
| 152 | + |
| 153 | + |
| 154 | +async def test_init_with_http_client_overrides_cached(): |
| 155 | + async with httpx.AsyncClient() as custom_client: |
| 156 | + provider = LiteLLMProvider(api_key='test-key', http_client=custom_client) |
| 157 | + |
| 158 | + # Verify the provider was created successfully with custom client |
| 159 | + assert isinstance(provider.client, AsyncOpenAI) |
| 160 | + assert provider.client.api_key == 'test-key' |
0 commit comments