feat: Ollama-Adapter (Managed und Attached), Engine-Wahl in der Registry (M2.6a, gegen Attrappe)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Binary file not shown.
@ -26,7 +26,10 @@ from openai import OpenAI
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
NEXUS = Path(os.environ.get("NEXUS_BIN", ROOT / "target" / "debug" / "nexus"))
|
||||
FAKE = ROOT / "crates" / "nexus-adapter" / "tests" / "fake_llama_server.py"
|
||||
FAKE_OLLAMA = ROOT / "crates" / "nexus-adapter" / "tests" / "fake_ollama.py"
|
||||
MODEL = "testmodell"
|
||||
OLLAMA_MODEL = "klein" # Name im Gateway
|
||||
OLLAMA_TAG = "qwen3:0.6b" # Name in Ollama
|
||||
ANTWORT = "Hallo vom Attrappen-Modell."
|
||||
GEHEIMER_PROMPT = "GEHEIMER-PROMPT-4711"
|
||||
FALSCHER_KEY = "nx-FALSCHER-SCHLUESSEL-999"
|
||||
@ -59,6 +62,7 @@ class GatewayTest(unittest.TestCase):
|
||||
cls.tmp = Path(tempfile.mkdtemp(prefix="nexus-sdk-"))
|
||||
cls.port = free_port()
|
||||
llama_from = free_port()
|
||||
ollama_from = free_port()
|
||||
cls.config = cls.tmp / "config.toml"
|
||||
cls.config.write_text(
|
||||
f'listen = "127.0.0.1:{cls.port}"\n'
|
||||
@ -70,11 +74,20 @@ class GatewayTest(unittest.TestCase):
|
||||
f"ready_timeout_secs = 20\n\n"
|
||||
f"[llama_cpp.env]\n"
|
||||
f'FAKE_LLAMA_MODE = "ok"\n'
|
||||
f'FAKE_LLAMA_LOAD_SECONDS = "0.5"\n'
|
||||
f'FAKE_LLAMA_LOAD_SECONDS = "0.5"\n\n'
|
||||
f"[ollama]\n"
|
||||
f'binary = "{FAKE_OLLAMA}"\n'
|
||||
f'models_dir = "{cls.tmp / "ollama-modelle"}"\n'
|
||||
f"port_from = {ollama_from}\n"
|
||||
f"port_to = {ollama_from + 9}\n"
|
||||
f"ready_timeout_secs = 20\n\n"
|
||||
f"[ollama.env]\n"
|
||||
f'FAKE_OLLAMA_MODE = "ok"\n'
|
||||
)
|
||||
model_file = cls.tmp / "modell.gguf"
|
||||
model_file.write_bytes(b"gguf")
|
||||
cls.run_cli("model", "add", "--name", MODEL, "--path", str(model_file), "--gpus", "0")
|
||||
cls.run_cli("model", "add", "--name", OLLAMA_MODEL, "--engine", "ollama", "--tag", OLLAMA_TAG, "--gpus", "0")
|
||||
cls.key = cls.create_key("sdk-normal")
|
||||
cls.key_vip = cls.create_key("sdk-vip", "--max-priority", "high")
|
||||
|
||||
@ -136,12 +149,17 @@ class GatewayTest(unittest.TestCase):
|
||||
def instance_log(self):
|
||||
return self.tmp / "data" / "logs" / "instance-1.log"
|
||||
|
||||
def ollama_log(self):
|
||||
return self.tmp / "data" / "logs" / "instance-ollama.log"
|
||||
|
||||
# --- client2antwort.1: Modellliste über das SDK ---------------------------
|
||||
|
||||
def test_01_modellliste_zeigt_ungeladenes_modell_und_laedt_nichts(self):
|
||||
ids = [m.id for m in self.client.models.list().data]
|
||||
self.assertIn(MODEL, ids)
|
||||
self.assertIn(OLLAMA_MODEL, ids)
|
||||
self.assertFalse(self.instance_log().exists(), "Die Liste darf nichts laden.")
|
||||
self.assertFalse(self.ollama_log().exists(), "Die Liste darf keinen Ollama-Server starten.")
|
||||
|
||||
def test_02_einzelnes_modell_abrufen(self):
|
||||
self.assertEqual(self.client.models.retrieve(MODEL).id, MODEL)
|
||||
@ -213,12 +231,35 @@ class GatewayTest(unittest.TestCase):
|
||||
)
|
||||
self.assertTrue(roh.headers["x-request-id"].startswith("req_"))
|
||||
|
||||
# --- Ollama (Modellname im Gateway weicht vom Ollama-Tag ab) ----------------
|
||||
|
||||
def test_10_ollama_chat_startet_server_und_ersetzt_den_modellnamen(self):
|
||||
self.assertFalse(self.ollama_log().exists())
|
||||
antwort = self.client.chat.completions.create(
|
||||
model=OLLAMA_MODEL, messages=[{"role": "user", "content": "Hallo"}]
|
||||
)
|
||||
# Die Attrappe nennt das Modell, das tatsächlich in der Anfrage stand.
|
||||
self.assertTrue(antwort.choices[0].message.content.endswith(OLLAMA_TAG), antwort.choices[0].message.content)
|
||||
self.assertTrue(self.ollama_log().exists(), "ollama serve muss gestartet worden sein.")
|
||||
|
||||
def test_11_ollama_streaming(self):
|
||||
stuecke = []
|
||||
for chunk in self.client.chat.completions.create(
|
||||
model=OLLAMA_MODEL, messages=[{"role": "user", "content": "Hallo"}], stream=True
|
||||
):
|
||||
if chunk.choices and chunk.choices[0].delta.content:
|
||||
stuecke.append(chunk.choices[0].delta.content)
|
||||
self.assertGreater(len(stuecke), 1)
|
||||
self.assertTrue("".join(stuecke).endswith(OLLAMA_TAG))
|
||||
|
||||
# --- Herunterfahren -------------------------------------------------------
|
||||
|
||||
def test_99_herunterfahren_beendet_das_modell(self):
|
||||
log = self.instance_log().read_text()
|
||||
pid = int(next(l for l in log.splitlines() if l.startswith("fake-llama: pid ")).split()[-1])
|
||||
self.assertTrue(pid_alive(pid), "Die Instanz sollte vor dem Herunterfahren laufen.")
|
||||
ollama_log = self.ollama_log().read_text()
|
||||
self.assertTrue(pid_alive(int(next(l for l in ollama_log.splitlines() if l.startswith("fake-ollama: pid ")).split()[-1])))
|
||||
self.server.send_signal(signal.SIGTERM)
|
||||
self.assertEqual(self.server.wait(timeout=20), 0)
|
||||
for _ in range(50):
|
||||
@ -226,6 +267,12 @@ class GatewayTest(unittest.TestCase):
|
||||
break
|
||||
time.sleep(0.1)
|
||||
self.assertFalse(pid_alive(pid), "Die Instanz läuft nach dem Herunterfahren noch.")
|
||||
ollama_pid = int(next(l for l in self.ollama_log().read_text().splitlines() if l.startswith("fake-ollama: pid ")).split()[-1])
|
||||
for _ in range(50):
|
||||
if not pid_alive(ollama_pid):
|
||||
break
|
||||
time.sleep(0.1)
|
||||
self.assertFalse(pid_alive(ollama_pid), "ollama serve läuft nach dem Herunterfahren noch.")
|
||||
|
||||
class LogTest(unittest.TestCase):
|
||||
"""Läuft nach GatewayTest (Name sortiert dahinter) und liest dessen Spuren."""
|
||||
|
||||
Reference in New Issue
Block a user