
LLM多智能体AutoGen教程
本篇演示了如何使用AutoGen编写一个自动完成代码编写和修改的案例,实际上我们还可以再增加一个审查代码的Agent负责review,并增加一个测试负责编写测试用例等。本篇只是AutoGen自动编码测试的小试牛刀,更高阶的用法等着我们去探索和实现。其中也遇到不少关于通义千问使用中的问题,希望对你有所帮助。那么,我们该如何学习大模型?作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。至于
1. 代码执行器
在AutoGen中有两种类型的代码执行器,一种是命令行式的执行代码(类似python script.py
),另一种是Jupyter式的执行代码(以jupyter kernel这种交互式的执行)。每种每类的执行器都可以运行在本地或者是Docker容器中,我个人的建议是不要直接运行在本地,而是使用Docker,谁知道LLM会不会生成rm -r *
呢?他们三个分别位于以下目录中。
- autogen.coding.docker_commandline_code_executor.DockerCommandLineCodeExecutor
- autogen.coding.local_commandline_code_executor.LocalCommandlineCodeExecutor
- autogen.coding.jupyter.jupyter_code_executor.JupyterCodeExecutor
以DockerCommandLineCodeExecutor
为例,他使用以下参数进行初始化:
- image - docker镜像名称,默认python:3-slim
- container_name - 容器名称,为None时会自动生成。
- time_out - 代码执行超时时间
- work_dir - 工作目录,默认代码执行目录,默认是当前目录,这里有个要注意的地方,后面实际使用的时候会说到。
- auto_remove - 是否执行完成后自动删除容器
- stop_container - 是否自动停止
它的工作原理比较简单,如下图所示。
使用如下代码,实例化一个Docker容器代码执行器就比较明确了。
work_dir = "code"
executor = DockerCommandLineCodeExecutor(
image="python:3-slim",
container_name="autogen-python3",
timeout=60,
work_dir=work_dir,
auto_remove=True,
stop_container=True
)
接下来就是实例化两个Agent了,一个外包弟弟Agent,专门负责写代码和修改代码,一个是用户代理,也就是我,负责提需求和审查代码。
2. 实例化Agent
在此之前,我们都使用使用ConversableAgent
进行实例化,其实AutoGen提供了两个ConversableAgent
的子类AssistantAgent
和UserProxyAgent
方便使用,就是为简化代码编写类Agent的初始化和人类代理的初始化。
其中UserProxyAgent
初始化参数和ConversableAgent
一致,只有一些默认配置变了,比如
- human_input_mode - 默认就是"ALWAYS",父类是
TERMINATE
- llm_config - 默认为
False
,父类是None
- code_execution_cofnig - 默认为空字典,父类是
None
- description - 默认是根据
human_input_mode
从字典DEFAULT_USER_PROXY_AGENT_DESCRIPTIONS
中选取描述,父类默认是和system_message一致。
DEFAULT_USER_PROXY_AGENT_DESCRIPTIONS = {
"ALWAYS": "An attentive HUMAN user who can answer questions about the task, and can perform tasks such as running Python code or inputting command line commands at a Linux terminal and reporting back the execution results.",
"TERMINATE": "A user that can run Python code or input command line commands at a Linux terminal and report back the execution results.",
"NEVER": "A computer terminal that performs no other action than running Python scripts (provided to it quoted in ```python code blocks), or sh shell scripts (provided to it quoted in ```sh code blocks).",
}
它的默认描述,是用于LLM自动选择Agent应答时候使用。
AssistantAgent
就是针对写代码的Agent进行一些默认化配置,比如
- system_message - 默认一段Prompt设定,由
DEFAULT_SYSTEM_MESSAGE
设定。 - human_input_mode - 默认为
NEVER
默认系统Prompt说明它的任务是通过编码和语言技能解决任务,要求在遇到需要收集信息,诸如:浏览网络,读取下载文件,打印网页或者文件,获取当前事件,检查操作系统等。亦或是在需要执行代码任务和输出结果时。这里再次引导LLM解决任务要一步一步,并且解释计划等,而且要求输出整段代码不要使用代码块。此外,要求它在在代码第一行放入文件名。此外,如果遇到错误,要自行分析问题,自行修正代码。最后,找到问题答案时候,在最后输出TERMINATE。
DEFAULT_SYSTEM_MESSAGE = """You are a helpful AI assistant.
Solve tasks using your coding and language skills.
In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.
1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time, check the operating system. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.
2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.
Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.
When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
If you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.
Reply "TERMINATE" in the end when everything is done.
"""
使用如下代码实例化外包弟弟Agent和用户Agent:
assistant = AssistantAgent(
name="assistant",
llm_config=llm_config,
code_execution_config=False,
)
user = UserProxyAgent(
name="executor",
code_execution_config={"executor": executor},
human_input_mode="ALWAYS",
default_auto_reply="continue"
)
你应该注意到,在实例化UserProxyAgent
中有设置default_auto_reply
为continue
,这是因为通义千问不允许空的回复给它,否则可能会400报错。
openai.BadRequestError: Error code: 400 - {'error': {'code': 'invalid_parameter_error', 'param': None, 'message': 'Role must be user or assistant and Content length must be greater than 0', 'type': 'invalid_request_error'}}
之后使用initial_chat
描述任务开始执行。
user.initiate_chat(assistant, max_turns=5, message="""
读取一个位于https://github.com/xxx/vmmr.txt文件,该文件每一行类似acura_cdx_2016格式,要求提取第一个_之前的字符串,它是车厂名字,提取所有的车厂后将其写到一个vm.txt文件中
""")
3. 执行
在它编写出第一段程序后,我审查了代码,我告诉它车厂需要去重,他立刻修改了代码,我审查后表示没问题。开始执行代码报错,因为没有按住requests包,他又自己安装request包,然后执行并将车厂名称文件保存到vm.txt。那么生成的代码和文件存储在哪里呢?就是我们在code_executor
中设置的work_dir,它在启动Docker容器时候会将该目录映射到自己的工作目录,因此如果你有文件需要处理,可以保存到这个目录,Docker中的程序就能读取到,如下图所示。
完整的流程比较厂就不贴了,只贴刚开始的
executor (to assistant):
读取一个位于[https://xxx/vmmr.txt文件,该文件每一行类似acura_cdx_2016格式,要求提取第一个_之前的字符串,它是车厂名字,提取所有的车厂后将其写到一个vm.txt文件中]
assistant (to executor): 为了提取出所有车厂的名字,我们可以使用Python来读取文件并处理每一行。以下是一个简单的脚本,它将从给定的URL下载文件,提取每个行的第一个_之前的字符串,并将其写入到名为"vm.txt"的输出文件中。
# filename: extract_car_makers.py import requests import re # Download the file from the given URL response = requests.get("https://xxxx/vmmr.txt") file_content = response.text # Extract car makers and write them to a file with open("vm.txt", "w") as output_file: for line in file_content.splitlines(): car_maker = re.search(r'^([^\_]+)', line) if car_maker: output_file.write(car_maker.group(1) + "\n") print("Car makers have been extracted and saved to vm.txt.")
请保存上面的代码为extract_car_makers.py文件并在命令行中执行:
python extract_car_makers.py
这将运行脚本并创建一个名为"vm.txt"的文件,其中包含所有提取的汽车制造商名称。 如果您在执行此脚本时遇到任何问题,请告诉我错误消息以便我帮助您解决。
################
Provide feedback to assistant. Press enter to skip and use auto-reply, or type ‘exit’ to end the conversation: 车厂名称需要去重
executor (to assistant): 车厂名称需要去重 …
最后输出的文件,也存储在我们设置的code
文件夹中。
4. 总结
本篇演示了如何使用AutoGen编写一个自动完成代码编写和修改的案例,实际上我们还可以再增加一个审查代码的Agent负责review,并增加一个测试负责编写测试用例等。本篇只是AutoGen自动编码测试的小试牛刀,更高阶的用法等着我们去探索和实现。其中也遇到不少关于通义千问使用中的问题,希望对你有所帮助。
那么,我们该如何学习大模型?
作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。
一、大模型全套的学习路线
学习大型人工智能模型,如GPT-3、BERT或任何其他先进的神经网络模型,需要系统的方法和持续的努力。既然要系统的学习大模型,那么学习路线是必不可少的,下面的这份路线能帮助你快速梳理知识,形成自己的体系。
L1级别:AI大模型时代的华丽登场
L2级别:AI大模型API应用开发工程
L3级别:大模型应用架构进阶实践
L4级别:大模型微调与私有化部署
一般掌握到第四个级别,市场上大多数岗位都是可以胜任,但要还不是天花板,天花板级别要求更加严格,对于算法和实战是非常苛刻的。建议普通人掌握到L4级别即可。
以上的AI大模型学习路线,不知道为什么发出来就有点糊,高清版可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费
】

二、640套AI大模型报告合集
这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。
三、大模型经典PDF籍
随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。
四、AI大模型商业化落地方案
作为普通人,入局大模型时代需要持续学习和实践,不断提高自己的技能和认知水平,同时也需要有责任感和伦理意识,为人工智能的健康发展贡献力量。
更多推荐
所有评论(0)