babyAGI(8)-babyCoder5主程序逻辑

前期代码都以阅读完毕,接下来我们来看主程序逻辑,建议大家好好看看流程图,有个流程的影响
在这里插入图片描述

1. 创建任务

下面一段代码主要用来创建任务以及打印相关信息,调用了四个agents

  • code_tasks_initializer_agent 初始化任务
  • code_tasks_refactor_agent 重构任务,确保任务符合agent格式
  • code_tasks_details_agent 给任务添加详细信息字段
  • code_tasks_context_agent 给任务添加上下文字段
print_colored_text(f"****Objective****", color='green')
print_char_by_char(OBJECTIVE, 0.00001, 10)# Create the tasks
print_colored_text("*****Working on tasks*****", "red")
print_colored_text(" - Creating initial tasks", "yellow")
task_agent_output = code_tasks_initializer_agent(OBJECTIVE)
print_colored_text(" - Reviewing and refactoring tasks to fit agents", "yellow")
task_agent_output = code_tasks_refactor_agent(OBJECTIVE, task_agent_output)
print_colored_text(" - Adding relevant technical details to the tasks", "yellow")
task_agent_output = code_tasks_details_agent(OBJECTIVE, task_agent_output)
print_colored_text(" - Adding necessary context to the tasks", "yellow")
task_agent_output = code_tasks_context_agent(OBJECTIVE, task_agent_output)
print()print_colored_text("*****TASKS*****", "green")
print_char_by_char(task_agent_output, 0.00000001, 10)

2. 任务列表处理

首先要做的是,将任务输出的值序列化为json和计算代码库中的文件的嵌入值,然后循环任务列表中的每一个任务,下面的代码将包含在循环中!

task_json = json.loads(task_agent_output)embeddings = Embeddings(current_directory)for task in task_json["tasks"]:

这段代码将通过task_assigner_agenttask_assigner_recommendation_agent将任务任务分配到指定的task_assigner中,也就是将任务分配到agent中
这段代码中包含人可以反馈的部分,感兴趣的话可以把注释去掉试试

task_description = task["description"]
task_isolated_context = task["isolated_context"]print_colored_text("*****TASK*****", "yellow")
print_char_by_char(task_description)
print_colored_text("*****TASK CONTEXT*****", "yellow")
print_char_by_char(task_isolated_context)# HUMAN FEEDBACK
# Uncomment below to enable human feedback before each task. This can be used to improve the quality of the tasks,
# skip tasks, etc. I believe it may be very relevant in future versions that may have more complex tasks and could
# allow a ton of automation when working on large projects.
#
# Get user input as a feedback to the task_description
# print_colored_text("*****TASK FEEDBACK*****", "yellow")
# user_input = input("\n>:")
# task_description = task_human_input_agent(task_description, user_input)
# if task_description == "<IGNORE_TASK>":
#     continue
# print_colored_text("*****IMPROVED TASK*****", "green")
# print_char_by_char(task_description)# Assign the task to an agent
task_assigner_recommendation = task_assigner_recommendation_agent(OBJECTIVE, task_description)
task_agent_output = task_assigner_agent(OBJECTIVE, task_description, task_assigner_recommendation)print_colored_text("*****ASSIGN*****", "yellow")
print_char_by_char(task_agent_output)# 获取选择完成的agent
chosen_agent = json.loads(task_agent_output)["agent"]

下面就要根据选择的agent开始执行任务了

3.具体任务执行

执行名称agent,调用command_executor_agent方法,这里调用了subprocess 库中的POpen方法

if chosen_agent == "command_executor_agent":command_executor_output = command_executor_agent(task_description, task["file_path"])print_colored_text("*****COMMAND*****", "green")print_char_by_char(command_executor_output)command_execution_output = execute_command_json(command_executor_output)

然后整个是一个else,执行代码相关的agent

3.1 调用code_writer_agent

写代码agent,在这里有以下几步

  • 计算代码库中的嵌入值
  • 获取当前目录路径
  • 使用file_management_agent计算出文件路径
  • 使用code_writer_agent 编写代码,传入任务描述、任务上下文以及相关代码
  • 将代码写入文件中
# CODE AGENTS
if chosen_agent == "code_writer_agent":# Compute embeddings for the codebase# This will recompute embeddings for all files in the 'playground' directoryprint_colored_text("*****RETRIEVING RELEVANT CODE CONTEXT*****", "yellow")embeddings.compute_repository_embeddings()relevant_chunks = embeddings.get_relevant_code_chunks(task_description, task_isolated_context)current_directory_files = execute_command_string("ls")file_management_output = file_management_agent(OBJECTIVE, task_description, current_directory_files, task["file_path"])print_colored_text("*****FILE MANAGEMENT*****", "yellow")print_char_by_char(file_management_output)file_path = json.loads(file_management_output)["file_path"]code_writer_output = code_writer_agent(task_description, task_isolated_context, relevant_chunks)print_colored_text("*****CODE*****", "green")print_char_by_char(code_writer_output)# Save the generated code to the file the agent selectedsave_code_to_file(code_writer_output, file_path)

3.2 调用code_refactor_agent

整个函数有以下几个步骤

  • 获取当前路径
  • 使用file_management_agent获取文件路径
  • 将代码分割为最大为80行的代码段,使用code_relevance_agent计算代码段与任务的相关性,存储到relevance_scores 列表中
  • 获取关联性最大的代码段,调用code_refactor_agent进行重构
  • 调用refactor_code,将重构的代码写入到文件中
elif chosen_agent == "code_refactor_agent":# The code refactor agent works with multiple agents:# For each task, the file_management_agent is used to select the file to edit.Then, the # code_relevance_agent is used to select the relevant code chunks from that filewith the # goal of finding the code chunk that is most relevant to the task description. This is # the code chunk that will be edited. Finally, the code_refactor_agent is used to edit # the code chunk.current_directory_files = execute_command_string("ls")file_management_output = file_management_agent(OBJECTIVE, task_description, current_directory_files, task["file_path"])file_path = json.loads(file_management_output)["file_path"]print_colored_text("*****FILE MANAGEMENT*****", "yellow")print_char_by_char(file_management_output)# Split the code into chunks and get the relevance scores for each chunkcode_chunks = split_code_into_chunks(file_path, 80)print_colored_text("*****ANALYZING EXISTING CODE*****", "yellow")relevance_scores = []for chunk in code_chunks:score = code_relevance_agent(OBJECTIVE, task_description, chunk["code"])relevance_scores.append(score)# Select the most relevant chunkselected_chunk = sorted(zip(relevance_scores, code_chunks), key=lambda x: x[0], reverse=True)[0][1]# Refactor the codemodified_code_output = code_refactor_agent(task_description, selected_chunk, context_chunks=[selected_chunk], isolated_context=task_isolated_context)# Extract the start_line and end_line of the selected chunk. This will be used to replace the code in the original filestart_line = selected_chunk["start_line"]end_line = selected_chunk["end_line"]# Count the number of lines in the modified_code_outputmodified_code_lines = modified_code_output.count("\n") + 1# Create a dictionary with the necessary information for the refactor_code functionmodified_code_info = {"start_line": start_line,"end_line": start_line + modified_code_lines - 1,"modified_code": modified_code_output}print_colored_text("*****REFACTORED CODE*****", "green")print_char_by_char(modified_code_output)# Save the refactored code to the filerefactor_code([modified_code_info], file_path)

下一篇文章,我将不在进行逐行解释,只发出比较重点的代码段,逻辑与之前的大致相同

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

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

相关文章

信息系统项目管理师——第18章项目绩效域管理(二)

项目工作绩效域 预期目标 高效且有数的项目绩效 2.适合项目和环境的项目过程 3.干系人适当的沟通和参与 4.对实物资源进行了有效管理 5.对采购进行了有效管理 6.有效处理了变更 7.通过持续学习和过程改进提高了团队能力 绩效要点 1.项目过程 2.项目制约因素 3.专注于工作过…

React - 连连看小游戏

简介 小时候经常玩连连看小游戏。在游戏中&#xff0c;当找到2个相同的元素就可以消除元素。 本文会借助react实现连连看小游戏。 实现效果 实现难点 1.item 生成 1. 每一个图片都是一个item&#xff0c;items数组的大小为size*size。 item对象包括grid布局的位置&#xff0c;…

【爬虫开发】爬虫从0到1全知识md笔记第4篇:Selenium课程概要,selenium的介绍【附代码文档】

爬虫开发从0到1全知识教程完整教程&#xff08;附代码资料&#xff09;主要内容讲述&#xff1a;爬虫课程概要&#xff0c;爬虫基础爬虫概述,,http协议复习。requests模块&#xff0c;requests模块1. requests模块介绍,2. response响应对象,3. requests模块发送请求,4. request…

入门用Hive构建数据仓库

在当今数据爆炸的时代&#xff0c;构建高效的数据仓库是企业实现数据驱动决策的关键。Apache Hive 是一个基于 Hadoop 的数据仓库工具&#xff0c;可以轻松地进行数据存储、查询和分析。本文将介绍什么是 Hive、为什么选择 Hive 构建数据仓库、如何搭建 Hive 环境以及如何在 Hi…

类与对象(一)

目录 一、类的引入和定义 二、类的访问限定符及封装 1&#xff09;访问限定符 2&#xff09;封装 三、类的作用域和实例化 1&#xff09;类的作用域 2&#xff09;实例化 四、类的大小 1&#xff09;类的大小计算方式 2&#xff09;特殊的类的大小 五、this指针 1&…

C++设计模式:观察者模式(三)

1、定义与动机 观察者模式定义&#xff1a;定义对象间的一种1对多&#xff08;变化&#xff09;的依赖关系&#xff0c;以便当一个对象&#xff08;Subject&#xff09;的状态发生比改变时&#xff0c;所有依赖于它的对象都得到通知并且自动更新 再软件构建过程中&#xff0c…

小程序如何设置余额充值和消费功能

小程序中设置余额充值和消费功能非常重要的&#xff0c;通过让客户在小程序中进行余额充值&#xff0c;不仅可以提高用户粘性&#xff0c;还可以促进消费&#xff0c;增加用户忠诚度。以下是如何在小程序中设置余额充值和消费功能的步骤&#xff1a; 1. **设计充值入口**&…

关于Idea无法正常启动

编辑这个文件 最后一行 加上 pause 双击文件 会显示报错信息

npm install node-sass报错

前言 在使用 node-sass 时&#xff0c;你可能会遇到安装 node-sass 时出现各种错误的情况。在本文中&#xff0c;我们将探讨一些常见的 node-sass 安装错误&#xff0c;以及如何解决它们。 无论你是初学者还是有经验的开发者&#xff0c;本文都将为你提供有用的信息和技巧&…

Vue学习笔记-S1

1 什么是Vue Vue是一款用于构建用户界面的渐进式JavaScripte框架&#xff0c;可基于数据渲染用户页面. 1.1 Vue的知识架构 Vue核心包&#xff1a;声明式渲染、组件系统Vue构建&#xff1a;客户端路由、状态管理、构建工具局部使用Vue&#xff1a;快速入门、常用指令、生命周…

解读TF存储卡

一、TF简史 TF卡的故事&#xff0c;要从20年前谈起…… 2000年1月&#xff0c;松下闪迪东芝成立SD协会&#xff08;SD Association&#xff0c;简称SDA&#xff09;&#xff0c;专注于制定并推广SD存储卡的产业标准。二十年后的今天&#xff0c;SDA企业会员已发展至800多家&a…

车载电子电器架构 —— 软件下载

车载电子电器架构 —— 软件下载 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明自己,无…

Vue依赖注入,详细解析

Prop 逐级透传问题​ 通常情况下&#xff0c;当我们需要从父组件向子组件传递数据时&#xff0c;会使用 props。想象一下这样的结构&#xff1a;有一些多层级嵌套的组件&#xff0c;形成了一颗巨大的组件树&#xff0c;而某个深层的子组件需要一个较远的祖先组件中的部分数据。…

各类聚类算法整理

各类聚类算法整理 0. 先验的基础知识1. K-Means2. GMM3. EM算法4.Spectral Clustering5. Mean Shift6. DBSCAN 本篇将介绍整理各种聚类算法&#xff0c;包括k-means&#xff0c;GMM(Guassian Mixture Models, 高斯混合)&#xff0c;EM(Expectation Maximization&#xff0c;期望…

C#基础知识总结

C语言、C和C#的区别 ✔ 面向对象编程&#xff08;OOP&#xff09;&#xff1a; C 是一种过程化的编程语言&#xff0c;它不直接支持面向对象编程。然而&#xff0c;C 是一种支持 OOP 的 C 的超集&#xff0c;它引入了类、对象、继承、多态等概念。C# 是完全面向对象的&#xff…

TCP三次握手过程及抓包分析

TCP三次握手过程 一、TCP分段格式二、TCP三次握手三、Wireshark抓包分析 一、TCP分段格式 二、TCP三次握手 三、Wireshark抓包分析

设计模式总结-组合模式

组合设计模式 模式动机模式定义模式结构组合模式实例与解析实例一&#xff1a;水果盘实例二&#xff1a;文件浏览 更复杂的组合总结 模式动机 对于树形结构&#xff0c;当容器对象&#xff08;如文件夹&#xff09;的某一个方法被调用时&#xff0c;将遍历整个树形结构&#x…

刷题之Leetcode27题(超级详细)

27. 移除元素 力扣题目链接(opens new window)https://leetcode.cn/problems/remove-element/ 给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素&#xff0c;并返回移除后数组的新长度。 不要使用额外的数组空间&#xff0c;你必须仅使用…

一篇文章带你掌握二叉树(附带二叉树基本操作完整代码演示,和两种思路)

【本长内容】 1. 掌握树的基本概念 2. 掌握二叉树概念及特性 3. 掌握二叉树的基本操作 4. 完成二叉树相关的面试题练习 1. 树形结构 1.1 概念 树是一种非线性的数据结构&#xff0c;它是由n&#xff08;n>0&#xff09;个有限结点组成一个具有层次关系的集合。把它叫做树是…

Vue - 2( 10000 字 Vue 入门级教程)

一&#xff1a;Vue 1.1 绑定样式 1.1.1 绑定 class 样式 <!DOCTYPE html> <html><head><meta charset"UTF-8" /><title>绑定样式</title><style>......</style><script type"text/javascript" src&…