Files
colibri-strix/chat.py
T

59 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""Interaktiver Chat mit Colibri GLM-5.2 — direkt im Terminal."""
import urllib.request, json, sys, os
API = "http://127.0.0.1:8082/v1/chat/completions"
KEY = "colibri-glm52-local"
MODEL = "glm-5.2"
HISTORY = []
print("\033[1;36m╔══════════════════════════════════╗")
print("║ Colibri GLM-5.2 — Terminal ║")
print("║ /quit • /clear • /help ║")
print("╚══════════════════════════════════╝\033[0m\n")
while True:
try:
user = input("\033[1;33mDu>\033[0m ")
except (EOFError, KeyboardInterrupt):
print("\n\033[2mTschüss!\033[0m")
break
if user.strip().lower() in ("/quit", "/exit", "/q"):
print("\033[2mTschüss!\033[0m")
break
if user.strip().lower() == "/clear":
HISTORY.clear()
print("\033[2mVerlauf gelöscht.\033[0m")
continue
if user.strip().lower() == "/help":
print(" /quit • Beenden\n /clear • Verlauf löschen\n /help • Hilfe")
continue
if not user.strip():
continue
HISTORY.append({"role": "user", "content": user})
data = json.dumps({
"model": MODEL,
"messages": HISTORY[-30:], # Letzte 30 Nachrichten
"max_tokens": 512,
"stream": False
}).encode()
req = urllib.request.Request(API, data=data, headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {KEY}"
})
print("\033[2m⏳\033[0m", end="", flush=True)
try:
resp = urllib.request.urlopen(req, timeout=300)
j = json.loads(resp.read().decode())
text = j["choices"][0]["message"]["content"]
print(f"\r\033[1;36mGLM>\033[0m {text}")
HISTORY.append({"role": "assistant", "content": text})
except Exception as e:
print(f"\r\033[1;31mFehler:\033[0m {e}")
HISTORY.pop() # Fehlgeschlagenen Request entfernen