V10.567 收斂 MCP 市場洞察 fallback
All checks were successful
CD Pipeline / deploy (push) Successful in 1m6s

This commit is contained in:
OoO
2026-06-02 10:09:36 +08:00
parent 462e354a71
commit d90f96fab3
5 changed files with 45 additions and 8 deletions

View File

@@ -33,10 +33,13 @@ from services.gemini_guard import (
logger = logging.getLogger(__name__)
MCP_CACHE_TTL_HOURS = int(os.getenv("MCP_CACHE_TTL_HOURS", "24"))
# MCP router 是即時情報主路徑router 不可用時先走 Ollama 三主機級聯做離線洞察
# MCP router 是即時情報主路徑router 不可用時先走 GCP Ollama 做離線洞察
# 市場洞察屬非即時必需批次工作,不把長分析轉嫁到 111 fallback。
# Gemini Grounding 僅作最後備援,避免再次回到 Gemini-first。
MCP_MODEL = os.getenv("MCP_GEMINI_MODEL", "gemini-2.0-flash")
MCP_FALLBACK_MODEL = "gemini-1.5-flash"
MCP_OLLAMA_TIMEOUT = int(os.getenv("MCP_OLLAMA_TIMEOUT", "25"))
MCP_OLLAMA_NUM_PREDICT = int(os.getenv("MCP_OLLAMA_NUM_PREDICT", "500"))
try:
from services.ollama_service import OllamaService
@@ -266,11 +269,11 @@ class MCPCollectorService:
return self._fallback_topic_content(topic, f"即時外部搜尋暫不可用:{type(e).__name__}")
def _ollama_topic_fallback(self, topic: str, query: str) -> Optional[str]:
"""MCP/搜尋不可用時先走 Ollama 三主機級聯Gemini 只留最後備援"""
"""MCP/搜尋不可用時先走 GCP Ollama111 不承接市場洞察長任務"""
if not _OLLAMA_AVAILABLE:
return None
try:
logger.info("[MCP] Using GCP-A/GCP-B/111 Ollama for market insight fallback topic=%s", topic)
logger.info("[MCP] Using GCP-A/GCP-B Ollama for market insight fallback topic=%s", topic)
ollama_model = os.getenv('OPENCLAW_OLLAMA_MODEL', 'qwen2.5-coder:7b')
ollama_prompt = (
f"你是一位精通台灣電商市場的分析師。目前無法取得即時搜尋結果,"
@@ -282,15 +285,16 @@ class MCPCollectorService:
prompt=ollama_prompt,
model=ollama_model,
temperature=0.4,
timeout=45,
options={'num_predict': 800},
timeout=MCP_OLLAMA_TIMEOUT,
options={'num_predict': MCP_OLLAMA_NUM_PREDICT},
allow_111_fallback=False,
)
content = (resp.content or '').strip() if resp.success else ''
if content and not self._looks_unreliable(content):
# 不進快取,因為這是預測性內容。
return content
if not resp.success:
logger.warning("[MCP] Ollama cascade fallback failed: %s", resp.error)
logger.warning("[MCP] GCP Ollama fallback failed: %s", resp.error)
except Exception as exc:
logger.warning("[MCP] Ollama fallback failed: %s", exc)
return None