From 49c39a182b200f00460c6fcce55c680af4718d57 Mon Sep 17 00:00:00 2001 From: Hyungi Ahn Date: Thu, 26 Mar 2026 13:34:20 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20LLM=20thinking=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EB=8C=80=EC=9D=91=20=E2=80=94=20max=5Ftokens=20=EC=A6=9D?= =?UTF-8?q?=EA=B0=80=20+=20JSON=20=EC=B6=94=EC=B6=9C=20=EA=B0=95=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.6 (1M context) --- scripts/pkm_utils.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/pkm_utils.py b/scripts/pkm_utils.py index f901701..eb66b45 100644 --- a/scripts/pkm_utils.py +++ b/scripts/pkm_utils.py @@ -111,22 +111,29 @@ def llm_generate(prompt: str, model: str = "mlx-community/Qwen3.5-35B-A3B-4bit", import requests messages = [] if json_mode: - messages.append({"role": "system", "content": "You must respond ONLY with valid JSON. No thinking, no explanation, no markdown."}) - # Qwen3.5: /nothink 접미사로 thinking 출력 억제 - messages.append({"role": "user", "content": prompt + " /nothink"}) + messages.append({"role": "system", "content": "IMPORTANT: Output ONLY valid JSON. No thinking process, no explanation, no markdown fences. Start your response with { and end with }."}) + messages.append({"role": "user", "content": prompt}) resp = requests.post(f"{host}/v1/chat/completions", json={ "model": model, "messages": messages, - "temperature": 0.3, - "max_tokens": 1024, - }, timeout=120) + "temperature": 0.1 if json_mode else 0.3, + "max_tokens": 2048, + }, timeout=180) resp.raise_for_status() content = resp.json()["choices"][0]["message"]["content"] - # JSON 블록 추출 (```json ... ``` 감싸기 대응) + # thinking 블록 제거 (Qwen3.5 thinking 모델 대응) + if "" in content and "" in content: + content = content.split("")[-1].strip() + # JSON 블록 추출 if "```json" in content: content = content.split("```json")[1].split("```")[0].strip() elif "```" in content: content = content.split("```")[1].split("```")[0].strip() + # { 로 시작하는 JSON 추출 + import re + json_match = re.search(r'\{[\s\S]*\}', content) + if json_match: + content = json_match.group(0) return content