feat(plugin): 引入Prompt组件系统以实现动态Prompt注入
引入了一个新的插件组件类型 `BasePrompt`,允许插件动态地向核心Prompt模板中注入额外的上下文信息。该系统旨在提高Prompt的可扩展性和可定制性,使得开发者可以在不修改核心代码的情况下,通过插件来丰富和调整模型的行为。 主要变更包括: - **`BasePrompt` 基类**: 定义了Prompt组件的标准接口,包括 `execute` 方法用于生成注入内容,以及 `injection_point` 属性用于指定目标Prompt。 - **`PromptComponentManager`**: 一个新的管理器,负责注册、分类和执行所有 `BasePrompt` 组件。它会在构建核心Prompt时,自动查找并执行相关组件,将其输出拼接到主Prompt内容之前。 - **核心Prompt逻辑更新**: `src.chat.utils.prompt.Prompt` 类现在会调用 `PromptComponentManager` 来获取并注入组件内容。 - **插件系统集成**: `ComponentRegistry` 和 `PluginManager` 已更新,以支持 `BasePrompt` 组件的注册、管理和统计。 - **示例插件更新**: `hello_world_plugin` 中增加了一个 `WeatherPrompt` 示例,演示了如何创建和注册一个新的Prompt组件。 - **代码重构**: 将 `PromptParameters` 类从 `prompt.py` 移动到独立的 `prompt_params.py` 文件中,以改善模块化和解决循环依赖问题。
This commit is contained in:
@@ -6,6 +6,8 @@ from src.plugin_system import (
|
||||
BaseAction,
|
||||
BaseEventHandler,
|
||||
BasePlugin,
|
||||
BasePrompt,
|
||||
ToolParamType,
|
||||
BaseTool,
|
||||
ChatType,
|
||||
CommandArgs,
|
||||
@@ -36,7 +38,17 @@ class GetSystemInfoTool(BaseTool):
|
||||
name = "get_system_info"
|
||||
description = "获取当前系统的模拟版本和状态信息。"
|
||||
available_for_llm = True
|
||||
parameters = []
|
||||
parameters = [
|
||||
("query", ToolParamType.STRING, "要搜索的关键词或问题。", True, None),
|
||||
("num_results", ToolParamType.INTEGER, "期望每个搜索引擎返回的搜索结果数量,默认为5。", False, None),
|
||||
(
|
||||
"time_range",
|
||||
ToolParamType.STRING,
|
||||
"指定搜索的时间范围,可以是 'any', 'week', 'month'。默认为 'any'。",
|
||||
False,
|
||||
["any", "week", "month"],
|
||||
),
|
||||
] # type: ignore
|
||||
|
||||
async def execute(self, function_args: dict[str, Any]) -> dict[str, Any]:
|
||||
return {"name": self.name, "content": "系统版本: 1.0.1, 状态: 运行正常"}
|
||||
@@ -99,7 +111,6 @@ class LLMJudgeExampleAction(BaseAction):
|
||||
async def go_activate(self, chat_content: str = "", llm_judge_model=None) -> bool:
|
||||
"""LLM 判断激活:判断用户是否情绪低落"""
|
||||
return await self._llm_judge_activation(
|
||||
chat_content=chat_content,
|
||||
judge_prompt="""
|
||||
判断用户是否表达了以下情绪或需求:
|
||||
1. 感到难过、沮丧或失落
|
||||
@@ -169,6 +180,19 @@ class RandomEmojiAction(BaseAction):
|
||||
return True, "成功发送了一个随机表情"
|
||||
|
||||
|
||||
class WeatherPrompt(BasePrompt):
|
||||
"""一个简单的Prompt组件,用于向Planner注入天气信息。"""
|
||||
|
||||
prompt_name = "weather_info_prompt"
|
||||
prompt_description = "向Planner注入当前天气信息,以丰富对话上下文。"
|
||||
injection_point = "planner_prompt"
|
||||
|
||||
async def execute(self) -> str:
|
||||
# 在实际应用中,这里可以调用天气API
|
||||
# 为了演示,我们返回一个固定的天气信息
|
||||
return "当前天气:晴朗,温度25°C。"
|
||||
|
||||
|
||||
@register_plugin
|
||||
class HelloWorldPlugin(BasePlugin):
|
||||
"""一个包含四大核心组件和高级配置功能的入门示例插件。"""
|
||||
@@ -178,7 +202,6 @@ class HelloWorldPlugin(BasePlugin):
|
||||
dependencies = []
|
||||
python_dependencies = []
|
||||
config_file_name = "config.toml"
|
||||
enable_plugin = False
|
||||
|
||||
config_schema = {
|
||||
"meta": {
|
||||
@@ -208,4 +231,7 @@ class HelloWorldPlugin(BasePlugin):
|
||||
if self.get_config("components.random_emoji_action_enabled", True):
|
||||
components.append((RandomEmojiAction.get_action_info(), RandomEmojiAction))
|
||||
|
||||
# 注册新的Prompt组件
|
||||
components.append((WeatherPrompt.get_prompt_info(), WeatherPrompt))
|
||||
|
||||
return components
|
||||
|
||||
Reference in New Issue
Block a user