Eng-Musa commited on
Commit
a171f5a
·
1 Parent(s): fa8c299

ping llm porviders fix

Browse files
Files changed (2) hide show
  1. __pycache__/main.cpython-312.pyc +0 -0
  2. main.py +20 -45
__pycache__/main.cpython-312.pyc CHANGED
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
 
main.py CHANGED
@@ -432,59 +432,34 @@ class PingLLMRequest(BaseModel):
432
 
433
  @app.post("/v2/ping-llm", response_model=APIResponse)
434
  def ping_llm(payload: PingLLMRequest = PingLLMRequest()):
435
- """
436
- Diagnostic endpoint. Picks the first provider in LLM_PROVIDERS; if it fails
437
- logs once and tries the next until all exhausted.
438
- """
439
  t_start = datetime.now(timezone.utc)
440
- last_error: Optional[Exception] = None
441
 
442
- for idx, provider in enumerate(LLM_PROVIDERS):
443
- p = provider.lower().strip()
444
  try:
445
  if p == "google":
446
  extractor = GeminiCvExtractor()
447
  res = extractor.ping(prompt=payload.prompt)
448
- model_name = res.get("model", GEMINI_MODEL)
449
- reply = res.get("reply", "")
450
- usage = res.get("usageMetadata", {})
451
- elif p == "fallback":
452
- model_name = "regex-fallback"
453
- reply = "Fallback parser (regex chunker) is operational"
454
- usage = {}
455
- else:
456
- logger.warning("Unknown provider '%s' in LLM_PROVIDERS. Skipping...", p)
457
- continue
458
-
459
- t_end = datetime.now(timezone.utc)
460
- duration_ms = int((t_end - t_start).total_seconds() * 1000)
461
-
462
- return APIResponse(
463
- message=f"{p} ping successful",
464
- statusCode=200,
465
- payload={
466
- "provider": p,
467
- "model": model_name,
468
- "prompt": payload.prompt,
469
- "reply": reply,
470
- "duration_ms": duration_ms,
471
- "duration_seconds": round(duration_ms / 1000, 2),
472
- "usageMetadata": usage,
473
- },
474
- )
475
  except Exception as exc:
476
- next_provider = LLM_PROVIDERS[idx + 1] if idx + 1 < len(LLM_PROVIDERS) else None
477
- logger.warning(
478
- "Provider '%s' ping failed: %s. %s",
479
- p, exc,
480
- f"Trying next provider '{next_provider}'..." if next_provider else "No more providers.",
481
- )
482
- last_error = exc
483
 
484
- return APIResponse(
485
- message=f"All providers failed. Last error: {last_error}",
486
- statusCode=500,
487
- )
488
 
489
 
490
  # ---------------------------------------------------------------------------
 
432
 
433
  @app.post("/v2/ping-llm", response_model=APIResponse)
434
  def ping_llm(payload: PingLLMRequest = PingLLMRequest()):
435
+ """Pings only real LLM providers from LLM_PROVIDERS (skips fallback). Returns latency and reply."""
 
 
 
436
  t_start = datetime.now(timezone.utc)
 
437
 
438
+ for p in (p.lower().strip() for p in LLM_PROVIDERS if p.lower().strip() != "fallback"):
 
439
  try:
440
  if p == "google":
441
  extractor = GeminiCvExtractor()
442
  res = extractor.ping(prompt=payload.prompt)
443
+ t_end = datetime.now(timezone.utc)
444
+ duration_ms = int((t_end - t_start).total_seconds() * 1000)
445
+ return APIResponse(
446
+ message="google ping successful",
447
+ statusCode=200,
448
+ payload={
449
+ "provider": "google",
450
+ "model": res.get("model", GEMINI_MODEL),
451
+ "prompt": payload.prompt,
452
+ "reply": res.get("reply", ""),
453
+ "duration_ms": duration_ms,
454
+ "duration_seconds": round(duration_ms / 1000, 2),
455
+ "usageMetadata": res.get("usageMetadata", {}),
456
+ },
457
+ )
 
 
 
 
 
 
 
 
 
 
 
 
458
  except Exception as exc:
459
+ logger.warning("Provider '%s' ping failed: %s", p, exc)
460
+ return APIResponse(message=f"{p} ping error: {exc}", statusCode=500)
 
 
 
 
 
461
 
462
+ return APIResponse(message="No LLM providers configured.", statusCode=500)
 
 
 
463
 
464
 
465
  # ---------------------------------------------------------------------------