From 65a6062cf443b61a8600c971d8342aced18c6cca Mon Sep 17 00:00:00 2001 From: ChangingSelf Date: Sun, 13 Apr 2025 17:31:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=BF=98=E5=8E=9F=E4=B8=8E=E8=BD=AC?= =?UTF-8?q?=E4=B9=89=E6=97=A0=E5=85=B3=E7=9A=84=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/utils/prompt_builder.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/plugins/utils/prompt_builder.py b/src/plugins/utils/prompt_builder.py index 64fad5276..f3de24e4f 100644 --- a/src/plugins/utils/prompt_builder.py +++ b/src/plugins/utils/prompt_builder.py @@ -2,6 +2,10 @@ from typing import Dict, Any, Optional, List, Union import re from contextlib import asynccontextmanager import asyncio +from src.common.logger import get_module_logger +# import traceback + +logger = get_module_logger("prompt_build") class PromptContext: @@ -169,12 +173,19 @@ class Prompt(str): # 处理位置参数 if args: + # print(len(template_args), len(args), template_args, args) for i in range(len(args)): - arg = args[i] - if isinstance(arg, Prompt): - formatted_args[template_args[i]] = arg.format(**kwargs) + if i < len(template_args): + arg = args[i] + if isinstance(arg, Prompt): + formatted_args[template_args[i]] = arg.format(**kwargs) + else: + formatted_args[template_args[i]] = arg else: - formatted_args[template_args[i]] = arg + logger.error( + f"构建提示词模板失败,解析到的参数列表{template_args},长度为{len(template_args)},输入的参数列表为{args},提示词模板为{template}" + ) + raise ValueError("格式化模板失败") # 处理关键字参数 if kwargs: @@ -201,7 +212,7 @@ class Prompt(str): f"格式化模板失败: {template}, args={formatted_args}, kwargs={formatted_kwargs} {str(e)}" ) from e - def format(self, *args, **kwargs) -> "Prompt": + def format(self, *args, **kwargs) -> "str": """支持位置参数和关键字参数的格式化,使用""" ret = type(self)( self.template, @@ -210,10 +221,8 @@ class Prompt(str): _should_register=False, **kwargs if kwargs else self._kwargs, ) - ret.template = str(ret) - # print(f"prompt build result: {ret} name: {ret.name} ") - # print(global_prompt_manager._prompts["schedule_prompt"]) - return ret + # print(f"prompt build result: {ret} name: {ret.name} ") + return str(ret) def __str__(self) -> str: if self._kwargs or self._args: