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:
@@ -196,19 +196,19 @@ class PromptManager:
|
||||
# 确保我们有有效的parameters实例用于注入逻辑
|
||||
params_for_injection = parameters or original_prompt.parameters
|
||||
|
||||
# 从组件管理器获取需要注入的内容
|
||||
components_prefix = await prompt_component_manager.execute_components_for(
|
||||
injection_point=original_prompt.name, params=params_for_injection
|
||||
# 应用所有匹配的注入规则,获取修改后的模板
|
||||
modified_template = await prompt_component_manager.apply_injections(
|
||||
target_prompt_name=original_prompt.name,
|
||||
original_template=original_prompt.template,
|
||||
params=params_for_injection,
|
||||
)
|
||||
# 如果有内容需要注入
|
||||
if components_prefix:
|
||||
logger.info(f"为'{name}'注入插件内容: \n{components_prefix}")
|
||||
# 将注入内容与原始模板拼接
|
||||
new_template = f"{components_prefix}\n\n{original_prompt.template}"
|
||||
# 创建一个新的、临时的Prompt实例。`should_register=False`是关键,
|
||||
# 它防止了这个临时版本污染全局或上下文注册表。
|
||||
|
||||
# 如果模板被修改了,就创建一个新的临时Prompt实例
|
||||
if modified_template != original_prompt.template:
|
||||
logger.info(f"为'{name}'应用了Prompt注入规则")
|
||||
# 创建一个新的临时Prompt实例,不进行注册
|
||||
temp_prompt = Prompt(
|
||||
template=new_template,
|
||||
template=modified_template,
|
||||
name=original_prompt.name,
|
||||
parameters=original_prompt.parameters,
|
||||
should_register=False, # 确保不重新注册
|
||||
@@ -1238,13 +1238,12 @@ async def create_prompt_async(
|
||||
|
||||
# 如果提供了名称,就尝试为它注入插件内容
|
||||
if name:
|
||||
components_prefix = await prompt_component_manager.execute_components_for(
|
||||
injection_point=name, params=final_params
|
||||
modified_template = await prompt_component_manager.apply_injections(
|
||||
target_prompt_name=name, original_template=template, params=final_params
|
||||
)
|
||||
if components_prefix:
|
||||
logger.debug(f"为'{name}'注入插件内容: \n{components_prefix}")
|
||||
# 将注入内容拼接到原始模板的前面
|
||||
template = f"{components_prefix}\n\n{template}"
|
||||
if modified_template != template:
|
||||
logger.debug(f"为'{name}'应用了Prompt注入规则")
|
||||
template = modified_template
|
||||
|
||||
# 使用可能已被修改的模板来创建最终的Prompt实例
|
||||
prompt = create_prompt(template, name, final_params)
|
||||
|
||||
Reference in New Issue
Block a user