|
| 1 | +from __future__ import annotations as _annotations |
| 2 | + |
| 3 | +from typing import overload |
| 4 | + |
| 5 | +from httpx import AsyncClient as AsyncHTTPClient |
| 6 | +from openai import AsyncOpenAI |
| 7 | + |
| 8 | +from pydantic_ai.profiles import ModelProfile |
| 9 | +from pydantic_ai.profiles.openai import OpenAIJsonSchemaTransformer, OpenAIModelProfile, openai_model_profile |
| 10 | +from pydantic_ai.providers import Provider |
| 11 | +from pydantic_ai_slim.pydantic_ai.models import cached_async_http_client |
| 12 | + |
| 13 | +try: |
| 14 | + from openai import AsyncOpenAI |
| 15 | +except ImportError as _import_error: # pragma: no cover |
| 16 | + raise ImportError( |
| 17 | + 'Please install the `openai` package to use the LiteLLM provider, ' |
| 18 | + 'you can use the `openai` optional group — `pip install "pydantic-ai-slim[openai]"`' |
| 19 | + ) from _import_error |
| 20 | + |
| 21 | + |
| 22 | +class LiteLLMProvider(Provider[AsyncOpenAI]): |
| 23 | + """Provider for LiteLLM API.""" |
| 24 | + |
| 25 | + @property |
| 26 | + def name(self) -> str: |
| 27 | + return 'litellm' |
| 28 | + |
| 29 | + @property |
| 30 | + def base_url(self) -> str: |
| 31 | + return self._base_url |
| 32 | + |
| 33 | + @property |
| 34 | + def client(self) -> AsyncOpenAI: |
| 35 | + return self._client |
| 36 | + |
| 37 | + def model_profile(self, model_name: str) -> ModelProfile | None: |
| 38 | + # For LiteLLM, we use a basic OpenAI profile since it's OpenAI-compatible |
| 39 | + # Users can override this with their own profile if needed |
| 40 | + profile = openai_model_profile(model_name) |
| 41 | + |
| 42 | + # As LiteLLMProvider is used with OpenAIModel, which used to use OpenAIJsonSchemaTransformer, |
| 43 | + # we maintain that behavior |
| 44 | + return OpenAIModelProfile(json_schema_transformer=OpenAIJsonSchemaTransformer).update(profile) |
| 45 | + |
| 46 | + @overload |
| 47 | + def __init__( |
| 48 | + self, |
| 49 | + *, |
| 50 | + api_key: str | None = None, |
| 51 | + api_base: str | None = None, |
| 52 | + custom_llm_provider: str | None = None, |
| 53 | + ) -> None: ... |
| 54 | + |
| 55 | + @overload |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + *, |
| 59 | + api_key: str | None = None, |
| 60 | + api_base: str | None = None, |
| 61 | + custom_llm_provider: str | None = None, |
| 62 | + http_client: AsyncHTTPClient, |
| 63 | + ) -> None: ... |
| 64 | + |
| 65 | + @overload |
| 66 | + def __init__(self, *, openai_client: AsyncOpenAI) -> None: ... |
| 67 | + |
| 68 | + def __init__( |
| 69 | + self, |
| 70 | + *, |
| 71 | + api_key: str | None = None, |
| 72 | + api_base: str | None = None, |
| 73 | + custom_llm_provider: str | None = None, |
| 74 | + openai_client: AsyncOpenAI | None = None, |
| 75 | + http_client: AsyncHTTPClient | None = None, |
| 76 | + ) -> None: |
| 77 | + """Initialize a LiteLLM provider. |
| 78 | +
|
| 79 | + Args: |
| 80 | + api_key: API key for the model provider. If None, LiteLLM will try to get it from environment variables. |
| 81 | + api_base: Base URL for the model provider. Use this for custom endpoints or self-hosted models. |
| 82 | + custom_llm_provider: Custom LLM provider name for LiteLLM. Use this if LiteLLM can't auto-detect the provider. |
| 83 | + openai_client: Pre-configured OpenAI client. If provided, other parameters are ignored. |
| 84 | + http_client: Custom HTTP client to use. |
| 85 | + """ |
| 86 | + if openai_client is not None: |
| 87 | + self._client = openai_client |
| 88 | + self._base_url = str(openai_client.base_url) |
| 89 | + return |
| 90 | + |
| 91 | + # Set up LiteLLM configuration |
| 92 | + if api_key: |
| 93 | + # Store API key in LiteLLM's global config if needed |
| 94 | + # LiteLLM will handle provider-specific API key names |
| 95 | + pass |
| 96 | + |
| 97 | + if custom_llm_provider: |
| 98 | + # LiteLLM can auto-detect most providers, but this allows override |
| 99 | + pass |
| 100 | + |
| 101 | + # Use api_base if provided, otherwise use a generic base URL |
| 102 | + # LiteLLM doesn't actually use this URL - it routes internally |
| 103 | + self._base_url = api_base or 'https://api.litellm.ai/v1' |
| 104 | + |
| 105 | + # Create OpenAI client that will be used with LiteLLM's completion function |
| 106 | + # The actual API calls will be intercepted and routed through LiteLLM |
| 107 | + if http_client is not None: |
| 108 | + self._client = AsyncOpenAI( |
| 109 | + base_url=self._base_url, api_key=api_key or 'litellm-placeholder', http_client=http_client |
| 110 | + ) |
| 111 | + else: |
| 112 | + http_client = cached_async_http_client(provider='litellm') |
| 113 | + self._client = AsyncOpenAI( |
| 114 | + base_url=self._base_url, api_key=api_key or 'litellm-placeholder', http_client=http_client |
| 115 | + ) |
| 116 | + |
| 117 | + # Store configuration for LiteLLM |
| 118 | + self._api_key = api_key |
| 119 | + self._api_base = api_base |
| 120 | + self._custom_llm_provider = custom_llm_provider |
0 commit comments