前期代码都以阅读完毕,接下来我们来看主程序逻辑,建议大家好好看看流程图,有个流程的影响
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_agent
和task_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)
下一篇文章,我将不在进行逐行解释,只发出比较重点的代码段,逻辑与之前的大致相同