Check if the model API is accessible and the specified model exists. Checks: 1. Network connectivity to the API endpoint 2. Model exists in the available models list Args: base_url: The API base URL model_name: The model name to check Returns: True
(base_url: str, api_key: str, model_name: str)
| 160 | |
| 161 | |
| 162 | def check_model_api(base_url: str, api_key: str, model_name: str) -> bool: |
| 163 | """ |
| 164 | Check if the model API is accessible and the specified model exists. |
| 165 | |
| 166 | Checks: |
| 167 | 1. Network connectivity to the API endpoint |
| 168 | 2. Model exists in the available models list |
| 169 | |
| 170 | Args: |
| 171 | base_url: The API base URL |
| 172 | model_name: The model name to check |
| 173 | |
| 174 | Returns: |
| 175 | True if all checks pass, False otherwise. |
| 176 | """ |
| 177 | print("🔍 Checking model API...") |
| 178 | print("-" * 50) |
| 179 | |
| 180 | all_passed = True |
| 181 | |
| 182 | # Check 1: Network connectivity |
| 183 | print(f"1. Checking API connectivity ({base_url})...", end=" ") |
| 184 | try: |
| 185 | # Parse the URL to get host and port |
| 186 | parsed = urlparse(base_url) |
| 187 | |
| 188 | # Create OpenAI client |
| 189 | client = OpenAI(base_url=base_url, api_key=api_key, timeout=10.0) |
| 190 | |
| 191 | # Try to list models (this tests connectivity) |
| 192 | models_response = client.models.list() |
| 193 | available_models = [model.id for model in models_response.data] |
| 194 | |
| 195 | print("✅ OK") |
| 196 | |
| 197 | # Check 2: Model exists |
| 198 | print(f"2. Checking model '{model_name}'...", end=" ") |
| 199 | if model_name in available_models: |
| 200 | print("✅ OK") |
| 201 | else: |
| 202 | print("❌ FAILED") |
| 203 | print(f" Error: Model '{model_name}' not found.") |
| 204 | print(f" Available models:") |
| 205 | for m in available_models[:10]: # Show first 10 models |
| 206 | print(f" - {m}") |
| 207 | if len(available_models) > 10: |
| 208 | print(f" ... and {len(available_models) - 10} more") |
| 209 | all_passed = False |
| 210 | |
| 211 | except Exception as e: |
| 212 | print("❌ FAILED") |
| 213 | error_msg = str(e) |
| 214 | |
| 215 | # Provide more specific error messages |
| 216 | if "Connection refused" in error_msg or "Connection error" in error_msg: |
| 217 | print(f" Error: Cannot connect to {base_url}") |
| 218 | print(" Solution:") |
| 219 | print(" 1. Check if the model server is running") |
nothing calls this directly
no outgoing calls
no test coverage detected