AI Agent在投资领域比较火爆,代码助手的投资也比较多,单AI Agent,多角色AI Agent逐渐会成为AI应用的方向。最近一个10岁孩子直接使用豆包和两个ChatGPT进行愚公该移山还是该搬家的辩论活动吸引了大家的眼球,用户作为主持人角色指导2个GPT进行正反方辩友进行辩论和1个GPT作为评委进行评价,这个就是典型的多角色AI代理的应用场景,使用了3个GPT,其实一个也可以的,虽然这个活动最终没有达到预期,Chat GPT作为评委的代理,虽然没有评论,但都记录了它“听”到的内容。这个场景是直接使用GPT作为代理的例子,多角色AI Agent打开走向AGI的想象空间。下面以我比较熟悉的领域,尝试实现一个多角色AI代理编写代码的例子。涉及的角色为:
用户UserInterface:输入任务指令,并在任务无法完成时,进行指导,如:修改代码。
需求分析RequirementAnalyzer:针对用户输入的指令进行需求分析。
代码结构ProgramStructure:针对需求,提出合理的代码结构。
编写代码CodeGenerator:根据需求及代码结构和语言设置,编写代码。
代码审查CodeReviewer:针对生成的代码进行代码审查,提出改进意见。
代码执行CodeExecutor:执行审查完成的代码,输出测试结果。
协作交换Coordinator:协作交换逻辑,通过多次迭代完成任务。
完整代码:
import gradio as gr
import requests
import subprocess
import logging
import json
from io import StringIO
import threading
import time
# 配置日志记录
log_stream = StringIO()
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(), # 输出到控制台
logging.StreamHandler(log_stream) # 输出到 StringIO 流
]
)
# 定义RequirementAnalyzer角色
class RequirementAnalyzer:
def __init__(self, api_url, model):
self.api_url = api_url
self.model = model
def analyze_instruction(self, instruction):
messages = [
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": f"分析以下指令: {instruction}"}
]
response = requests.post(self.api_url, json={"messages": messages, "model": self.model, "stream": True}, stream=True)
analysis = self.process_stream(response)
logging.info(f"[RequirementAnalyzer] 分析的需求: {analysis}")
return analysis
def process_stream(self, response):
content = ""
for chunk in response.iter_content(chunk_size=None):
if chunk:
chunk_data = chunk.decode('utf-8')
if "done" in chunk_data:
chunk_data = chunk_data.split("data: ")[-1]
chunk_json = json.loads(chunk_data)
if chunk_json["done"]:
break
else:
content += chunk_json["message"]["content"]
return content
# 定义ProgramStructure角色
class ProgramStructure:
def __init__(self, api_url, model):
self.api_url = api_url
self.model = model
def determine_structure(self, requirement):
messages = [
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": f"确定以下需求的类结构: {requirement}"}
]
response = requests.post(self.api_url, json={"messages": messages, "model": self.model, "stream": True}, stream=True)
structure = self.process_stream(response)
logging.info(f"[ProgramStructure] 确定的结构: {structure}")
return structure
def process_stream(self, response):
content = ""
for chunk in response.iter_content(chunk_size=None):
if chunk:
chunk_data = chunk.decode('utf-8')
if "done" in chunk_data:
chunk_data = chunk_data.split("data: ")[-1]
chunk_json = json.loads(chunk_data)
if chunk_json["done"]:
break
else:
content += chunk_json["message"]["content"]
return content
# 定义CodeGenerator角色
class CodeGenerator:
def __init__(self, api_url, model):
self.api_url = api_url
self.model = model
def generate_code(self, requirement, structure, language):
messages = [
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": f"根据以下需求和结构编写{language}代码: {requirement}, {structure}"}
]
response = requests.post(self.api_url, json={"messages": messages, "model": self.model, "stream": True}, stream=True)
code = self.process_stream(response)
code = self.extract_code(code) # 提取代码部分
logging.info(f"[CodeGenerator] 生成的