From 4e41b1597284b878dcb69253259f00bb0d43503f Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Tue, 28 Apr 2026 20:41:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=B5=81=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA:=20=E4=B8=8D=E7=AE=A1mixin/=E9=9D=9Emixin?= =?UTF-8?q?=E4=B8=AD=E9=80=94=E6=96=AD=E8=BF=9E=E9=83=BD=E9=87=8D=E8=AF=95?= =?UTF-8?q?;=20=E6=9B=B4=E6=96=B0ga.py=E9=94=99=E8=AF=AF=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ga.py | 2 +- llmcore.py | 23 +++++------------------ 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/ga.py b/ga.py index d66dca94..4b8456ad 100644 --- a/ga.py +++ b/ga.py @@ -447,7 +447,7 @@ class GenericAgentHandler(BaseHandler): if not response or (not content.strip() and not thinking.strip()): yield "[Warn] LLM returned an empty response. Retrying...\n" return StepOutcome({}, next_prompt="[System] Blank response, regenerate and tooluse") - if len(content) > 50 and ('未收到完整响应 !!!]' in content[-100:] or '!!!Error: [SSL:' in content[-100:]): + if len(content) > 50 and ('[!!! 流异常中断' in content[-100:] or '!!!Error:' in content[-100:]): return StepOutcome({}, next_prompt="[System] Incomplete response. Regenerate and tooluse.") if 'max_tokens !!!]' in content[-100:]: return StepOutcome({}, next_prompt="[System] max_tokens limit reached. Use multi small steps to do it.") diff --git a/llmcore.py b/llmcore.py index 982954c0..6e65b1b6 100644 --- a/llmcore.py +++ b/llmcore.py @@ -374,14 +374,14 @@ def _stream_with_retry(sess, url, headers, payload, parse_fn): while True: streamed = True; yield next(gen) except StopIteration as e: return e.value or [] except (requests.Timeout, requests.ConnectionError) as e: - if attempt < sess.max_retries and not streamed: + err = f"!!!Error: {type(e).__name__}" + if attempt < sess.max_retries: d = _delay(None, attempt) print(f"[LLM Retry] {type(e).__name__}, retry in {d:.1f}s ({attempt+1}/{sess.max_retries+1})") - time.sleep(d); continue - err = f"!!!Error: {type(e).__name__}" + yield err; time.sleep(d); continue yield err; return [{"type": "text", "text": err}] except Exception as e: - err = f"!!!Error: {type(e).__name__}: {e}" + err = f"\n\n[!!! 流异常中断 {type(e).__name__}: {e} !!!]" if streamed else f"!!!Error: {type(e).__name__}: {e}" yield err; return [{"type": "text", "text": err}] def _openai_stream(sess, messages): @@ -746,19 +746,6 @@ class ToolClient: _write_llm_log('Response', raw_text) return self._parse_mixed_response(raw_text) - def _estimate_content_len(self, content): - if isinstance(content, str): return len(content) - if isinstance(content, list): - total = 0 - for part in content: - if not isinstance(part, dict): continue - if part.get("type") == "text": - total += len(part.get("text", "")) - elif part.get("type") == "image_url": - total += 1000 - return total - return len(str(content)) - def _prepare_tool_instruction(self, tools): tool_instruction = "" if not tools: return tool_instruction @@ -799,7 +786,7 @@ Follow these steps to think and act: user += f"=== {role} ===\n" for tr in m.get('tool_results', []): user += f'{tr["content"]}\n' user += str(m['content']) + "\n" - self.total_cd_tokens += self._estimate_content_len(user) + self.total_cd_tokens += len(user) // 3 if self.total_cd_tokens > 9000: self.last_tools = '' user += "=== ASSISTANT ===\n" return system + user