jay0826 commited on
Commit
0d68cd2
·
verified ·
1 Parent(s): 61ad2cc

更新 2 個檔(discordbot.py、recent.npz)

Browse files
Files changed (2) hide show
  1. data/echo/recent.npz +2 -2
  2. discordbot.py +54 -9
data/echo/recent.npz CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3470e00e06cbb951658a2478752db9d508696733a9c606dae3439473e65ea8b6
3
- size 286913
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7bee63931d753aea8ec5760d2c785e5498bdb9982f4cbb3a0bbfbd98c7dda9f7
3
+ size 286735
discordbot.py CHANGED
@@ -1608,6 +1608,27 @@ def set_google_search_cooldown(seconds: int = GOOGLE_SEARCH_COOLDOWN_SECONDS) ->
1608
  google_search_cooldown_until = max(google_search_cooldown_until, time.time() + max(5, int(seconds)))
1609
 
1610
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1611
  def is_rate_limit_error(err: str) -> bool:
1612
  e = (err or "").lower()
1613
  return "429" in e or "rate limit" in e or "resource_exhausted" in e or "quota" in e
@@ -3934,6 +3955,8 @@ def ask_gemini_raw(
3934
  _PARAM_REJECTED.add((plan.model, "thinking:" + str(_t_unsure)))
3935
  print(f"[Gemini] {plan.model} 拿掉 thinking_level={_t_unsure} 就過了 → 記住,重開前不再送", flush=True)
3936
  _t_unsure = None
 
 
3937
  if tool_active and not grounding_src_line:
3938
  grounding_src_line = _grounding_sources_line(response)
3939
 
@@ -3987,9 +4010,10 @@ def ask_gemini_raw(
3987
  # ★2026-11-14 帶 Google 搜尋工具的那一發回 429、又沒講是哪一種上限:多半是搜尋工具的額度(新模型免費層不一定給),
3988
  # 不是這支模型的。以前照樣把「金鑰×模型」關 23 小時、換下一把再帶工具撞一次 → 兩把金鑰一起沒了。
3989
  # → 搜尋工具進冷卻、這次改不帶工具再試;金鑰不冷卻。真的是模型額度的話,不帶工具那發會再回 429,照下面處理。
3990
- set_google_search_cooldown()
3991
  plan.use_tools = False
3992
- print(f"[Gemini] {plan.model} 帶搜尋工具回 429(沒講是哪一種上限)→ 搜尋工具先冷卻,這次不帶工具再試", flush=True)
 
3993
  continue
3994
 
3995
  if is_rate_limit_error(err):
@@ -4112,7 +4136,9 @@ def answer_with_web_fallback(
4112
  mime_type: Optional[str] = None,
4113
  force_tools: bool = False,
4114
  model_override: Optional[str] = None,
 
4115
  ) -> str:
 
4116
  user_query = (user_query or "").strip()
4117
 
4118
  if is_code_question(user_query):
@@ -4183,6 +4209,10 @@ def answer_with_web_fallback(
4183
  print(f"[搜尋兜底] {user_query[:30]}:{';'.join(_trace)} → 直接列查到的", flush=True)
4184
  return ("主人,香草查到了下面這些,但整理的時候模型沒回好喵,先把查到的原樣給主人:\n" + heads[:1500])
4185
 
 
 
 
 
4186
  # 無搜尋或搜尋失敗 → 直接用 Gemini 回答
4187
  print(f"[搜尋兜底] {user_query[:30]}:{';'.join(_trace) or '不用搜尋'} → 不帶搜尋直接答", flush=True)
4188
  return ask_gemini_raw(full_context, image_bytes=image_bytes, mime_type=mime_type, use_tools=False, model_override=model_override)
@@ -15981,13 +16011,28 @@ async def on_message(message: discord.Message):
15981
  print(f"[搜尋] 回答說得像查過、但沒有來源 → 補搜:{(text or '')[:30]}", flush=True)
15982
  if (not reply or looks_insufficient(reply) or _claims
15983
  or (should_force_web_search(text or "") and admits_no_live_info(reply))) and not url_context:
15984
- reply = await asyncio.wait_for(
15985
- asyncio.to_thread(
15986
- answer_with_web_fallback,
15987
- text, context_prompt, image_bytes, mime_type, _search_first or _claims, _model_override,
15988
- ),
15989
- timeout=45.0,
15990
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15991
  except asyncio.TimeoutError:
15992
  reply = "主人,香草這次等太久了喵! 可能是搜尋服務在忙,請稍後再試一次喵~"
15993
  finally:
 
1608
  google_search_cooldown_until = max(google_search_cooldown_until, time.time() + max(5, int(seconds)))
1609
 
1610
 
1611
+ _GS_429: Dict[str, float] = {"n": 0, "last": 0.0}
1612
+ _GS_429_STEPS = (180, 600, 1800, 3600, 10800)
1613
+
1614
+
1615
+ def google_search_429_cooldown() -> int:
1616
+ """★2026-11-15 搜尋工具回 429:連續撞到就拉長冷卻(以前固定 3 分鐘,聊天第一發開始帶工具後,每 3 分鐘就有一則先白撞一次)。
1617
+ 6 小時沒撞過就從頭算;帶工具成功一次也從頭算(google_search_ok)。回這次冷卻幾秒。"""
1618
+ now = time.time()
1619
+ if now - _GS_429["last"] > 6 * 3600:
1620
+ _GS_429["n"] = 0
1621
+ _GS_429["n"] += 1
1622
+ _GS_429["last"] = now
1623
+ secs = _GS_429_STEPS[min(int(_GS_429["n"]) - 1, len(_GS_429_STEPS) - 1)]
1624
+ set_google_search_cooldown(secs)
1625
+ return secs
1626
+
1627
+
1628
+ def google_search_ok() -> None:
1629
+ _GS_429["n"] = 0
1630
+
1631
+
1632
  def is_rate_limit_error(err: str) -> bool:
1633
  e = (err or "").lower()
1634
  return "429" in e or "rate limit" in e or "resource_exhausted" in e or "quota" in e
 
3955
  _PARAM_REJECTED.add((plan.model, "thinking:" + str(_t_unsure)))
3956
  print(f"[Gemini] {plan.model} 拿掉 thinking_level={_t_unsure} 就過了 → 記住,重開前不再送", flush=True)
3957
  _t_unsure = None
3958
+ if tool_active:
3959
+ google_search_ok()
3960
  if tool_active and not grounding_src_line:
3961
  grounding_src_line = _grounding_sources_line(response)
3962
 
 
4010
  # ★2026-11-14 帶 Google 搜尋工具的那一發回 429、又沒講是哪一種上限:多半是搜尋工具的額度(新模型免費層不一定給),
4011
  # 不是這支模型的。以前照樣把「金鑰×模型」關 23 小時、換下一把再帶工具撞一次 → 兩把金鑰一起沒了。
4012
  # → 搜尋工具進冷卻、這次改不帶工具再試;金鑰不冷卻。真的是模型額度的話,不帶工具那發會再回 429,照下面處理。
4013
+ _gs_s = google_search_429_cooldown()
4014
  plan.use_tools = False
4015
+ print(f"[Gemini] {plan.model} 帶搜尋工具回 429(沒講是哪一種上限)→ 搜尋工具冷卻 {_gs_s // 60} 分鐘"
4016
+ f"(連續第 {int(_GS_429['n'])} 次),這次不帶工具再試", flush=True)
4017
  continue
4018
 
4019
  if is_rate_limit_error(err):
 
4136
  mime_type: Optional[str] = None,
4137
  force_tools: bool = False,
4138
  model_override: Optional[str] = None,
4139
+ prev_reply: str = "",
4140
  ) -> str:
4141
+ """prev_reply:聊天第一發的回答(★2026-11-15)。不用搜尋時就用它,不再把同一題問第二次。"""
4142
  user_query = (user_query or "").strip()
4143
 
4144
  if is_code_question(user_query):
 
4209
  print(f"[搜尋兜底] {user_query[:30]}:{';'.join(_trace)} → 直接列查到的", flush=True)
4210
  return ("主人,香草查到了下面這些,但整理的時候模型沒回好喵,先把查到的原樣給主人:\n" + heads[:1500])
4211
 
4212
+ # ★2026-11-15 不用搜尋、第一發也有回答 → 用它(以前同一題再問一次,碰上塞車就超過 45 秒、整則變「等太久」)
4213
+ if not need_search and (prev_reply or "").strip():
4214
+ print(f"[搜尋兜底] {user_query[:30]}:不用搜尋 → 用第一次的回答", flush=True)
4215
+ return prev_reply
4216
  # 無搜尋或搜尋失敗 → 直接用 Gemini 回答
4217
  print(f"[搜尋兜底] {user_query[:30]}:{';'.join(_trace) or '不用搜尋'} → 不帶搜尋直接答", flush=True)
4218
  return ask_gemini_raw(full_context, image_bytes=image_bytes, mime_type=mime_type, use_tools=False, model_override=model_override)
 
16011
  print(f"[搜尋] 回答說得像查過、但沒有來源 → 補搜:{(text or '')[:30]}", flush=True)
16012
  if (not reply or looks_insufficient(reply) or _claims
16013
  or (should_force_web_search(text or "") and admits_no_live_info(reply))) and not url_context:
16014
+ _first = reply
16015
+ try:
16016
+ _fb = await asyncio.wait_for(
16017
+ asyncio.to_thread(
16018
+ answer_with_web_fallback,
16019
+ text, context_prompt, image_bytes, mime_type, _search_first or _claims, _model_override,
16020
+ _first,
16021
+ ),
16022
+ timeout=45.0,
16023
+ )
16024
+ except asyncio.TimeoutError:
16025
+ _fb = ""
16026
+ print(f"[搜尋兜底] {(text or '')[:30]}:超過 45 秒" + (",用第一次的回答" if _first else ""), flush=True)
16027
+ # ★2026-11-15 兜底逾時/失敗:第一次有回答就用它(以前整則丟掉、回「等太久」)
16028
+ if _fb and not _RV_LLM_FAIL_RE.match(_fb):
16029
+ reply = _fb
16030
+ elif _first:
16031
+ reply = _first + ("\n-# 想上網補查但沒成功,上面這段沒有查證、可能不是最新的" if _claims else "")
16032
+ elif _fb:
16033
+ reply = _fb
16034
+ else:
16035
+ raise asyncio.TimeoutError
16036
  except asyncio.TimeoutError:
16037
  reply = "主人,香草這次等太久了喵! 可能是搜尋服務在忙,請稍後再試一次喵~"
16038
  finally: