refactor: 移除通用频道管理(LangBot)模块,IM Bot 改为直接对接;迁移 _call_real_llm 至 im_llm

This commit is contained in:
QihuAI Design committed 2026-07-14 08:21:56 +08:00
1 parent 945eeb8ba0
commit ab54d7344c
15 files changed
+3208 -8784

No files matched your search

-1
View File
@@ -1,6 +1,5 @@
from . import auth
from . import chat
from . import channels
from . import knowledge
from . import market
from . import token_usage
+1 -1
View File
@@ -608,7 +608,7 @@ def _qihua_reply(text: str) -> str:
自行创建 DB 会话,避免上层线程无可用 session 时崩溃。"""
db = None
try:
from app.api.channels import _call_real_llm
from app.api.im_llm import _call_real_llm
db = SessionLocal()
return _call_real_llm(db, None, text, "个人微信")
except Exception as e:
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+45
View File
@@ -0,0 +1,45 @@
"""
IM Bot 真实 LLM 调用。
原实现位于 channels.py(通用频道管理模块),该模块已移除。
为保持 IM Bot(微信机器人等)正常生成回复,将 _call_real_llm 迁移至此独立模块。
"""
from typing import Optional
import httpx
from sqlalchemy.orm import Session
from ..capability_registry import resolve_auto_model
from ..models import AIModel, ModelProvider
def _call_real_llm(db: Session, user=None, prompt: str = "", platform: str = "im") -> str:
"""调用七虎AI 默认文本模型生成回复,供 IM Bot 复用。"""
try:
ai_model = resolve_auto_model("text", db)
if not ai_model:
return "(未配置默认文本模型,无法生成回复)"
provider = db.query(ModelProvider).filter(ModelProvider.id == ai_model.provider_id).first()
api_key = ai_model.api_key or (provider.api_key if provider else None)
api_url = ai_model.endpoint
if not api_url and provider and provider.api_base_url:
api_url = f"{provider.api_base_url.rstrip('/')}/v1/chat/completions"
if not (api_key and api_url):
return "(模型未配置 API Key 或地址,无法生成回复)"
messages = [
{"role": "system", "content": "你是七虎AI 助手,负责在即时通讯中回复用户消息。"},
{"role": "user", "content": prompt},
]
request_body = {"model": ai_model.name, "messages": messages, "max_tokens": 2048}
with httpx.Client(timeout=ai_model.timeout or 120) as client:
resp = client.post(
api_url,
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
json=request_body,
)
if resp.status_code != 200:
return f"(AI 模型调用失败 HTTP {resp.status_code})"
data = resp.json()
return data.get("choices", [{}])[0].get("message", {}).get("content", "")
except Exception as e:
return f"(AI 回复生成失败:{e})"