feat: 实施Kokoro Flow Chatter提示管理系统
- 在Kokoro Flow Chatter中添加提示模块以管理提示信息。 引入PromptBuilder,用于根据会话上下文构建提示。 - 为各种场景创建提示模板,包括用户消息、机器人规划和等待更新。 - 开发一个回复模块,使用LLM API生成回复。 - 建立会话管理机制,以处理用户交互并维护会话状态。 - 实现日志记录功能,以便更好地追踪操作和错误。
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 - 配置
|
Kokoro Flow Chatter - 配置
|
||||||
|
|
||||||
可以通过 TOML 配置文件覆盖默认值
|
可以通过 TOML 配置文件覆盖默认值
|
||||||
"""
|
"""
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 上下文构建器
|
Kokoro Flow Chatter 上下文构建器
|
||||||
|
|
||||||
为 KFC V2 提供完整的情境感知能力。
|
为 KFC 提供完整的情境感知能力。
|
||||||
包含:
|
包含:
|
||||||
- 关系信息 (relation_info)
|
- 关系信息 (relation_info)
|
||||||
- 记忆块 (memory_block)
|
- 记忆块 (memory_block)
|
||||||
@@ -23,7 +23,7 @@ if TYPE_CHECKING:
|
|||||||
from src.chat.message_receive.chat_stream import ChatStream
|
from src.chat.message_receive.chat_stream import ChatStream
|
||||||
from src.common.data_models.message_manager_data_model import StreamContext
|
from src.common.data_models.message_manager_data_model import StreamContext
|
||||||
|
|
||||||
logger = get_logger("kfc_v2_context_builder")
|
logger = get_logger("kfc_context_builder")
|
||||||
|
|
||||||
|
|
||||||
def _get_config():
|
def _get_config():
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 - 数据模型
|
Kokoro Flow Chatter - 数据模型
|
||||||
|
|
||||||
定义核心数据结构:
|
定义核心数据结构:
|
||||||
- EventType: 活动流事件类型
|
- EventType: 活动流事件类型
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 - 主动思考器
|
Kokoro Flow Chatter - 主动思考器
|
||||||
|
|
||||||
独立组件,负责:
|
独立组件,负责:
|
||||||
1. 等待期间的连续思考(更新心理状态)
|
1. 等待期间的连续思考(更新心理状态)
|
||||||
@@ -27,7 +27,7 @@ from .session import KokoroSession, get_session_manager
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from src.chat.message_receive.chat_stream import ChatStream
|
from src.chat.message_receive.chat_stream import ChatStream
|
||||||
|
|
||||||
logger = get_logger("kfc_v2_proactive_thinker")
|
logger = get_logger("kfc_proactive_thinker")
|
||||||
|
|
||||||
|
|
||||||
class ProactiveThinker:
|
class ProactiveThinker:
|
||||||
@@ -49,8 +49,8 @@ class ProactiveThinker:
|
|||||||
THINKING_TRIGGERS = [0.3, 0.6, 0.85]
|
THINKING_TRIGGERS = [0.3, 0.6, 0.85]
|
||||||
|
|
||||||
# 任务名称
|
# 任务名称
|
||||||
TASK_WAITING_CHECK = "kfc_v2_waiting_check"
|
TASK_WAITING_CHECK = "kfc_waiting_check"
|
||||||
TASK_PROACTIVE_CHECK = "kfc_v2_proactive_check"
|
TASK_PROACTIVE_CHECK = "kfc_proactive_check"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.session_manager = get_session_manager()
|
self.session_manager = get_session_manager()
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 - 提示词构建器
|
Kokoro Flow Chatter - 提示词构建器
|
||||||
|
|
||||||
使用项目统一的 Prompt 管理系统构建提示词
|
使用项目统一的 Prompt 管理系统构建提示词
|
||||||
"""
|
"""
|
||||||
@@ -22,7 +22,7 @@ from .prompts import PROMPT_NAMES
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from src.chat.message_receive.chat_stream import ChatStream
|
from src.chat.message_receive.chat_stream import ChatStream
|
||||||
|
|
||||||
logger = get_logger("kfc_v2_prompt_builder")
|
logger = get_logger("kfc_prompt_builder")
|
||||||
|
|
||||||
|
|
||||||
class PromptBuilder:
|
class PromptBuilder:
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 - 提示词模板注册
|
Kokoro Flow Chatter - 提示词模板注册
|
||||||
|
|
||||||
使用项目统一的 Prompt 管理系统注册所有 KFC V2 使用的提示词模板
|
使用项目统一的 Prompt 管理系统注册所有 KFC V2 使用的提示词模板
|
||||||
"""
|
"""
|
||||||
@@ -10,8 +10,8 @@ from src.chat.utils.prompt import Prompt
|
|||||||
# KFC V2 主提示词模板
|
# KFC V2 主提示词模板
|
||||||
# =================================================================================================
|
# =================================================================================================
|
||||||
|
|
||||||
KFC_V2_MAIN_PROMPT = Prompt(
|
kfc_MAIN_PROMPT = Prompt(
|
||||||
name="kfc_v2_main",
|
name="kfc_main",
|
||||||
template="""# 你与 {user_name} 的私聊
|
template="""# 你与 {user_name} 的私聊
|
||||||
|
|
||||||
## 1. 你是谁
|
## 1. 你是谁
|
||||||
@@ -51,8 +51,8 @@ KFC_V2_MAIN_PROMPT = Prompt(
|
|||||||
# 输出格式模板
|
# 输出格式模板
|
||||||
# =================================================================================================
|
# =================================================================================================
|
||||||
|
|
||||||
KFC_V2_OUTPUT_FORMAT = Prompt(
|
kfc_OUTPUT_FORMAT = Prompt(
|
||||||
name="kfc_v2_output_format",
|
name="kfc_output_format",
|
||||||
template="""请用以下 JSON 格式回复:
|
template="""请用以下 JSON 格式回复:
|
||||||
```json
|
```json
|
||||||
{{
|
{{
|
||||||
@@ -81,8 +81,8 @@ KFC_V2_OUTPUT_FORMAT = Prompt(
|
|||||||
# 情景模板 - 根据不同情境使用不同的当前情况描述
|
# 情景模板 - 根据不同情境使用不同的当前情况描述
|
||||||
# =================================================================================================
|
# =================================================================================================
|
||||||
|
|
||||||
KFC_V2_SITUATION_NEW_MESSAGE = Prompt(
|
kfc_SITUATION_NEW_MESSAGE = Prompt(
|
||||||
name="kfc_v2_situation_new_message",
|
name="kfc_situation_new_message",
|
||||||
template="""现在是 {current_time}。
|
template="""现在是 {current_time}。
|
||||||
|
|
||||||
{user_name} 刚刚给你发了消息。这是一次新的对话发起(不是对你之前消息的回复)。
|
{user_name} 刚刚给你发了消息。这是一次新的对话发起(不是对你之前消息的回复)。
|
||||||
@@ -95,8 +95,8 @@ KFC_V2_SITUATION_NEW_MESSAGE = Prompt(
|
|||||||
- 或者组合多个动作""",
|
- 或者组合多个动作""",
|
||||||
)
|
)
|
||||||
|
|
||||||
KFC_V2_SITUATION_REPLY_IN_TIME = Prompt(
|
kfc_SITUATION_REPLY_IN_TIME = Prompt(
|
||||||
name="kfc_v2_situation_reply_in_time",
|
name="kfc_situation_reply_in_time",
|
||||||
template="""现在是 {current_time}。
|
template="""现在是 {current_time}。
|
||||||
|
|
||||||
你之前发了消息后一直在等 {user_name} 的回复。
|
你之前发了消息后一直在等 {user_name} 的回复。
|
||||||
@@ -106,8 +106,8 @@ KFC_V2_SITUATION_REPLY_IN_TIME = Prompt(
|
|||||||
请决定你接下来要怎么回应。""",
|
请决定你接下来要怎么回应。""",
|
||||||
)
|
)
|
||||||
|
|
||||||
KFC_V2_SITUATION_REPLY_LATE = Prompt(
|
kfc_SITUATION_REPLY_LATE = Prompt(
|
||||||
name="kfc_v2_situation_reply_late",
|
name="kfc_situation_reply_late",
|
||||||
template="""现在是 {current_time}。
|
template="""现在是 {current_time}。
|
||||||
|
|
||||||
你之前发了消息后在等 {user_name} 的回复。
|
你之前发了消息后在等 {user_name} 的回复。
|
||||||
@@ -117,8 +117,8 @@ KFC_V2_SITUATION_REPLY_LATE = Prompt(
|
|||||||
请决定你接下来要怎么回应。(可以选择轻轻抱怨一下迟到,也可以装作没在意)""",
|
请决定你接下来要怎么回应。(可以选择轻轻抱怨一下迟到,也可以装作没在意)""",
|
||||||
)
|
)
|
||||||
|
|
||||||
KFC_V2_SITUATION_TIMEOUT = Prompt(
|
kfc_SITUATION_TIMEOUT = Prompt(
|
||||||
name="kfc_v2_situation_timeout",
|
name="kfc_situation_timeout",
|
||||||
template="""现在是 {current_time}。
|
template="""现在是 {current_time}。
|
||||||
|
|
||||||
你之前发了消息后一直在等 {user_name} 的回复。
|
你之前发了消息后一直在等 {user_name} 的回复。
|
||||||
@@ -132,8 +132,8 @@ KFC_V2_SITUATION_TIMEOUT = Prompt(
|
|||||||
4. 算了不等了(max_wait_seconds = 0)""",
|
4. 算了不等了(max_wait_seconds = 0)""",
|
||||||
)
|
)
|
||||||
|
|
||||||
KFC_V2_SITUATION_PROACTIVE = Prompt(
|
kfc_SITUATION_PROACTIVE = Prompt(
|
||||||
name="kfc_v2_situation_proactive",
|
name="kfc_situation_proactive",
|
||||||
template="""现在是 {current_time}。
|
template="""现在是 {current_time}。
|
||||||
|
|
||||||
你和 {user_name} 已经有一段时间没聊天了(沉默了 {silence_duration})。
|
你和 {user_name} 已经有一段时间没聊天了(沉默了 {silence_duration})。
|
||||||
@@ -154,16 +154,16 @@ KFC_V2_SITUATION_PROACTIVE = Prompt(
|
|||||||
# =================================================================================================
|
# =================================================================================================
|
||||||
|
|
||||||
# 用户消息条目
|
# 用户消息条目
|
||||||
KFC_V2_ENTRY_USER_MESSAGE = Prompt(
|
kfc_ENTRY_USER_MESSAGE = Prompt(
|
||||||
name="kfc_v2_entry_user_message",
|
name="kfc_entry_user_message",
|
||||||
template="""【{time}】{user_name} 说:
|
template="""【{time}】{user_name} 说:
|
||||||
"{content}"
|
"{content}"
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Bot 规划条目(有等待)
|
# Bot 规划条目(有等待)
|
||||||
KFC_V2_ENTRY_BOT_PLANNING = Prompt(
|
kfc_ENTRY_BOT_PLANNING = Prompt(
|
||||||
name="kfc_v2_entry_bot_planning",
|
name="kfc_entry_bot_planning",
|
||||||
template="""【你的想法】
|
template="""【你的想法】
|
||||||
内心:{thought}
|
内心:{thought}
|
||||||
行动:{actions_description}
|
行动:{actions_description}
|
||||||
@@ -173,8 +173,8 @@ KFC_V2_ENTRY_BOT_PLANNING = Prompt(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Bot 规划条目(无等待)
|
# Bot 规划条目(无等待)
|
||||||
KFC_V2_ENTRY_BOT_PLANNING_NO_WAIT = Prompt(
|
kfc_ENTRY_BOT_PLANNING_NO_WAIT = Prompt(
|
||||||
name="kfc_v2_entry_bot_planning_no_wait",
|
name="kfc_entry_bot_planning_no_wait",
|
||||||
template="""【你的想法】
|
template="""【你的想法】
|
||||||
内心:{thought}
|
内心:{thought}
|
||||||
行动:{actions_description}
|
行动:{actions_description}
|
||||||
@@ -183,30 +183,30 @@ KFC_V2_ENTRY_BOT_PLANNING_NO_WAIT = Prompt(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 等待期间心理变化
|
# 等待期间心理变化
|
||||||
KFC_V2_ENTRY_WAITING_UPDATE = Prompt(
|
kfc_ENTRY_WAITING_UPDATE = Prompt(
|
||||||
name="kfc_v2_entry_waiting_update",
|
name="kfc_entry_waiting_update",
|
||||||
template="""【等待中... {elapsed_minutes:.1f} 分钟过去了】
|
template="""【等待中... {elapsed_minutes:.1f} 分钟过去了】
|
||||||
你想:{waiting_thought}
|
你想:{waiting_thought}
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
# 收到及时回复时的标注
|
# 收到及时回复时的标注
|
||||||
KFC_V2_ENTRY_REPLY_IN_TIME = Prompt(
|
kfc_ENTRY_REPLY_IN_TIME = Prompt(
|
||||||
name="kfc_v2_entry_reply_in_time",
|
name="kfc_entry_reply_in_time",
|
||||||
template="""→ (对方在你预期时间内回复了,等了 {elapsed_minutes:.1f} 分钟)
|
template="""→ (对方在你预期时间内回复了,等了 {elapsed_minutes:.1f} 分钟)
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
# 收到迟到回复时的标注
|
# 收到迟到回复时的标注
|
||||||
KFC_V2_ENTRY_REPLY_LATE = Prompt(
|
kfc_ENTRY_REPLY_LATE = Prompt(
|
||||||
name="kfc_v2_entry_reply_late",
|
name="kfc_entry_reply_late",
|
||||||
template="""→ (对方回复迟了,你原本只打算等 {max_wait_minutes:.1f} 分钟,实际等了 {elapsed_minutes:.1f} 分钟)
|
template="""→ (对方回复迟了,你原本只打算等 {max_wait_minutes:.1f} 分钟,实际等了 {elapsed_minutes:.1f} 分钟)
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
# 主动思考触发
|
# 主动思考触发
|
||||||
KFC_V2_ENTRY_PROACTIVE_TRIGGER = Prompt(
|
kfc_ENTRY_PROACTIVE_TRIGGER = Prompt(
|
||||||
name="kfc_v2_entry_proactive_trigger",
|
name="kfc_entry_proactive_trigger",
|
||||||
template="""【沉默了 {silence_duration}】
|
template="""【沉默了 {silence_duration}】
|
||||||
你开始考虑要不要主动找对方聊点什么...
|
你开始考虑要不要主动找对方聊点什么...
|
||||||
""",
|
""",
|
||||||
@@ -214,18 +214,18 @@ KFC_V2_ENTRY_PROACTIVE_TRIGGER = Prompt(
|
|||||||
|
|
||||||
# 导出所有模板名称,方便外部引用
|
# 导出所有模板名称,方便外部引用
|
||||||
PROMPT_NAMES = {
|
PROMPT_NAMES = {
|
||||||
"main": "kfc_v2_main",
|
"main": "kfc_main",
|
||||||
"output_format": "kfc_v2_output_format",
|
"output_format": "kfc_output_format",
|
||||||
"situation_new_message": "kfc_v2_situation_new_message",
|
"situation_new_message": "kfc_situation_new_message",
|
||||||
"situation_reply_in_time": "kfc_v2_situation_reply_in_time",
|
"situation_reply_in_time": "kfc_situation_reply_in_time",
|
||||||
"situation_reply_late": "kfc_v2_situation_reply_late",
|
"situation_reply_late": "kfc_situation_reply_late",
|
||||||
"situation_timeout": "kfc_v2_situation_timeout",
|
"situation_timeout": "kfc_situation_timeout",
|
||||||
"situation_proactive": "kfc_v2_situation_proactive",
|
"situation_proactive": "kfc_situation_proactive",
|
||||||
"entry_user_message": "kfc_v2_entry_user_message",
|
"entry_user_message": "kfc_entry_user_message",
|
||||||
"entry_bot_planning": "kfc_v2_entry_bot_planning",
|
"entry_bot_planning": "kfc_entry_bot_planning",
|
||||||
"entry_bot_planning_no_wait": "kfc_v2_entry_bot_planning_no_wait",
|
"entry_bot_planning_no_wait": "kfc_entry_bot_planning_no_wait",
|
||||||
"entry_waiting_update": "kfc_v2_entry_waiting_update",
|
"entry_waiting_update": "kfc_entry_waiting_update",
|
||||||
"entry_reply_in_time": "kfc_v2_entry_reply_in_time",
|
"entry_reply_in_time": "kfc_entry_reply_in_time",
|
||||||
"entry_reply_late": "kfc_v2_entry_reply_late",
|
"entry_reply_late": "kfc_entry_reply_late",
|
||||||
"entry_proactive_trigger": "kfc_v2_entry_proactive_trigger",
|
"entry_proactive_trigger": "kfc_entry_proactive_trigger",
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 - Replyer
|
Kokoro Flow Chatter - Replyer
|
||||||
|
|
||||||
简化的回复生成模块,使用插件系统的 llm_api
|
简化的回复生成模块,使用插件系统的 llm_api
|
||||||
"""
|
"""
|
||||||
@@ -17,7 +17,7 @@ from .session import KokoroSession
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from src.chat.message_receive.chat_stream import ChatStream
|
from src.chat.message_receive.chat_stream import ChatStream
|
||||||
|
|
||||||
logger = get_logger("kfc_v2_replyer")
|
logger = get_logger("kfc_replyer")
|
||||||
|
|
||||||
|
|
||||||
async def generate_response(
|
async def generate_response(
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 - 会话管理
|
Kokoro Flow Chatter - 会话管理
|
||||||
|
|
||||||
极简的会话状态管理:
|
极简的会话状态管理:
|
||||||
- Session 只有 IDLE 和 WAITING 两种状态
|
- Session 只有 IDLE 和 WAITING 两种状态
|
||||||
@@ -23,12 +23,12 @@ from .models import (
|
|||||||
WaitingConfig,
|
WaitingConfig,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = get_logger("kfc_v2_session")
|
logger = get_logger("kfc_session")
|
||||||
|
|
||||||
|
|
||||||
class KokoroSession:
|
class KokoroSession:
|
||||||
"""
|
"""
|
||||||
Kokoro Flow Chatter V2 会话
|
Kokoro Flow Chatter 会话
|
||||||
|
|
||||||
为每个私聊用户维护一个独立的会话,包含:
|
为每个私聊用户维护一个独立的会话,包含:
|
||||||
- 基本信息(user_id, stream_id)
|
- 基本信息(user_id, stream_id)
|
||||||
Reference in New Issue
Block a user