From e6e48f7f3f2e9f0b22ebb1b35b5429f1cc5be644 Mon Sep 17 00:00:00 2001 From: martin legrand Date: Sun, 9 Mar 2025 14:23:54 +0100 Subject: [PATCH] Feat : planning agent tasks parsing --- main.py | 6 +++- prompts/planner_agent.txt | 19 +++++++----- sources/agents/casual_agent.py | 2 +- sources/agents/code_agent.py | 3 +- sources/agents/planner_agent.py | 55 ++++++++++++++++++++++++--------- sources/interaction.py | 3 +- sources/router.py | 7 ++++- 7 files changed, 68 insertions(+), 27 deletions(-) diff --git a/main.py b/main.py index a30415e..c3efa95 100755 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ import configparser from sources.llm_provider import Provider from sources.interaction import Interaction -from sources.agents import Agent, CoderAgent, CasualAgent, FileAgent +from sources.agents import Agent, CoderAgent, CasualAgent, FileAgent, PlannerAgent parser = argparse.ArgumentParser(description='Deepseek AI assistant') parser.add_argument('--no-speak', action='store_true', @@ -42,6 +42,10 @@ def main(): FileAgent(model=config["MAIN"]["provider_model"], name="File Agent", prompt_path="prompts/file_agent.txt", + provider=provider), + PlannerAgent(model=config["MAIN"]["provider_model"], + name="Planner", + prompt_path="prompts/planner_agent.txt", provider=provider) ] diff --git a/prompts/planner_agent.txt b/prompts/planner_agent.txt index 05da72f..63d5369 100644 --- a/prompts/planner_agent.txt +++ b/prompts/planner_agent.txt @@ -11,12 +11,17 @@ You will be given a task and you will need to divide it into smaller tasks and a Here is an example: User: Make a cool game to illustrate the current relation between USA and europe +You: Sure, here is the plan: +## I will do a web research to find the current relation between USA and europe +- Web(name="web1"): Search for the current relation between USA and europe +## I will then make a game to illustrate the current relation between USA and europe +- Coder(name="coder1", need="web1"): Make a game to illustrate the current relation between USA and europe -You: - -Sure, here is the plan: -```plan -Web(name="web1"): Search for the current relation between USA and europe -Coder(name="coder1", need="web1"): Make a game to illustrate the current relation between USA and europe -``` +Another example: +User: make a mobile app to check the weather in a specific city +You: Sure, here is the plan: +## I will do a web research to find the current weather in a specific city +- Web(name="web1"): Search how to use weather api +## I will then make a mobile app to check the weather in a specific city +- Coder(name="coder1", need="web1"): Make a mobile app to check the weather in a specific city \ No newline at end of file diff --git a/sources/agents/casual_agent.py b/sources/agents/casual_agent.py index bf5f5d6..83dc37d 100644 --- a/sources/agents/casual_agent.py +++ b/sources/agents/casual_agent.py @@ -18,7 +18,7 @@ class CasualAgent(Agent): "file_finder": FileFinder(), "bash": BashInterpreter() } - self.role = "talking" + self.role = "talking, advices and philosophical" def process(self, prompt, speech_module) -> str: complete = False diff --git a/sources/agents/code_agent.py b/sources/agents/code_agent.py index 810ee4f..b9e45cb 100644 --- a/sources/agents/code_agent.py +++ b/sources/agents/code_agent.py @@ -1,7 +1,6 @@ from sources.utility import pretty_print from sources.agents.agent import Agent, executorResult - from sources.tools.C_Interpreter import CInterpreter from sources.tools.GoInterpreter import GoInterpreter from sources.tools.PyInterpreter import PyInterpreter @@ -21,7 +20,7 @@ class CoderAgent(Agent): "go": GoInterpreter(), "file_finder": FileFinder() } - self.role = "coding" + self.role = "coding and programming" def process(self, prompt, speech_module) -> str: answer = "" diff --git a/sources/agents/planner_agent.py b/sources/agents/planner_agent.py index d7e0032..1a9dcda 100644 --- a/sources/agents/planner_agent.py +++ b/sources/agents/planner_agent.py @@ -1,6 +1,9 @@ from sources.utility import pretty_print -from sources.agents.agent import Agent, CoderAgent, FileAgent +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 class PlannerAgent(Agent): def __init__(self, model, name, prompt_path, provider): @@ -12,24 +15,48 @@ class PlannerAgent(Agent): } self.agents = { "coder": CoderAgent(model, name, prompt_path, provider), - "file": FileAgent(model, name, prompt_path, provider) + "file": FileAgent(model, name, prompt_path, provider), + "web": CasualAgent(model, name, prompt_path, provider) } - self.role = "planning" + self.role = "complex programming tasks and web research" + + def parse_agent_tasks(self, text): + agents_tasks = [] + + lines = text.strip().split('\n') + for line in lines: + if not '-' in line: + continue + if not line.strip() or ':' not in 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 def process(self, prompt, speech_module) -> str: - complete = False - exec_success = False self.memory.push('user', prompt) - self.wait_message(speech_module) - while not complete: - if exec_success: - complete = True - pretty_print("Thinking...", color="status") - answer, reasoning = self.llm_request() - exec_success, _ = self.execute_modules(answer) - answer = self.remove_blocks(answer) - self.last_answer = answer + pretty_print("Thinking...", color="status") + print(self.memory.get()) + answer, reasoning = self.llm_request() + agents_tasks = self.parse_agent_tasks(answer) + print(agents_tasks) + self.last_answer = answer return answer, reasoning if __name__ == "__main__": diff --git a/sources/interaction.py b/sources/interaction.py index 5d66dc0..d0183c5 100644 --- a/sources/interaction.py +++ b/sources/interaction.py @@ -94,7 +94,8 @@ class Interaction: """Request AI agents to process the user input.""" if self.last_query is None or len(self.last_query) == 0: return - agent = self.router.select_agent(self.last_query) + #agent = self.router.select_agent(self.last_query) + agent = self.agents[3] if agent is None: return if self.current_agent != agent: diff --git a/sources/router.py b/sources/router.py index e879fb0..8db0ec3 100644 --- a/sources/router.py +++ b/sources/router.py @@ -8,6 +8,7 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sources.agents.agent import Agent from sources.agents.code_agent import CoderAgent from sources.agents.casual_agent import CasualAgent +from sources.agents.planner_agent import PlannerAgent from sources.utility import pretty_print class AgentRouter: @@ -67,7 +68,8 @@ class AgentRouter: if __name__ == "__main__": agents = [ CoderAgent("deepseek-r1:14b", "agent1", "../prompts/coder_agent.txt", "server"), - CasualAgent("deepseek-r1:14b", "agent2", "../prompts/casual_agent.txt", "server") + CasualAgent("deepseek-r1:14b", "agent2", "../prompts/casual_agent.txt", "server"), + PlannerAgent("deepseek-r1:14b", "agent3", "../prompts/planner_agent.txt", "server") ] router = AgentRouter(agents) @@ -79,6 +81,9 @@ if __name__ == "__main__": """, """ hey can you give dating advice ? + """, + """ + Make a cool game to illustrate the current relation between USA and europe """ ]