Initial commit: LocalPilot:本地模型运行时,复用 llama-server 并提供 Ollama 兼容 provider

This commit is contained in:
WpyQwq
2026-09-19 11:57:59 +08:00
commit d159212416
27 changed files with 3022 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
import json
import tempfile
import unittest
from pathlib import Path
from localpilot.config import AppConfig, ModelConfig, infer_kind, load_config
class ConfigTests(unittest.TestCase):
def test_infer_extensions(self) -> None:
self.assertEqual(infer_kind(ModelConfig(id="x", path="E:/x/model.gguf")), "gguf")
self.assertEqual(infer_kind(ModelConfig(id="x", path="E:/x/model.onnx")), "onnx")
self.assertEqual(infer_kind(ModelConfig(id="x", kind="cloud")), "cloud")
def test_load_config(self) -> None:
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / "config.json"
path.write_text(json.dumps({"models": [{"id": "x", "kind": "gguf", "path": "x.gguf"}]}), encoding="utf-8")
config = load_config(path)
self.assertIsInstance(config, AppConfig)
self.assertEqual(config.models[0].id, "x")
if __name__ == "__main__":
unittest.main()
+28
View File
@@ -0,0 +1,28 @@
import tempfile
import unittest
from pathlib import Path
from localpilot.config import AppConfig, ModelConfig, RuntimeConfig
from localpilot.manager import ModelManager, parse_keep_alive
class ManagerTests(unittest.TestCase):
def test_parse_keep_alive(self) -> None:
self.assertEqual(parse_keep_alive("5m", 30), 300)
self.assertEqual(parse_keep_alive("30s", 30), 30)
self.assertEqual(parse_keep_alive(0, 30), 0)
self.assertEqual(parse_keep_alive(-1, 30), float("inf"))
self.assertEqual(parse_keep_alive("bad", 30), 30)
def test_register_and_remove_only_changes_registry(self) -> None:
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / "config.json"
manager = ModelManager(AppConfig(runtime=RuntimeConfig()), path)
manager.register_model(ModelConfig(id="demo", kind="gguf", path="E:/model/demo.gguf"))
self.assertEqual(manager.config.models[0].id, "demo")
self.assertTrue(path.exists())
if __name__ == "__main__":
unittest.main()
+27
View File
@@ -0,0 +1,27 @@
import unittest
from localpilot.modelfile import model_config_from_modelfile, parse_modelfile
class ModelfileTests(unittest.TestCase):
def test_parse_and_convert(self) -> None:
text = '''
FROM E:/models/demo.gguf
PARAMETER temperature 0.2
PARAMETER top_p 0.9
PARAMETER num_ctx 8192
SYSTEM """You are concise."""
MESSAGE user hello
'''
parsed = parse_modelfile(text)
self.assertEqual(parsed.source, "E:/models/demo.gguf")
self.assertEqual(parsed.system, "You are concise.")
model = model_config_from_modelfile("demo", text)
self.assertEqual(model.parameters["temperature"], 0.2)
self.assertEqual(model.options["ctx_size"], 8192)
self.assertEqual(model.messages[0]["role"], "user")
if __name__ == "__main__":
unittest.main()
+41
View File
@@ -0,0 +1,41 @@
import unittest
import json
import tempfile
from pathlib import Path
from fastapi.testclient import TestClient
from localpilot.server import create_app
from localpilot.server import _content_to_text, _sse
class ServerHelperTests(unittest.TestCase):
def test_content_to_text(self) -> None:
self.assertEqual(_content_to_text("hello"), "hello")
self.assertEqual(_content_to_text([{"type": "text", "text": "a"}, {"type": "text", "text": "b"}]), "ab")
def test_sse(self) -> None:
value = _sse({"ok": True})
self.assertTrue(value.startswith("data: "))
self.assertTrue(value.endswith("\n\n"))
def test_ollama_surface_and_registry(self) -> None:
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / "config.json"
path.write_text(json.dumps({"models": []}), encoding="utf-8")
client = TestClient(create_app(path))
self.assertEqual(client.get("/api/version").status_code, 200)
self.assertEqual(client.get("/api/tags").json(), {"models": []})
registered = client.post("/api/models/register", json={"id": "demo", "kind": "gguf", "path": "E:/demo.gguf"})
self.assertEqual(registered.status_code, 200)
self.assertEqual(client.get("/api/tags").json()["models"][0]["name"], "demo")
created = client.post("/api/create", json={"model": "created", "modelfile": "FROM E:/models/created.gguf\nPARAMETER temperature 0.2"})
self.assertEqual(created.status_code, 200)
copied = client.post("/api/copy", json={"source": "created", "destination": "created-copy"})
self.assertEqual(copied.status_code, 200)
removed = client.delete("/api/models/demo")
self.assertEqual(removed.status_code, 200)
if __name__ == "__main__":
unittest.main()