autogen的理解和实践

2023-11-30 12:52
文章标签 实践 理解 autogen

本文主要是介绍autogen的理解和实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

什么是autogen?

AutoGen 是一个框架,支持使用多个代理来开发 LLM 应用程序,这些代理可以相互对话来解决任务。AutoGen 代理是可定制的、可对话的,并且无缝地允许人类参与。他们可以采用法学硕士、人力投入和工具组合的各种模式运作。简单来说,就是设置不同的聊天代理对象来的相互对话交互来实现用户想要实现的目标和任务

基于chatgpt模型来构建,除了以下的几个chatgpt4的模型以外,它还支持fine-Truning模型

import autogenconfig_list = autogen.config_list_from_json("OAI_CONFIG_LIST",filter_dict={"model": ["gpt-4", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],},
)

如何使用autogen?

autogen的安装需要依赖于python>=3.8的版本,这点我们可以通过anaconda来进行环境配置和管理

pip install pyautogen

配置好环境之后,我们就可以使用autogen了

需要说明的是:autogen作用的实现主要是基于助手代理和用户代理两种对象,我们可以进行不同的配置实现代理对象对话场景的多样化:

助理代理的参数设置 

def __init__(name: str,system_message: Optional[str] = DEFAULT_SYSTEM_MESSAGE,llm_config: Optional[Union[Dict, bool]] = None,is_termination_msg: Optional[Callable[[Dict], bool]] = None,max_consecutive_auto_reply: Optional[int] = None,human_input_mode: Optional[str] = "NEVER",code_execution_config: Optional[Union[Dict, bool]] = False,**kwargs)
name str - 代理名称。
system_message str - ChatCompletion 推理的系统消息。如果您想重新编程代理,请覆盖此属性。
llm_config dict - llm 推理配置。
is_termination_msg function - 一个函数,它接受字典形式的消息并返回一个布尔值,指示收到的消息是否是终止消息。该字典可以包含以下键:“content”、“role”、“name”、“function_call”。
max_consecutive_auto_reply int - 连续自动回复的最大数量。默认为 None (没有提供限制,类属性 MAX_CONSECUTIVE_AUTO_REPLY 将在这种情况下用作限制)。该限制仅在 human_input_mode 不是“ALWAYS”时起作用。

用户代理的参数设置

def __init__(name: str,is_termination_msg: Optional[Callable[[Dict], bool]] = None,max_consecutive_auto_reply: Optional[int] = None,human_input_mode: Optional[str] = "ALWAYS",function_map: Optional[Dict[str, Callable]] = None,code_execution_config: Optional[Union[Dict, bool]] = None,default_auto_reply: Optional[Union[str, Dict, None]] = "",llm_config: Optional[Union[Dict, bool]] = False,system_message: Optional[str] = "")
name str - 代理的名称。
is_termination_msg function - 一个函数,它接受字典形式的消息并返回一个布尔值,指示收到的消息是否是终止消息。该字典可以包含以下键:“content”、“role”、“name”、“function_call”。
max_consecutive_auto_reply int - 连续自动回复的最大数量。默认为 None (没有提供限制,类属性 MAX_CONSECUTIVE_AUTO_REPLY 将在这种情况下用作限制)。该限制仅在 human_input_mode 不是“ALWAYS”时起作用。
human_input_mode str - 每次收到消息时是否要求人工输入。可能的值为“始终”、“终止”、“从不”。(1) 当“ALWAYS”时,代理每次收到消息时都会提示人工输入。在此模式下,当人工输入为“exit”时,或者当 is_termination_msg 为 True 并且没有人工输入时,对话停止。(2) 当“TERMINATE”时,只有当收到终止消息或自动回复次数达到 max_consecutive_auto_reply 时,代理才会提示人工输入。(3) 当“NEVER”时,代理将永远不会提示人工输入。该模式下,当自动回复次数达到 max_consecutive_auto_reply 或 is_termination_msg 为 True 时,会话停止。
function_map dict [str, callable] - 将函数名称(传递给 openai)映射到可调用函数。
code_execution_config dict 或 False - 代码执行的配置。要禁用代码执行,请设置为 False。否则,设置为具有以下键的字典:
work_dir(可选,str):代码执行的工作目录。如果没有,将使用默认工作目录。默认工作目录是“path_to_autogen”下的“extensions”目录。
use_docker (可选、list、str 或 bool):用于执行代码的 docker 映像。如果提供了图像名称的列表或字符串,则代码将在 docker 容器中执行,并成功拉取第一个图像。如果 None、False 或空,则代码将在当前环境中执行。默认为 True,将转换为列表。如果代码在当前环境中执行,则该代码必须是可信的。
timeout (可选,int):最大执行时间(以秒为单位)。
last_n_messages(实验性,可选,int):要回顾代码执行的消息数。默认为 1。
default_auto_reply str 或 dict 或 None - 当未生成代码执行或基于 llm 的回复时默认的自动回复消息。
llm_config dict 或 False - llm 推理配置。
system_message str - 用于 ChatCompletion 推理的系统消息。仅当 llm_config 不为 False 时使用。用它来重新编程代理。

autogen能做什么

1.多主体对话和任务执行

任务事例1:
今天是几号?比较一下META和特斯拉今年迄今的收益,将收益变化保存stock_price_ytd.png文件,并将该文件打开

import autogen
import openai
openai.api_key="your-api-key"
coder = autogen.AssistantAgent(name="coder",llm_config={"seed": 42,  # seed for caching and reproducibility"temperature": 0,  # temperature for sampling},  # configuration for autogen's enhanced inference API which is compatible with OpenAI AP)
user_proxy = autogen.UserProxyAgent(name="user_proxy",human_input_mode="NEVER",max_consecutive_auto_reply=10,is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
)
# executor = autogen.AssistantAgent("executor",system_message="Executes the code generated by the coder and returns the data result")
task = "What date is today? Compare the year-to-date gain for META and TESLA"user_proxy.initiate_chat(coder, message=task)
user_proxy.send(recipient=coder,message="""Plot a chart of their stock price change YTD and save to stock_price_ytd.png.""",
)
user_proxy.send(recipient= coder,message="open stock_price_ytd.png"
)
# print("nihao")

 结果逻辑:
他会按照以下逻辑输出任务的执行逻辑

1.编写并执行python代码计算今天日期

2.使用 `yfinance ` 获取股票价格的库,如果没有安装,将对其进行安装

3.计算 META 和 TESLA 的年初至今收益

4.在当前目录加载文件

5.打开该文件,可视化结果如下:

任务实例2:

让autogen对百度首页总结功能测试点并进行自动化测试 

import autogen
import openai
openai.api_key="your api key"
coder = autogen.AssistantAgent(name="coder",llm_config={"seed": 42,  # seed for caching and reproducibility"temperature": 0,  # temperature for sampling},  # configuration for autogen's enhanced inference API which is compatible with OpenAI AP)
user_proxy = autogen.UserProxyAgent(name="user_proxy",human_input_mode="NEVER",max_consecutive_auto_reply=10,is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
)
# executor = autogen.AssistantAgent("executor",system_message="Executes the code generated by the coder and returns the data result")
task = "From the point of view of functional testing, analyze what test points Baidu home page has"
user_proxy.initiate_chat(coder, message=task)
# 生成对应的测试脚本
user_proxy.send("Generate automated test scripts based on these test points",coder)
From the point of view of functional testing, analyze what test points Baidu home page has--------------------------------------------------------------------------------
coder (to user_proxy):Functional testing is a type of software testing that validates the software system against the functional requirements/specifications. The purpose of functional tests is to test each function of the software application by providing appropriate input, verifying the output against the functional requirements.When it comes to testing the Baidu home page, we can consider the following test points:1. **Search Functionality**: This is the primary function of Baidu. We need to ensure that the search functionality works as expected. This includes testing with different types of input (e.g., text, numbers, special characters), checking the search results are relevant, and testing the performance of the search.2. **Language Selection**: Baidu offers multiple language options. We need to test if the language selection works correctly and if the page content changes accordingly.3. **Navigation Links**: The home page contains various navigation links (e.g., News, Maps, Images). We need to test if these links are working correctly and lead to the correct pages.4. **Login Functionality**: If there is a login option, we need to test the login functionality with valid and invalid inputs.5. **Accessibility**: We need to test if the website is accessible and usable, including testing for mobile responsiveness, browser compatibility, and accessibility for users with disabilities.6. **Ads Display**: If there are ads on the home page, we need to test if they are displayed correctly and if they lead to the correct websites when clicked.7. **Privacy and Security**: We need to test if the website is secure and if it respects user privacy. This includes testing for secure connections (HTTPS), checking if cookies are used appropriately, and testing the privacy settings.8. **Performance**: We need to test the performance of the website, including load times and how the website handles high traffic.Please note that this is a high-level analysis and the actual test points may vary based on the specific requirements and functionality of the Baidu home page.TERMINATE--------------------------------------------------------------------------------
user_proxy (to coder):Generate automated test scripts based on these test points--------------------------------------------------------------------------------
coder (to user_proxy):To generate automated test scripts, we can use Selenium, a popular tool for automating web browsers. Here's a basic Python script using Selenium WebDriver to test some of the points mentioned above. Please note that this is a simplified example and real-world test scripts would be more complex. Also, you need to have Selenium and the appropriate WebDriver installed on your system to run these scripts.```python
# filename: baidu_test.pyfrom selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time# Create a new instance of the Firefox driver
driver = webdriver.Firefox()# Go to the Baidu home page
driver.get("https://www.baidu.com")# Test the search functionality
search_box = driver.find_element_by_name("wd")
search_box.send_keys("Selenium")
search_box.send_keys(Keys.RETURN)
time.sleep(2)  # Wait for the page to load# Check if the search results contain "Selenium"
assert "Selenium" in driver.page_source# Test the navigation links
news_link = driver.find_element_by_link_text("新闻")
news_link.click()
time.sleep(2)  # Wait for the page to load# Check if the News page is loaded
assert "新闻" in driver.title# Close the browser
driver.quit()
```To run this script, save it to a file named `baidu_test.py` and run it with Python.Please note that this script only tests two points: the search functionality and one of the navigation links. Testing other points like language selection, login functionality, accessibility, ads display, privacy and security, and performance would require more complex scripts and possibly other tools in addition to Selenium.Also, please note that the actual identifiers (like the name of the search box and the text of the navigation link) may vary, and you need to inspect the Baidu home page to get the correct identifiers.-------------------------------------------------------------------------------->>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
execute_code was called without specifying a value for use_docker. Since the python docker package is not available, code will be run natively. Note: this fallback behavior is subject to change
user_proxy (to coder):exitcode: 1 (execution failed)
Code output: 
Traceback (most recent call last):File "baidu_test.py", line 3, in <module>from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'--------------------------------------------------------------------------------
coder (to user_proxy):It seems like the Selenium module is not installed in your Python environment. You can install it using pip, which is a package manager for Python. Here is the command to install Selenium:```sh
pip install selenium
```After installing Selenium, you should be able to run the test script without the ModuleNotFoundError. Please try running the test script again after installing Selenium.-------------------------------------------------------------------------------->>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is sh)...
execute_code was called without specifying a value for use_docker. Since the python docker package is not available, code will be run natively. Note: this fallback behavior is subject to change
user_proxy (to coder):exitcode: 0 (execution succeeded)
Code output: 
Collecting seleniumDownloading selenium-4.14.0-py3-none-any.whl.metadata (6.9 kB)
Requirement already satisfied: urllib3<3,>=1.26 in /opt/anaconda3/envs/python3/lib/python3.9/site-packages (from urllib3[socks]<3,>=1.26->selenium) (2.0.7)
Collecting trio~=0.17 (from selenium)Downloading trio-0.22.2-py3-none-any.whl.metadata (4.7 kB)
Collecting trio-websocket~=0.9 (from selenium)Downloading trio_websocket-0.11.1-py3-none-any.whl.metadata (4.7 kB)
Requirement already satisfied: certifi>=2021.10.8 in /opt/anaconda3/envs/python3/lib/python3.9/site-packages (from selenium) (2023.7.22)
Requirement already satisfied: attrs>=20.1.0 in /opt/anaconda3/envs/python3/lib/python3.9/site-packages (from trio~=0.17->selenium) (23.1.0)
Collecting sortedcontainers (from trio~=0.17->selenium)Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)
Requirement already satisfied: idna in /opt/anaconda3/envs/python3/lib/python3.9/site-packages (from trio~=0.17->selenium) (3.4)
Collecting outcome (from trio~=0.17->selenium)Downloading outcome-1.3.0.post0-py2.py3-none-any.whl.metadata (2.6 kB)
Collecting sniffio (from trio~=0.17->selenium)Downloading sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting exceptiongroup>=1.0.0rc9 (from trio~=0.17->selenium)Downloading exceptiongroup-1.1.3-py3-none-any.whl.metadata (6.1 kB)
Collecting wsproto>=0.14 (from trio-websocket~=0.9->selenium)Downloading wsproto-1.2.0-py3-none-any.whl (24 kB)
Collecting pysocks!=1.5.7,<2.0,>=1.5.6 (from urllib3[socks]<3,>=1.26->selenium)Downloading PySocks-1.7.1-py3-none-any.whl (16 kB)
Collecting h11<1,>=0.9.0 (from wsproto>=0.14->trio-websocket~=0.9->selenium)Downloading h11-0.14.0-py3-none-any.whl (58 kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 1.6 MB/s eta 0:00:00
Downloading selenium-4.14.0-py3-none-any.whl (9.9 MB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 1.7 MB/s eta 0:00:00
Downloading trio-0.22.2-py3-none-any.whl (400 kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 400.2/400.2 kB 1.5 MB/s eta 0:00:00
Downloading trio_websocket-0.11.1-py3-none-any.whl (17 kB)
Downloading exceptiongroup-1.1.3-py3-none-any.whl (14 kB)
Downloading outcome-1.3.0.post0-py2.py3-none-any.whl (10 kB)
Installing collected packages: sortedcontainers, sniffio, pysocks, outcome, h11, exceptiongroup, wsproto, trio, trio-websocket, selenium
Successfully installed exceptiongroup-1.1.3 h11-0.14.0 outcome-1.3.0.post0 pysocks-1.7.1 selenium-4.14.0 sniffio-1.3.0 sortedcontainers-2.4.0 trio-0.22.2 trio-websocket-0.11.1 wsproto-1.2.0--------------------------------------------------------------------------------
coder (to user_proxy):Great! Now that Selenium is installed, you can run the test script again. Here is the script for your convenience:```python
# filename: baidu_test.pyfrom selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time# Create a new instance of the Firefox driver
driver = webdriver.Firefox()# Go to the Baidu home page
driver.get("https://www.baidu.com")# Test the search functionality
search_box = driver.find_element_by_name("wd")
search_box.send_keys("Selenium")
search_box.send_keys(Keys.RETURN)
time.sleep(2)  # Wait for the page to load# Check if the search results contain "Selenium"
assert "Selenium" in driver.page_source# Test the navigation links
news_link = driver.find_element_by_link_text("新闻")
news_link.click()
time.sleep(2)  # Wait for the page to load# Check if the News page is loaded
assert "新闻" in driver.title# Close the browser
driver.quit()
```Please run this script and let me know the result.--------------------------------------------------------------------------------

结果分析:

1.生成测试点

2.编写python脚本

3.执行自动化测试

2.增强推理:所谓的逻辑增强,就是我们为助手代理设置不同的参数来模拟不同的对话场景从而实现更复杂的逻辑对话

可调节的超参数包括:

  1. model - 这是必需的输入,指定要使用的模型 ID。
  2. 提示/消息 - 模型的输入提示/消息,为文本生成任务提供上下文。
  3. max_tokens - 在输出中生成的标记(单词或单词片段)的最大数量。
  4. temperature - 0 到 1 之间的值,控制生成文本的随机性。较高的温度将导致更加随机和多样化的文本,而较低的温度将导致更加可预测的文本。
  5. top_p - 0 到 1 之间的值,控制每个令牌生成的采样概率质量。较低的 top_p 值将使其更有可能根据最可能的标记生成文本,而较高的值将允许模型探索更广泛的可能标记。
  6. n - 针对给定提示生成的响应数。生成多个响应可以提供更多样化且可能更有用的输出,但它也会增加请求的成本。
  7. stop - 一个字符串列表,当在生成的文本中遇到该字符串时,将导致生成停止。这可用于控制输出的长度或有效性。
  8. Presence_penalty、Frequency_penalty - 控制生成文本中某些单词或短语的存在和频率的相对重要性的值。
  9. best_of - 为给定提示选择“最佳”(每个标记具有最高日志概率的响应)响应时生成服务器端的响应数。

autogen与chatgpt的对比

Autogen的优势: 
1. 自动化:用户可以通过代理给autogen颁布任务,这个任务可能需要划分为不同的步骤去实现,用户可以通过参数的设置使用户全程0参与,autogen生成的代理助手和用户代理可以帮我们分步骤自动化实现任务
2. 代码生成与执行:Autogen可以根据特定需求生成代码,这对需要快速创建原型或实现功能的开发人员很有帮助;除此之外,autogen因为内置了python解释器,你可以命令它执行它生成的python代码
3.面向任务:Autogen被设计为执行特定的任务,这可以使它在某些用例中更有效。 
 
Autogen的缺点: 
1. 有限的范围:Autogen是为特定任务设计的,所以它可能不像其他人工智能模型那样通用。 
2. 复杂性:根据任务的不同,Autogen可能需要大量的设置和配置。 
3.依赖于用户输入:Autogen依赖于用户输入来执行任务,因此如果用户输入不清楚或不正确,它可能不那么有效。

4.autogen没有像chatgpt那样如此稳定,虽然官方文档没有说明这一点,但是当我在本机使用autogen进行个性化提问时,有时会因为超时导致接口调用失败

5.使用chatgpt4作为模型,数据交互成本太高(太贵了)

autogen对测试QA测试效率的优化 

1.autogen可以帮我们生成一定的测试用例,完善case生成

2.autogen可以自主生成脚本并执行自动化测试,执行自动化测试时可以根据场景配合使用

3.相对于chatgpt,它能够检查并运行我们的脚本,如果出现错误,给出可能的错误原因并尝试纠正代码逻辑,提高QA效率

这篇关于autogen的理解和实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/437102

相关文章

golang内存对齐的项目实践

《golang内存对齐的项目实践》本文主要介绍了golang内存对齐的项目实践,内存对齐不仅有助于提高内存访问效率,还确保了与硬件接口的兼容性,是Go语言编程中不可忽视的重要优化手段,下面就来介绍一下... 目录一、结构体中的字段顺序与内存对齐二、内存对齐的原理与规则三、调整结构体字段顺序优化内存对齐四、内

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

python实现简易SSL的项目实践

《python实现简易SSL的项目实践》本文主要介绍了python实现简易SSL的项目实践,包括CA.py、server.py和client.py三个模块,文中通过示例代码介绍的非常详细,对大家的学习... 目录运行环境运行前准备程序实现与流程说明运行截图代码CA.pyclient.pyserver.py参

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

深入理解Apache Airflow 调度器(最新推荐)

《深入理解ApacheAirflow调度器(最新推荐)》ApacheAirflow调度器是数据管道管理系统的关键组件,负责编排dag中任务的执行,通过理解调度器的角色和工作方式,正确配置调度器,并... 目录什么是Airflow 调度器?Airflow 调度器工作机制配置Airflow调度器调优及优化建议最

Spring Boot统一异常拦截实践指南(最新推荐)

《SpringBoot统一异常拦截实践指南(最新推荐)》本文介绍了SpringBoot中统一异常处理的重要性及实现方案,包括使用`@ControllerAdvice`和`@ExceptionHand... 目录Spring Boot统一异常拦截实践指南一、为什么需要统一异常处理二、核心实现方案1. 基础组件

SpringBoot项目中Maven剔除无用Jar引用的最佳实践

《SpringBoot项目中Maven剔除无用Jar引用的最佳实践》在SpringBoot项目开发中,Maven是最常用的构建工具之一,通过Maven,我们可以轻松地管理项目所需的依赖,而,... 目录1、引言2、Maven 依赖管理的基础概念2.1 什么是 Maven 依赖2.2 Maven 的依赖传递机

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

Java内存泄漏问题的排查、优化与最佳实践

《Java内存泄漏问题的排查、优化与最佳实践》在Java开发中,内存泄漏是一个常见且令人头疼的问题,内存泄漏指的是程序在运行过程中,已经不再使用的对象没有被及时释放,从而导致内存占用不断增加,最终... 目录引言1. 什么是内存泄漏?常见的内存泄漏情况2. 如何排查 Java 中的内存泄漏?2.1 使用 J