- Add the 样片模式 flow: 480p draft generation, a linkable draft task ID, and pre-flight verification before the 1080p final request. - Allow reference images and audio in video edit/extend requests, matching the provider's 2.5 contract. - Move 主模型 and 样片模式 ahead of 生成模式, default 主模型 to seedance 2.5, and migrate saved widget values. - Refresh the video panel, asset API response logs, and LLM node handling; cover the new behavior with offline tests.
631 lines
26 KiB
Python
631 lines
26 KiB
Python
"""
|
||
提示词专家节点
|
||
ComfyUI 自定义节点,通过 OpenAI 兼容协议调用市面上主流的 AI 对话大模型
|
||
支持多模态(图片输入),单轮对话,非流式输出
|
||
|
||
API 密钥和地址通过插件统一配置(环境变量或 .config 文件),与 Google Gemini 节点一致
|
||
"""
|
||
|
||
import os
|
||
import time
|
||
import base64
|
||
import json
|
||
from io import BytesIO
|
||
from typing import Optional, Tuple, List
|
||
|
||
import torch
|
||
from PIL import Image
|
||
from comfy_api.latest import io
|
||
|
||
from ..utils.image_utils import tensor_to_pil
|
||
from ..utils.config import get_api_key_or_raise, get_base_url_by_route
|
||
from ..utils.file_types import FileList
|
||
|
||
# ============================================================================
|
||
# 模型配置
|
||
# ============================================================================
|
||
|
||
DEFAULT_MODEL = "claude-opus-5-5"
|
||
|
||
SUPPORTED_MODELS = [
|
||
DEFAULT_MODEL,
|
||
"gpt-6-sol",
|
||
"gpt-6-astra",
|
||
"gpt-5.6-sol",
|
||
"gpt-5.5",
|
||
"gemini-4-argon",
|
||
"gemini-3.1-pro-preview",
|
||
"deepseek-v4-pro",
|
||
"claude-opus-5",
|
||
"doubao-seed-2.0-pro",
|
||
]
|
||
|
||
MAX_IMAGE_INPUTS = 9
|
||
|
||
REASONING_DEPTH_OPTIONS = ["低", "中", "高"]
|
||
REASONING_DEPTH_VALUE_MAP = {
|
||
"低": "low",
|
||
"中": "medium",
|
||
"高": "high",
|
||
"low": "low",
|
||
"medium": "medium",
|
||
"high": "high",
|
||
}
|
||
|
||
# 节点内实时 token 预览开关。设为 True 即可恢复原打字机效果。
|
||
ENABLE_NODE_TYPEWRITER_PREVIEW = False
|
||
|
||
# 图片缩放最大尺寸
|
||
MAX_IMAGE_DIMENSION = 1568
|
||
|
||
# 图片最大文件大小(20MB)
|
||
MAX_IMAGE_SIZE = 20 * 1024 * 1024
|
||
|
||
|
||
def _collect_autogrow_inputs(value) -> list:
|
||
"""收集已连接的 Autogrow 输入,并兼容单个旧值。"""
|
||
if value is None:
|
||
return []
|
||
if isinstance(value, dict):
|
||
return [item for item in value.values() if item is not None]
|
||
return [value]
|
||
|
||
|
||
class UniversalLLMChat(io.ComfyNode):
|
||
"""
|
||
提示词专家
|
||
|
||
功能:
|
||
- 通过 OpenAI 兼容协议调用主流大模型
|
||
- 支持多模态(图片输入)
|
||
- 单轮对话,非流式输出
|
||
- API 密钥和地址继承插件统一配置
|
||
"""
|
||
|
||
@classmethod
|
||
def define_schema(cls):
|
||
image_inputs = io.Autogrow.Input(
|
||
"图片组",
|
||
template=io.Autogrow.TemplateNames(
|
||
input=io.Image.Input("图片"),
|
||
names=[f"图片{i}" for i in range(1, MAX_IMAGE_INPUTS + 1)],
|
||
min=0,
|
||
),
|
||
tooltip=f"连接后自动增加输入端口,合计最多 {MAX_IMAGE_INPUTS} 张图片。",
|
||
)
|
||
return io.Schema(
|
||
node_id="UniversalLLMChat",
|
||
display_name="提示词专家",
|
||
category="text/generation",
|
||
inputs=[
|
||
io.Combo.Input(
|
||
"模型",
|
||
options=SUPPORTED_MODELS,
|
||
default=DEFAULT_MODEL,
|
||
),
|
||
io.Combo.Input(
|
||
"思考深度",
|
||
options=REASONING_DEPTH_OPTIONS,
|
||
default="高",
|
||
),
|
||
io.Int.Input(
|
||
"seed",
|
||
default=0,
|
||
min=0,
|
||
max=2**31 - 1,
|
||
step=1,
|
||
display_mode=io.NumberDisplay.number,
|
||
control_after_generate=io.ControlAfterGenerate.randomize,
|
||
),
|
||
io.String.Input(
|
||
"api(可选)",
|
||
default="",
|
||
multiline=False,
|
||
placeholder="留空则使用默认 API Key",
|
||
),
|
||
io.String.Input(
|
||
"提示词",
|
||
default="",
|
||
multiline=True,
|
||
),
|
||
io.Video.Input("视频", optional=True),
|
||
io.Custom("FILE_LIST").Input("文件", optional=True),
|
||
image_inputs,
|
||
],
|
||
outputs=[
|
||
io.String.Output(display_name="回复"),
|
||
],
|
||
hidden=[io.Hidden.unique_id],
|
||
is_output_node=True,
|
||
# 接收旧版固定图片端口及旧令牌字段,避免旧工作流直接失效。
|
||
accept_all_inputs=True,
|
||
)
|
||
|
||
def _resize_image(self, img: Image.Image) -> Image.Image:
|
||
"""如果图片过长边超过限制,等比缩放"""
|
||
w, h = img.size
|
||
max_dim = max(w, h)
|
||
if max_dim > MAX_IMAGE_DIMENSION:
|
||
scale = MAX_IMAGE_DIMENSION / max_dim
|
||
new_w, new_h = int(w * scale), int(h * scale)
|
||
print(f"提示词专家: 图片缩放 {w}x{h} -> {new_w}x{new_h}")
|
||
return img.resize((new_w, new_h), Image.Resampling.LANCZOS)
|
||
return img
|
||
|
||
def _image_to_data_url(self, img: Image.Image) -> str:
|
||
"""将 PIL Image 转为 data URL(JPEG base64)"""
|
||
img = self._resize_image(img)
|
||
if img.mode in ('RGBA', 'P'):
|
||
img = img.convert('RGB')
|
||
|
||
for quality in [92, 82, 72, 60, 45]:
|
||
buf = BytesIO()
|
||
img.save(buf, format='JPEG', quality=quality, optimize=True)
|
||
data = buf.getvalue()
|
||
if len(data) <= MAX_IMAGE_SIZE:
|
||
b64 = base64.b64encode(data).decode('utf-8')
|
||
return f"data:image/jpeg;base64,{b64}"
|
||
|
||
b64 = base64.b64encode(data).decode('utf-8')
|
||
return f"data:image/jpeg;base64,{b64}"
|
||
|
||
# 文件大小限制
|
||
MAX_FILE_SIZE = 50 * 1024 * 1024 # 单文件 50MB
|
||
MAX_TOTAL_FILE_SIZE = 50 * 1024 * 1024 # 所有文件总计 50MB
|
||
|
||
# 常见 MIME 类型映射
|
||
MIME_MAP = {
|
||
".pdf": "application/pdf",
|
||
".txt": "text/plain",
|
||
".md": "text/markdown",
|
||
".csv": "text/csv",
|
||
".json": "application/json",
|
||
".py": "text/x-python",
|
||
".js": "text/javascript",
|
||
".html": "text/html",
|
||
".xml": "application/xml",
|
||
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||
".zip": "application/zip",
|
||
}
|
||
|
||
# 纯文本类型,直接读取内容
|
||
TEXT_EXTS = {".txt", ".md", ".csv", ".json", ".py", ".js", ".ts", ".html",
|
||
".xml", ".yaml", ".yml", ".toml", ".ini", ".cfg", ".log",
|
||
".sh", ".bat", ".sql", ".css", ".scss", ".jsx", ".tsx"}
|
||
|
||
def _load_files(self, file_paths_str: str) -> List[dict]:
|
||
"""读取文件列表,返回 content part 数组"""
|
||
if not file_paths_str or not file_paths_str.strip():
|
||
return []
|
||
|
||
paths = [p.strip() for p in file_paths_str.split(",") if p.strip()]
|
||
parts = []
|
||
total_size = 0
|
||
|
||
for path in paths:
|
||
if not os.path.isfile(path):
|
||
raise ValueError(f"文件不存在: {path}")
|
||
|
||
file_size = os.path.getsize(path)
|
||
if file_size > self.MAX_FILE_SIZE:
|
||
raise ValueError(f"文件 {os.path.basename(path)} 大小 {file_size / 1024 / 1024:.1f}MB 超过单文件 50MB 限制")
|
||
|
||
total_size += file_size
|
||
if total_size > self.MAX_TOTAL_FILE_SIZE:
|
||
raise ValueError(f"所有文件总大小超过 50MB 限制")
|
||
|
||
ext = os.path.splitext(path)[1].lower()
|
||
mime = self.MIME_MAP.get(ext, "application/octet-stream")
|
||
filename = os.path.basename(path)
|
||
|
||
if ext in self.TEXT_EXTS:
|
||
# 文本文件直接读取内容
|
||
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
||
text_content = f.read()
|
||
parts.append({
|
||
"type": "text",
|
||
"text": f"[文件: {filename}]\n```\n{text_content}\n```",
|
||
})
|
||
else:
|
||
# 二进制文件转 base64,使用 file 格式(OpenAI 兼容协议)
|
||
with open(path, "rb") as f:
|
||
file_data = base64.b64encode(f.read()).decode("utf-8")
|
||
parts.append({
|
||
"type": "file",
|
||
"file": {
|
||
"filename": filename,
|
||
"file_data": f"data:{mime};base64,{file_data}",
|
||
},
|
||
})
|
||
|
||
print(f"提示词专家: 加载文件 {filename} ({file_size / 1024:.1f}KB, {mime})")
|
||
|
||
return parts
|
||
|
||
def _build_input(
|
||
self,
|
||
prompt: str,
|
||
image_tensors: Optional[List[torch.Tensor]] = None,
|
||
file_paths: str = "",
|
||
file_list: Optional[FileList] = None,
|
||
video=None,
|
||
) -> list:
|
||
"""构建 chat/completions 格式的 messages 数组"""
|
||
image_data_urls = []
|
||
pil_images_cache = [] # 保留 PIL Image 用于总体积重新编码
|
||
|
||
if image_tensors:
|
||
for tensor in image_tensors:
|
||
if tensor is None:
|
||
continue
|
||
for img in tensor_to_pil(tensor):
|
||
img_resized = self._resize_image(img)
|
||
if img_resized.mode in ('RGBA', 'P'):
|
||
img_resized = img_resized.convert('RGB')
|
||
pil_images_cache.append(img_resized)
|
||
image_data_urls.append(self._image_to_data_url(img_resized))
|
||
|
||
# 多图总体积控制
|
||
if pil_images_cache and len(pil_images_cache) > 1:
|
||
total_bytes = sum(
|
||
len(base64.b64decode(url.split(',', 1)[1])) for url in image_data_urls
|
||
)
|
||
if total_bytes > MAX_IMAGE_SIZE:
|
||
print(f"提示词专家: 图片总体积 {total_bytes / 1024 / 1024:.2f}MB 超过 {MAX_IMAGE_SIZE // 1024 // 1024}MB 限制,正在压缩...")
|
||
|
||
# 降质量
|
||
compressed = False
|
||
for quality in [80, 70, 60, 50, 40, 30, 20]:
|
||
new_urls = []
|
||
for img in pil_images_cache:
|
||
buf = BytesIO()
|
||
img.save(buf, format='JPEG', quality=quality, optimize=True)
|
||
b64 = base64.b64encode(buf.getvalue()).decode('utf-8')
|
||
new_urls.append(f"data:image/jpeg;base64,{b64}")
|
||
total_bytes = sum(len(base64.b64decode(u.split(',', 1)[1])) for u in new_urls)
|
||
if total_bytes <= MAX_IMAGE_SIZE:
|
||
image_data_urls = new_urls
|
||
print(f"提示词专家: 图片压缩完成,总体积 {total_bytes / 1024 / 1024:.2f}MB ({len(pil_images_cache)}张图片,质量{quality})")
|
||
compressed = True
|
||
break
|
||
|
||
# 降分辨率
|
||
if not compressed:
|
||
for scale in [0.75, 0.5, 0.35]:
|
||
new_urls = []
|
||
for img in pil_images_cache:
|
||
w, h = img.size
|
||
resized = img.resize((int(w * scale), int(h * scale)), Image.Resampling.LANCZOS)
|
||
buf = BytesIO()
|
||
resized.save(buf, format='JPEG', quality=20, optimize=True)
|
||
b64 = base64.b64encode(buf.getvalue()).decode('utf-8')
|
||
new_urls.append(f"data:image/jpeg;base64,{b64}")
|
||
total_bytes = sum(len(base64.b64decode(u.split(',', 1)[1])) for u in new_urls)
|
||
if total_bytes <= MAX_IMAGE_SIZE:
|
||
image_data_urls = new_urls
|
||
print(f"提示词专家: 图片压缩完成,总体积 {total_bytes / 1024 / 1024:.2f}MB ({len(pil_images_cache)}张图片,缩放{int(scale*100)}%)")
|
||
compressed = True
|
||
break
|
||
|
||
if not compressed:
|
||
print(f"提示词专家: 无法将 {len(pil_images_cache)} 张图片压缩到 {MAX_IMAGE_SIZE // 1024 // 1024}MB 以内,请减少图片数量或降低分辨率")
|
||
raise ValueError(f"图片总体积 {total_bytes / 1024 / 1024:.2f}MB 超过限制,无法压缩到 {MAX_IMAGE_SIZE // 1024 // 1024}MB 以内")
|
||
|
||
# 处理视频输入(ComfyUI VIDEO 类型)
|
||
video_url_str = ""
|
||
if video is not None:
|
||
# 从 VIDEO 对象中提取文件路径
|
||
vp = None
|
||
if isinstance(video, dict):
|
||
vp = video.get("video") or video.get("path") or video.get("file") or video.get("filename")
|
||
if not vp:
|
||
for val in video.values():
|
||
if isinstance(val, str) and os.path.exists(val):
|
||
vp = val
|
||
break
|
||
elif isinstance(video, str):
|
||
vp = video
|
||
else:
|
||
for attr in ("video", "path", "filename"):
|
||
if hasattr(video, attr):
|
||
vp = getattr(video, attr)
|
||
break
|
||
if not vp and hasattr(video, "__dict__"):
|
||
for attr_val in video.__dict__.values():
|
||
if isinstance(attr_val, str) and os.path.isfile(attr_val):
|
||
vp = attr_val
|
||
break
|
||
|
||
if not vp or not os.path.isfile(vp):
|
||
raise ValueError(f"视频文件不存在或路径无效: {vp}")
|
||
|
||
mime_map = {
|
||
".mp4": "video/mp4", ".mpeg": "video/mpeg", ".mpg": "video/mpg",
|
||
".mov": "video/quicktime", ".avi": "video/x-msvideo",
|
||
".flv": "video/x-flv", ".webm": "video/webm",
|
||
".wmv": "video/x-ms-wmv", ".mkv": "video/x-matroska",
|
||
}
|
||
ext = os.path.splitext(vp)[1].lower()
|
||
mime = mime_map.get(ext, "video/mp4")
|
||
file_size = os.path.getsize(vp)
|
||
print(f"提示词专家: 加载视频 {os.path.basename(vp)} ({file_size / 1024 / 1024:.1f}MB, {mime})")
|
||
with open(vp, "rb") as f:
|
||
b64 = base64.b64encode(f.read()).decode("utf-8")
|
||
video_url_str = f"data:{mime};base64,{b64}"
|
||
|
||
# 加载文件:优先使用 FILE_LIST,其次使用字符串路径
|
||
file_parts = []
|
||
if file_list:
|
||
for fd in file_list:
|
||
print(f"提示词专家: 使用文件 {fd.filename}{fd.extension} ({fd.size / 1024:.1f}KB)")
|
||
file_parts.append({
|
||
"type": "file",
|
||
"file": {
|
||
"filename": fd.filename + fd.extension,
|
||
"file_data": f"data:{fd.mime_type};base64,{fd.data}",
|
||
},
|
||
})
|
||
elif file_paths:
|
||
file_parts = self._load_files(file_paths)
|
||
|
||
# 纯文本,无图片无文件无视频
|
||
if not image_data_urls and not file_parts and not video_url_str:
|
||
return [{"role": "user", "content": prompt}]
|
||
|
||
content_parts = []
|
||
|
||
# 图片
|
||
for url in image_data_urls:
|
||
content_parts.append({
|
||
"type": "image_url",
|
||
"image_url": {"url": url},
|
||
})
|
||
|
||
# 视频:用 image_url 类型传 data URL(Gemini OpenAI 兼容层支持此格式)
|
||
# 同时保留 video_url 类型作为备用(其他支持 video_url 的模型)
|
||
if video_url_str:
|
||
content_parts.append({
|
||
"type": "image_url",
|
||
"image_url": {"url": video_url_str},
|
||
})
|
||
|
||
# 文件
|
||
for fp in file_parts:
|
||
content_parts.append(fp)
|
||
|
||
content_parts.append({
|
||
"type": "text",
|
||
"text": prompt,
|
||
})
|
||
|
||
return [{"role": "user", "content": content_parts}]
|
||
|
||
@staticmethod
|
||
def _send_stream_token(node_id, token, done=False):
|
||
"""通过 PromptServer 向前端推送流式 token"""
|
||
try:
|
||
from server import PromptServer
|
||
PromptServer.instance.send_sync(
|
||
"o1key.stream_token",
|
||
{"node_id": str(node_id), "token": token, "done": done},
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
@classmethod
|
||
def execute(
|
||
cls,
|
||
模型: str,
|
||
思考深度: str = "高",
|
||
seed: int = 0,
|
||
提示词: str = "",
|
||
视频=None,
|
||
文件: Optional[FileList] = None,
|
||
**kwargs,
|
||
) -> io.NodeOutput:
|
||
worker = cls()
|
||
result = worker.generate(
|
||
模型=模型,
|
||
思考深度=思考深度,
|
||
seed=seed,
|
||
提示词=提示词,
|
||
视频=视频,
|
||
文件=文件,
|
||
node_id=str(cls.hidden.unique_id or ""),
|
||
**kwargs,
|
||
)
|
||
return io.NodeOutput(*result)
|
||
|
||
def generate(
|
||
self,
|
||
模型: str,
|
||
思考深度: str = "高",
|
||
seed: int = 0,
|
||
提示词: str = "",
|
||
视频=None,
|
||
文件: Optional[FileList] = None,
|
||
node_id: str = "",
|
||
**kwargs,
|
||
) -> Tuple[str]:
|
||
start_time = time.time()
|
||
|
||
try:
|
||
# 用户填写 api 时覆盖默认 API Key;保留旧字段名兼容旧工作流。
|
||
api_value = kwargs.get(
|
||
"api(可选)",
|
||
kwargs.get("分组令牌(可留空)", kwargs.get("令牌", "")),
|
||
)
|
||
effective_api_key = str(api_value).strip() if api_value else ""
|
||
if not effective_api_key:
|
||
effective_api_key = get_api_key_or_raise("O1KEY_API_KEY")
|
||
base_url = get_base_url_by_route()
|
||
reasoning_effort = REASONING_DEPTH_VALUE_MAP.get(思考深度, "medium")
|
||
|
||
image_tensors = _collect_autogrow_inputs(kwargs.get("图片组"))
|
||
if not image_tensors:
|
||
# 兼容 Autogrow 改造前的「图片」及「图片1~图片9」端口。
|
||
旧图片 = kwargs.get("图片")
|
||
if 旧图片 is not None:
|
||
image_tensors.append(旧图片)
|
||
image_tensors.extend(
|
||
kwargs[f"图片{i}"]
|
||
for i in range(1, MAX_IMAGE_INPUTS + 1)
|
||
if kwargs.get(f"图片{i}") is not None
|
||
)
|
||
|
||
# 构建 input
|
||
input_data = self._build_input(提示词, image_tensors, "", 文件, 视频)
|
||
|
||
img_count = sum(len(tensor_to_pil(t)) for t in image_tensors)
|
||
file_count = len(文件) if 文件 else 0
|
||
input_desc = "文本"
|
||
if img_count: input_desc += f" + {img_count}张图片"
|
||
if 视频 is not None: input_desc += " + 视频"
|
||
if file_count: input_desc += f" + {file_count}个文件"
|
||
|
||
print(f"提示词专家: 模型 = {模型}")
|
||
print(f"提示词专家: 思考深度 = {思考深度} ({reasoning_effort})")
|
||
print(f"提示词专家: seed = {seed}")
|
||
print(f"提示词专家: 输入 = {input_desc}")
|
||
|
||
# 构建请求体(chat/completions 格式)
|
||
request_body = {
|
||
"model": 模型,
|
||
"messages": input_data,
|
||
"stream": True,
|
||
"reasoning_effort": reasoning_effort,
|
||
"seed": seed,
|
||
}
|
||
|
||
# 打印请求体,base64 截断显示
|
||
def _truncate_for_log(obj):
|
||
if isinstance(obj, dict):
|
||
return {k: _truncate_for_log(v) for k, v in obj.items()}
|
||
if isinstance(obj, list):
|
||
return [_truncate_for_log(i) for i in obj]
|
||
if isinstance(obj, str) and obj.startswith("data:"):
|
||
return obj[:60] + f"...[{len(obj)}chars]"
|
||
return obj
|
||
print(f"提示词专家: 请求原始内容 = {json.dumps(_truncate_for_log(request_body), ensure_ascii=False)}")
|
||
|
||
# 发送请求(在独立线程中运行异步请求,避免与 ComfyUI 事件循环冲突)
|
||
import aiohttp
|
||
import asyncio
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
|
||
async def _do_request():
|
||
headers = {
|
||
"Content-Type": "application/json",
|
||
"Authorization": f"Bearer {effective_api_key}",
|
||
}
|
||
url = f"{base_url}/v1/chat/completions"
|
||
# 流式接口不设整体 total 上限(否则会掐断高思考深度的长生成),
|
||
# 改用连接超时 + 单次读取超时:只要在 sock_read 间隔内有数据返回就不超时。
|
||
timeout = aiohttp.ClientTimeout(total=None, sock_connect=30, sock_read=300)
|
||
|
||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||
async with session.post(url, headers=headers, json=request_body) as resp:
|
||
status = resp.status
|
||
|
||
if status != 200:
|
||
body = await resp.text()
|
||
print(f"提示词专家: 响应体 = {body}")
|
||
try:
|
||
err_data = json.loads(body)
|
||
err_msg = err_data.get("error", {}).get("message", body[:200])
|
||
except Exception:
|
||
err_msg = body[:200]
|
||
|
||
if status == 401:
|
||
raise ValueError(f"认证失败:API Key 无效或已过期")
|
||
elif status == 403:
|
||
raise ValueError(f"无权访问模型 {模型}")
|
||
elif status == 429:
|
||
raise ValueError(f"请求频率超限,请稍后重试")
|
||
elif status == 404:
|
||
raise ValueError(f"模型 {模型} 不存在或 API 地址错误")
|
||
else:
|
||
raise RuntimeError(f"API 错误 ({status}): {err_msg}")
|
||
|
||
# 流式读取,拼接 delta content
|
||
reply_parts = []
|
||
response_body_parts = []
|
||
async for raw_line in resp.content:
|
||
line = raw_line.decode("utf-8").strip()
|
||
if not line or not line.startswith("data:"):
|
||
continue
|
||
data_str = line[len("data:"):].strip()
|
||
response_body_parts.append(f"data: {data_str}")
|
||
if data_str == "[DONE]":
|
||
break
|
||
try:
|
||
chunk = json.loads(data_str)
|
||
except Exception:
|
||
continue
|
||
stream_error = chunk.get("error")
|
||
if stream_error:
|
||
response_body = "\n".join(response_body_parts)
|
||
print(f"提示词专家: 响应体 = {response_body}")
|
||
if isinstance(stream_error, dict):
|
||
error_message = stream_error.get("message", "上游服务暂时不可用")
|
||
error_type = stream_error.get("type", "upstream_error")
|
||
else:
|
||
error_message = str(stream_error)
|
||
error_type = "upstream_error"
|
||
raise RuntimeError(f"上游服务错误 ({error_type}): {error_message}")
|
||
choices = chunk.get("choices")
|
||
if not choices:
|
||
continue
|
||
delta = choices[0].get("delta", {})
|
||
content = delta.get("content")
|
||
if content:
|
||
reply_parts.append(content)
|
||
if ENABLE_NODE_TYPEWRITER_PREVIEW:
|
||
UniversalLLMChat._send_stream_token(node_id, content)
|
||
|
||
response_body = "\n".join(response_body_parts)
|
||
print(f"提示词专家: 响应体 = {response_body}")
|
||
if ENABLE_NODE_TYPEWRITER_PREVIEW:
|
||
UniversalLLMChat._send_stream_token(node_id, "", done=True)
|
||
reply = "".join(reply_parts)
|
||
if not reply:
|
||
raise RuntimeError("模型未返回有效文本内容")
|
||
return reply
|
||
|
||
def _run_in_thread():
|
||
loop = asyncio.new_event_loop()
|
||
try:
|
||
return loop.run_until_complete(_do_request())
|
||
finally:
|
||
loop.close()
|
||
|
||
with ThreadPoolExecutor(max_workers=1) as pool:
|
||
reply = pool.submit(_run_in_thread).result()
|
||
|
||
elapsed = time.time() - start_time
|
||
print(f"提示词专家: 生成完成 (耗时: {elapsed:.2f}s)")
|
||
if reply:
|
||
preview = reply[:100] + "..." if len(reply) > 100 else reply
|
||
print(f"提示词专家: 回复预览: {preview}")
|
||
|
||
return (reply,)
|
||
|
||
except ValueError as e:
|
||
if str(e) == "未授权!":
|
||
print("提示词专家: 请联系作者授权后方可使用!")
|
||
raise ValueError("未授权!") from None
|
||
error_msg = str(e).split('\n')[0]
|
||
print(f"提示词专家: ❌ {error_msg}")
|
||
raise
|
||
|
||
except Exception as e:
|
||
import asyncio as _asyncio
|
||
if isinstance(e, _asyncio.TimeoutError):
|
||
error_msg = "请求超时:服务端长时间未返回数据(可能是模型思考过久或网络不稳定),请重试或降低思考深度/图片数量"
|
||
else:
|
||
error_msg = str(e).split('\n')[0] or f"未知错误({type(e).__name__})"
|
||
print(f"提示词专家: ❌ {error_msg}")
|
||
raise RuntimeError(error_msg) from None
|