Add selectable LLM profiles and treat LAN Ollama as local.
Saved presets keep URL and model per stage so detect can switch to Ollama without re-entering settings or sending plaintext to OpenRouter. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
af6fe55577
commit
bef422a429
20 files changed
+903
-114
No files matched your search
@@ -31,6 +31,7 @@ UNIT_MODULES = frozenset(
|
||||
"test_local_backup",
|
||||
"test_provenance",
|
||||
"test_model_catalog",
|
||||
"test_local_url",
|
||||
"test_privacy_detect_eval",
|
||||
"test_journal_shape",
|
||||
"test_journal_body",
|
||||
|
||||
@@ -107,6 +107,37 @@ def main() -> None:
|
||||
expect(saved.status_code == 200, f"save generate settings {saved.text}")
|
||||
saved_roles = {item["role"]: item for item in saved.json()["roles"]}
|
||||
expect(saved_roles["generate"]["model"] == "openai/gpt-4o", "generate model persisted")
|
||||
expect("profiles" in saved.json(), "llm profiles are listed")
|
||||
titles = {item["title"] for item in saved.json()["profiles"]}
|
||||
expect("Ollama (LAN)" in titles, "LAN Ollama profile is seeded")
|
||||
ollama = next(item for item in saved.json()["profiles"] if item["title"] == "Ollama (LAN)")
|
||||
expect(ollama["local"] is True, "LAN Ollama is a local trusted endpoint")
|
||||
expect(ollama["url"].startswith("http://192.168.2.144:11434"), "Ollama URL is the agreed LAN host")
|
||||
|
||||
switched = client.put(
|
||||
"/api/admin/llm-profiles/" + ollama["id"],
|
||||
headers=headers,
|
||||
json={
|
||||
"title": "Ollama (LAN)",
|
||||
"name": "ollama",
|
||||
"url": ollama["url"],
|
||||
"model": "llama3.1:latest",
|
||||
"zdr": True,
|
||||
"no_train": True,
|
||||
},
|
||||
)
|
||||
expect(switched.status_code == 200, f"save ollama model {switched.text}")
|
||||
activated = client.post(
|
||||
"/api/admin/providers/detect/activate",
|
||||
headers=headers,
|
||||
json={"profile_id": ollama["id"]},
|
||||
)
|
||||
expect(activated.status_code == 200, f"activate ollama detect {activated.text}")
|
||||
detect_role = {item["role"]: item for item in activated.json()["roles"]}["detect"]
|
||||
expect(detect_role["local"] is True, "detect uses local ollama after profile switch")
|
||||
expect(detect_role["model"] == "llama3.1:latest", "detect keeps the saved ollama model")
|
||||
generate_role = {item["role"]: item for item in activated.json()["roles"]}["generate"]
|
||||
expect(generate_role["model"] == "openai/gpt-4o", "generate profile stays on its own model")
|
||||
|
||||
unknown = client.put(
|
||||
"/api/admin/providers/training",
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
"""Local trusted-zone URL tests. No database."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
os.environ.setdefault("KANSHO_PROVIDER_KEY", "")
|
||||
os.environ.setdefault("KANSHO_DETECT_PROVIDER_KEY", "")
|
||||
Path(tempfile.gettempdir()).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
from providers import is_local_url
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
expect(is_local_url("http://127.0.0.1:11434/v1/chat/completions"), "loopback is local")
|
||||
expect(is_local_url("http://localhost:11434/v1/chat/completions"), "localhost is local")
|
||||
expect(is_local_url("http://192.168.2.144:11434/v1/chat/completions"), "RFC1918 Ollama host is local")
|
||||
expect(is_local_url("http://10.0.0.8:11434/v1/chat/completions"), "10/8 is local")
|
||||
expect(is_local_url("http://ollama:11434/v1/chat/completions"), "compose hostname ollama is local")
|
||||
expect(not is_local_url("https://openrouter.ai/api/v1/chat/completions"), "OpenRouter is not local")
|
||||
expect(not is_local_url("https://8.8.8.8/v1/chat/completions"), "public IP is not local")
|
||||
print("local url tests passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -210,6 +210,30 @@ def test_list_provider_models() -> None:
|
||||
expect(list_provider_models(fake) == [], "fake provider has no catalog")
|
||||
|
||||
|
||||
def test_list_ollama_tags() -> None:
|
||||
reset_catalog()
|
||||
|
||||
def fake_get(url, *args, **kwargs):
|
||||
if str(url).endswith("/api/tags"):
|
||||
return FakeResponse({"models": [{"name": "llama3.1:latest"}, {"name": "qwen2.5:7b"}]})
|
||||
return FakeResponse({"nope": True})
|
||||
|
||||
config = ProviderConfig(
|
||||
role="detect",
|
||||
name="ollama",
|
||||
mode="http",
|
||||
url="http://192.168.2.144:11434/v1/chat/completions",
|
||||
model="llama3.1:latest",
|
||||
key="",
|
||||
local=True,
|
||||
zdr=True,
|
||||
no_train=True,
|
||||
)
|
||||
with patch("model_catalog.httpx.get", fake_get):
|
||||
items = list_provider_models(config)
|
||||
expect([item["id"] for item in items] == ["llama3.1:latest", "qwen2.5:7b"], "ollama tags fill the picker")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
test_reads_context_and_completion()
|
||||
test_model_not_in_catalog()
|
||||
@@ -217,6 +241,7 @@ def main() -> None:
|
||||
test_cache_hit_and_ttl_expiry()
|
||||
test_env_fallback_and_fail_closed()
|
||||
test_list_provider_models()
|
||||
test_list_ollama_tags()
|
||||
print("model catalog tests passed.")
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user