fix: 还原与转义无关的改动

This commit is contained in:
ChangingSelf
2025-04-13 17:31:10 +08:00
parent e496b24f2c
commit 65a6062cf4

View File

@@ -2,6 +2,10 @@ from typing import Dict, Any, Optional, List, Union
import re import re
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
import asyncio import asyncio
from src.common.logger import get_module_logger
# import traceback
logger = get_module_logger("prompt_build")
class PromptContext: class PromptContext:
@@ -169,12 +173,19 @@ class Prompt(str):
# 处理位置参数 # 处理位置参数
if args: if args:
# print(len(template_args), len(args), template_args, args)
for i in range(len(args)): for i in range(len(args)):
if i < len(template_args):
arg = args[i] arg = args[i]
if isinstance(arg, Prompt): if isinstance(arg, Prompt):
formatted_args[template_args[i]] = arg.format(**kwargs) formatted_args[template_args[i]] = arg.format(**kwargs)
else: else:
formatted_args[template_args[i]] = arg formatted_args[template_args[i]] = arg
else:
logger.error(
f"构建提示词模板失败,解析到的参数列表{template_args},长度为{len(template_args)},输入的参数列表为{args},提示词模板为{template}"
)
raise ValueError("格式化模板失败")
# 处理关键字参数 # 处理关键字参数
if kwargs: if kwargs:
@@ -201,7 +212,7 @@ class Prompt(str):
f"格式化模板失败: {template}, args={formatted_args}, kwargs={formatted_kwargs} {str(e)}" f"格式化模板失败: {template}, args={formatted_args}, kwargs={formatted_kwargs} {str(e)}"
) from e ) from e
def format(self, *args, **kwargs) -> "Prompt": def format(self, *args, **kwargs) -> "str":
"""支持位置参数和关键字参数的格式化,使用""" """支持位置参数和关键字参数的格式化,使用"""
ret = type(self)( ret = type(self)(
self.template, self.template,
@@ -210,10 +221,8 @@ class Prompt(str):
_should_register=False, _should_register=False,
**kwargs if kwargs else self._kwargs, **kwargs if kwargs else self._kwargs,
) )
ret.template = str(ret)
# print(f"prompt build result: {ret} name: {ret.name} ") # print(f"prompt build result: {ret} name: {ret.name} ")
# print(global_prompt_manager._prompts["schedule_prompt"]) return str(ret)
return ret
def __str__(self) -> str: def __str__(self) -> str:
if self._kwargs or self._args: if self._kwargs or self._args: