Spaces:
Runtime error
Runtime error
Commit ·
783e323
1
Parent(s): 3396bcc
Cleanup pt. 2
Browse filesMisc. files and so on. Will work on the core functionality soon
- backend/ollama.py +15 -9
backend/ollama.py
CHANGED
|
@@ -35,18 +35,24 @@ def build_prompt(title: Optional[str], text: str) -> str:
|
|
| 35 |
)
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
def resolve_model(model: Optional[str]) -> str:
|
| 39 |
requested = model or ""
|
| 40 |
|
| 41 |
-
# Prefer what Ollama actually has installed.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
if installed:
|
| 52 |
if not requested:
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
|
| 38 |
+
# Cache for installed models to avoid repeated network calls
|
| 39 |
+
_installed_models_cache: Optional[list] = None
|
| 40 |
+
|
| 41 |
def resolve_model(model: Optional[str]) -> str:
|
| 42 |
requested = model or ""
|
| 43 |
|
| 44 |
+
# Prefer what Ollama actually has installed, using cache to avoid repeated network calls.
|
| 45 |
+
global _installed_models_cache
|
| 46 |
+
if _installed_models_cache is None:
|
| 47 |
+
try:
|
| 48 |
+
with httpx.Client(timeout=5.0) as client:
|
| 49 |
+
r = client.get(f"{OLLAMA_BASE_URL}/api/tags")
|
| 50 |
+
r.raise_for_status()
|
| 51 |
+
payload = r.json() if r.content else {}
|
| 52 |
+
_installed_models_cache = [m.get("name") for m in payload.get("models", []) if m.get("name")]
|
| 53 |
+
except Exception:
|
| 54 |
+
_installed_models_cache = []
|
| 55 |
+
installed = _installed_models_cache
|
| 56 |
|
| 57 |
if installed:
|
| 58 |
if not requested:
|