2024年10月7日

智能体实战之autogen:智能体生成shell脚本并执行

作者 柳 永强
内容目录

使用智能体生成脚本并执行

智能体如何使用工具,是研究智能体非常重要的一部分。通过使用脚本,可以赋予智能体使用Linux系统的各式各样的命令行工具来执行特定任务,本文提供非常简单的例子,使用autogen生成脚本并执行用户指定的任务。

智能体介绍

参考文章:什么是智能体

Autogen

AutoGen是一个开源编程框架,用于构建AI智能体并促进多个智能体之间的合作以解决任务。AutoGen旨在提供一个易于使用且灵活的框架,以加速智能体AI的开发和研究,类似于深度学习中的PyTorch。它提供的功能包括能够与其他智能体对话的智能体、支持LLM和工具使用、自治和人机协作工作流程以及多智能体对话模式。

主要功能

  • AutoGen使得基于多智能体对话构建下一代LLM应用程序变得轻松便捷。它简化了复杂LLM工作流程的协调、自动化和优化,最大化LLM模型的性能,并克服其弱点。
  • 它支持多种对话模式以应对复杂工作流程。通过可定制且可对话的智能体,开发人员可以使用AutoGen构建与对话自主性、智能体数量和智能体对话拓扑相关的各种对话模式。
  • 它提供了一系列具有不同复杂性的工作系统。这些系统涵盖了来自各种领域和复杂性的广泛应用,展示了AutoGen如何轻松支持多样的对话模式。

环境准备

  • Linux or Windows WSL
  • python 3.10+
  • 安装 anaconda

本文使用anaconda构建python虚拟环境:


#创建autogen的虚拟环境
conda create --name autogen python=3.11

#进入该虚拟环境
conda activate autogen

# 安装autogen
pip install autogen-agentchat~=0.2

导入相关的依赖库

import os
import tempfile

from autogen import ConversableAgent
from autogen.coding import LocalCommandLineCodeExecutor

构建系统提示词

使用系统提示词定义智能体的角色,指导行为,提供一定的背景信息,同时也可以约束智能体,当用户行为超出智能体能力边界时,提示用户。

以下提示词指导智能体通过生成脚本,来收集Linux系统的一些信息。该提示词为one-shot提示词。提供代码块的样例给大模型。

以下系统提示词将传递给代码生成智能体,指导代码生成智能体生成脚本代码。


script_executor_message = """You are shell script assistant, a expert of linux and shell script, and you will generate shell script and get information according to user's prompt.
Gnerate a script according user's requirments with below format:

    #```bash  此处因为显示格式的原因加了注释,实际使用时请删除#
    #!/bin/bash

    # query all files information in current directory
    ls -la

    #``` 此处因为显示格式的原因加了注释,实际使用时请删除#
Always thinking step by step to about users questions, make sure your answer is correct and helpful.
If user did not ask question about Linux system, then please response like:

Please ask me question about Linux system, I only could help you on that.

"""

创建和脚本执行器

代码执行器是autogen智能体的一个组件,在创建autogen智能体实例之前需要先初始化代码执行器,并作为参数传递给autogen智能体的实例,代码执行器主要用来执行代码。

# Create a temporary directory to store the code files.
temp_dir = tempfile.TemporaryDirectory()

# Create a local command line code executor.
script_executor = LocalCommandLineCodeExecutor(
    timeout=10,  # Timeout for each code execution in seconds.
    work_dir=temp_dir.name,  # Use the temporary directory to store the code files.
)

创建代码生成智能体实例

ConversableAgent class 是对智能体的抽象,autogen中使用该类来创建智能体实例。通常你需要提供一些参数,max_consecutive_auto_reply=1 配置智能体只能回复一次,system_message用来配置系统提示词。llm_config 用来配置大模型作为后端,我选择使用deepseek-chat模型,国产模型性价比非常高,api调用也没有太多限制。代码生成方面,deepseek已经非常优秀了。code_execution_config=False 配置关闭代码执行功能。human_input_mode="NEVER"配置智能体自动回复,不需要人的介入。

以下代码创建了一个负责生成脚本代码的智能体实例。

script_generate_agent = ConversableAgent(
    "code_writer_agent",
    max_consecutive_auto_reply=1,
    system_message=script_executor_message,
    llm_config={"config_list": [{"model": "deepseek-chat", "api_key": os.environ["OPENAI_API_KEY"], "base_url":"https://api.deepseek.com"}]},
    code_execution_config=False,  # Turn off code execution for this agent.
    human_input_mode="NEVER",
)

创建代码执行智能体

以下代码创建了代码执行的智能体实例,只负责解析和执行代码,因此该智能体不需要接入大模型,配置llm_config=False。code_execution_config={"executor": script_executor} 用来配置代码执行器。

# Create an agent with code executor configuration.
code_executor_agent = ConversableAgent(
    "code_executor_agent",
    max_consecutive_auto_reply=1,
    system_message=script_executor_message,
    llm_config=False,
    code_execution_config={"executor": script_executor},  # Use the local command line code executor.
    human_input_mode="NEVER", 
)

初始化任务

以下代码最终将会启动两个智能体,首先脚本生成智能体将根据输入信息生成相应的脚步,代码执行智能体接受脚本生成智能体的输出作为输入,解析脚本,并执行。

chat_result = code_executor_agent.initiate_chat(
    script_generate_agent,
    message="Get Linux OS information",
)

执行结果如下:

$ python simple_script_executor.py
flaml.automl is not available. Please install flaml[automl] to enable AutoML functionalities.
code_executor_agent (to code_writer_agent):

Get Linux OS information

--------------------------------------------------------------------------------
[autogen.oai.client: 10-07 11:47:55] {349} WARNING - Model deepseek-chat is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
code_writer_agent (to code_executor_agent):

    #```bash 此处因为显示格式的原因加了注释,实际使用时请删除#
    #!/bin/bash

    # Get Linux OS information

    # Display the Linux distribution information
    echo "Linux Distribution Information:"
    lsb_release -a 2>/dev/null || cat /etc/*release 2>/dev/null || cat /etc/issue 2>/dev/null
    echo ""

    # Display kernel version
    echo "Kernel Version:"
    uname -r
    echo ""

    # Display system architecture
    echo "System Architecture:"
    uname -m
    echo ""

    # Display CPU information
    echo "CPU Information:"
    lscpu
    echo ""

    # Display memory information
    echo "Memory Information:"
    free -h
    echo ""

    # Display disk usage
    echo "Disk Usage:"
    df -h
    echo ""

    # Display network interfaces
    echo "Network Interfaces:"
    ip a
    echo ""

    # Display hostname
    echo "Hostname:"
    hostname
    echo ""

    # Display uptime
    echo "Uptime:"
    uptime
    echo ""
    #``` 此处因为显示格式的原因加了注释,实际使用时请删除#

This script will provide a comprehensive overview of your Linux system, including distribution information, kernel version, system architecture, CPU information, memory usage, disk usage, network interfaces, hostname, and uptime.

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING CODE BLOCK (inferred language is bash)...
code_executor_agent (to code_writer_agent):

exitcode: 0 (execution succeeded)
Code output: Linux Distribution Information:
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy

Kernel Version:
5.15.153.1-microsoft-standard-WSL2

System Architecture:
x86_64

CPU Information:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      39 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             12
On-line CPU(s) list:                0-11
Vendor ID:                          GenuineIntel
Model name:                         12th Gen Intel(R) Core(TM) i5-12400
CPU family:                         6
Model:                              151
Thread(s) per core:                 2
Core(s) per socket:                 6
Socket(s):                          1
Stepping:                           2
BogoMIPS:                           4991.99
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq vmx ssse3 fma cx16 pdcm sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni umip waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr flush_l1d arch_capabilities
Virtualization:                     VT-x
Hypervisor vendor:                  Microsoft
Virtualization type:                full
L1d cache:                          288 KiB (6 instances)
L1i cache:                          192 KiB (6 instances)
L2 cache:                           7.5 MiB (6 instances)
L3 cache:                           18 MiB (1 instance)
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Memory Information:
               total        used        free      shared  buff/cache   available
Mem:            15Gi       695Mi        14Gi       3.0Mi       591Mi        14Gi
Swap:          4.0Gi          0B       4.0Gi

Disk Usage:
Filesystem      Size  Used Avail Use% Mounted on
none            7.8G     0  7.8G   0% /usr/lib/modules/5.15.153.1-microsoft-standard-WSL2
none            7.8G  4.0K  7.8G   1% /mnt/wsl
drivers         451G  282G  169G  63% /usr/lib/wsl/drivers
/dev/sdc        251G   24G  215G  10% /
none            7.8G   84K  7.8G   1% /mnt/wslg
none            7.8G     0  7.8G   0% /usr/lib/wsl/lib
rootfs          7.8G  2.2M  7.8G   1% /init
none            7.8G  780K  7.8G   1% /run
none            7.8G     0  7.8G   0% /run/lock
none            7.8G     0  7.8G   0% /run/shm
tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
none            7.8G   76K  7.8G   1% /mnt/wslg/versions.txt
none            7.8G   76K  7.8G   1% /mnt/wslg/doc

Network Interfaces:
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 10.25.25.254/32 brd 10.25.25.254 scope global lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:dd:f0:b7 brd ff:ff:ff:ff:ff:ff
    inet 111.21.22.44/20 brd 111.21.22.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fedd:f0b7/64 scope link
       valid_lft forever preferred_lft forever

Hostname:
PC-XXXXXXYY

Uptime:
 11:47:56 up 1 min,  1 user,  load average: 0.54, 0.16, 0.05