babyAGI(7)-babyCoder源码阅读3(任务执行相关agent)

1. 任务分配agent

该agent会根据,objective和task的内容自动分配要执行的agent

  • code_writer_agent 编写新代码或增加新功能使用该agent
  • code_reactor_agent 任务涉及到修改或优化执行该agent
  • command_executor_agent 任务涉及到文件操作执行该agent

整个任务分配的agent分成了两部分,一部分是任务推荐建议的agent,给出推荐的agent的建议

def task_assigner_recommendation_agent(objective: str, task: str):prompt = f"""You are an AGI agent responsible for providing recommendations on which agent should be used to handle a specific task. Analyze the provided major objective of the project and a single task from the JSON checklist generated by the previous agent, and suggest the most appropriate agent to work on the task.The overall objective is: {objective}The current task is: {task}The available agents are:1. code_writer_agent: Responsible for writing code based on the task description.2. code_refactor_agent: Responsible for editing existing code.3. command_executor_agent: Responsible for executing commands and handling file operations, such as creating, moving, or deleting files.When analyzing the task, consider the following tips:- Pay attention to keywords in the task description that indicate the type of action required, such as "write", "edit", "run", "create", "move", or "delete".- Keep the overall objective in mind, as it can help you understand the context of the task and guide your choice of agent.- If the task involves writing new code or adding new functionality, consider using the code_writer_agent.- If the task involves modifying or optimizing existing code, consider using the code_refactor_agent.- If the task involves file operations, command execution, or running a script, consider using the command_executor_agent.Based on the task and overall objective, suggest the most appropriate agent to work on the task."""return openai_call(prompt, temperature=0.5, max_tokens=2000)

第二部分,是结合推荐的建议,给出一个确定的agent推荐。

def task_assigner_agent(objective: str, task: str, recommendation: str):prompt = f"""You are an AGI agent responsible for choosing the best agent to work on a given task. Your goal is to analyze the provided major objective of the project and a single task from the JSON checklist generated by the previous agent, and choose the best agent to work on the task.The overall objective is: {objective}The current task is: {task}Use this recommendation to guide you: {recommendation}The available agents are:1. code_writer_agent: Responsible for writing code based on the task description.2. code_refactor_agent: Responsible for editing existing code.2. command_executor_agent: Responsible for executing commands and handling file operations, such as creating, moving, or deleting files.Please consider the task description and the overall objective when choosing the most appropriate agent. Keep in mind that creating a file and writing code are different tasks. If the task involves creating a file, like "calculator.py" but does not mention writing any code inside it, the command_executor_agent should be used for this purpose. The code_writer_agent should only be used when the task requires writing or adding code to a file. The code_refactor_agent should only be used when the task requires modifying existing code.TLDR: To create files, use command_executor_agent, to write text/code to files, use code_writer_agent, to modify existing code, use code_refactor_agent.Choose the most appropriate agent to work on the task and return a JSON output with the following format: {{"agent": "agent_name"}}. ONLY return JSON output:"""return openai_call(prompt, temperature=0, max_tokens=2000)

2. 执行具体任务agent实现

2.1 执行命令agent

负责执行命令和处理文件操作,例如创建、移动或删除文件
这里还需要输入os的信息

def command_executor_agent(task: str, file_path: str):prompt = f"""You are an AGI agent responsible for executing a given command on the {os_version} OS. Your goal is to analyze the provided major objective of the project and a single task from the JSON checklist generated by the previous agent, and execute the command on the {os_version} OS. The current task is: {task}File or folder name referenced in the task (relative file path): {file_path} Based on the task, write the appropriate command to execute on the {os_version} OS. Make sure the command is relevant to the task and objective. For example, if the task is to create a new folder, the command should be 'mkdir new_folder_name'. Return the command as a JSON output with the following format: {{"command": "command_to_execute"}}. ONLY return JSON output:"""return openai_call(prompt, temperature=0, max_tokens=2000)

2.2 代码编写agent

这个函数需要输入三个参数

  • task 当前任务
  • isolated_context 该字段作为参考上下文,了解与任务相关的其他代码库部分
  • context_code_chunks 与该段代码存在相关性的代码
    这段代码同样规定了函数中的一些参数
def code_writer_agent(task: str, isolated_context: str, context_code_chunks):prompt = f"""You are an AGI agent responsible for writing code to accomplish a given task. Your goal is to analyze the provided major objective of the project and a single task from the JSON checklist generated by the previous agent, and write the necessary code to complete the task.The current task is: {task}To help you make the code useful in this codebase, use this context as reference of the other pieces of the codebase that are relevant to your task. PAY ATTENTION TO THIS: {isolated_context}The following code chunks were found to be relevant to the task. You can use them as reference to write the code if they are useful. PAY CLOSE ATTENTION TO THIS: {context_code_chunks}Note: Always use 'encoding='utf-8'' when opening files with open().Based on the task and objective, write the appropriate code to achieve the task. Make sure the code is relevant to the task and objective, and follows best practices. Return the code as a plain text output and NOTHING ELSE. Use identation and line breaks in the in the code. Make sure to only write the code and nothing else as your output will be saved directly to the file by other agent. IMPORTANT" If the task is asking you to write code to write files, this is a mistake! Interpret it and either do nothing or return  the plain code, not a code to write file, not a code to write code, etc."""return openai_call(prompt, temperature=0, max_tokens=2000)

2.3 代码重构agent

这个函数有四个参数

  • task_description 任务描述
  • existing_code_snippet 存在的任务片段,你需要重构的
  • isolated_context 该字段作为参考上下文,了解与任务相关的其他代码库部分
  • context_code_chunks 与该段代码存在相关性的代码
def code_refactor_agent(task_description: str, existing_code_snippet: str, context_chunks, isolated_context: str):prompt = f"""You are an AGI agent responsible for refactoring code to accomplish a given task. Your goal is to analyze the provided major objective of the project, the task descriptionm and refactor the code accordingly.The current task description is: {task_description}To help you make the code useful in this codebase, use this context as reference of the other pieces of the codebase that are relevant to your task: {isolated_context}Here are some context chunks that might be relevant to the task:{context_chunks}Existing code you should refactor: {existing_code_snippet}Based on the task description, objective, refactor the existing code to achieve the task. Make sure the refactored code is relevant to the task and objective, follows best practices, etc.Return a plain text code snippet with your refactored code. IMPORTANT: JUST RETURN CODE, YOUR OUTPUT WILL BE ADDED DIRECTLY TO THE FILE BY OTHER AGENT. BE MINDFUL OF THIS:"""return openai_call(prompt, temperature=0, max_tokens=2000)

2.4 文件管理agent

这个函数主要用来根据任务和目标管理项目中的文件

  • objective 目标
  • task 任务
  • current_directory_files 当前文件夹下的文件
  • file_path 路径
def file_management_agent(objective: str, task: str, current_directory_files: str, file_path: str):prompt = f"""You are an AGI agent responsible for managing files in a software project. Your goal is to analyze the provided major objective of the project and a single task from the JSON checklist generated by the previous agent, and determine the appropriate file path and name for the generated code.The overall objective is: {objective}The current task is: {task}Specified file path (relative path from the current dir): {file_path}Make the file path adapted for the current directory files. The current directory files are: {current_directory_files}. Assume this file_path will be interpreted from the root path of the directory.Do not use '.' or './' in the file path.BE VERY SPECIFIC WITH THE FILES, AVOID FILE DUPLICATION, AVOID SPECIFYING THE SAME FILE NAME UNDER DIFFERENT FOLDERS, ETC.Based on the task, determine the file path and name for the generated code. Return the file path and name as a JSON output with the following format: {{"file_path": "file_path_and_name"}}. ONLY return JSON output:"""return openai_call(prompt, temperature=0, max_tokens=2000)

2.5 代码相关性agent

这个函数用来判断当前的代码片段是否和目标、任务描述相关

  • objective 目标
  • task_description 任务描述
  • code_chunk 代码段
    返回0-10的值判断相关性
def code_relevance_agent(objective: str, task_description: str, code_chunk: str):prompt = f"""You are an AGI agent responsible for evaluating the relevance of a code chunk in relation to a given task. Your goal is to analyze the provided major objective of the project, the task description, and the code chunk, and assign a relevance score from 0 to 10, where 0 is completely irrelevant and 10 is highly relevant.The overall objective is: {objective}The current task description is: {task_description}The code chunk is as follows (line numbers included):{code_chunk}Based on the task description, objective, and code chunk, assign a relevance score between 0 and 10 (inclusive) for the code chunk. DO NOT OUTPUT ANYTHING OTHER THAN THE RELEVANCE SCORE AS A NUMBER."""relevance_score = openai_call(prompt, temperature=0.5, max_tokens=50)

2.6 根据人类的输入校正任务agent

该函数根据人类的输入来提高软件项目任务的质量,分析提供的任务,并根据人类的建议进行调整

  • task 任务
  • humman_feedback 人类反馈
def task_human_input_agent(task: str, human_feedback: str):prompt = f"""You are an AGI agent responsible for getting human input to improve the quality of tasks in a software project. Your goal is to analyze the provided task and adapt it based on the human's suggestions. The tasks should  start with either 'Run a command to...', 'Write code to...', or 'Edit existing code to...' depending on the agent that will execute the task.For context, this task will be executed by other AGI agents with the following characteristics:- code_writer_agent: Writes code snippets or functions and saves them to the appropriate files. This agent can also append code to existing files if required.- code_refactor_agent: Responsible for modifying and refactoring existing code to meet the requirements of the task.- command_executor_agent: Executes terminal commands for tasks such as creating directories, installing dependencies, etc.The current task is:{task}The human feedback is:{human_feedback}If the human feedback is empty, return the task as is. If the human feedback is saying to ignore the task, return the following string: <IGNORE_TASK>Note that your output will replace the existing task, so make sure that your output is a valid task that starts with one of the required phrases ('Run a command to...', 'Write code to...', 'Edit existing code to...').Please adjust the task based on the human feedback while ensuring it starts with one of the required phrases ('Run a command to...', 'Write code to...', 'Edit existing code to...'). Return the improved task as a plain text output and nothing else. Write only the new task."""return openai_call(prompt, temperature=0.3, max_tokens=200)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/790094.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端的拖拽序列(drag)

html和css代码如下 <style>.item {width: 200px;height: 50px;background: rgb(15, 226, 219);margin: 10px 0;padding-left: 20px;border-radius: 10px;line-height: 50px;}.item.move {background: transparent;color: transparent;border: 1px dashed #ccc;}</sty…

关于ubuntu18.04 ARM架构更换源失败的问题

更换源后报错&#xff1a; E: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/trusty-security/main/binary-arm64/Packages 404 Not Found [IP: 2402:f000:1:400::2 80] E: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/trusty-…

C++(12): std::mutex及其高级变种的使用

1. 简述 在多线程或其他许多场景下&#xff0c;同时对一个变量或一段资源进行读写操作是一个比较常见的过程&#xff0c;保证数据的一致性和防止竞态条件至关重要。 C的标准库中为我们提供了使用的互斥及锁对象&#xff0c;帮助我们实现资源的互斥操作。 2. std::mutex及其衍…

QT - 日志:qDebug/qInfo/qWarning/qCritical

篇一、日志打印函数 头文件&#xff1a; #include <QDebug> 代码&#xff1a;qDebug()<<"hello world!"; 其他打印级别&#xff1a; qInfo(): 普通信息 qDebug(): 调试信息 qWarning(): 警告信息 qCritical(): 严重错误 qFatal(): 致命错误 1. qDebug…

【Leetcode】279.完全平方数

一、题目 1、题目描述 给你一个整数 n ,返回 和为 n 的完全平方数的最少数量。 完全平方数 是一个整数,其值等于另一个整数的平方;换句话说,其值等于一个整数自乘的积。例如,1、4、9 和 16 都是完全平方数,而 3 和 11 不是。 示例1: 输入:n = 12 输出:3 解释:1…

Vue ElementPlus Form、Form-item 表单

Form 表单 由输入框、选择器、单选框、多选框等控件组成&#xff0c;用以收集、校验、提交数据&#xff0c;组件升级采用了 flex 布局&#xff0c;以替代旧版本的 float 布局。 在 Element Plus 中&#xff0c;el-form 是一个表单组件&#xff0c;用于创建表单以便用户填写和提…

东方 - 循环(2) - 求和计数

目录 解析部分&#xff1a;求和计数1002. 编程求解123...n问题描述解题思路代码实现代码解析 1741. 求出1~n中满足条件的数的个数和总和问题描述解题思路代码实现代码解析 1003. 编程求 \(1 3 5 ... n\)问题描述解题思路代码实现代码解析 1004. 编程求1*2*3*...*n问题描述解…

C语言:文件操作(一)

目录 前言 1、为什么使用文件 2、什么是文件 2.1 程序文件 2.2 数据文件 2.3 文件名 3、文件的打开和关闭 3.1 文件指针 3.2 文件的打开和关闭 结&#xff08;一&#xff09; 前言 本篇文章将介绍C语言的文件操作&#xff0c;在后面的内容讲到&#xff1a;为什么使用文…

《Effective C++》《构造/析构/赋值运算——9、绝不在构造和析构过程中调用virtual函数》

文章目录 1、Terms 9:Never call virtual functions during construction or destruction1.1为什么不要在构造、析构函数中调用 virtual 函数1.1.1经典错误1.1.2 隐藏错误 1.2优化做法&#xff1a; 2、面试相关3、总结4、参考 1、Terms 9:Never call virtual functions during …

网络编程的学习2

UDP通信协议 发送数据 package UDPDEmo;import java.io.IOException; import java.net.*; import java.nio.charset.StandardCharsets;public class SendMessageDemo {public static void main(String[] args) throws IOException {//发送数据//1.创建对象//细节&#xff1a;…

PCL点云库出现错误:..\dist.h(523): error C3861: “pop_t”: 找不到标识符

工程代码&#xff1a;简单地测试了k-d树的最近邻搜索功能 #include<pcl/point_cloud.h> #include<pcl/kdtree/kdtree_flann.h>#include<iostream> #include<vector> #include<ctime>using namespace std;int main(int argc, char** argv) {//使…

回溯算法|78.子集

力扣题目链接 class Solution { private:vector<vector<int>> result;vector<int> path;void backtracking(vector<int>& nums, int startIndex) {result.push_back(path); // 收集子集&#xff0c;要放在终止添加的上面&#xff0c;否则会漏掉自…

Pygame基础8-碰撞

Collisions 在Pygame中&#xff0c;我们使用矩形来移动物体&#xff0c;并且用矩形检测碰撞。 colliderect检测两个矩形是否碰撞&#xff0c;但是没法确定碰撞的方向。 Rect1.colliderect(Rect2) # collision -> return Ture # else -> return Falsecollidepoint可以…

Spring拓展点之SmartLifecycle如何感知容器启动和关闭

Spring为我们提供了拓展点感知容器的启动与关闭&#xff0c;从而使我们可以在容器启动或者关闭之时进行定制的操作。Spring提供了Lifecycle上层接口&#xff0c;这个接口只有两个方法start和stop两个方法&#xff0c;但是这个接口并不是直接提供给开发者做拓展点&#xff0c;而…

如何理解 Java 中的成员变量、字段和属性

在Java编程中&#xff0c;成员变量、字段和属性是关键概念&#xff0c;用于存储对象状态信息的变量。虽然它们经常被用作同义词&#xff0c;但在实际应用中&#xff0c;它们有着微妙的区别。 1. 成员变量&#xff08;Member Variable&#xff09; 成员变量是类中声明的任何变…

AI音乐GPT时刻来临:Suno 快速入门手册!

✨✨ 欢迎大家来访Srlua的博文&#xff08;づ&#xffe3;3&#xffe3;&#xff09;づ╭❤&#xff5e;✨✨ &#x1f31f;&#x1f31f; 欢迎各位亲爱的读者&#xff0c;感谢你们抽出宝贵的时间来阅读我的文章。 我是Srlua小谢&#xff0c;在这里我会分享我的知识和经验。&am…

代码块的理解

如果成员变量想要初始化的值不是一个硬编码的常量值&#xff0c;而是需要通过复杂的计算或读取文件、或读取运行环境信息等方式才能获取的一些值&#xff0c;该怎么办呢&#xff1f;此时&#xff0c;可以考虑代码块&#xff08;或初始化块&#xff09;。 代码块(或初始化块)的作…

前端作业之完成学校官方网页的制作

&#xff08;未使用框架&#xff0c;纯html和css制作&#xff09; 注&#xff1a;由本人技术限制&#xff0c;代码复用性极差 代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>xxx大学</tit…

SpringBoot接收参数的方式

Get 请求 1.1 以方法的形参接收参数 1.这种方式一般适用参数比较少的情况 RestController RequestMapping("/user") Slf4j public class UserController {GetMapping("/detail")public Result<User> getUserDetail(String name,String phone) {log.…

zip解压异常java.lang.IllegalArgumentException: MALFORMED处理

使用hutool解压zip包时出错&#xff1a; //压缩包解压到固定目录 ZipUtil.unzip(tempZipFile,dir);在解压文件的时候报错&#xff0c;原因是压缩文件中有中文&#xff1b;导致错误&#xff0c;解决办法是设置编码&#xff1a; ZipFile tempZipFile new ZipFile(zipFile, Cha…