Norsec93 commited on
Commit
bfc8568
·
verified ·
1 Parent(s): 4b98919

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -16
main.py CHANGED
@@ -138,21 +138,22 @@ async def proxy(request: Request):
138
  sys_toks = count_tokens([m for m in trimmed if m.get("role") == "system"])
139
  print(f"[NIM Proxy] Msgs: {len(trimmed)} | TotalToks: {total_toks} | SysToks: {sys_toks} | User: {user_name} | Char: {char_name}")
140
 
141
- # CRITICAL FIX: reasoning_effort must be TOP-LEVEL on NIM, not inside chat_template_kwargs.
142
- # Also include enable_thinking + clear_thinking (official NIM knobs) and thinking: true
143
- # (undocumented but worked before) to cover all bases.
 
144
  nim_body = {
145
  "model": body.get("model", "z-ai/glm-5.2"),
146
  "messages": trimmed,
147
  "stream": body.get("stream", True),
148
- "reasoning_effort": "max", # TOP LEVEL — NIM ignores this if nested
149
  "chat_template_kwargs": {
150
- "thinking": True, # Undocumented fallback that worked for you before
151
- "enable_thinking": True, # Official NIM boolean knob
152
- "clear_thinking": False, # Preserve reasoning across turns
153
  },
154
- "temperature": body.get("temperature", 1.0),
155
- "top_p": body.get("top_p", 0.95),
156
  }
157
  for key in ["max_tokens", "top_k", "frequency_penalty", "presence_penalty", "repetition_penalty", "stop", "seed"]:
158
  if key in body and body[key] is not None:
@@ -177,7 +178,7 @@ async def proxy(request: Request):
177
  buffer = ""
178
  thinking_started = False
179
  thinking_ended = False
180
- reasoning_seen = False # Debug flag
181
 
182
  async for chunk in resp.aiter_text():
183
  buffer += chunk
@@ -189,7 +190,7 @@ async def proxy(request: Request):
189
  continue
190
  if "[DONE]" in line:
191
  if thinking_started and not thinking_ended:
192
- close_data = {"choices": [{"delta": {"content": "\n</thinking>\n\n"}}]}
193
  yield f"data: {json.dumps(close_data)}\n\n"
194
  yield line + "\n\n"
195
  continue
@@ -204,7 +205,6 @@ async def proxy(request: Request):
204
  reasoning = delta.get("reasoning_content", "")
205
  content = delta.get("content", "")
206
 
207
- # DEBUG: log the first time we see reasoning tokens
208
  if reasoning and not reasoning_seen:
209
  reasoning_seen = True
210
  print(f"[NIM Proxy] REASONING DETECTED: {reasoning[:120]}...")
@@ -213,13 +213,13 @@ async def proxy(request: Request):
213
 
214
  if reasoning:
215
  if not thinking_started:
216
- output += "<thinking>\n"
217
  thinking_started = True
218
  output += reasoning
219
 
220
  if content:
221
  if thinking_started and not thinking_ended:
222
- output += "\n</thinking>\n\n"
223
  thinking_ended = True
224
  output += content
225
 
@@ -230,10 +230,9 @@ async def proxy(request: Request):
230
  yield line + "\n"
231
 
232
  if thinking_started and not thinking_ended:
233
- close_data = {"choices": [{"delta": {"content": "\n</thinking>\n\n"}}]}
234
  yield f"data: {json.dumps(close_data)}\n\n"
235
 
236
- # DEBUG: warn if no reasoning was seen at all
237
  if not reasoning_seen:
238
  print("[NIM Proxy] WARNING: No reasoning_content tokens received in this stream.")
239
 
 
138
  sys_toks = count_tokens([m for m in trimmed if m.get("role") == "system"])
139
  print(f"[NIM Proxy] Msgs: {len(trimmed)} | TotalToks: {total_toks} | SysToks: {sys_toks} | User: {user_name} | Char: {char_name}")
140
 
141
+ # RP-optimized defaults:
142
+ # - reasoning_effort: "max" at TOP LEVEL (nested inside chat_template_kwargs is ignored by NIM)
143
+ # - temperature defaults to 0.85 for tighter character consistency if Janitor doesn't send one
144
+ # - top_p 0.95 keeps creativity while preventing token drift
145
  nim_body = {
146
  "model": body.get("model", "z-ai/glm-5.2"),
147
  "messages": trimmed,
148
  "stream": body.get("stream", True),
149
+ "reasoning_effort": "max",
150
  "chat_template_kwargs": {
151
+ "thinking": True,
152
+ "enable_thinking": True,
153
+ "clear_thinking": False,
154
  },
155
+ "temperature": body.get("temperature") if body.get("temperature") is not None else 0.85,
156
+ "top_p": body.get("top_p") if body.get("top_p") is not None else 0.95,
157
  }
158
  for key in ["max_tokens", "top_k", "frequency_penalty", "presence_penalty", "repetition_penalty", "stop", "seed"]:
159
  if key in body and body[key] is not None:
 
178
  buffer = ""
179
  thinking_started = False
180
  thinking_ended = False
181
+ reasoning_seen = False
182
 
183
  async for chunk in resp.aiter_text():
184
  buffer += chunk
 
190
  continue
191
  if "[DONE]" in line:
192
  if thinking_started and not thinking_ended:
193
+ close_data = {"choices": [{"delta": {"content": "\n \n\n"}}]}
194
  yield f"data: {json.dumps(close_data)}\n\n"
195
  yield line + "\n\n"
196
  continue
 
205
  reasoning = delta.get("reasoning_content", "")
206
  content = delta.get("content", "")
207
 
 
208
  if reasoning and not reasoning_seen:
209
  reasoning_seen = True
210
  print(f"[NIM Proxy] REASONING DETECTED: {reasoning[:120]}...")
 
213
 
214
  if reasoning:
215
  if not thinking_started:
216
+ output += " \n"
217
  thinking_started = True
218
  output += reasoning
219
 
220
  if content:
221
  if thinking_started and not thinking_ended:
222
+ output += "\n \n\n"
223
  thinking_ended = True
224
  output += content
225
 
 
230
  yield line + "\n"
231
 
232
  if thinking_started and not thinking_ended:
233
+ close_data = {"choices": [{"delta": {"content": "\n \n\n"}}]}
234
  yield f"data: {json.dumps(close_data)}\n\n"
235
 
 
236
  if not reasoning_seen:
237
  print("[NIM Proxy] WARNING: No reasoning_content tokens received in this stream.")
238