Get an LLM client instance for the specified or default provider. Args: provider: Optional provider name ('anthropic', 'openai', 'ollama', 'docker'). If not specified, uses the configured default provider. model: Optional model name to us
(
provider: Optional[str] = None,
model: Optional[str] = None
)
| 147 | |
| 148 | |
| 149 | def get_llm_client( |
| 150 | provider: Optional[str] = None, |
| 151 | model: Optional[str] = None |
| 152 | ) -> Optional[LLMClient]: |
| 153 | """ |
| 154 | Get an LLM client instance for the specified or default provider. |
| 155 | |
| 156 | Args: |
| 157 | provider: Optional provider name ('anthropic', 'openai', 'ollama', |
| 158 | 'docker'). If not specified, uses the configured default |
| 159 | provider. |
| 160 | model: Optional model name to use. If not specified, uses the |
| 161 | configured default model for the provider. |
| 162 | |
| 163 | Returns: |
| 164 | An LLMClient instance, or None if no provider is configured. |
| 165 | |
| 166 | Raises: |
| 167 | ValueError: If an invalid provider is specified. |
| 168 | LLMClientError: If the client cannot be initialized. |
| 169 | """ |
| 170 | from pgadmin.llm.utils import ( |
| 171 | get_default_provider, |
| 172 | get_anthropic_api_url, get_anthropic_api_key, get_anthropic_model, |
| 173 | get_openai_api_url, get_openai_api_key, get_openai_model, |
| 174 | get_ollama_api_url, get_ollama_model, |
| 175 | get_docker_api_url, get_docker_model, |
| 176 | is_pref_api_url_rejected, |
| 177 | is_pref_api_key_path_rejected, |
| 178 | ) |
| 179 | |
| 180 | # Determine which provider to use |
| 181 | if provider is None: |
| 182 | provider = get_default_provider() |
| 183 | if provider is None: |
| 184 | return None |
| 185 | |
| 186 | provider = provider.lower() |
| 187 | |
| 188 | def _rejected_url_error(prov): |
| 189 | return LLMClientError(LLMError( |
| 190 | message=( |
| 191 | f"The configured {prov} API URL is not in the " |
| 192 | "allowed list (ALLOWED_LLM_API_URLS). Add it to " |
| 193 | "config_local.py, or clear the API URL preference " |
| 194 | "to use the system default." |
| 195 | ), |
| 196 | provider=prov, |
| 197 | )) |
| 198 | |
| 199 | def _rejected_key_file_error(prov): |
| 200 | return LLMClientError(LLMError( |
| 201 | message=( |
| 202 | f"The configured {prov} API key file is not within " |
| 203 | "your private user storage. Move the key file to " |
| 204 | "your private storage directory, or clear the API " |
| 205 | "Key File preference to use the system default." |
| 206 | ), |