feat(plugin_system): 引入高级Prompt注入规则系统以取代旧注入点机制
引入了一套全新的、基于规则的Prompt注入系统,以取代原有的 `injection_point` 机制。这套新系统提供了更强大、更灵活的Prompt内容注入能力。 主要变更包括: - **引入 `InjectionRule` 和 `InjectionType`**:定义了注入规则的数据结构和注入类型(如 `PREPEND`, `APPEND`, `REPLACE`, `REMOVE`, `INSERT_AFTER`),允许插件开发者精确控制注入行为。 - **重构 `PromptComponentManager`**:核心逻辑从简单地拼接字符串 (`execute_components_for`) 重构为按优先级应用注入规则 (`apply_injections`),支持正则表达式匹配和更复杂的注入操作。 - **向后兼容**:`PromptInfo` 中增加了兼容逻辑,能自动将旧的 `injection_point` 定义转换为新的 `injection_rules`,确保现有插件无需立即修改即可正常工作。 - **更新 `BasePrompt`**:废弃了 `injection_point` 属性,并推荐使用新的 `injection_rules` 属性。 - **更新示例插件**:`hello_world_plugin` 已更新,展示了新注入规则的使用方法。 BREAKING CHANGE: `BasePrompt` 中的 `injection_point` 属性已被废弃。虽然目前存在向后兼容逻辑,但未来版本将移除该属性。所有Prompt组件都应迁移至使用 `injection_rules` 以获得更强的控制力和未来的兼容性。
This commit is contained in:
@@ -3,7 +3,7 @@ from typing import Any
|
||||
|
||||
from src.chat.utils.prompt_params import PromptParameters
|
||||
from src.common.logger import get_logger
|
||||
from src.plugin_system.base.component_types import ComponentType, PromptInfo
|
||||
from src.plugin_system.base.component_types import ComponentType, InjectionRule, PromptInfo
|
||||
|
||||
logger = get_logger("base_prompt")
|
||||
|
||||
@@ -16,7 +16,7 @@ class BasePrompt(ABC):
|
||||
|
||||
子类可以通过类属性定义其行为:
|
||||
- prompt_name: Prompt组件的唯一名称。
|
||||
- injection_point: 指定要注入的目标Prompt名称(或名称列表)。
|
||||
- injection_rules: 定义注入规则的列表。
|
||||
"""
|
||||
|
||||
prompt_name: str = ""
|
||||
@@ -24,11 +24,15 @@ class BasePrompt(ABC):
|
||||
prompt_description: str = ""
|
||||
"""Prompt组件的描述"""
|
||||
|
||||
# 定义此组件希望注入到哪个或哪些核心Prompt中
|
||||
# 可以是一个字符串(单个目标)或字符串列表(多个目标)
|
||||
# 例如: "planner_prompt" 或 ["s4u_style_prompt", "normal_style_prompt"]
|
||||
injection_point: str | list[str] = ""
|
||||
"""要注入的目标Prompt名称或列表"""
|
||||
# 定义此组件希望如何注入到核心Prompt中
|
||||
# 这是一个 InjectionRule 对象的列表,可以实现复杂的注入逻辑
|
||||
# 例如: [InjectionRule(target_prompt="planner_prompt", injection_type=InjectionType.APPEND, priority=50)]
|
||||
injection_rules: list[InjectionRule] = []
|
||||
"""定义注入规则的列表"""
|
||||
|
||||
# 旧的注入点定义,用于向后兼容。如果定义了这个,它将被自动转换为 injection_rules。
|
||||
injection_point: str | list[str] | None = None
|
||||
"""[已废弃] 要注入的目标Prompt名称或列表,请使用 injection_rules"""
|
||||
|
||||
def __init__(self, params: PromptParameters, plugin_config: dict | None = None):
|
||||
"""初始化Prompt组件
|
||||
@@ -87,9 +91,11 @@ class BasePrompt(ABC):
|
||||
if not cls.prompt_name:
|
||||
raise ValueError("Prompt组件必须定义 'prompt_name' 类属性。")
|
||||
|
||||
# 同时传递新旧两种定义,PromptInfo的__post_init__将处理兼容性问题
|
||||
return PromptInfo(
|
||||
name=cls.prompt_name,
|
||||
component_type=ComponentType.PROMPT,
|
||||
description=cls.prompt_description,
|
||||
injection_rules=cls.injection_rules,
|
||||
injection_point=cls.injection_point,
|
||||
)
|
||||
|
||||
@@ -2,6 +2,38 @@ from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
|
||||
class InjectionType(Enum):
|
||||
"""Prompt注入类型枚举"""
|
||||
|
||||
PREPEND = "prepend" # 在开头添加
|
||||
APPEND = "append" # 在末尾添加
|
||||
REPLACE = "replace" # 替换指定内容
|
||||
REMOVE = "remove" # 删除指定内容
|
||||
INSERT_AFTER = "insert_after" # 在指定内容之后插入
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass
|
||||
class InjectionRule:
|
||||
"""Prompt注入规则"""
|
||||
|
||||
target_prompt: str # 目标Prompt的名称
|
||||
injection_type: InjectionType = InjectionType.PREPEND # 注入类型
|
||||
priority: int = 100 # 优先级,数字越小越先执行
|
||||
target_content: str | None = None # 用于REPLACE、REMOVE和INSERT_AFTER操作的目标内容(支持正则表达式)
|
||||
|
||||
def __post_init__(self):
|
||||
if self.injection_type in [
|
||||
InjectionType.REPLACE,
|
||||
InjectionType.REMOVE,
|
||||
InjectionType.INSERT_AFTER,
|
||||
] and self.target_content is None:
|
||||
raise ValueError(f"'{self.injection_type.value}'类型的注入规则必须提供 'target_content'。")
|
||||
|
||||
|
||||
from maim_message import Seg
|
||||
|
||||
from src.llm_models.payload_content.tool_option import ToolCall as ToolCall
|
||||
@@ -271,13 +303,30 @@ class EventInfo(ComponentInfo):
|
||||
class PromptInfo(ComponentInfo):
|
||||
"""Prompt组件信息"""
|
||||
|
||||
injection_point: str | list[str] = ""
|
||||
"""要注入的目标Prompt名称或列表"""
|
||||
injection_rules: list[InjectionRule] = field(default_factory=list)
|
||||
"""定义此组件如何注入到其他Prompt中"""
|
||||
|
||||
# 旧的injection_point,用于向后兼容
|
||||
injection_point: str | list[str] | None = None
|
||||
|
||||
def __post_init__(self):
|
||||
super().__post_init__()
|
||||
self.component_type = ComponentType.PROMPT
|
||||
|
||||
# 向后兼容逻辑:如果定义了旧的 injection_point,则自动转换为新的 injection_rules
|
||||
if self.injection_point:
|
||||
if not self.injection_rules: # 仅当rules为空时转换
|
||||
points = []
|
||||
if isinstance(self.injection_point, str):
|
||||
points.append(self.injection_point)
|
||||
elif isinstance(self.injection_point, list):
|
||||
points = self.injection_point
|
||||
|
||||
for point in points:
|
||||
self.injection_rules.append(InjectionRule(target_prompt=point))
|
||||
# 转换后可以清空旧字段,避免混淆
|
||||
self.injection_point = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class PluginInfo:
|
||||
|
||||
Reference in New Issue
Block a user