🤖 自动格式化代码 [skip ci]
This commit is contained in:
@@ -122,7 +122,9 @@ class HeartFChatting:
|
|||||||
self.expressor = DefaultExpressor(chat_id=self.stream_id)
|
self.expressor = DefaultExpressor(chat_id=self.stream_id)
|
||||||
self.replyer = DefaultReplyer(chat_id=self.stream_id)
|
self.replyer = DefaultReplyer(chat_id=self.stream_id)
|
||||||
self.action_manager = ActionManager()
|
self.action_manager = ActionManager()
|
||||||
self.action_planner = PlannerFactory.create_planner(log_prefix=self.log_prefix, action_manager=self.action_manager)
|
self.action_planner = PlannerFactory.create_planner(
|
||||||
|
log_prefix=self.log_prefix, action_manager=self.action_manager
|
||||||
|
)
|
||||||
self.action_modifier = ActionModifier(action_manager=self.action_manager)
|
self.action_modifier = ActionModifier(action_manager=self.action_manager)
|
||||||
self.action_observation = ActionObservation(observe_id=self.stream_id)
|
self.action_observation = ActionObservation(observe_id=self.stream_id)
|
||||||
|
|
||||||
|
|||||||
@@ -3,23 +3,24 @@ from typing import List, Dict, Any
|
|||||||
from src.chat.focus_chat.planners.action_manager import ActionManager
|
from src.chat.focus_chat.planners.action_manager import ActionManager
|
||||||
from src.chat.focus_chat.info.info_base import InfoBase
|
from src.chat.focus_chat.info.info_base import InfoBase
|
||||||
|
|
||||||
|
|
||||||
class BasePlanner(ABC):
|
class BasePlanner(ABC):
|
||||||
"""规划器基类"""
|
"""规划器基类"""
|
||||||
|
|
||||||
def __init__(self, log_prefix: str, action_manager: ActionManager):
|
def __init__(self, log_prefix: str, action_manager: ActionManager):
|
||||||
self.log_prefix = log_prefix
|
self.log_prefix = log_prefix
|
||||||
self.action_manager = action_manager
|
self.action_manager = action_manager
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def plan(self, all_plan_info: List[InfoBase], running_memorys: List[Dict[str, Any]]) -> Dict[str, Any]:
|
async def plan(self, all_plan_info: List[InfoBase], running_memorys: List[Dict[str, Any]]) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
规划下一步行动
|
规划下一步行动
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
all_plan_info: 所有计划信息
|
all_plan_info: 所有计划信息
|
||||||
running_memorys: 回忆信息
|
running_memorys: 回忆信息
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: 规划结果
|
Dict[str, Any]: 规划结果
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -8,45 +8,46 @@ from src.common.logger_manager import get_logger
|
|||||||
|
|
||||||
logger = get_logger("planner_factory")
|
logger = get_logger("planner_factory")
|
||||||
|
|
||||||
|
|
||||||
class PlannerFactory:
|
class PlannerFactory:
|
||||||
"""规划器工厂类,用于创建不同类型的规划器实例"""
|
"""规划器工厂类,用于创建不同类型的规划器实例"""
|
||||||
|
|
||||||
# 注册所有可用的规划器类型
|
# 注册所有可用的规划器类型
|
||||||
_planner_types: Dict[str, Type[BasePlanner]] = {
|
_planner_types: Dict[str, Type[BasePlanner]] = {
|
||||||
"complex": ComplexActionPlanner,
|
"complex": ComplexActionPlanner,
|
||||||
"simple": SimpleActionPlanner,
|
"simple": SimpleActionPlanner,
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def register_planner(cls, name: str, planner_class: Type[BasePlanner]) -> None:
|
def register_planner(cls, name: str, planner_class: Type[BasePlanner]) -> None:
|
||||||
"""
|
"""
|
||||||
注册新的规划器类型
|
注册新的规划器类型
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: 规划器类型名称
|
name: 规划器类型名称
|
||||||
planner_class: 规划器类
|
planner_class: 规划器类
|
||||||
"""
|
"""
|
||||||
cls._planner_types[name] = planner_class
|
cls._planner_types[name] = planner_class
|
||||||
logger.info(f"注册新的规划器类型: {name}")
|
logger.info(f"注册新的规划器类型: {name}")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_planner(cls, log_prefix: str, action_manager: ActionManager) -> BasePlanner:
|
def create_planner(cls, log_prefix: str, action_manager: ActionManager) -> BasePlanner:
|
||||||
"""
|
"""
|
||||||
创建规划器实例
|
创建规划器实例
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
log_prefix: 日志前缀
|
log_prefix: 日志前缀
|
||||||
action_manager: 动作管理器实例
|
action_manager: 动作管理器实例
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
BasePlanner: 规划器实例
|
BasePlanner: 规划器实例
|
||||||
"""
|
"""
|
||||||
planner_type = global_config.focus_chat.planner_type
|
planner_type = global_config.focus_chat.planner_type
|
||||||
|
|
||||||
if planner_type not in cls._planner_types:
|
if planner_type not in cls._planner_types:
|
||||||
logger.warning(f"{log_prefix} 未知的规划器类型: {planner_type},使用默认规划器")
|
logger.warning(f"{log_prefix} 未知的规划器类型: {planner_type},使用默认规划器")
|
||||||
planner_type = "complex"
|
planner_type = "complex"
|
||||||
|
|
||||||
planner_class = cls._planner_types[planner_type]
|
planner_class = cls._planner_types[planner_type]
|
||||||
logger.info(f"{log_prefix} 使用{planner_type}规划器")
|
logger.info(f"{log_prefix} 使用{planner_type}规划器")
|
||||||
return planner_class(log_prefix=log_prefix, action_manager=action_manager)
|
return planner_class(log_prefix=log_prefix, action_manager=action_manager)
|
||||||
|
|||||||
@@ -56,8 +56,9 @@ def init_prompt():
|
|||||||
""",
|
""",
|
||||||
"planner_prompt",
|
"planner_prompt",
|
||||||
)
|
)
|
||||||
|
|
||||||
Prompt("""
|
Prompt(
|
||||||
|
"""
|
||||||
{raw_output}
|
{raw_output}
|
||||||
请从上面这段内容中提取出JSON内容,不要有任何其他文字或解释。
|
请从上面这段内容中提取出JSON内容,不要有任何其他文字或解释。
|
||||||
以严格的 JSON 格式输出,且仅包含 JSON 内容,不要有任何其他文字或解释。
|
以严格的 JSON 格式输出,且仅包含 JSON 内容,不要有任何其他文字或解释。
|
||||||
@@ -68,11 +69,8 @@ def init_prompt():
|
|||||||
}}
|
}}
|
||||||
|
|
||||||
请输出你提取的JSON,不要有任何其他文字或解释:""",
|
请输出你提取的JSON,不要有任何其他文字或解释:""",
|
||||||
"planner_prompt_json",
|
"planner_prompt_json",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Prompt(
|
Prompt(
|
||||||
"""
|
"""
|
||||||
@@ -93,7 +91,7 @@ class ActionPlanner(BasePlanner):
|
|||||||
max_tokens=1000,
|
max_tokens=1000,
|
||||||
request_type="focus.planner", # 用于动作规划
|
request_type="focus.planner", # 用于动作规划
|
||||||
)
|
)
|
||||||
|
|
||||||
self.utils_llm = LLMRequest(
|
self.utils_llm = LLMRequest(
|
||||||
model=global_config.model.utils_small,
|
model=global_config.model.utils_small,
|
||||||
max_tokens=1000,
|
max_tokens=1000,
|
||||||
@@ -193,20 +191,20 @@ class ActionPlanner(BasePlanner):
|
|||||||
try:
|
try:
|
||||||
prompt = f"{prompt}"
|
prompt = f"{prompt}"
|
||||||
llm_content, (reasoning_content, _) = await self.planner_llm.generate_response_async(prompt=prompt)
|
llm_content, (reasoning_content, _) = await self.planner_llm.generate_response_async(prompt=prompt)
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
f"{self.log_prefix}规划器Prompt:\n{prompt}\n\n决策动作:{action},\n动作信息: '{action_data}'\n理由: {reasoning}"
|
f"{self.log_prefix}规划器Prompt:\n{prompt}\n\n决策动作:{action},\n动作信息: '{action_data}'\n理由: {reasoning}"
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.debug(f"{self.log_prefix}LLM 原始响应: {llm_content}")
|
logger.debug(f"{self.log_prefix}LLM 原始响应: {llm_content}")
|
||||||
logger.debug(f"{self.log_prefix}LLM 原始理由响应: {reasoning_content}")
|
logger.debug(f"{self.log_prefix}LLM 原始理由响应: {reasoning_content}")
|
||||||
except Exception as req_e:
|
except Exception as req_e:
|
||||||
logger.error(f"{self.log_prefix}LLM 请求执行失败: {req_e}")
|
logger.error(f"{self.log_prefix}LLM 请求执行失败: {req_e}")
|
||||||
reasoning = f"LLM 请求失败,你的模型出现问题: {req_e}"
|
reasoning = f"LLM 请求失败,你的模型出现问题: {req_e}"
|
||||||
action = "no_reply"
|
action = "no_reply"
|
||||||
|
|
||||||
# try:
|
# try:
|
||||||
# prompt_json = await global_prompt_manager.get_prompt_async("planner_prompt_json")
|
# prompt_json = await global_prompt_manager.get_prompt_async("planner_prompt_json")
|
||||||
# prompt_json = prompt_json.format(raw_output=llm_content)
|
# prompt_json = prompt_json.format(raw_output=llm_content)
|
||||||
# llm_content_json, (reasoning_content_json, _) = await self.utils_llm.generate_response_async(prompt=prompt_json)
|
# llm_content_json, (reasoning_content_json, _) = await self.utils_llm.generate_response_async(prompt=prompt_json)
|
||||||
# logger.debug(f"{self.log_prefix}LLM格式化JSON: {llm_content_json}")
|
# logger.debug(f"{self.log_prefix}LLM格式化JSON: {llm_content_json}")
|
||||||
@@ -215,8 +213,6 @@ class ActionPlanner(BasePlanner):
|
|||||||
# logger.error(f"{self.log_prefix}解析LLM响应JSON失败,模型返回不标准: {json_e}. LLM原始输出: '{llm_content}'")
|
# logger.error(f"{self.log_prefix}解析LLM响应JSON失败,模型返回不标准: {json_e}. LLM原始输出: '{llm_content}'")
|
||||||
# reasoning = f"解析LLM响应JSON失败: {json_e}. 将使用默认动作 'no_reply'."
|
# reasoning = f"解析LLM响应JSON失败: {json_e}. 将使用默认动作 'no_reply'."
|
||||||
# action = "no_reply"
|
# action = "no_reply"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if llm_content:
|
if llm_content:
|
||||||
try:
|
try:
|
||||||
@@ -241,7 +237,7 @@ class ActionPlanner(BasePlanner):
|
|||||||
for key, value in parsed_json.items():
|
for key, value in parsed_json.items():
|
||||||
if key not in ["action", "reasoning"]:
|
if key not in ["action", "reasoning"]:
|
||||||
action_data[key] = value
|
action_data[key] = value
|
||||||
|
|
||||||
action_data["identity"] = self_info
|
action_data["identity"] = self_info
|
||||||
|
|
||||||
# 对于reply动作不需要额外处理,因为相关字段已经在上面的循环中添加到action_data
|
# 对于reply动作不需要额外处理,因为相关字段已经在上面的循环中添加到action_data
|
||||||
@@ -358,7 +354,7 @@ class ActionPlanner(BasePlanner):
|
|||||||
param_text = f"参数:\n{param_text}"
|
param_text = f"参数:\n{param_text}"
|
||||||
else:
|
else:
|
||||||
param_text = "无需参数"
|
param_text = "无需参数"
|
||||||
|
|
||||||
using_action_prompt = using_action_prompt.format(
|
using_action_prompt = using_action_prompt.format(
|
||||||
action_name=using_actions_name,
|
action_name=using_actions_name,
|
||||||
action_description=using_actions_info["description"],
|
action_description=using_actions_info["description"],
|
||||||
|
|||||||
Reference in New Issue
Block a user