MCPcopy Create free account
hub / github.com/zai-org/Open-AutoGLM / check_model_api

Function check_model_api

main.py:272–352  ·  view source on GitHub ↗

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 api_key: The API k

(base_url: str, model_name: str, api_key: str = "EMPTY")

Source from the content-addressed store, hash-verified

270
271
272def check_model_api(base_url: str, model_name: str, api_key: str = "EMPTY") -> bool:
273 """
274 Check if the model API is accessible and the specified model exists.
275
276 Checks:
277 1. Network connectivity to the API endpoint
278 2. Model exists in the available models list
279
280 Args:
281 base_url: The API base URL
282 model_name: The model name to check
283 api_key: The API key for authentication
284
285 Returns:
286 True if all checks pass, False otherwise.
287 """
288 print("🔍 Checking model API...")
289 print("-" * 50)
290
291 all_passed = True
292
293 # Check 1: Network connectivity using chat API
294 print(f"1. Checking API connectivity ({base_url})...", end=" ")
295 try:
296 # Create OpenAI client
297 client = OpenAI(base_url=base_url, api_key=api_key, timeout=30.0)
298
299 # Use chat completion to test connectivity (more universally supported than /models)
300 response = client.chat.completions.create(
301 model=model_name,
302 messages=[{"role": "user", "content": "Hi"}],
303 max_tokens=5,
304 temperature=0.0,
305 stream=False,
306 )
307
308 # Check if we got a valid response
309 if response.choices and len(response.choices) > 0:
310 print("✅ OK")
311 else:
312 print("❌ FAILED")
313 print(" Error: Received empty response from API")
314 all_passed = False
315
316 except Exception as e:
317 print("❌ FAILED")
318 error_msg = str(e)
319
320 # Provide more specific error messages
321 if "Connection refused" in error_msg or "Connection error" in error_msg:
322 print(f" Error: Cannot connect to {base_url}")
323 print(" Solution:")
324 print(" 1. Check if the model server is running")
325 print(" 2. Verify the base URL is correct")
326 print(f" 3. Try: curl {base_url}/chat/completions")
327 elif "timed out" in error_msg.lower() or "timeout" in error_msg.lower():
328 print(f" Error: Connection to {base_url} timed out")
329 print(" Solution:")

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected