1) FixBug
2) Add Documents abount TASK/TODO Refactor
This commit is contained in:
@@ -115,22 +115,22 @@ class ChatSessionDB:
|
||||
action_result = None
|
||||
mentions = None
|
||||
if msg.mentions:
|
||||
mentions = json.dumps(msg.mentions)
|
||||
mentions = json.dumps(msg.mentions,ensure_ascii=False)
|
||||
|
||||
match msg.msg_type:
|
||||
case AgentMsgType.TYPE_MSG:
|
||||
pass
|
||||
case AgentMsgType.TYPE_ACTION:# THIS Action is not AIAction
|
||||
action_name = msg.func_name
|
||||
action_params = json.dumps(msg.args)
|
||||
action_params = json.dumps(msg.args,ensure_ascii=False)
|
||||
action_result = msg.result_str
|
||||
case AgentMsgType.TYPE_INTERNAL_CALL:
|
||||
action_name = msg.func_name
|
||||
action_params = json.dumps(msg.args)
|
||||
action_params = json.dumps(msg.args,ensure_ascii=False)
|
||||
action_result = msg.result_str
|
||||
case AgentMsgType.TYPE_EVENT:
|
||||
action_name = msg.event_name
|
||||
action_params = json.dumps(msg.event_args)
|
||||
action_params = json.dumps(msg.event_args,ensure_ascii=False)
|
||||
if tags is None:
|
||||
tags = []
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import json
|
||||
import logging
|
||||
from typing import Optional,Set,List,Dict,Callable
|
||||
|
||||
from ..proto.ai_function import AIFunction,AIAction,SimpleAIAction
|
||||
from ..proto.ai_function import AIFunction,AIAction, AIFunction2Action,SimpleAIAction
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -15,10 +15,7 @@ class LLMProcessContext:
|
||||
|
||||
@staticmethod
|
||||
def function2action(ai_func:AIFunction) -> AIAction:
|
||||
async def exec_func(params:Dict) -> str:
|
||||
return await ai_func.execute(params)
|
||||
return SimpleAIAction(ai_func.get_id(),ai_func.get_detail_description(),exec_func)
|
||||
|
||||
return AIFunction2Action(ai_func)
|
||||
|
||||
@staticmethod
|
||||
def aifunctions_to_inner_functions(all_inner_function:List[AIFunction]) -> List[Dict]:
|
||||
@@ -30,7 +27,7 @@ class LLMProcessContext:
|
||||
this_func["name"] = func_name
|
||||
this_func["description"] = inner_func.get_description()
|
||||
this_func["parameters"] = inner_func.get_openai_parameters()
|
||||
result_len += len(json.dumps(this_func)) / 4
|
||||
result_len += len(json.dumps(this_func,ensure_ascii=False)) / 4
|
||||
result_func.append(this_func)
|
||||
return result_func
|
||||
|
||||
@@ -117,7 +114,7 @@ class SimpleLLMContext(LLMProcessContext):
|
||||
self.actions: Dict[str,AIAction] = {}
|
||||
self.action_sets : Dict[str,Dict[str,AIAction]] = {}
|
||||
|
||||
def load_action_set_from_config(self,preset,config:Dict[str,str]) -> bool:
|
||||
def load_action_set_from_config(self,preset,config:Dict[str,str]) -> Dict:
|
||||
if preset is None:
|
||||
result = {}
|
||||
else:
|
||||
@@ -217,43 +214,42 @@ class SimpleLLMContext(LLMProcessContext):
|
||||
self.func_sets = self.parent.func_sets
|
||||
|
||||
action_def:Dict= config.get("actions")
|
||||
if action_def is None:
|
||||
logger.error(f"load_from_config failed! actions not found!")
|
||||
return False
|
||||
self.actions = self.load_action_set_from_config(self.actions,action_def)
|
||||
if self.actions is None:
|
||||
logger.error(f"load_from_config failed! load_action_set_from_config failed!")
|
||||
return False
|
||||
|
||||
for set_name in action_def.keys():
|
||||
if set_name == "enable":
|
||||
continue
|
||||
if set_name == "disable":
|
||||
continue
|
||||
|
||||
sub_set = config.get(set_name)
|
||||
self.action_sets[set_name] = self.load_action_set_from_config(None,sub_set)
|
||||
if self.action_sets[set_name] is None:
|
||||
if action_def:
|
||||
self.actions = self.load_action_set_from_config(self.actions,action_def)
|
||||
if self.actions is None:
|
||||
logger.error(f"load_from_config failed! load_action_set_from_config failed!")
|
||||
return False
|
||||
|
||||
function_def:Dict = config.get("functions")
|
||||
self.functions = self.load_function_set_from_config(self.functions,function_def)
|
||||
if self.functions is None:
|
||||
logger.error(f"load_from_config failed! load_function_set_from_config failed!")
|
||||
return False
|
||||
|
||||
for set_name in function_def.keys():
|
||||
if set_name == "enable":
|
||||
continue
|
||||
if set_name == "disable":
|
||||
continue
|
||||
for set_name in action_def.keys():
|
||||
if set_name == "enable":
|
||||
continue
|
||||
if set_name == "disable":
|
||||
continue
|
||||
|
||||
sub_set = config.get(set_name)
|
||||
self.func_sets[set_name] = self.load_function_set_from_config(None,sub_set)
|
||||
if self.func_sets[set_name] is None:
|
||||
sub_set = config.get(set_name)
|
||||
self.action_sets[set_name] = self.load_action_set_from_config(None,sub_set)
|
||||
if self.action_sets[set_name] is None:
|
||||
logger.error(f"load_from_config failed! load_action_set_from_config failed!")
|
||||
return False
|
||||
|
||||
function_def:Dict = config.get("functions")
|
||||
if function_def:
|
||||
self.functions = self.load_function_set_from_config(self.functions,function_def)
|
||||
if self.functions is None:
|
||||
logger.error(f"load_from_config failed! load_function_set_from_config failed!")
|
||||
return False
|
||||
|
||||
for set_name in function_def.keys():
|
||||
if set_name == "enable":
|
||||
continue
|
||||
if set_name == "disable":
|
||||
continue
|
||||
|
||||
sub_set = config.get(set_name)
|
||||
self.func_sets[set_name] = self.load_function_set_from_config(None,sub_set)
|
||||
if self.func_sets[set_name] is None:
|
||||
logger.error(f"load_from_config failed! load_function_set_from_config failed!")
|
||||
return False
|
||||
|
||||
#values_def = config.get("values")
|
||||
#if values_def:
|
||||
@@ -280,6 +276,7 @@ class SimpleLLMContext(LLMProcessContext):
|
||||
# func = self.func_sets[set_name].get(func_name)
|
||||
# if func is not None:
|
||||
# return func
|
||||
return None
|
||||
|
||||
def get_function_set(self,set_name:str = None) -> List[AIFunction]:
|
||||
if set_name is None:
|
||||
@@ -291,15 +288,12 @@ class SimpleLLMContext(LLMProcessContext):
|
||||
return None
|
||||
|
||||
|
||||
# def get_ai_action(self,op_name:str) -> AIOperation:
|
||||
# op = self.actions.get(op_name)
|
||||
# if op is not None:
|
||||
# return op
|
||||
# for set_name in self.action_sets.keys():
|
||||
# op = self.action_sets[set_name].get(op_name)
|
||||
# if op is not None:
|
||||
# return op
|
||||
# return None
|
||||
def get_ai_action(self,op_name:str) -> AIAction:
|
||||
for action in self.actions.values():
|
||||
if action.get_name() == op_name:
|
||||
return action
|
||||
|
||||
return None
|
||||
|
||||
def get_action_set(self,set_name:str = None) -> List[AIFunction]:
|
||||
if set_name is None:
|
||||
|
||||
@@ -46,9 +46,9 @@ class BaseLLMProcess(ABC):
|
||||
async def prepare_prompt(self,input:Dict) -> LLMPrompt:
|
||||
pass
|
||||
|
||||
|
||||
@abstractmethod
|
||||
async def get_inner_function_for_exec(self,func_name:str) -> AIFunction:
|
||||
return GlobaToolsLibrary.get_instance().get_tool_function(func_name)
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def post_llm_process(self,actions:List[ActionNode],input:Dict,llm_result:LLMResult) -> bool:
|
||||
@@ -89,7 +89,7 @@ class BaseLLMProcess(ABC):
|
||||
try:
|
||||
func_name = inner_func_call_node.get("name")
|
||||
arguments = json.loads(inner_func_call_node.get("arguments"))
|
||||
logger.info(f"LLMProcess execute inner func:{func_name} :\n\t {json.dumps(arguments)}")
|
||||
logger.info(f"LLMProcess execute inner func:{func_name} :\n\t {json.dumps(arguments,ensure_ascii=False)}")
|
||||
|
||||
func_node : AIFunction = await self.get_inner_function_for_exec(func_name)
|
||||
if func_node is None:
|
||||
@@ -455,7 +455,7 @@ class LLMAgentMessageProcess(BaseLLMProcess):
|
||||
logger.info(f"enable kb")
|
||||
|
||||
|
||||
prompt.append_system_message(json.dumps(system_prompt_dict))
|
||||
prompt.append_system_message(json.dumps(system_prompt_dict,ensure_ascii=False))
|
||||
## 扩展已知信息 (这可能是一个LLM过程)
|
||||
prompt.append_system_message(await self.get_extend_known_info(msg,prompt))
|
||||
|
||||
@@ -555,8 +555,8 @@ class ReviewTaskProcess(BaseLLMProcess):
|
||||
system_prompt_dict["role_description"] = self.role_description
|
||||
system_prompt_dict["process_rule"] = self.process_description
|
||||
system_prompt_dict["reply_format"] = self.reply_format
|
||||
prompt.append_system_message(json.dumps(system_prompt_dict))
|
||||
prompt.append_user_message(json.dumps(agent_task.to_dict()))
|
||||
prompt.append_system_message(json.dumps(system_prompt_dict,ensure_ascii=False))
|
||||
prompt.append_user_message(json.dumps(agent_task.to_dict(),ensure_ascii=False))
|
||||
return prompt
|
||||
|
||||
|
||||
|
||||
+12
-10
@@ -76,7 +76,7 @@ class LocalAgentTaskManger(AgentTaskManager):
|
||||
self._save_obj_path(task.task_id,task_path)
|
||||
logger.info("create_task at %s",detail_path)
|
||||
async with aiofiles.open(detail_path, mode='w', encoding="utf-8") as f:
|
||||
await f.write(json.dumps(task.to_dict()))
|
||||
await f.write(json.dumps(task.to_dict(),ensure_ascii=False))
|
||||
return "create task ok"
|
||||
except Exception as e:
|
||||
logger.error("create_task failed:%s",e)
|
||||
@@ -97,7 +97,7 @@ class LocalAgentTaskManger(AgentTaskManager):
|
||||
todo_path = f"{self.root_path}/{owner_task_path}/#{step_order} {todo.title}.todo"
|
||||
self._save_obj_path(todo.todo_id,todo_path)
|
||||
async with aiofiles.open(todo_path, mode='w', encoding="utf-8") as f:
|
||||
await f.write(json.dumps(todo.to_dict()))
|
||||
await f.write(json.dumps(todo.to_dict(),ensure_ascii=False))
|
||||
logger.info("create_todos at %s OK!",todo_path)
|
||||
step_order += 1
|
||||
except Exception as e:
|
||||
@@ -121,7 +121,7 @@ class LocalAgentTaskManger(AgentTaskManager):
|
||||
logs = []
|
||||
logs.append(log.to_dict())
|
||||
json_obj["logs"] = logs
|
||||
await f.write(json.dumps(json_obj))
|
||||
await f.write(json.dumps(json_obj,ensure_ascii=False))
|
||||
|
||||
|
||||
async def get_worklog(self,obj_id:str)->List[AgentWorkLog]:
|
||||
@@ -263,7 +263,7 @@ class LocalAgentTaskManger(AgentTaskManager):
|
||||
detail_path = f"{self.root_path}/{task.task_path}/detail"
|
||||
try:
|
||||
async with aiofiles.open(detail_path, mode='w', encoding="utf-8") as f:
|
||||
await f.write(json.dumps(task.to_dict()))
|
||||
await f.write(json.dumps(task.to_dict(),ensure_ascii=False))
|
||||
except Exception as e:
|
||||
logger.error("update_task failed:%s",e)
|
||||
return str(e)
|
||||
@@ -277,7 +277,7 @@ class LocalAgentTaskManger(AgentTaskManager):
|
||||
|
||||
try:
|
||||
async with aiofiles.open(todo_path, mode='w', encoding="utf-8") as f:
|
||||
await f.write(json.dumps(todo.to_dict()))
|
||||
await f.write(json.dumps(todo.to_dict(),ensure_ascii=False))
|
||||
except Exception as e:
|
||||
logger.error("update_todo failed:%s",e)
|
||||
return str(e)
|
||||
@@ -311,18 +311,20 @@ class LocalAgentTaskManger(AgentTaskManager):
|
||||
|
||||
|
||||
class AgentWorkspace:
|
||||
def __init__(self,owner_agent_id:str) -> None:
|
||||
self.agent_id : str = owner_agent_id
|
||||
self.task_mgr : AgentTaskManager = LocalAgentTaskManger(owner_agent_id)
|
||||
def __init__(self,owner_id:str) -> None:
|
||||
self.owner_id : str = owner_id
|
||||
self.task_mgr : AgentTaskManager = LocalAgentTaskManger(owner_id)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def register_ai_functions():
|
||||
async def create_task(params):
|
||||
_workspace = params.get("_workspace")
|
||||
_agent_id = params.get("_agentid")
|
||||
if _workspace is None:
|
||||
return "_workspace not found"
|
||||
|
||||
if params.get("creator") is None:
|
||||
params["creator"] = _agent_id
|
||||
taskObj = AgentTask.create_by_dict(params)
|
||||
parent_id = params.get("parent")
|
||||
return await _workspace.task_mgr.create_task(taskObj,parent_id)
|
||||
@@ -371,7 +373,7 @@ class AgentWorkspace:
|
||||
return "_workspace not found"
|
||||
all_task = await _workspace.task_mgr.list_task(None)
|
||||
if all_task:
|
||||
return json.dumps([task.to_dict() for task in all_task])
|
||||
return json.dumps([task.to_dict() for task in all_task],ensure_ascii=False)
|
||||
else :
|
||||
return "no task"
|
||||
list_task_ai_function = SimpleAIFunction("agent.workspace.list_task",
|
||||
|
||||
Reference in New Issue
Block a user