From e61bcdb4351756a13d548e41fb689f8a31fbff65 Mon Sep 17 00:00:00 2001 From: tcmofashi Date: Sun, 13 Apr 2025 02:02:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86prompt?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E5=8A=9F=E8=83=BD=E7=9A=84=E8=8B=A5=E5=B9=B2?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reasoning_prompt_builder.py | 2 +- src/plugins/utils/prompt_builder.py | 32 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/plugins/chat_module/reasoning_chat/reasoning_prompt_builder.py b/src/plugins/chat_module/reasoning_chat/reasoning_prompt_builder.py index 15f6424c1..5c356af7f 100644 --- a/src/plugins/chat_module/reasoning_chat/reasoning_prompt_builder.py +++ b/src/plugins/chat_module/reasoning_chat/reasoning_prompt_builder.py @@ -195,7 +195,7 @@ class PromptBuilder: prompt = await global_prompt_manager.format_prompt( "reasoning_prompt_main", relation_prompt_all=await global_prompt_manager.get_prompt_async("relationship_prompt"), - replation_prompt=relation_prompt, + relation_prompt=relation_prompt, sender_name=sender_name, memory_prompt=memory_prompt, prompt_info=prompt_info, diff --git a/src/plugins/utils/prompt_builder.py b/src/plugins/utils/prompt_builder.py index abf5fe392..a4ac64dd0 100644 --- a/src/plugins/utils/prompt_builder.py +++ b/src/plugins/utils/prompt_builder.py @@ -1,7 +1,5 @@ -# import re -import ast from typing import Dict, Any, Optional, List, Union - +import re from contextlib import asynccontextmanager import asyncio @@ -98,13 +96,11 @@ class Prompt(str): args = list(args) # 解析模板 - tree = ast.parse(f"f'''{fstr}'''", mode="eval") - template_args = set() - for node in ast.walk(tree): - if isinstance(node, ast.FormattedValue): - expr = ast.get_source_segment(fstr, node.value) - if expr: - template_args.add(expr) + template_args = [] + result = re.findall(r"\{(.*?)\}", fstr) + for expr in result: + if expr and expr not in template_args: + template_args.append(expr) # 如果提供了初始参数,立即格式化 if kwargs or args: @@ -141,14 +137,11 @@ class Prompt(str): @classmethod def _format_template(cls, template: str, args: List[Any] = None, kwargs: Dict[str, Any] = None) -> str: - fmt_str = f"f'''{template}'''" - tree = ast.parse(fmt_str, mode="eval") template_args = [] - for node in ast.walk(tree): - if isinstance(node, ast.FormattedValue): - expr = ast.get_source_segment(fmt_str, node.value) - if expr and expr not in template_args: - template_args.append(expr) + result = re.findall(r"\{(.*?)\}", template) + for expr in result: + if expr and expr not in template_args: + template_args.append(expr) formatted_args = {} formatted_kwargs = {} @@ -180,13 +173,16 @@ class Prompt(str): template = template.format(**formatted_kwargs) return template except (IndexError, KeyError) as e: - raise ValueError(f"格式化模板失败: {template}, args={formatted_args}, kwargs={formatted_kwargs}") from e + raise ValueError( + f"格式化模板失败: {template}, args={formatted_args}, kwargs={formatted_kwargs} {str(e)}" + ) from e def format(self, *args, **kwargs) -> "Prompt": """支持位置参数和关键字参数的格式化,使用""" ret = type(self)( self.template, self.name, args=list(args) if args else self._args, **kwargs if kwargs else self._kwargs ) + ret.template = str(ret) # print(f"prompt build result: {ret} name: {ret.name} ") return ret From e9702daf9859ebabf47eed226fcc5c7baa30f1c0 Mon Sep 17 00:00:00 2001 From: tcmofashi Date: Sun, 13 Apr 2025 02:21:39 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dformat=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E9=87=8D=E5=A4=8D=E6=B3=A8=E5=86=8CPrompt=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/utils/prompt_builder.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/plugins/utils/prompt_builder.py b/src/plugins/utils/prompt_builder.py index a4ac64dd0..1572eaac9 100644 --- a/src/plugins/utils/prompt_builder.py +++ b/src/plugins/utils/prompt_builder.py @@ -94,7 +94,7 @@ class Prompt(str): # 如果传入的是元组,转换为列表 if isinstance(args, tuple): args = list(args) - + should_register = kwargs.pop("_should_register", True) # 解析模板 template_args = [] result = re.findall(r"\{(.*?)\}", fstr) @@ -116,13 +116,14 @@ class Prompt(str): obj._kwargs = kwargs # 修改自动注册逻辑 - if global_prompt_manager._context._current_context: - # 如果存在当前上下文,则注册到上下文中 - # asyncio.create_task(global_prompt_manager._context.register_async(obj)) - pass - else: - # 否则注册到全局管理器 - global_prompt_manager.register(obj) + if should_register: + if global_prompt_manager._context._current_context: + # 如果存在当前上下文,则注册到上下文中 + # asyncio.create_task(global_prompt_manager._context.register_async(obj)) + pass + else: + # 否则注册到全局管理器 + global_prompt_manager.register(obj) return obj @classmethod @@ -180,10 +181,15 @@ class Prompt(str): def format(self, *args, **kwargs) -> "Prompt": """支持位置参数和关键字参数的格式化,使用""" ret = type(self)( - self.template, self.name, args=list(args) if args else self._args, **kwargs if kwargs else self._kwargs + self.template, + self.name, + args=list(args) if args else self._args, + _should_register=False, + **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 ret def __str__(self) -> str: