File size: 12,032 Bytes
ec6eb20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Chat server for the king model. Runs on GPU pod, port 8100.
Features: SSE streaming, thinking/answer split, concurrent requests, no token cap.
~8GB VRAM for a 4B model. HF transformers backend (~37 tok/s on B200).
"""
import json
import sys
import time
import re
import torch
import threading
from http.server import HTTPServer, BaseHTTPRequestHandler
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer

MODEL_NAME = sys.argv[1] if len(sys.argv) > 1 else "aceini/q-dist"
PORT = int(sys.argv[2]) if len(sys.argv) > 2 else 8100

print(f"[chat] Loading {MODEL_NAME}...", flush=True)
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_NAME, torch_dtype=torch.bfloat16, device_map="auto"
)
model.eval()
vram_gb = round(torch.cuda.memory_allocated() / 1e9, 1)
print(f"[chat] Model loaded. VRAM: {vram_gb}GB", flush=True)

_gen_lock = threading.Lock()


class ChatHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        if self.path != "/v1/chat/completions":
            self.send_error(404)
            return

        length = int(self.headers.get("Content-Length", 0))
        body = json.loads(self.rfile.read(length))
        messages = body.get("messages", [])
        max_tokens = body.get("max_tokens", 2048)  # No hard cap
        temperature = body.get("temperature", 0.7)
        top_p = body.get("top_p", 0.9)
        stream = body.get("stream", False)

        try:
            text = tokenizer.apply_chat_template(
                messages, tokenize=False, add_generation_prompt=True
            )
        except Exception:
            parts = [f"{m.get('role','user')}: {m.get('content','')}" for m in messages]
            parts.append("assistant:")
            text = "\n".join(parts)

        inputs = tokenizer(text, return_tensors="pt").to(model.device)
        input_len = inputs["input_ids"].shape[1]

        gen_kwargs = dict(
            **inputs,
            max_new_tokens=max_tokens,
            do_sample=temperature > 0,
            temperature=max(temperature, 0.01),
            top_p=top_p,
            repetition_penalty=1.1,
        )

        if stream:
            self._stream_response(gen_kwargs, input_len)
        else:
            self._sync_response(gen_kwargs, input_len)

    def _sync_response(self, gen_kwargs, input_len):
        t0 = time.time()
        with _gen_lock:
            with torch.no_grad():
                output = model.generate(**gen_kwargs)
        elapsed = time.time() - t0
        new_tokens = output[0][input_len:]
        n_tokens = len(new_tokens)
        raw = tokenizer.decode(new_tokens, skip_special_tokens=False)
        # Strip special tokens but keep <think>/<\/think>
        for st in getattr(tokenizer, 'all_special_tokens', []):
            if st not in ("<think>", "</think>"):
                raw = raw.replace(st, "")
        tps = n_tokens / elapsed if elapsed > 0 else 0

        thinking, answer = _split_thinking(raw)

        result = {
            "choices": [{"message": {"role": "assistant", "content": answer}, "finish_reason": "stop"}],
            "model": MODEL_NAME,
            "usage": {"completion_tokens": n_tokens, "tokens_per_second": round(tps, 1), "generation_time_s": round(elapsed, 2)},
        }
        if thinking:
            result["thinking"] = thinking

        self._send_json(200, result)

    def _stream_response(self, gen_kwargs, input_len):
        self.send_response(200)
        self.send_header("Content-Type", "text/event-stream")
        self.send_header("Cache-Control", "no-cache")
        self.send_header("Connection", "keep-alive")
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()

        streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=False, skip_prompt=True)
        gen_kwargs["streamer"] = streamer

        t0 = time.time()
        n_tokens = [0]
        full_text = []
        # Phase detection: only use <think> tags (reliable).
        # For models that use "Thinking Process:" style, we don't try to split
        # in streaming — the full split happens server-side when stream=false.
        phase = ["answer"]  # default to answer
        think_done = [False]
        has_think_tags = [False]

        def generate():
            with _gen_lock:
                with torch.no_grad():
                    model.generate(**gen_kwargs)

        thread = threading.Thread(target=generate)
        thread.start()

        # Build list of special token strings to strip from output
        _special_strs = set()
        if hasattr(tokenizer, 'all_special_tokens'):
            _special_strs = set(tokenizer.all_special_tokens)
        # Always strip common ones
        _special_strs.update(["<|endoftext|>", "<|im_end|>", "<|im_start|>", "<|end|>"])

        try:
            for chunk in streamer:
                # Strip special tokens from chunk
                clean_chunk = chunk
                for st in _special_strs:
                    clean_chunk = clean_chunk.replace(st, "")
                if not clean_chunk:
                    continue

                full_text.append(clean_chunk)
                joined = "".join(full_text)
                n_tokens[0] += max(1, len(tokenizer.encode(chunk, add_special_tokens=False)))
                elapsed = time.time() - t0
                tps = n_tokens[0] / elapsed if elapsed > 0 else 0

                # Phase detection: only handle explicit <think>/<\/think> tags
                if not think_done[0]:
                    if "<think>" in joined and not has_think_tags[0]:
                        has_think_tags[0] = True
                        phase[0] = "thinking"

                    if has_think_tags[0] and "</think>" in joined:
                        think_done[0] = True
                        phase[0] = "answer"
                        after = joined.split("</think>", 1)[1].strip()
                        self._sse({"choices": [{"delta": {"phase": "answer"}, "finish_reason": None}], "usage": {"tokens_per_second": round(tps, 1)}})
                        if after:
                            self._sse({"choices": [{"delta": {"content": after, "phase": "answer"}, "finish_reason": None}], "usage": {"tokens_per_second": round(tps, 1)}})
                        continue

                # Strip think tags from output
                out = clean_chunk.replace("<think>", "").replace("</think>", "")
                if not out:
                    continue

                self._sse({
                    "choices": [{"delta": {"content": out, "phase": phase[0]}, "finish_reason": None}],
                    "usage": {"tokens_per_second": round(tps, 1)},
                })
        except (BrokenPipeError, ConnectionResetError):
            pass
        finally:
            thread.join()

        elapsed = time.time() - t0
        tps = n_tokens[0] / elapsed if elapsed > 0 else 0
        try:
            # For models without <think> tags, split thinking from answer retroactively
            final_text = "".join(full_text)
            thinking_text, answer_text = _split_thinking(final_text)

            done_event = {
                "choices": [{"delta": {}, "finish_reason": "stop"}],
                "usage": {"completion_tokens": n_tokens[0], "tokens_per_second": round(tps, 1), "generation_time_s": round(elapsed, 2)},
            }
            if thinking_text:
                done_event["thinking"] = thinking_text
                done_event["answer"] = answer_text
            self._sse(done_event)
            self.wfile.write(b"data: [DONE]\n\n")
            self.wfile.flush()
        except (BrokenPipeError, ConnectionResetError):
            pass

    def _sse(self, data):
        self.wfile.write(f"data: {json.dumps(data)}\n\n".encode())
        self.wfile.flush()

    def _send_json(self, code, data):
        self.send_response(code)
        self.send_header("Content-Type", "application/json")
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()
        self.wfile.write(json.dumps(data).encode())

    def do_GET(self):
        if self.path == "/health":
            self._send_json(200, {
                "status": "ok", "model": MODEL_NAME,
                "vram_gb": round(torch.cuda.memory_allocated() / 1e9, 1),
            })
        else:
            self.send_error(404)

    def do_OPTIONS(self):
        self.send_response(200)
        self.send_header("Access-Control-Allow-Origin", "*")
        self.send_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
        self.send_header("Access-Control-Allow-Headers", "Content-Type")
        self.end_headers()

    def log_message(self, format, *args):
        pass


def _split_thinking(text):
    """Split thinking from answer. Handles <think> tags and 'Thinking Process:' headers."""
    # 1. Explicit <think>...</think> tags
    if "</think>" in text:
        parts = text.split("</think>", 1)
        thinking = parts[0].replace("<think>", "").strip()
        answer = parts[1].strip()
        return thinking, answer if answer else "(stopped during thinking)"
    if text.lstrip().startswith("<think>"):
        return text.lstrip()[7:].strip(), "(thinking cut short)"

    # 2. "Thinking Process:" / "Thought:" / "Reasoning:" style headers
    # The model outputs structured thinking then transitions to the actual answer
    # Pattern: thinking block → double newline → answer (often starts differently)
    for header in ["Thinking Process:", "**Thinking Process:**", "Thought:", "Reasoning:", "Let me think"]:
        if text.strip().startswith(header):
            # Find the answer after the thinking block ends
            # Look for patterns like: numbered list ending → double newline → non-list content
            # Or: thinking block → "---" → answer
            # Or: "Draft:" / "Response:" / "Answer:" / "Final" section that's the actual output
            answer_markers = [
                r'\n\n---\n',
                r'\n\n(?:(?:Final )?(?:Answer|Response|Output|Result)[:\s])',
                r'\n\n(?:Here\'s|Here is)',
            ]
            for pattern in answer_markers:
                match = re.search(pattern, text)
                if match:
                    thinking = text[:match.start()].strip()
                    answer = text[match.end():].strip() if text[match.end():].strip() else text[match.start():].strip()
                    return thinking, answer

            # Fallback: find last double-newline followed by short non-list content
            # This catches cases where thinking ends and a clean answer starts
            parts = text.rsplit('\n\n', 1)
            if len(parts) == 2 and not parts[1].strip().startswith(('*', '-', '#', 'Option')):
                last_block = parts[1].strip()
                # If the last block looks like an actual answer (not another thinking step)
                if len(last_block) > 10 and not any(last_block.startswith(m) for m in ['*', '-', '1.', '2.', '3.', '4.']):
                    return parts[0].strip(), last_block

            # If we can't find a clean split, return everything as thinking
            return text.strip(), "(thinking — answer not yet generated)"

    return None, text


class ThreadedHTTPServer(HTTPServer):
    def process_request(self, request, client_address):
        t = threading.Thread(target=self._handle, args=(request, client_address), daemon=True)
        t.start()

    def _handle(self, request, client_address):
        try:
            self.finish_request(request, client_address)
        except Exception:
            self.handle_error(request, client_address)
        finally:
            self.shutdown_request(request)


if __name__ == "__main__":
    print(f"[chat] Serving on port {PORT} (threaded, streaming)", flush=True)
    server = ThreadedHTTPServer(("0.0.0.0", PORT), ChatHandler)
    server.serve_forever()