feat: rebuild news summarizer on local Ollama
Swap Nous Portal for the newsPipeline local_llm generate path (POST /api/generate, GET /api/tags). No API key. Settings model selector lists Ollama tags; idle if the host is down.
This commit is contained in:
parent
d2bf55183c
commit
58ff43bdee
15 changed files with 336 additions and 339 deletions
|
|
@ -64,31 +64,26 @@ def _put(path: str, json: dict) -> httpx.Response:
|
|||
return _request("PUT", path, json=json)
|
||||
|
||||
|
||||
def test_get_news_models_without_key_returns_fallback(monkeypatch):
|
||||
monkeypatch.delenv("NOUS_API_KEY", raising=False)
|
||||
monkeypatch.setattr("keystore.get_api_key", _missing_key)
|
||||
def test_get_news_models_ollama_down_returns_fallback(monkeypatch):
|
||||
async def boom_http_get(url, *, headers, timeout):
|
||||
raise httpx.ConnectError("ollama down")
|
||||
|
||||
import settings_store
|
||||
monkeypatch.setattr(settings_store, "_http_get", boom_http_get)
|
||||
settings_store._models_cache = None
|
||||
|
||||
resp = _get("/api/news/models")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["source"] == "fallback"
|
||||
ids = [m["id"] for m in body["models"]]
|
||||
assert "Hermes-4.3-36B" in ids
|
||||
from settings_store import FALLBACK_MODELS
|
||||
assert ids == FALLBACK_MODELS
|
||||
assert "qwen3:30b-a3b" in ids
|
||||
|
||||
|
||||
async def _missing_key(name: str):
|
||||
return None
|
||||
|
||||
|
||||
async def _present_key(name: str):
|
||||
return "test-nous-api-key-1234"
|
||||
|
||||
|
||||
def test_get_news_models_live_from_upstream(monkeypatch):
|
||||
monkeypatch.setattr("keystore.get_api_key", _present_key)
|
||||
monkeypatch.setenv("NOUS_BASE_URL", "https://inference-api.nousresearch.com/v1")
|
||||
def test_get_news_models_live_from_ollama_tags(monkeypatch):
|
||||
monkeypatch.setenv("LLM_URL", "http://ollama:11434")
|
||||
monkeypatch.delenv("OSINT_USER_AGENT", raising=False)
|
||||
|
||||
captured: dict = {}
|
||||
|
|
@ -100,7 +95,7 @@ def test_get_news_models_live_from_upstream(monkeypatch):
|
|||
return None
|
||||
|
||||
def json(self):
|
||||
return {"data": [{"id": "live-model-a"}, {"id": "Hermes-4.3-36B"}]}
|
||||
return {"models": [{"name": "qwen3:30b-a3b"}, {"name": "llama3.2:latest"}]}
|
||||
|
||||
async def fake_http_get(url, *, headers, timeout):
|
||||
captured["url"] = url
|
||||
|
|
@ -117,30 +112,10 @@ def test_get_news_models_live_from_upstream(monkeypatch):
|
|||
body = resp.json()
|
||||
assert body["source"] == "live"
|
||||
ids = [m["id"] for m in body["models"]]
|
||||
assert ids == ["live-model-a", "Hermes-4.3-36B"]
|
||||
assert captured["url"] == "https://inference-api.nousresearch.com/v1/models"
|
||||
assert ids == ["qwen3:30b-a3b", "llama3.2:latest"]
|
||||
assert captured["url"] == "http://ollama:11434/api/tags"
|
||||
assert captured["headers"]["User-Agent"] == "osint-dashboard-news-summarizer"
|
||||
assert captured["headers"]["Authorization"] == "Bearer test-nous-api-key-1234"
|
||||
timeout = captured["timeout"]
|
||||
assert timeout == 8 or getattr(timeout, "read", timeout) == 8 or float(timeout) == 8.0
|
||||
|
||||
|
||||
def test_get_news_models_upstream_failure_returns_fallback(monkeypatch):
|
||||
monkeypatch.setattr("keystore.get_api_key", _present_key)
|
||||
|
||||
async def boom_http_get(url, *, headers, timeout):
|
||||
raise httpx.ConnectError("upstream down")
|
||||
|
||||
import settings_store
|
||||
monkeypatch.setattr(settings_store, "_http_get", boom_http_get)
|
||||
settings_store._models_cache = None
|
||||
|
||||
resp = _get("/api/news/models")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["source"] == "fallback"
|
||||
ids = [m["id"] for m in body["models"]]
|
||||
assert "Hermes-4.3-36B" in ids
|
||||
assert "Authorization" not in captured["headers"]
|
||||
|
||||
|
||||
def test_put_settings_empty_returns_422():
|
||||
|
|
@ -156,17 +131,17 @@ def test_put_settings_whitespace_only_returns_422():
|
|||
@requires_db
|
||||
def test_put_settings_round_trip(clean_settings, monkeypatch):
|
||||
monkeypatch.delenv("SUMMARY_MODEL", raising=False)
|
||||
monkeypatch.delenv("NOUS_BASE_URL", raising=False)
|
||||
monkeypatch.delenv("LLM_URL", raising=False)
|
||||
|
||||
put_resp = _put("/api/settings", {"summary_model": "google/gemini-2.5-flash"})
|
||||
put_resp = _put("/api/settings", {"summary_model": "qwen3:30b-a3b"})
|
||||
assert put_resp.status_code == 200
|
||||
put_body = put_resp.json()
|
||||
assert put_body["summary_model"] == "google/gemini-2.5-flash"
|
||||
assert put_body["nous_base_url"] == "https://inference-api.nousresearch.com/v1"
|
||||
assert put_body["summary_model"] == "qwen3:30b-a3b"
|
||||
assert put_body["llm_url"] == "http://127.0.0.1:11434"
|
||||
|
||||
get_resp = _get("/api/settings")
|
||||
assert get_resp.status_code == 200
|
||||
get_body = get_resp.json()
|
||||
assert get_body["summary_model"] == "google/gemini-2.5-flash"
|
||||
assert get_body["nous_base_url"] == "https://inference-api.nousresearch.com/v1"
|
||||
assert set(get_body.keys()) == {"summary_model", "nous_base_url"}
|
||||
assert get_body["summary_model"] == "qwen3:30b-a3b"
|
||||
assert get_body["llm_url"] == "http://127.0.0.1:11434"
|
||||
assert set(get_body.keys()) == {"summary_model", "llm_url"}
|
||||
|
|
|
|||
Loading…
Reference in a new issue