add local knowledge base environment

This commit is contained in:
tsukasa
2023-12-04 17:15:09 +08:00
parent b7b968d5f7
commit 1031d527c1
7 changed files with 382 additions and 603 deletions
+3 -3
View File
@@ -2,7 +2,7 @@
from .proto.agent_msg import *
from .proto.compute_task import *
from .agent.agent_base import AgentPrompt,CustomAIAgent
from .agent.agent_base import AgentPrompt,CustomAIAgent, AgentTodo
from .agent.chatsession import AIChatSession
from .agent.agent import AIAgent,AIAgentTemplete, BaseAIAgent
from .agent.role import AIRole,AIRoleGroup
@@ -16,11 +16,11 @@ from .frame.tunnel import AgentTunnel
from .frame.contact_manager import ContactManager,Contact,FamilyMember
from .frame.queue_compute_node import Queue_ComputeNode
from .environment.environment import Environment,EnvironmentEvent
from .environment.environment import BaseEnvironment,SimpleEnvironment,CompositeEnvironment
from .environment.workflow_env import WorkflowEnvironment,CalenderEnvironment,CalenderEvent,PaintEnvironment
from .environment.text_to_speech_function import TextToSpeechFunction
from .environment.image_2_text_function import Image2TextFunction
from .environment.workspace_env import ShellEnvironment,WorkspaceEnvironment
from .environment.workspace_env import ShellEnvironment,WorkspaceEnvironment,TodoListEnvironment,TodoListType
from .storage.storage import ResourceLocation,AIStorage,UserConfig,UserConfigItem
+8 -52
View File
@@ -145,7 +145,6 @@ class AIAgent(BaseAIAgent):
self.owner_promp_str = None
self.contact_prompt_str = None
self.history_len = 10
self.read_report_prompt = None
todo_prompts = {}
@@ -161,11 +160,8 @@ class AIAgent(BaseAIAgent):
}
self.todo_prompts = todo_prompts
self.learn_token_limit = 4000
self.chat_db = None
self.unread_msg = Queue() # msg from other agent
self.owner_env : Environment = None
self.owenr_bus = None
self.enable_function_list = None
@@ -187,7 +183,7 @@ class AIAgent(BaseAIAgent):
logger.error("agent instance_id is None!")
return False
self.agent_id = config["instance_id"]
self.agent_workspace = WorkspaceEnvironment(self.agent_id)
self.agent_workspace = config["workspace"]
if config.get("fullname") is None:
logger.error(f"agent {self.agent_id} fullname is None!")
@@ -233,9 +229,6 @@ class AIAgent(BaseAIAgent):
if config.get("contact_prompt") is not None:
self.contact_prompt_str = config["contact_prompt"]
if config.get("owner_env") is not None:
self.owner_env = config.get("owner_env")
if config.get("powerby") is not None:
self.powerby = config["powerby"]
@@ -276,16 +269,9 @@ class AIAgent(BaseAIAgent):
def get_max_token_size(self) -> int:
return self.max_token_size
def get_llm_learn_token_limit(self) -> int:
return self.learn_token_limit
def get_learn_prompt(self) -> AgentPrompt:
return self.learn_prompt
def get_agent_role_prompt(self) -> AgentPrompt:
return self.role_prompt
def _get_remote_user_prompt(self,remote_user:str) -> AgentPrompt:
cm = ContactManager.get_instance()
contact = cm.find_contact_by_name(remote_user)
@@ -312,34 +298,6 @@ class AIAgent(BaseAIAgent):
return None
def _get_inner_functions(self) -> dict:
if self.owner_env is None:
return None,0
all_inner_function = self.owner_env.get_all_ai_functions()
if all_inner_function is None:
return None,0
result_func = []
result_len = 0
for inner_func in all_inner_function:
func_name = inner_func.get_name()
if self.enable_function_list is not None:
if len(self.enable_function_list) > 0:
if func_name not in self.enable_function_list:
logger.debug(f"ageint {self.agent_id} ignore inner func:{func_name}")
continue
this_func = {}
this_func["name"] = func_name
this_func["description"] = inner_func.get_description()
this_func["parameters"] = inner_func.get_parameters()
result_len += len(json.dumps(this_func)) / 4
result_func.append(this_func)
return result_func,result_len
def get_agent_prompt(self) -> AgentPrompt:
return self.agent_prompt
@@ -347,12 +305,9 @@ class AIAgent(BaseAIAgent):
return self.agent_think_prompt
def _format_msg_by_env_value(self,prompt:AgentPrompt):
if self.owner_env is None:
return
for msg in prompt.messages:
old_content = msg.get("content")
msg["content"] = old_content.format_map(self.owner_env)
msg["content"] = old_content.format_map(self.agent_workspace)
async def _handle_event(self,event):
if event.type == "AgentThink":
@@ -549,7 +504,7 @@ class AIAgent(BaseAIAgent):
if todo_count > 0:
have_known_info = True
known_info_str += f"## todo\n{todos_str}\n"
inner_functions,function_token_len = BaseAIAgent.get_inner_functions(self.owner_env)
inner_functions,function_token_len = BaseAIAgent.get_inner_functions(self.agent_workspace)
system_prompt_len = self.token_len(prompt=prompt)
input_len = len(msg.body)
if msg.msg_type == AgentMsgType.TYPE_GROUPMSG:
@@ -568,7 +523,7 @@ class AIAgent(BaseAIAgent):
logger.debug(f"Agent {self.agent_id} do llm token static system:{system_prompt_len},function:{function_token_len},history:{history_token_len},input:{input_len}, totoal prompt:{system_prompt_len + function_token_len + history_token_len} ")
task_result = await self.do_llm_complection(prompt,msg, env=self.owner_env,inner_functions=inner_functions)
task_result = await self.do_llm_complection(prompt,msg, env=self.agent_workspace,inner_functions=inner_functions)
if task_result.result_code != ComputeTaskResultCode.OK:
error_resp = msg.create_error_resp(task_result.error_str)
return error_resp
@@ -771,6 +726,7 @@ class AIAgent(BaseAIAgent):
case AgentTodoResult.TODO_RESULT_CODE_LLM_ERROR:
continue
case AgentTodoResult.TODO_RESULT_CODE_OK:
todo.result = do_result
await todo_list.update_todo(todo.todo_id,AgentTodo.TODO_STATE_WAITING_CHECK)
case AgentTodoResult.TODO_RESULT_CODE_EXEC_OP_ERROR:
await todo_list.update_todo(todo.todo_id,AgentTodo.TODO_STATE_EXEC_FAILED)
@@ -913,12 +869,12 @@ class AIAgent(BaseAIAgent):
resp = await AIBus.get_default_bus().post_message(msg)
logging.info(f"agent {self.agent_id} send msg to {msg.target} result:{resp}")
op_errors, have_error = await workspace.exec_op_list(llm_result.op_list, self.agent_id)
result_str, have_error = await workspace.exec_op_list(llm_result.op_list, self.agent_id)
if have_error:
result.result_code = AgentTodoResult.TODO_RESULT_CODE_EXEC_OP_ERROR
#result.error_str = error_str
return result
result.result_str = result_str
return result
async def _llm_check_todo(self, todo: AgentTodo, prompt: AgentPrompt, workspace: WorkspaceEnvironment) -> AgentTodoResult:
@@ -937,7 +893,7 @@ class AIAgent(BaseAIAgent):
return result
async def _llm_review_todo(self, todo:AgentTodo, prompt: AgentPrompt, workspace: WorkspaceEnvironment):
inner_functions,_ = BaseAIAgent.get_inner_functions(self.owner_env)
inner_functions,_ = BaseAIAgent.get_inner_functions(workspace)
task_result:ComputeTaskResult = await self.do_llm_complection(prompt,inner_functions=inner_functions)
if task_result.result_code != ComputeTaskResultCode.OK:
+9 -16
View File
@@ -11,10 +11,9 @@ logger = logging.getLogger(__name__)
class BaseEnvironment:
@abstractmethod
def get_id(self) -> str:
def __init__(self, workspace: str) -> None:
pass
# @abstractmethod
# #TODO: how to use env? different env has different prompt
# def get_env_prompt(self) -> str:
@@ -50,14 +49,11 @@ class BaseEnvironment:
# cls._all_env[env.get_id()] = env
class SimpleEnvironment(BaseEnvironment):
def __init__(self, env_id: str) -> None:
self.env_id = env_id
def __init__(self, workspace: str) -> None:
super().__init__(workspace)
self.functions: Dict[str,AIFunction] = {}
self.operations: Dict[str,AIOperation] = {}
def get_id(self) -> str:
return self.env_id
def add_ai_function(self,func:AIFunction) -> None:
self.functions[func.get_name()] = func
@@ -89,17 +85,14 @@ class SimpleEnvironment(BaseEnvironment):
class CompositeEnvironment(BaseEnvironment):
def __init__(self, env_id: str) -> None:
self.env_id = env_id
self.envs:Dict[str,BaseEnvironment] = {}
def __init__(self, workspace: str) -> None:
super().__init__(workspace)
self.envs:List[BaseEnvironment] = {}
self.functions: Dict[str,AIFunction] = {}
self.operations: Dict[str,AIOperation] = {}
def get_id(self) -> str:
return self.env_id
def add_env(self, env: BaseEnvironment) -> None:
self.envs[env.get_id()] = env
self.envs.append[env]
functions = env.get_all_ai_functions()
for func in functions:
self.functions[func.get_name()] = func
+71 -28
View File
@@ -4,7 +4,9 @@ import json
import logging
import os
import aiofiles
from typing import Any,List
import sqlite3
import asyncio
from typing import Any,List,Dict
import chardet
from ..agent.agent_base import AgentMsg,AgentTodo,AgentPrompt,AgentTodoResult
from ..agent.ai_function import AIFunction,SimpleAIFunction, SimpleAIOperation
@@ -12,7 +14,6 @@ from ..storage.storage import AIStorage,ResourceLocation
from .environment import SimpleEnvironment, CompositeEnvironment
logger = logging.getLogger(__name__)
class TodoListType:
@@ -20,12 +21,28 @@ class TodoListType:
TO_LEARN = "learn"
class TodoListEnvironment(SimpleEnvironment):
def __init__(self, root_path, list_type) -> None:
super.__init__(list_type)
self.root_path = os.path.join(root_path, list_type)
def __init__(self, workspace, list_type) -> None:
super().__init__(workspace)
self.root_path = os.path.join(workspace, list_type)
if not os.path.exists(self.root_path):
os.makedirs(self.root_path)
self.known_todo = {}
self.db_path = os.path.join(self.root_path, "todo.db")
self.conn = None
try:
self.conn = sqlite3.connect(self.db_path)
except Exception as e:
logger.error("Error occurred while connecting to database: %s", e)
return None
cursor = self.conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS todo_list (
id TEXT,
path TEXT
)
''')
self.conn.commit()
async def create_todo(params):
todoObj = AgentTodo.from_dict(params["todo"])
@@ -48,6 +65,23 @@ class TodoListEnvironment(SimpleEnvironment):
func_handler=update_todo,
))
def _get_todo_path(self,todo_id:str) -> str:
cursor = self.conn.cursor()
cursor.execute('''
SELECT path FROM todo_list WHERE id = ?
''',(todo_id,))
row = cursor.fetchone()
if row:
return row[0]
else:
return None
def _save_todo_path(self,todo_id:str,path:str):
cursor = self.conn.cursor()
cursor.execute('''
INSERT INTO todo_list (id,path) VALUES (?,?)
''',(todo_id,path))
self.conn.commit()
# Task/todo system , create,update,delete,query
async def get_todo_tree(self,path:str = None,deep:int = 4):
@@ -142,7 +176,6 @@ class TodoListEnvironment(SimpleEnvironment):
if not relative_path.startswith('/'):
relative_path = '/' + relative_path
result_todo.todo_path = relative_path
self.known_todo[result_todo.todo_id] = result_todo
else:
logger.error("get_todo_by_path:%s,parse failed!",path)
@@ -150,18 +183,12 @@ class TodoListEnvironment(SimpleEnvironment):
except Exception as e:
logger.error("get_todo_by_path:%s,failed:%s",path,e)
return None
async def get_todo(self,id:str) -> AgentTodo:
return self.known_todo.get(id)
async def create_todo(self,parent_id:str,todo:AgentTodo) -> str:
try:
if parent_id:
if parent_id not in self.known_todo:
logger.error("create_todo failed: parent_id not found!")
return False
parent_path = self.known_todo.get(parent_id).todo_path
parent_path = self._get_todo_path(parent_id)
todo_path = f"{parent_path}/{todo.title}"
else:
todo_path = todo.title
@@ -172,10 +199,10 @@ class TodoListEnvironment(SimpleEnvironment):
detail_path = f"{dir_path}/detail"
if todo.todo_path is None:
todo.todo_path = todo_path
self._save_todo_path(todo.todo_id,todo_path)
logger.info("create_todo %s",detail_path)
async with aiofiles.open(detail_path, mode='w', encoding="utf-8") as f:
await f.write(json.dumps(todo.to_dict()))
self.known_todo[todo.todo_id] = todo
except Exception as e:
logger.error("create_todo failed:%s",e)
return str(e)
@@ -184,7 +211,8 @@ class TodoListEnvironment(SimpleEnvironment):
async def update_todo(self,todo_id:str,new_stat:str)->str:
try:
todo : AgentTodo = self.known_todo.get(todo_id)
todo_path = self._get_todo_path(todo_id)
todo : AgentTodo = self.get_todo_by_fullpath(todo_path)
if todo:
todo.state = new_stat
detail_path = f"{self.root_path}/{todo.todo_path}/detail"
@@ -196,6 +224,20 @@ class TodoListEnvironment(SimpleEnvironment):
except Exception as e:
return str(e)
async def wait_todo_done(self,todo_id:str) -> AgentTodo:
todo_path = self._get_todo_path(todo_id)
async def check_done():
while True:
todo : AgentTodo = self.get_todo_by_fullpath(todo_path)
if todo:
if todo.state == AgentTodo.TODO_STATE_DONE:
break
await asyncio.sleep(1)
asyncio.create_task(check_done())
return self.get_todo_by_fullpath(todo_path)
async def append_worklog(self, todo:AgentTodo, result:AgentTodoResult):
worklog = f"{self.root_path}/{todo.todo_path}/.worklog"
@@ -213,10 +255,9 @@ class TodoListEnvironment(SimpleEnvironment):
await f.write(json.dumps(json_obj))
class FilesystemEnvironment(SimpleEnvironment):
def __init__(self, root_path: str, env_id: str) -> None:
super().__init__(env_id)
self.root_path = root_path
def __init__(self, workspace: str) -> None:
super().__init__(workspace)
self.root_path = workspace
# if op["op"] == "create":
# await self.create(op["path"],op["content"])
@@ -346,8 +387,8 @@ class FilesystemEnvironment(SimpleEnvironment):
return None
class ShellEnvironment(SimpleEnvironment):
def __init__(self, env_id: str) -> None:
super().__init__(env_id)
def __init__(self, workspace: str) -> None:
super().__init__(workspace)
operator_param = {
"command": "command will execute",
@@ -381,20 +422,22 @@ class ShellEnvironment(SimpleEnvironment):
class WorkspaceEnvironment(CompositeEnvironment):
def __init__(self, env_id: str) -> None:
super().__init__(env_id)
myai_path = AIStorage.get_instance().get_myai_dir()
self.root_path = f"{myai_path}/workspace/{env_id}"
root_path = f"{myai_path}/workspace/{env_id}"
super().__init__(root_path)
self.root_path = root_path
if not os.path.exists(self.root_path):
os.makedirs()
self.todo_list = {}
self.todo_list: Dict[str, TodoListEnvironment] = {}
self.todo_list[TodoListType.TO_WORK] = TodoListEnvironment(self.root_path,TodoListType.TO_WORK)
self.todo_list[TodoListType.TO_LEARN] = TodoListEnvironment(self.root_path,TodoListType.TO_LEARN)
# default environments in workspace
self.add_env(self.todo_list[TodoListType.TO_WORK])
self.add_env(ShellEnvironment("shell"))
self.add_env(FilesystemEnvironment(self.root_path, "fs"))
self.add_env(ShellEnvironment(self.root_path))
self.add_env(FilesystemEnvironment(self.root_path))
def set_root_path(self,path:str):
self.root_path = path