Spaces:
Sleeping
Sleeping
Restore files the worker imports (parity with original Space)
Browse files- Agent/llm_health_check.py +36 -0
Agent/llm_health_check.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, time, json, sys
|
| 2 |
+
from llama_index.llms.ollama import Ollama
|
| 3 |
+
|
| 4 |
+
BASE_URL = os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434")
|
| 5 |
+
MODEL = os.environ.get("FINETUNED_MODEL_NAME") or os.environ.get("OLLAMA_MODEL_NAME", "gemma3:1b")
|
| 6 |
+
|
| 7 |
+
print(f"Checking Ollama LLM connectivity: base_url={BASE_URL} model={MODEL}")
|
| 8 |
+
|
| 9 |
+
# Lightweight client check without llama_index first (raw HTTP) to see if daemon responds
|
| 10 |
+
try:
|
| 11 |
+
import requests
|
| 12 |
+
r = requests.get(f"{BASE_URL.rstrip('/')}/api/tags", timeout=5)
|
| 13 |
+
print(f"/api/tags status: {r.status_code}")
|
| 14 |
+
if r.ok:
|
| 15 |
+
tags = r.json().get('models', [])
|
| 16 |
+
names = [m.get('name') for m in tags]
|
| 17 |
+
print(f"Available models: {names}")
|
| 18 |
+
if MODEL not in names:
|
| 19 |
+
print(f"Model '{MODEL}' not yet pulled. Run: ollama pull {MODEL}")
|
| 20 |
+
else:
|
| 21 |
+
print("Non-200 response from Ollama daemon")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Raw HTTP check failed: {e}")
|
| 24 |
+
|
| 25 |
+
# LlamaIndex call
|
| 26 |
+
try:
|
| 27 |
+
llm = Ollama(model=MODEL, base_url=BASE_URL, temperature=0.1, request_timeout=30)
|
| 28 |
+
t0 = time.time()
|
| 29 |
+
resp = llm.complete(prompt="SELECT 1;")
|
| 30 |
+
dt = time.time() - t0
|
| 31 |
+
print(f"LLM call success in {dt:.2f}s. Raw text: {resp.text[:120]!r}")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"LLM complete() failed: {type(e).__name__}: {e}")
|
| 34 |
+
sys.exit(1)
|
| 35 |
+
|
| 36 |
+
print("Health check finished.")
|