Initial local LLM web app
This commit is contained in:
commit
afa8f3780f
7 files changed
+395
No files matched your search
@@ -0,0 +1,59 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
|
||||
import ollama
|
||||
|
||||
|
||||
ROOT = Path(__file__).parent
|
||||
MODEL = "qwen2.5:3b"
|
||||
|
||||
|
||||
class Handler(SimpleHTTPRequestHandler):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, directory=str(ROOT), **kwargs)
|
||||
|
||||
def do_POST(self) -> None:
|
||||
if self.path != "/api/chat":
|
||||
self.send_error(404)
|
||||
return
|
||||
|
||||
length = int(self.headers.get("content-length", 0))
|
||||
data = json.loads(self.rfile.read(length) or b"{}")
|
||||
messages = data.get("messages", [])
|
||||
|
||||
try:
|
||||
response = ollama.chat(
|
||||
model=MODEL,
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": "You are a concise local assistant. Reply in Korean unless the user asks otherwise.",
|
||||
},
|
||||
*messages,
|
||||
],
|
||||
)
|
||||
payload = {"reply": response["message"]["content"]}
|
||||
self._send_json(200, payload)
|
||||
except Exception as exc:
|
||||
self._send_json(500, {"error": str(exc)})
|
||||
|
||||
def _send_json(self, status: int, payload: dict[str, str]) -> None:
|
||||
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
||||
self.send_response(status)
|
||||
self.send_header("content-type", "application/json; charset=utf-8")
|
||||
self.send_header("content-length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
server = ThreadingHTTPServer(("127.0.0.1", 8787), Handler)
|
||||
print("LLM web is running at http://127.0.0.1:8787")
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user