-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathchat.py
More file actions
executable file
·304 lines (270 loc) · 10.2 KB
/
Copy pathchat.py
File metadata and controls
executable file
·304 lines (270 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
"""
██╗ ██╗██╗██╗ ██╗███████╗
██║ ██║██║██║ ██║██╔════╝
███████║██║██║ ██║█████╗
██╔══██║██║╚██╗ ██╔╝██╔══╝
██║ ██║██║ ╚████╔╝ ███████╗
╚═╝ ╚═╝╚═╝ ╚═══╝ ╚══════╝ chat
"""
import json, sys, os, time, threading, urllib.request, urllib.error
from collections import deque
from datetime import datetime
from rich.console import Console, Group
from rich.panel import Panel
from rich.text import Text
from rich.markdown import Markdown
from rich.rule import Rule
from rich.table import Table
from rich.columns import Columns
from rich.align import Align
SERVER = os.environ.get("LLAMA_URL", "http://localhost:8000")
console = Console()
# ── state ──────────────────────────────────────────
messages = []
session_tokens = 0
session_time = 0.0
session_turns = 0
model_name = ""
model_detail = ""
# ── detect model ───────────────────────────────────
def detect():
global model_name, model_detail
try:
req = urllib.request.Request(f"{SERVER}/props")
with urllib.request.urlopen(req, timeout=3) as r:
d = json.loads(r.read())
alias = d.get("model_alias", "") or d.get("model_path", "")
if "35B-A3B" in alias:
model_name = "Qwen3.5-35B-A3B"
model_detail = "MoE 34.7B · 3B active · IQ2_M · Metal"
elif "9B" in alias:
model_name = "Qwen3.5-9B"
model_detail = "8.95B dense · Q4_K_M · Metal"
else:
model_name = alias.replace(".gguf","").split("/")[-1]
model_detail = "local model"
except Exception:
model_name = "connecting..."
model_detail = ""
# ── streaming request ──────────────────────────────
def stream(msgs):
payload = json.dumps({
"model": "local",
"messages": msgs,
"max_tokens": 4096,
"temperature": 0.7,
"stream": True,
}).encode()
req = urllib.request.Request(
f"{SERVER}/v1/chat/completions",
data=payload,
headers={"Content-Type": "application/json"},
)
full = ""
start = time.time()
tokens = 0
with urllib.request.urlopen(req, timeout=300) as resp:
buf = ""
while True:
ch = resp.read(1)
if not ch:
break
buf += ch.decode("utf-8", errors="replace")
while "\n" in buf:
line, buf = buf.split("\n", 1)
line = line.strip()
if not line or not line.startswith("data: "):
continue
raw = line[6:]
if raw == "[DONE]":
elapsed = time.time() - start
speed = tokens / elapsed if elapsed > 0 else 0
return full, tokens, elapsed, speed
try:
obj = json.loads(raw)
delta = obj["choices"][0].get("delta", {})
c = delta.get("content", "")
if c:
full += c
tokens += 1
yield c, None
except Exception:
pass
elapsed = time.time() - start
speed = tokens / elapsed if elapsed > 0 else 0
return full, tokens, elapsed, speed
# ── non-streaming fallback ─────────────────────────
def ask(msgs):
payload = json.dumps({
"model": "local",
"messages": msgs,
"max_tokens": 4096,
"temperature": 0.7,
}).encode()
req = urllib.request.Request(
f"{SERVER}/v1/chat/completions",
data=payload,
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req, timeout=300) as resp:
d = json.loads(resp.read())
content = d["choices"][0]["message"]["content"]
t = d.get("timings", {})
u = d.get("usage", {})
tokens = u.get("completion_tokens", 0)
speed = t.get("predicted_per_second", 0)
elapsed = t.get("predicted_ms", 0) / 1000
return content, tokens, elapsed, speed
# ── render helpers ─────────────────────────────────
def header():
t = Text()
t.append(" HIVE ", style="bold black on bright_yellow")
t.append(" ", style="default")
t.append(model_name, style="bold cyan")
if model_detail:
t.append(f" {model_detail}", style="dim")
return Panel(t, style="bright_yellow", height=3)
def stat_bar(tokens, elapsed, speed):
s = Text()
clr = "bright_green" if speed > 20 else "yellow" if speed > 10 else "red"
s.append(f" {speed:.1f} tok/s", style=f"bold {clr}")
s.append(f" · {tokens} tokens", style="dim")
s.append(f" · {elapsed:.1f}s", style="dim")
return s
def session_stats():
avg = session_tokens / session_time if session_time > 0 else 0
t = Table(show_header=False, box=None, padding=(0, 2))
t.add_column(style="bold cyan", width=14)
t.add_column()
t.add_row("Turns", str(session_turns))
t.add_row("Tokens", f"{session_tokens:,}")
t.add_row("Time", f"{session_time:.1f}s")
t.add_row("Avg speed", f"{avg:.1f} tok/s")
return Panel(t, title="[bold cyan]Session", border_style="cyan")
def help_panel():
h = Text()
h.append("/clear", style="bold cyan")
h.append(" reset · ", style="dim")
h.append("/stats", style="bold cyan")
h.append(" session info · ", style="dim")
h.append("/system", style="bold cyan")
h.append(" <msg> set persona · ", style="dim")
h.append("/model", style="bold cyan")
h.append(" show model · ", style="dim")
h.append("/quit", style="bold cyan")
h.append(" exit", style="dim")
return h
# ── main loop ──────────────────────────────────────
def main():
global session_tokens, session_time, session_turns
detect()
console.clear()
console.print(header())
console.print(help_panel())
console.print()
while True:
# prompt
try:
console.print("[bold bright_yellow]▶[/] ", end="")
user = input()
except (EOFError, KeyboardInterrupt):
console.print()
break
if not user.strip():
continue
cmd = user.strip().lower()
if cmd in ("/quit", "/exit", "/q"):
break
elif cmd == "/clear":
messages.clear()
console.clear()
console.print(header())
console.print("[dim] History cleared.[/]\n")
continue
elif cmd == "/stats":
console.print(session_stats())
console.print()
continue
elif cmd == "/model":
detect()
console.print(f" [bold cyan]{model_name}[/] [dim]{model_detail}[/]\n")
continue
elif cmd == "/help":
console.print(help_panel())
console.print()
continue
elif cmd.startswith("/system "):
sys_msg = user[8:].strip()
# Insert or replace system message
if messages and messages[0]["role"] == "system":
messages[0]["content"] = sys_msg
else:
messages.insert(0, {"role": "system", "content": sys_msg})
console.print(f" [dim italic]System: {sys_msg[:80]}{'...' if len(sys_msg)>80 else ''}[/]\n")
continue
messages.append({"role": "user", "content": user})
# response
console.print()
full = ""
tokens = 0
elapsed = 0.0
speed = 0.0
try:
# Try streaming
gen = stream(messages)
first = True
result = None
for chunk in gen:
if isinstance(chunk, tuple) and len(chunk) == 2:
text_chunk, meta = chunk
if text_chunk is not None:
if first:
console.print(" ", end="")
first = False
console.print(text_chunk, end="", highlight=False)
full += text_chunk
elif isinstance(chunk, tuple) and len(chunk) == 4:
# Final return value
full, tokens, elapsed, speed = chunk
break
if not full:
# Streaming returned nothing, try non-streaming
raise Exception("empty stream")
# If stream worked but didn't give stats, estimate
if tokens == 0:
tokens = len(full.split())
elapsed = 0
speed = 0
console.print()
except Exception:
# Fallback non-streaming
try:
console.print(" [dim]thinking...[/]", end="\r")
full, tokens, elapsed, speed = ask(messages)
console.print(f" {full}")
except Exception as e:
console.print(f" [bold red]Error:[/] {e}\n")
messages.pop() # remove failed user message
continue
# Stats line
if speed > 0:
console.print(stat_bar(tokens, elapsed, speed))
elif tokens > 0:
console.print(f" [dim]{tokens} tokens[/]")
console.print()
messages.append({"role": "assistant", "content": full})
session_tokens += tokens
session_time += elapsed
session_turns += 1
# goodbye
console.print()
if session_turns > 0:
avg = session_tokens / session_time if session_time > 0 else 0
console.print(
f" [bold bright_yellow]HIVE[/] [dim]"
f"{session_turns} turns · {session_tokens:,} tokens · {avg:.1f} avg tok/s[/]"
)
console.print()
if __name__ == "__main__":
main()