修复导入顺序错误
This commit is contained in:
@@ -222,8 +222,12 @@ class ChatWorkflowService:
|
||||
floor = request_data.get("floor", 0)
|
||||
|
||||
try:
|
||||
from backend.services.token_usage_service import token_usage_service
|
||||
from backend.models.internal import TokenUsageStatus
|
||||
try:
|
||||
from backend.services.token_usage_service import token_usage_service
|
||||
from backend.models.internal import TokenUsageStatus
|
||||
except ImportError:
|
||||
from services.token_usage_service import token_usage_service
|
||||
from models.internal import TokenUsageStatus
|
||||
|
||||
await token_usage_service.record_usage(
|
||||
chat_id=chat_id,
|
||||
@@ -1110,8 +1114,12 @@ class ChatWorkflowService:
|
||||
floor = request_data.get("floor", 0)
|
||||
|
||||
try:
|
||||
from backend.services.token_usage_service import token_usage_service
|
||||
from backend.models.internal import TokenUsageStatus
|
||||
try:
|
||||
from backend.services.token_usage_service import token_usage_service
|
||||
from backend.models.internal import TokenUsageStatus
|
||||
except ImportError:
|
||||
from services.token_usage_service import token_usage_service
|
||||
from models.internal import TokenUsageStatus
|
||||
|
||||
# 估算 token 数量
|
||||
prompt_tokens = len(str(prompt_messages)) // 4
|
||||
|
||||
@@ -63,6 +63,30 @@ class PresetService:
|
||||
"""保存预设到 JSON 文件"""
|
||||
path = PresetService._get_preset_path(name)
|
||||
try:
|
||||
# 确保 prompts 数组和 prompt_order 的顺序一致
|
||||
if "prompts" in data and "prompt_order" in data:
|
||||
prompts = data["prompts"]
|
||||
prompt_order = data.get("prompt_order", [{}])[0].get("order", [])
|
||||
|
||||
if prompts and prompt_order:
|
||||
# 创建 identifier 到 prompt 的映射
|
||||
prompt_map = {prompt["identifier"]: prompt for prompt in prompts}
|
||||
|
||||
# 按照 prompt_order 的顺序重新排列 prompts
|
||||
reordered_prompts = []
|
||||
for order_item in prompt_order:
|
||||
identifier = order_item.get("identifier")
|
||||
if identifier and identifier in prompt_map:
|
||||
reordered_prompts.append(prompt_map[identifier])
|
||||
|
||||
# 添加 prompt_order 中不存在的 prompts(如果有)
|
||||
existing_identifiers = {item.get("identifier") for item in prompt_order}
|
||||
for prompt in prompts:
|
||||
if prompt["identifier"] not in existing_identifiers:
|
||||
reordered_prompts.append(prompt)
|
||||
|
||||
data["prompts"] = reordered_prompts
|
||||
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
except Exception as e:
|
||||
@@ -83,12 +107,12 @@ class PresetService:
|
||||
with open(json_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# 计算组件数量
|
||||
entries = data.get("entries", [])
|
||||
# 计算组件数量 - 支持 SillyTavern 格式 (prompts) 和内部格式 (entries)
|
||||
prompts = data.get("prompts", [])
|
||||
component_count = len(entries) if entries else len(prompts)
|
||||
entries = data.get("entries", [])
|
||||
component_count = len(prompts) if prompts else len(entries)
|
||||
|
||||
# 提取温度参数(支持内部结构和 SillyTavern 结构)
|
||||
# 提取温度参数 - 使用 SillyTavern 标准字段名
|
||||
temperature = data.get("temperature", 1.0)
|
||||
|
||||
# 从文件名提取预设名称(去掉时间戳和后缀)
|
||||
@@ -205,7 +229,7 @@ class PresetService:
|
||||
@staticmethod
|
||||
def reorder_components(name: str, component_order: List[str]) -> Dict[str, Any]:
|
||||
"""
|
||||
重新排序预设组件
|
||||
重新排序预设组件 - 支持 SillyTavern 标准格式
|
||||
|
||||
Args:
|
||||
name: 预设名称
|
||||
@@ -218,8 +242,29 @@ class PresetService:
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Preset '{name}' not found")
|
||||
|
||||
# 支持内部结构的 entries
|
||||
if "entries" in data and isinstance(data["entries"], list):
|
||||
# 支持 SillyTavern 格式的 prompts
|
||||
if "prompts" in data and isinstance(data["prompts"], list):
|
||||
# 创建 identifier 到 prompt 的映射
|
||||
prompt_map = {prompt["identifier"]: prompt for prompt in data["prompts"]}
|
||||
|
||||
# 按新顺序重新排列
|
||||
reordered_prompts = []
|
||||
for identifier in component_order:
|
||||
if identifier in prompt_map:
|
||||
reordered_prompts.append(prompt_map[identifier])
|
||||
|
||||
data["prompts"] = reordered_prompts
|
||||
|
||||
# 更新 prompt_order
|
||||
if "prompt_order" in data and isinstance(data["prompt_order"], list) and len(data["prompt_order"]) > 0:
|
||||
data["prompt_order"][0]["order"] = [
|
||||
{"identifier": identifier, "enabled": True}
|
||||
for identifier in component_order
|
||||
if identifier in prompt_map
|
||||
]
|
||||
|
||||
# 也支持内部格式的 entries(向后兼容)
|
||||
elif "entries" in data and isinstance(data["entries"], list):
|
||||
# 创建 identifier 到 entry 的映射
|
||||
entry_map = {entry["identifier"]: entry for entry in data["entries"]}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user