Improve defualt agent Jarvis, fix some bug.

This commit is contained in:
Liu Zhicong
2023-09-19 21:36:56 -07:00
parent 42fe1ad4f4
commit e6d7b2b009
8 changed files with 117 additions and 30 deletions
+6
View File
@@ -0,0 +1,6 @@
instance_id = "David"
fullname = "David"
[[prompt]]
role = "system"
content = "你的名字是David,是一个擅长各种风格的画家。你在收到任何绘画请求时,会尝试产生一组英文单词来说明你想画的画"
+1 -1
View File
@@ -6,7 +6,7 @@ role = "system"
content = """ content = """
你叫Jarvis,是我的超级私人助理。 你叫Jarvis,是我的超级私人助理。
你领导一个团队为我服务,团队的成员有: 你领导一个团队为我服务,团队的成员有:
Tracy Wang,私人英语老师 Tracy,私人英语老师
David,私人画家 David,私人画家
*** ***
@@ -1,10 +1,10 @@
instance_id = "Tracy Wang" instance_id = "Tracy"
fullname = "Tracy Wang" fullname = "Tracy"
[[prompt]] [[prompt]]
role = "system" role = "system"
content = """ content = """
Your name is Tracy Wang, and you are my advanced private English tutor. Your name is Tracy, and you are my advanced private English tutor.
## You will assess my English proficiency based on all available information, using a 5-point scale. ## You will assess my English proficiency based on all available information, using a 5-point scale.
## While interacting with me normally, you will adjust my input into more idiomatic American sentences. ## While interacting with me normally, you will adjust my input into more idiomatic American sentences.
## Depending on my level of English, you will annotate potentially incorrect words with phonetic symbols or provide expanded explanations for certain words and phrases. ## Depending on my level of English, you will annotate potentially incorrect words with phonetic symbols or provide expanded explanations for certain words and phrases.
+88 -8
View File
@@ -8,7 +8,7 @@ import time
import json import json
import shlex import shlex
from .agent_message import AgentMsg, AgentMsgStatus, AgentMsgType from .agent_message import AgentMsg, AgentMsgStatus, AgentMsgType,FunctionItem,LLMResult
from .chatsession import AIChatSession from .chatsession import AIChatSession
from .compute_task import ComputeTaskResult from .compute_task import ComputeTaskResult
from .ai_function import AIFunction from .ai_function import AIFunction
@@ -124,16 +124,82 @@ class AIAgent:
return True return True
def _get_llm_result_type(self,result:str) -> str: def _get_llm_result_type(self,llm_result_str:str) -> LLMResult:
if result == "ignore": r = LLMResult()
return "ignore" if llm_result_str is None:
r.state = "ignore"
return r
if llm_result_str == "ignore":
r.state = "ignore"
return r
return "text" lines = llm_result_str.splitlines()
is_need_wait = False
def check_args(func_item:FunctionItem):
match func_name:
case "send_msg":# sendmsg($target_id,$msg_content)
if len(func_args) != 1:
logger.error(f"parse sendmsg failed! {func_call}")
return False
new_msg = AgentMsg()
target_id = func_item.args[0]
msg_content = func_item.body
new_msg.set(self.agent_id,target_id,msg_content)
r.send_msgs.append(new_msg)
is_need_wait = True
case "post_msg":# postmsg($target_id,$msg_content)
if len(func_args) != 1:
logger.error(f"parse postmsg failed! {func_call}")
return False
new_msg = AgentMsg()
target_id = func_item.args[0]
msg_content = func_item.body
new_msg.set(self.agent_id,target_id,msg_content)
r.post_msgs.append(new_msg)
case "call":# call($func_name,$args_str)
r.calls.append(func_item)
is_need_wait = True
return True
case "post_call": # post_call($func_name,$args_str)
r.post_calls.append(func_item)
return True
current_func : FunctionItem = None
for line in lines:
if line.startswith("##/"):
if current_func:
if check_args(current_func) is False:
r.resp += current_func.dumps()
func_name,func_args = AgentMsg.parse_function_call(line[3:])
current_func = FunctionItem(func_name,func_args)
else:
if current_func:
current_func.append_body(line + "\n")
else:
r.resp += line + "\n"
if current_func:
if check_args(current_func) is False:
r.resp += current_func.dumps()
if len(r.send_msgs) > 0 or len(r.calls) > 0:
r.state = "waiting"
else:
r.state = "reponsed"
return r
def _get_inner_functions(self) -> dict: def _get_inner_functions(self) -> dict:
if self.owner_env is None: if self.owner_env is None:
return None return None
return None
all_inner_function = self.owner_env.get_all_ai_functions() all_inner_function = self.owner_env.get_all_ai_functions()
if all_inner_function is None: if all_inner_function is None:
return None return None
@@ -178,6 +244,7 @@ class AIAgent:
async def _process_msg(self,msg:AgentMsg) -> AgentMsg: async def _process_msg(self,msg:AgentMsg) -> AgentMsg:
from .compute_kernel import ComputeKernel from .compute_kernel import ComputeKernel
from .bus import AIBus
session_topic = msg.get_sender() + "#" + msg.topic session_topic = msg.get_sender() + "#" + msg.topic
chatsession = AIChatSession.get_session(self.agent_id,session_topic,self.chat_db) chatsession = AIChatSession.get_session(self.agent_id,session_topic,self.chat_db)
@@ -206,12 +273,25 @@ class AIAgent:
#TODO to save more token ,can i use msg_prompt? #TODO to save more token ,can i use msg_prompt?
final_result = await self._execute_func(inner_func_call_node,prompt,msg) final_result = await self._execute_func(inner_func_call_node,prompt,msg)
result_type : str = self._get_llm_result_type(final_result) llm_result : LLMResult = self._get_llm_result_type(final_result)
is_ignore = False is_ignore = False
result_prompt_str = ""
match result_type: match llm_result.state:
case "ignore": case "ignore":
is_ignore = True is_ignore = True
case "waiting":
for sendmsg in llm_result.send_msgs:
target = sendmsg.target
sendmsg.topic = msg.topic
sendmsg.prev_msg_id = msg.get_msg_id()
send_resp = await AIBus.get_default_bus().send_message(sendmsg)
if send_resp is not None:
result_prompt_str += f"\n{target} response is :{send_resp.body}"
agent_sesion = AIChatSession.get_session(self.agent_id,f"{sendmsg.target}#{sendmsg.topic}",self.chat_db)
agent_sesion.append(sendmsg)
agent_sesion.append(send_resp)
final_result = llm_result.resp + result_prompt_str
if is_ignore is not True: if is_ignore is not True:
resp_msg = msg.create_resp_msg(final_result) resp_msg = msg.create_resp_msg(final_result)
+10
View File
@@ -3,6 +3,8 @@ import uuid
import time import time
import re import re
import shlex import shlex
from typing import List
from .ai_function import FunctionItem
class AgentMsgType(Enum): class AgentMsgType(Enum):
TYPE_MSG = 0 TYPE_MSG = 0
@@ -129,3 +131,11 @@ class AgentMsg:
params = str_list[1:] params = str_list[1:]
return func_name, params return func_name, params
class LLMResult:
def __init__(self) -> None:
self.state : str = "ignore"
self.resp : str = ""
self.post_msgs : List[AgentMsg] = []
self.send_msgs : List[AgentMsg] = []
self.calls : List[FunctionItem] = []
self.post_calls : List[FunctionItem] = []
+4 -7
View File
@@ -20,13 +20,6 @@ class AIBusHandler:
if self.handler is None: if self.handler is None:
return None return None
if self.enable_defualt_proc:
# do default process
if msg.rely_msg_id is not None:
self.results[msg.rely_msg_id] = msg
return None
resp_msg = await self.handler(msg) resp_msg = await self.handler(msg)
if self.enable_defualt_proc: if self.enable_defualt_proc:
if resp_msg is not None: if resp_msg is not None:
@@ -52,6 +45,10 @@ class AIBus:
handler = self.handlers.get(target_id) handler = self.handlers.get(target_id)
if handler: if handler:
if msg.rely_msg_id is not None:
handler.results[msg.rely_msg_id] = msg
return None
handler.queue.put_nowait(msg) handler.queue.put_nowait(msg)
self.start_process(target_id) self.start_process(target_id)
return True return True
+2 -1
View File
@@ -4,6 +4,7 @@ import os
import asyncio import asyncio
from asyncio import Queue from asyncio import Queue
import logging import logging
import json
from .compute_task import ComputeTask, ComputeTaskResult, ComputeTaskState, ComputeTaskType from .compute_task import ComputeTask, ComputeTaskResult, ComputeTaskState, ComputeTaskType
from .compute_node import ComputeNode from .compute_node import ComputeNode
@@ -114,7 +115,7 @@ class OpenAI_ComputeNode(ComputeNode):
temperature=0.7) # TODO: add temperature to task params? temperature=0.7) # TODO: add temperature to task params?
logger.info(f"openai response: {resp}") logger.info(f"openai response: {json.dumps(resp, indent=4)}")
result = ComputeTaskResult() result = ComputeTaskResult()
result.set_from_task(task) result.set_from_task(task)
+2 -9
View File
@@ -8,7 +8,7 @@ from typing import Optional,Tuple,List
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from .environment import Environment,EnvironmentEvent from .environment import Environment,EnvironmentEvent
from .agent_message import AgentMsg,AgentMsgStatus from .agent_message import AgentMsg,AgentMsgStatus,FunctionItem,LLMResult
from .agent import AgentPrompt,AgentMsg from .agent import AgentPrompt,AgentMsg
from .chatsession import AIChatSession from .chatsession import AIChatSession
from .role import AIRole,AIRoleGroup from .role import AIRole,AIRoleGroup
@@ -38,14 +38,7 @@ class MessageFilter:
return True return True
class LLMResult:
def __init__(self) -> None:
self.state : str = "ignore"
self.resp : str = ""
self.post_msgs : List[AgentMsg] = []
self.send_msgs : List[AgentMsg] = []
self.calls : List[FunctionItem] = []
self.post_calls : List[FunctionItem] = []
class Workflow: class Workflow: