chore(align): sync Git with running container as authoritative source
One-time alignment to close drift between the running container (qihu-aiflow / image qihuaiflow:with-deps) and this Git repository. Container hotfixes previously persisted only via `docker commit` are now captured in Git so Git becomes the single source of truth. Backend: - chat_service.py: accumulated fixes incl. EigenFlux README guidance - chat_context.py / chat_media.py: URL/web-repo context injection - api/url_reader.py: NEW SSRF-safe web/README reader (Issue #725) - skills_admin.py & Skills.tsx: 14-scene capability unification - pgvector_search.py, orchestrator.py, platform_knowledge.py, market.py, admin/models.py: drift sync Frontend: - ChatPage.tsx: collapsible AI think/act block (Issue #724) - MarketPage / EvolutionDashboard / TokenDashboard styling sync Migrations: - 018_normalize_marketapp_scene.sql: fixed capability allowlist (prevents capability wipe on restart) Alignment performed 2026-07-23 22:42:43. No deletions; secrets/venv/node_modules/ caches were not touched (tracked-source-only copy).
This commit is contained in:
1 parent
a1fd8b074d
commit
26075272d6
1 file changed
+283
-234
+283
-234
@@ -1,234 +1,283 @@
|
|||||||
"""
|
"""
|
||||||
IM Bot 真实 LLM 调用。
|
IM Bot 真实 LLM 调用。
|
||||||
|
|
||||||
原实现位于 channels.py(通用频道管理模块),该模块已移除。
|
原实现位于 channels.py(通用频道管理模块),该模块已移除。
|
||||||
为保持 IM Bot(微信机器人等)正常生成回复,将 _call_real_llm 迁移至此独立模块。
|
为保持 IM Bot(微信机器人等)正常生成回复,将 _call_real_llm 迁移至此独立模块。
|
||||||
|
|
||||||
行为(对齐 AI 聊天):
|
行为(对齐 AI 聊天 — 2026-07-23 同步至最新):
|
||||||
- Issue 7:图像/视频/语音意图优先走平台媒体模型(与 AI 聊天同一套 Auto 路由),
|
- Issue 7:图像/视频/语音意图优先走平台媒体模型(与 AI 聊天同一套 Auto 路由),
|
||||||
不再对"制作图片"这类请求只回文本。
|
不再对"制作图片"这类请求只回文本。
|
||||||
- Issue 8:默认 Auto 模型;每轮先自动发现本账号已安装且平台已部署的技能,
|
- Issue 8:默认 Auto 模型;每轮先自动发现本账号已安装且平台已部署的技能,
|
||||||
多个适用时由模型自行判断选 1 个最优,通过 function calling 执行。
|
多个适用时由模型自行判断选 1 个最优,通过 function calling 执行。
|
||||||
- 2026-07-15:修复 trust_env=False + proxy 处理,避免容器坏代理导致调用失败;
|
- 2026-07-15:修复 trust_env=False + proxy 处理,避免容器坏代理导致调用失败;
|
||||||
媒体生成前先经 _optimize_media_prompt 精炼提示词,提升 AGNES 生成质量。
|
媒体生成前先经 _optimize_media_prompt 精炼提示词,提升 AGNES 生成质量。
|
||||||
"""
|
- 2026-07-23:对齐 Web Chat 最新能力:
|
||||||
import asyncio
|
* 模型 failover(Auto 模式自动切换备选模型)
|
||||||
import json
|
* 平台知识库注入(get_platform_knowledge,技能清单实时同步)
|
||||||
import logging
|
"""
|
||||||
import re as _re
|
import asyncio
|
||||||
from datetime import datetime, timezone, timedelta
|
import json
|
||||||
from typing import Optional
|
import logging
|
||||||
from urllib.parse import urlparse as _urlparse
|
import re as _re
|
||||||
|
from datetime import datetime, timezone, timedelta
|
||||||
import httpx
|
from typing import Optional
|
||||||
from sqlalchemy.orm import Session
|
from urllib.parse import urlparse as _urlparse
|
||||||
|
|
||||||
from ..capability_registry import resolve_auto_model
|
import httpx
|
||||||
from ..models import AIModel, ModelProvider, TokenUsageLog, gen_uuid
|
from sqlalchemy.orm import Session
|
||||||
from app.core.observability import get_trace_id
|
|
||||||
|
from ..capability_registry import resolve_auto_model
|
||||||
logger = logging.getLogger(__name__)
|
from ..models import AIModel, ModelProvider, TokenUsageLog, gen_uuid
|
||||||
|
from app.core.observability import get_trace_id
|
||||||
|
|
||||||
# 香港出网代理(实测对 AGNES 延迟更低,2026-07-15 用户实测),与 chat.py 同值。
|
logger = logging.getLogger(__name__)
|
||||||
_HK_PROXY = "http://qyyfw:XHR521yhf@103.242.14.208:11495"
|
|
||||||
|
|
||||||
|
# 香港出网代理(实测对 AGNES 延迟更低,2026-07-15 用户实测),与 chat.py 同值。
|
||||||
def _proxy_for_url(url: str) -> str | None:
|
_HK_PROXY = "http://qyyfw:XHR521yhf@103.242.14.208:11495"
|
||||||
"""按目标域名选择是否走代理(与 chat.py 同逻辑)。
|
|
||||||
|
|
||||||
2026-07-15 调整:AGNES 改走香港代理(实测延迟更低),不再直连。
|
def _proxy_for_url(url: str) -> str | None:
|
||||||
国内域名(尊重 NO_PROXY)直连,更稳定。
|
"""按目标域名选择是否走代理(与 chat.py 同逻辑)。
|
||||||
"""
|
|
||||||
import os
|
2026-07-15 调整:AGNES 改走香港代理(实测延迟更低),不再直连。
|
||||||
host = (_urlparse(url).hostname or "").lower()
|
国内域名(尊重 NO_PROXY)直连,更稳定。
|
||||||
if "agnes-ai.com" in host or "agnes-ai.space" in host:
|
"""
|
||||||
return _HK_PROXY
|
import os
|
||||||
if "qihuyun.com" in host:
|
host = (_urlparse(url).hostname or "").lower()
|
||||||
return None
|
if "agnes-ai.com" in host or "agnes-ai.space" in host:
|
||||||
_no_proxy = [h.strip().lower() for h in os.environ.get("NO_PROXY", "").split(",") if h.strip()]
|
return _HK_PROXY
|
||||||
for _np in _no_proxy:
|
if "qihuyun.com" in host:
|
||||||
if host == _np or host.endswith("." + _np) or _np in host:
|
return None
|
||||||
return None
|
_no_proxy = [h.strip().lower() for h in os.environ.get("NO_PROXY", "").split(",") if h.strip()]
|
||||||
return os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") or "http://172.18.0.1:7890"
|
for _np in _no_proxy:
|
||||||
|
if host == _np or host.endswith("." + _np) or _np in host:
|
||||||
|
return None
|
||||||
def _run_async(coro, timeout=None):
|
return os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") or "http://172.18.0.1:7890"
|
||||||
"""在同步上下文中运行协程,兼容「无事件循环」与「已有事件循环」两种场景。
|
|
||||||
|
|
||||||
IM 个人微信在独立线程中调用(无事件循环)→ asyncio.run;
|
def _run_async(coro, timeout=None):
|
||||||
IM 多平台 Webhook 可能在 FastAPI 协程内调用(已有事件循环)→ 丢到线程池执行。
|
"""在同步上下文中运行协程,兼容「无事件循环」与「已有事件循环」两种场景。
|
||||||
|
|
||||||
timeout: 可选,秒数;超时抛出 asyncio.TimeoutError(由调用方捕获)。
|
IM 个人微信在独立线程中调用(无事件循环)→ asyncio.run;
|
||||||
"""
|
IM 多平台 Webhook 可能在 FastAPI 协程内调用(已有事件循环)→ 丢到线程池执行。
|
||||||
try:
|
|
||||||
loop = asyncio.get_running_loop()
|
timeout: 可选,秒数;超时抛出 asyncio.TimeoutError(由调用方捕获)。
|
||||||
except RuntimeError:
|
"""
|
||||||
loop = None
|
try:
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
if timeout is not None:
|
except RuntimeError:
|
||||||
async def _timed():
|
loop = None
|
||||||
return await asyncio.wait_for(coro, timeout=timeout)
|
|
||||||
target = _timed()
|
if timeout is not None:
|
||||||
else:
|
async def _timed():
|
||||||
target = coro
|
return await asyncio.wait_for(coro, timeout=timeout)
|
||||||
|
target = _timed()
|
||||||
if loop is not None and loop.is_running():
|
else:
|
||||||
import concurrent.futures
|
target = coro
|
||||||
with concurrent.futures.ThreadPoolExecutor(1) as ex:
|
|
||||||
return ex.submit(lambda: asyncio.run(target)).result(timeout=(timeout + 10) if timeout else None)
|
if loop is not None and loop.is_running():
|
||||||
return asyncio.run(target)
|
import concurrent.futures
|
||||||
|
with concurrent.futures.ThreadPoolExecutor(1) as ex:
|
||||||
|
return ex.submit(lambda: asyncio.run(target)).result(timeout=(timeout + 10) if timeout else None)
|
||||||
def _log_media_usage(db: Session, user_id: str, ai_model, media_type: str, prompt: str):
|
return asyncio.run(target)
|
||||||
"""IM Bot 媒体生成记账:把图片/视频消耗写入 TokenUsageLog,供驾驶舱统计。
|
|
||||||
|
|
||||||
此前 IM Bot 路径完全不写 TokenUsageLog,导致微信生成的图片/视频在驾驶舱里“查无此项”。
|
def _log_media_usage(db: Session, user_id: str, ai_model, media_type: str, prompt: str):
|
||||||
媒体 API 不返回 token 用量,这里用与 chat.py _generate_media_task 一致的估算。
|
"""IM Bot 媒体生成记账:把图片/视频消耗写入 TokenUsageLog,供驾驶舱统计。
|
||||||
"""
|
|
||||||
if not user_id or not ai_model:
|
此前 IM Bot 路径完全不写 TokenUsageLog,导致微信生成的图片/视频在驾驶舱里"查无此项"。
|
||||||
return
|
媒体 API 不返回 token 用量,这里用与 chat.py _generate_media_task 一致的估算。
|
||||||
try:
|
"""
|
||||||
base = 1200 if media_type == "video" else 400
|
if not user_id or not ai_model:
|
||||||
est = base + (len(prompt or "") // 4)
|
return
|
||||||
db.add(TokenUsageLog(
|
try:
|
||||||
id=gen_uuid(),
|
base = 1200 if media_type == "video" else 400
|
||||||
user_id=user_id,
|
est = base + (len(prompt or "") // 4)
|
||||||
model_id=ai_model.id,
|
db.add(TokenUsageLog(
|
||||||
model_name=ai_model.name,
|
id=gen_uuid(),
|
||||||
trace_id=get_trace_id(),
|
user_id=user_id,
|
||||||
prompt_tokens=est,
|
model_id=ai_model.id,
|
||||||
completion_tokens=0,
|
model_name=ai_model.name,
|
||||||
total_tokens=est,
|
trace_id=get_trace_id(),
|
||||||
created_at=datetime.now(timezone(timedelta(hours=8))).replace(tzinfo=None),
|
prompt_tokens=est,
|
||||||
))
|
completion_tokens=0,
|
||||||
db.commit()
|
total_tokens=est,
|
||||||
except Exception as e:
|
created_at=datetime.now(timezone(timedelta(hours=8))).replace(tzinfo=None),
|
||||||
try:
|
))
|
||||||
db.rollback()
|
db.commit()
|
||||||
except Exception:
|
except Exception as e:
|
||||||
pass
|
try:
|
||||||
logger.warning(f"[im_llm] 媒体 Token 记账失败(忽略): {e}")
|
db.rollback()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
def _call_real_llm(db: Session, user=None, prompt: str = "", platform: str = "im", user_id: str = None) -> "str | dict":
|
logger.warning(f"[im_llm] 媒体 Token 记账失败(忽略): {e}")
|
||||||
"""调用七虎AI 生成回复,供 IM Bot 复用。支持媒体意图路由与已安装技能自动发现。"""
|
|
||||||
try:
|
|
||||||
from app.api.chat import (
|
def _call_real_llm(db: Session, user=None, prompt: str = "", platform: str = "im", user_id: str = None) -> "str | dict":
|
||||||
_generate_media,
|
"""调用七虎AI 生成回复,供 IM Bot 复用。支持媒体意图路由、已安装技能自动发现、模型 failover。"""
|
||||||
detect_media_intent,
|
try:
|
||||||
_discover_installed_skills,
|
from app.api.chat import (
|
||||||
_build_system_prompt,
|
_generate_media,
|
||||||
_execute_skill_tool,
|
detect_media_intent,
|
||||||
)
|
_discover_installed_skills,
|
||||||
|
_build_system_prompt,
|
||||||
# ===== 1) 媒体意图路由(Issue 7)=====
|
_execute_skill_tool,
|
||||||
intent = detect_media_intent(prompt)
|
)
|
||||||
if intent:
|
|
||||||
ai_model = resolve_auto_model(intent, db)
|
# ===== 1) 媒体意图路由(Issue 7)=====
|
||||||
if ai_model:
|
intent = detect_media_intent(prompt)
|
||||||
try:
|
if intent:
|
||||||
result = _run_async(_generate_media(db, ai_model, prompt, user_id), timeout=300)
|
ai_model = resolve_auto_model(intent, db)
|
||||||
except Exception:
|
if ai_model:
|
||||||
# 任何未预期异常(如 AGNES 连接重置)统一兜底为友好提示,
|
try:
|
||||||
# 绝不直接把原始异常(如 "Server disconnected")甩给用户
|
result = _run_async(_generate_media(db, ai_model, prompt, user_id), timeout=300)
|
||||||
result = "⚠️ 媒体生成暂时失败(连接或服务波动),请稍后重试。"
|
except Exception:
|
||||||
if result:
|
# 任何未预期异常(如 AGNES 连接重置)统一兜底为友好提示,
|
||||||
# 媒体生成成功:返回结构化对象,交由 im_bot_user 在微信内联发送图片/视频
|
# 绝不直接把原始异常(如 "Server disconnected")甩给用户
|
||||||
s = result.strip()
|
result = "⚠️ 媒体生成暂时失败(连接或服务波动),请稍后重试。"
|
||||||
if s.startswith("<img") or s.startswith("<video"):
|
if result:
|
||||||
m = _re.search(r'src="([^"]+)"', s)
|
# 媒体生成成功:返回结构化对象,交由 im_bot_user 在微信内联发送图片/视频
|
||||||
url = m.group(1) if m else ""
|
s = result.strip()
|
||||||
media_type = "image" if s.startswith("<img") else "video"
|
if s.startswith("<img") or s.startswith("<video"):
|
||||||
if not url:
|
m = _re.search(r'src="([^"]+)"', s)
|
||||||
return "(生成完成,但未能提取媒体地址)"
|
url = m.group(1) if m else ""
|
||||||
# IM Bot 路径此前完全不记账 → 补写媒体消耗,驾驶舱才看得到图片/视频数据
|
media_type = "image" if s.startswith("<img") else "video"
|
||||||
_log_media_usage(db, user_id, ai_model, media_type, prompt)
|
if not url:
|
||||||
return {"__media__": True, "media_type": media_type, "url": url}
|
return "(生成完成,但未能提取媒体地址)"
|
||||||
# 媒体生成失败:_generate_media 已返回友好中文(如限频/队列繁忙),
|
# IM Bot 路径此前完全不记账 → 补写媒体消耗,驾驶舱才看得到图片/视频数据
|
||||||
# 直接透传,绝不把原始提示词/标签/JSON 透传给用户
|
_log_media_usage(db, user_id, ai_model, media_type, prompt)
|
||||||
if s.startswith("⚠️") or s.startswith("(媒体"):
|
return {"__media__": True, "media_type": media_type, "url": url}
|
||||||
return s
|
# 媒体生成失败:_generate_media 已返回友好中文(如限频/队列繁忙),
|
||||||
return "(媒体生成未能返回有效内容,请重试)"
|
# 直接透传,绝不把原始提示词/标签/JSON 透传给用户
|
||||||
|
if s.startswith("⚠️") or s.startswith("(媒体"):
|
||||||
# ===== 2) 文本回复 + 技能自动发现(Issue 8)=====
|
return s
|
||||||
ai_model = resolve_auto_model("text", db)
|
return "(媒体生成未能返回有效内容,请重试)"
|
||||||
if not ai_model:
|
|
||||||
return "(未配置默认文本模型,无法生成回复)"
|
# ===== 2) 文本回复:模型 failover + 技能自动发现 + 平台知识库注入 =====
|
||||||
provider = db.query(ModelProvider).filter(ModelProvider.id == ai_model.provider_id).first()
|
from ..platform_knowledge import get_platform_knowledge
|
||||||
api_key = ai_model.api_key or (provider.api_key if provider else None)
|
|
||||||
api_url = ai_model.endpoint
|
# 构建候选模型列表(对齐 Web Chat failover)
|
||||||
if not api_url and provider and provider.api_base_url:
|
ai_model = resolve_auto_model("text", db)
|
||||||
api_url = f"{provider.api_base_url.rstrip('/')}/v1/chat/completions"
|
_try_models = [ai_model] if (ai_model and ai_model.is_enabled) else []
|
||||||
if not (api_key and api_url):
|
# Auto 模式附加备选(IM Bot 始终 Auto)
|
||||||
return "(模型未配置 API Key 或地址,无法生成回复)"
|
_fallbacks = db.query(AIModel).filter(
|
||||||
|
AIModel.is_enabled == True,
|
||||||
# 自动发现本账号已安装且平台已部署的技能
|
AIModel.id != (ai_model.id if ai_model else ""),
|
||||||
skills = []
|
).order_by(AIModel.is_default.desc()).limit(4).all()
|
||||||
if user_id:
|
_try_models.extend([m for m in _fallbacks if m not in _try_models])
|
||||||
try:
|
|
||||||
skills = _discover_installed_skills(db, user_id)[:15]
|
if not _try_models:
|
||||||
except Exception:
|
return "(未配置可用文本模型,无法生成回复)"
|
||||||
skills = []
|
|
||||||
|
# 技能自动发现(提前执行一次,不必每个候选模型重复做)
|
||||||
system_prompt, function_definitions = _build_system_prompt(
|
skills = []
|
||||||
skills=skills,
|
if user_id:
|
||||||
agents=[],
|
try:
|
||||||
kb_context="",
|
skills = _discover_installed_skills(db, user_id)[:15]
|
||||||
user_content=prompt,
|
except Exception:
|
||||||
mcp=[],
|
skills = []
|
||||||
auto_discovered=bool(skills),
|
|
||||||
)
|
# 平台知识库注入(对齐 Web Chat——微信用户提问"平台有哪些技能"也能得到完整清单)
|
||||||
messages = [
|
kb_context = ""
|
||||||
{"role": "system", "content": system_prompt},
|
try:
|
||||||
{"role": "user", "content": prompt},
|
pk = get_platform_knowledge(db=db)
|
||||||
]
|
if pk:
|
||||||
|
kb_context = f"【平台知识库——优先使用以下知识回答用户问题】\n{pk}"
|
||||||
max_rounds = 3
|
except Exception:
|
||||||
ai_content = ""
|
pass
|
||||||
for _ in range(max_rounds):
|
|
||||||
request_body = {"model": ai_model.name, "messages": messages, "max_tokens": 2048}
|
# 尝试每个候选模型
|
||||||
if function_definitions and skills:
|
_retry_log = []
|
||||||
request_body["tools"] = [
|
for _try_model in _try_models:
|
||||||
{"type": "function", "function": fd} for fd in function_definitions
|
ai_model = _try_model
|
||||||
]
|
|
||||||
request_body["tool_choice"] = "auto"
|
provider = db.query(ModelProvider).filter(ModelProvider.id == ai_model.provider_id).first()
|
||||||
with httpx.Client(
|
api_key = ai_model.api_key or (provider.api_key if provider else None)
|
||||||
timeout=ai_model.timeout or 120,
|
api_url = ai_model.endpoint
|
||||||
proxy=_proxy_for_url(api_url),
|
if not api_url and provider and provider.api_base_url:
|
||||||
trust_env=False,
|
api_url = f"{provider.api_base_url.rstrip('/')}/v1/chat/completions"
|
||||||
) as client:
|
if not (api_key and api_url):
|
||||||
resp = client.post(
|
_retry_log.append(f"{ai_model.name}: 缺少密钥或端点")
|
||||||
api_url,
|
continue
|
||||||
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
|
|
||||||
json=request_body,
|
system_prompt, function_definitions = _build_system_prompt(
|
||||||
)
|
skills=skills,
|
||||||
if resp.status_code != 200:
|
agents=[],
|
||||||
return f"(AI 模型调用失败 HTTP {resp.status_code})"
|
kb_context=kb_context,
|
||||||
data = resp.json()
|
user_content=prompt,
|
||||||
message = data.get("choices", [{}])[0].get("message", {})
|
mcp=[],
|
||||||
tool_calls = message.get("tool_calls", [])
|
auto_discovered=bool(skills),
|
||||||
if tool_calls and skills:
|
)
|
||||||
messages.append({
|
messages = [
|
||||||
"role": "assistant",
|
{"role": "system", "content": system_prompt},
|
||||||
"content": message.get("content") or "",
|
{"role": "user", "content": prompt},
|
||||||
"tool_calls": tool_calls,
|
]
|
||||||
})
|
|
||||||
for tc in tool_calls:
|
# 尝试调用
|
||||||
func_name = tc.get("function", {}).get("name", "")
|
try:
|
||||||
try:
|
max_rounds = 3
|
||||||
func_args = json.loads(tc.get("function", {}).get("arguments", "{}"))
|
ai_content = ""
|
||||||
except Exception:
|
_http_ok = True
|
||||||
func_args = {"input": tc.get("function", {}).get("arguments", "")}
|
for round_num in range(max_rounds):
|
||||||
tool_result = _execute_skill_tool(db, func_name, func_args, skills, prompt)
|
request_body = {"model": ai_model.name, "messages": messages, "max_tokens": 2048}
|
||||||
messages.append({
|
if function_definitions and skills:
|
||||||
"role": "tool",
|
request_body["tools"] = [
|
||||||
"tool_call_id": tc.get("id", "call_1"),
|
{"type": "function", "function": fd} for fd in function_definitions
|
||||||
"content": tool_result,
|
]
|
||||||
})
|
request_body["tool_choice"] = "auto"
|
||||||
continue
|
with httpx.Client(
|
||||||
ai_content = message.get("content", "")
|
timeout=ai_model.timeout or 120,
|
||||||
break
|
proxy=_proxy_for_url(api_url),
|
||||||
return ai_content or "(AI 回复为空)"
|
trust_env=False,
|
||||||
except Exception as e:
|
) as client:
|
||||||
return f"(AI 回复生成失败:{e})"
|
resp = client.post(
|
||||||
|
api_url,
|
||||||
|
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
|
||||||
|
json=request_body,
|
||||||
|
)
|
||||||
|
if resp.status_code != 200:
|
||||||
|
_retry_log.append(f"{ai_model.name}: HTTP {resp.status_code}")
|
||||||
|
_http_ok = False
|
||||||
|
break
|
||||||
|
data = resp.json()
|
||||||
|
message = data.get("choices", [{}])[0].get("message", {})
|
||||||
|
tool_calls = message.get("tool_calls", [])
|
||||||
|
if tool_calls and skills:
|
||||||
|
messages.append({
|
||||||
|
"role": "assistant",
|
||||||
|
"content": message.get("content") or "",
|
||||||
|
"tool_calls": tool_calls,
|
||||||
|
})
|
||||||
|
for tc in tool_calls:
|
||||||
|
func_name = tc.get("function", {}).get("name", "")
|
||||||
|
try:
|
||||||
|
func_args = json.loads(tc.get("function", {}).get("arguments", "{}"))
|
||||||
|
except Exception:
|
||||||
|
func_args = {"input": tc.get("function", {}).get("arguments", "")}
|
||||||
|
tool_result = _execute_skill_tool(db, func_name, func_args, skills, prompt)
|
||||||
|
messages.append({
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": tc.get("id", "call_1"),
|
||||||
|
"content": tool_result,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
ai_content = message.get("content", "")
|
||||||
|
break
|
||||||
|
|
||||||
|
# 检查本轮调用是否成功
|
||||||
|
if _http_ok and ai_content:
|
||||||
|
return ai_content or "(AI 回复为空)"
|
||||||
|
# 本轮失败:记录日志,切换备选
|
||||||
|
logger.warning(f"[im_llm] 模型 {ai_model.name} 失败,切换备选...")
|
||||||
|
continue
|
||||||
|
except Exception as exc:
|
||||||
|
_retry_log.append(f"{ai_model.name}: {str(exc)[:60]}")
|
||||||
|
logger.warning(f"[im_llm] 模型 {ai_model.name} 异常: {exc}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 全部失败
|
||||||
|
_summary = "; ".join(_retry_log[-3:])
|
||||||
|
return f"(已尝试 {len(_try_models)} 个模型均调用失败。最后错误: {_summary}。请稍后重试。)"
|
||||||
|
except Exception as e:
|
||||||
|
return f"(AI 回复生成失败:{e})"
|
||||||
Reference in new issue
Block a user