Feat : planning agent task parsing and agents assignation

This commit is contained in:
martin legrand
2025-03-10 13:42:07 +01:00
parent 6ea24974c1
commit aece8de10b
11 changed files with 146 additions and 52 deletions
+2
View File
@@ -103,6 +103,8 @@ class Agent():
return answer, reasoning
def wait_message(self, speech_module):
if speech_module is None:
return
messages = ["Please be patient sir, I am working on it.",
"At it, sir. In the meantime, how about a joke?",
"Computing... I recommand you have a coffee while I work.",
+2 -2
View File
@@ -1,5 +1,5 @@
from sources.utility import pretty_print
from sources.utility import pretty_print, animate_thinking
from sources.agents.agent import Agent
from sources.tools.webSearch import webSearch
from sources.tools.flightSearch import FlightSearch
@@ -29,7 +29,7 @@ class CasualAgent(Agent):
while not complete:
if exec_success:
complete = True
pretty_print("Thinking...", color="status")
animate_thinking("Thinking...", color="status")
answer, reasoning = self.llm_request()
exec_success, _ = self.execute_modules(answer)
answer = self.remove_blocks(answer)
+2 -2
View File
@@ -1,5 +1,5 @@
from sources.utility import pretty_print
from sources.utility import pretty_print, animate_thinking
from sources.agents.agent import Agent, executorResult
from sources.tools.C_Interpreter import CInterpreter
from sources.tools.GoInterpreter import GoInterpreter
@@ -29,7 +29,7 @@ class CoderAgent(Agent):
self.memory.push('user', prompt)
while attempt < max_attempts:
pretty_print("Thinking...", color="status")
animate_thinking("Thinking...", color="status")
self.wait_message(speech_module)
answer, reasoning = self.llm_request()
exec_success, _ = self.execute_modules(answer)
+2 -2
View File
@@ -1,5 +1,5 @@
from sources.utility import pretty_print
from sources.utility import pretty_print, animate_thinking
from sources.agents.agent import Agent
from sources.tools.fileFinder import FileFinder
from sources.tools.BashInterpreter import BashInterpreter
@@ -25,7 +25,7 @@ class FileAgent(Agent):
while not complete:
if exec_success:
complete = True
pretty_print("Thinking...", color="status")
animate_thinking("Thinking...", color="status")
answer, reasoning = self.llm_request()
exec_success, _ = self.execute_modules(answer)
answer = self.remove_blocks(answer)
+55 -26
View File
@@ -1,9 +1,10 @@
from sources.utility import pretty_print
import json
from sources.utility import pretty_print, animate_thinking
from sources.agents.agent import Agent
from sources.agents.code_agent import CoderAgent
from sources.agents.file_agent import FileAgent
from sources.agents.casual_agent import CasualAgent
from sources.tools.tools import Tools
class PlannerAgent(Agent):
def __init__(self, model, name, prompt_path, provider):
@@ -12,50 +13,78 @@ class PlannerAgent(Agent):
"""
super().__init__(model, name, prompt_path, provider)
self.tools = {
"json": Tools()
}
self.tools['json'].tag = "json"
self.agents = {
"coder": CoderAgent(model, name, prompt_path, provider),
"file": FileAgent(model, name, prompt_path, provider),
"web": CasualAgent(model, name, prompt_path, provider)
}
self.role = "complex programming tasks and web research"
self.tag = "json"
def parse_agent_tasks(self, text):
agents_tasks = []
tasks = []
tasks_names = []
lines = text.strip().split('\n')
for line in lines:
if not '-' in line:
if line is None or len(line) == 0:
continue
if not line.strip() or ':' not in line:
line = line.strip()
if '##' in line or line[0].isdigit():
tasks_names.append(line)
continue
agent_part, task = line.split(':', 1)
task = task.strip()
agent_info = agent_part.strip().split('(')
agent_type = agent_info[0].strip()
params_part = agent_info[1].rstrip(')').split(',')
params = {}
for param in params_part:
key, value = param.split('=')
params[key.strip()] = value.strip().strip('"')
agent = {
'type': agent_type,
'name': params['name'],
'task': task
}
if 'need' in params:
agent['need'] = params['need']
agents_tasks.append(agent)
return agents_tasks
blocks, _ = self.tools["json"].load_exec_block(text)
if blocks == None:
return (None, None)
for block in blocks:
line_json = json.loads(block)
if 'plan' in line_json:
for task in line_json['plan']:
agent = {
'agent': task['agent'],
'id': task['id'],
'task': task['task']
}
if 'need' in task:
agent['need'] = task['need']
tasks.append(agent)
if len(tasks_names) != len(tasks):
names = [task['task'] for task in tasks]
return zip(names, tasks)
return zip(tasks_names, tasks)
def make_prompt(self, task, needed_infos):
prompt = f"""
You are given the following informations:
{needed_infos}
Your task is:
{task}
"""
return prompt
def process(self, prompt, speech_module) -> str:
self.memory.push('user', prompt)
self.wait_message(speech_module)
pretty_print("Thinking...", color="status")
print(self.memory.get())
animate_thinking("Thinking...", color="status")
agents_tasks = (None, None)
answer, reasoning = self.llm_request()
agents_tasks = self.parse_agent_tasks(answer)
print(agents_tasks)
if agents_tasks == (None, None):
return "Failed to parse the tasks", reasoning
for task_name, task in agents_tasks:
pretty_print(f"I will {task_name}.", color="info")
agent_prompt = self.make_prompt(task['task'], task['need'])
pretty_print(f"Assigned agent {task['agent']} to {task_name}", color="info")
speech_module.speak(f"I will {task_name}. I assigned the {task['agent']} agent to the task.")
try:
self.agents[task['agent'].lower()].process(agent_prompt, None)
except Exception as e:
pretty_print(f"Error: {e}", color="failure")
speech_module.speak(f"I encountered an error: {e}")
break
self.last_answer = answer
return answer, reasoning