AutoGen框架进行多智能体协作—AI Agentic Design Patterns with AutoGen(一)

news2024/11/17 19:39:52

1. 多代理对话:单口喜剧

在AutoGen中,Agent是一个可以代表人类意图执行操作的实体,发送消息,接收消息,执行操作,生成回复,并与其他代理交互。AutoGen具有一个名为Conversible Agent的内置代理类,它将不同类型的代理统一在同一个编程抽象中。

在这里插入图片描述
 Conversible Agent带有许多内置功能,如使用大型语言模型配置生成回复、执行代码或函数、保持人工干预并检查停止响应等。你可以打开或关闭每个组件,并根据应用程序的需求进行定制,通过这些不同的功能,你可以使用相同的接口创建具有不同角色的代理。

实验地址:https://learn.deeplearning.ai/courses/ai-agentic-design-patterns-with-autogen/lesson/2/multi-agent-conversation-and-stand-up-comedy

2. 实验运行

2.1 配置环境

包版本:

# requirements file
# note which revision of python, for example 3.9.6
# in this file, insert all the pip install needs, include revision

# python==3.10.13
pyautogen==0.2.25
chess==1.10.0
matplotlib
numpy
pandas
yfinance

utils工具类

# Add your utilities or helper functions to this file.

import os
from dotenv import load_dotenv, find_dotenv

# these expect to find a .env file at the directory above the lesson.                                                                                                                     # the format for that file is (without the comment)                                                                                                                                       #API_KEYNAME=AStringThatIsTheLongAPIKeyFromSomeService                                                                                                                                     
def load_env():
    _ = load_dotenv(find_dotenv())

def get_openai_api_key():
    load_env()
    openai_api_key = os.getenv("OPENAI_API_KEY")
    return openai_api_key
from utils import get_openai_api_key
OPENAI_API_KEY = get_openai_api_key()
llm_config = {"model": "gpt-3.5-turbo"}

2.2 定义一个AutoGen代理

from autogen import ConversableAgent

agent = ConversableAgent(
    name="chatbot",
    llm_config=llm_config,
    human_input_mode="NEVER",
)
reply = agent.generate_reply(
    messages=[{"content": "Tell me a joke.", "role": "user"}]
)
print(reply)

输出如下:

Sure, here's one for you:

Why couldn't the bicycle stand up by itself?

Because it was two tired!

2.3 让代理之间对话

设置凯西和乔这两个代理之间的对话,同时保留他们交互的记忆。

cathy = ConversableAgent(
    name="cathy",
    system_message=
    "Your name is Cathy and you are a stand-up comedian.",
    llm_config=llm_config,
    human_input_mode="NEVER",
)

joe = ConversableAgent(
    name="joe",
    system_message=
    "Your name is Joe and you are a stand-up comedian. "
    "Start the next joke from the punchline of the previous joke.",
    llm_config=llm_config,
    human_input_mode="NEVER",
)
chat_result = joe.initiate_chat(
    recipient=cathy, 
    message="I'm Joe. Cathy, let's keep the jokes rolling.",
    max_turns=2,
)

输出如下:

joe (to cathy):

I'm Joe. Cathy, let's keep the jokes rolling.

--------------------------------------------------------------------------------
cathy (to joe):

Sure thing, Joe! Why did the scarecrow win an award? Because he was outstanding in his field!

--------------------------------------------------------------------------------
joe (to cathy):

Haha, Cathy, that's a good one! Speaking of awards, I entered a pun contest and I really feel like I'm in my element.

--------------------------------------------------------------------------------
cathy (to joe):

That's awesome, Joe! I bet you'll clean up with those puns! Just remember, even if you don't win, at least you can say you gave it your punniest shot!

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

2.4 查看历史结果

import pprint

pprint.pprint(chat_result.chat_history)

输出如下:

[{'content': "I'm Joe. Cathy, let's keep the jokes rolling.",
  'role': 'assistant'},
 {'content': 'Sure thing, Joe! Why did the scarecrow win an award? Because he '
             'was outstanding in his field!',
  'role': 'user'},
 {'content': "Haha, Cathy, that's a good one! Speaking of awards, I entered a "
             "pun contest and I really feel like I'm in my element.",
  'role': 'assistant'},
 {'content': "That's awesome, Joe! I bet you'll clean up with those puns! Just "
             "remember, even if you don't win, at least you can say you gave "
             'it your punniest shot!',
  'role': 'user'}]

查看花费

pprint.pprint(chat_result.cost)

输出如下:

{'usage_excluding_cached_inference': {'gpt-3.5-turbo-0125': {'completion_tokens': 93,
                                                             'cost': 0.0002435,
                                                             'prompt_tokens': 208,
                                                             'total_tokens': 301},
                                      'total_cost': 0.0002435},
 'usage_including_cached_inference': {'gpt-3.5-turbo-0125': {'completion_tokens': 93,
                                                             'cost': 0.0002435,
                                                             'prompt_tokens': 208,
                                                             'total_tokens': 301},
                                      'total_cost': 0.0002435}}

生成摘要总结

pprint.pprint(chat_result.summary)

输出如下:

("That's awesome, Joe! I bet you'll clean up with those puns! Just remember, "
 "even if you don't win, at least you can say you gave it your punniest shot!")、

2.5 获取更好的对话摘要

chat_result = joe.initiate_chat(
    cathy, 
    message="I'm Joe. Cathy, let's keep the jokes rolling.", 
    max_turns=2, 
    summary_method="reflection_with_llm",
    summary_prompt="Summarize the conversation",
)

输出如下:

joe (to cathy):

I'm Joe. Cathy, let's keep the jokes rolling.

--------------------------------------------------------------------------------
cathy (to joe):

Sure thing, Joe! Why did the scarecrow win an award? Because he was outstanding in his field!

--------------------------------------------------------------------------------
joe (to cathy):

Haha, Cathy, that's a good one! Speaking of awards, I entered a pun contest and I really feel like I'm in my element.

--------------------------------------------------------------------------------
cathy (to joe):

That's awesome, Joe! I bet you'll clean up with those puns! Just remember, even if you don't win, at least you can say you gave it your punniest shot!

--------------------------------------------------------------------------------
pprint.pprint(chat_result.summary)

输出如下:

('Joe and Cathy enjoy exchanging jokes and puns. Joe entered a pun contest and '
 "Cathy encouraged him to give it his best shot, reminding him that he's in "
 'his element with puns.')

2.6 设置对话终止条件

当开启了多轮对话时,往往需要对话终止条件去停止对话生成,配置如下

cathy = ConversableAgent(
    name="cathy",
    system_message=
    "Your name is Cathy and you are a stand-up comedian. "
    "When you're ready to end the conversation, say 'I gotta go'.",
    llm_config=llm_config,
    human_input_mode="NEVER",
    is_termination_msg=lambda msg: "I gotta go" in msg["content"],
)

joe = ConversableAgent(
    name="joe",
    system_message=
    "Your name is Joe and you are a stand-up comedian. "
    "When you're ready to end the conversation, say 'I gotta go'.",
    llm_config=llm_config,
    human_input_mode="NEVER",
    is_termination_msg=lambda msg: "I gotta go" in msg["content"] or "Goodbye" in msg["content"],
)
chat_result = joe.initiate_chat(
    recipient=cathy,
    message="I'm Joe. Cathy, let's keep the jokes rolling."
)

输出如下:

joe (to cathy):

I'm Joe. Cathy, let's keep the jokes rolling.

--------------------------------------------------------------------------------
cathy (to joe):

Hey Joe! Sure thing, I'm always ready for some laughs. So, did you hear about the mathematician who’s afraid of negative numbers? He'll stop at nothing to avoid them!

--------------------------------------------------------------------------------
joe (to cathy):

Haha, that's a good one, Cathy! Here's a math joke for you: Why was the equal sign so humble? Because he knew he wasn't less than or greater than anyone else!

--------------------------------------------------------------------------------
cathy (to joe):

Haha, I love that one, Joe! Math jokes are always a plus in my book. Speaking of books, did you hear about the claustrophobic astronaut? He just needed a little space!

--------------------------------------------------------------------------------
joe (to cathy):

Haha, I love a good space joke! It's out of this world! Speaking of space, did you hear about the claustrophobic astronaut's favorite part of a computer? The space bar!

--------------------------------------------------------------------------------
cathy (to joe):

Haha, that's a stellar one, Joe! You're really taking these jokes to new heights! But hey, I think it's time for me to launch off. I gotta go!

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

可以看到经过三轮对话就结束了。我们再次查看历史对话信息:

cathy.send(message="What's last joke we talked about?", recipient=joe)

输出如下:

cathy (to joe):

What's last joke we talked about?

--------------------------------------------------------------------------------
joe (to cathy):

The last joke we talked about was the claustrophobic astronaut's favorite part of a computer being the space bar! If you need more jokes, feel free to come back anytime. See you later!

--------------------------------------------------------------------------------
cathy (to joe):

Thanks, Joe! It's been a blast chatting with you. Take care and remember, keep laughing! Bye!

--------------------------------------------------------------------------------
joe (to cathy):

You're welcome, Cathy! I had a great time too. Take care and keep smiling! Goodbye!

--------------------------------------------------------------------------------
cathy (to joe):

Goodbye!

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

3. 总结

以上只是使用代理构建对话的基本示例。在接下来的课程中,我们将学习其他对话模式和一些代理设计模式,包括工具使用、反思、规划和代码执行等。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2176349.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Ps:打开与置入

在 Adobe Photoshop 中,理解不同的“打开”和“置入”命令及其用途,可以根据不同的需求选择最佳方式来管理和编辑图像文件。 ◆ ◆ ◆ 打开 1、Ps菜单:文件/打开 File/Open 快捷键:Ctrl O 用于直接打开现有的图像文件。 打开的…

httpsok-v1.17.0-SSL证书自动续签

🔥httpsok-v1.17.0-SSL证书自动续签 介绍 httpsok 是一个便捷的 HTTPS 证书自动续签工具,基于全新的设计理念,专为 Nginx 、OpenResty 服务器设计。已服务众多中小企业,稳定、安全、可靠。 一行命令,一分钟轻松搞定…

naocs注册中心,配置管理,openfeign在idea中实现模块间的调用,getway的使用

一 naocs注册中心步骤 1 nacos下载安装 解压安装包,直接运行bin目录下的startup.cmd 这里双击运行出现问题的情况下 (版本低的naocs) 在bin目录下 打开cmd 运行以下命令 startup.cmd -m standalone 访问地址: http://localh…

【Linux】趣味讲解“权限“的那些事(重点讲解文件权限,内含su、sudo、chmod、chown、umask等指令)

文章目录 前言1. Linux下用户的分类1.1 su 指令1.1.1 使用su指令切换到其它的用户上1.1.2 使用su指令切换到root上1.1.3 su指令的总结 1.2 sudo指令(对某条指令进行提权)1.2.1 sudo指令的语法1.2.2 由sudo指令引发的思考问题 2. 什么叫做权限2.2 文件权限2.2.1 文件类型2.2.2 文…

UART驱动学习一(UART硬件介绍)

一、UART硬件介绍 1. 串口的硬件介绍 UART的全称是Universal Asynchronous Receiver and Transmitter,即异步发送和接收。串口在嵌入式中用途非常的广泛,主要的用途有: 打印调试信息;外接各种模块:GPS、蓝牙&#xf…

JavaWeb 12.Tomcat10

希望明天能出太阳 或者如果没有太阳的话 希望我能变得更加阳光一点 —— 24.9.25 一、常见的JavaWeb服务器 Web服务器通常由硬件和软件共同构成 硬件:电脑,提供服务供其他客户电脑访问 软件:电脑上安装的服务器软件,安装后能提…

【鸿蒙HarmonyOS NEXT】数据存储之分布式键值数据库

【鸿蒙HarmonyOS NEXT】数据存储之分布式键值数据库 一、环境说明二、分布式键值数据库介绍三、示例代码加以说明四、小结 一、环境说明 DevEco Studio 版本: API版本:以12为主 二、分布式键值数据库介绍 KVStore简介: 分布式键值数据库…

手机电脑无缝对接,虫洞软件让多屏协同触手可及

在数字化时代,我们的日常生活和工作越来越依赖于电子设备,尤其是智能手机和电脑。但你是否曾因在手机和电脑之间频繁切换而感到烦恼?现在,有了虫洞软件,这一切都将成为过去式。 虫洞——电脑与手机的桥梁 虫洞软件&a…

Kubernetes整体架构与核心组件

一个 Kubernetes 集群的机器节点有两种角色—— Master 和 Node,都可由一个或多个节点组成,且同一个节点可以既是 Master 也是 Node。其中 Master 节点负责全局决策、资源调度、Node 与 Pod 管理,等等,属于管控节点;No…

【unity进阶知识4】封装unity协程工具,避免 GC(垃圾回收)

文章目录 前言封装协程工具类,避免 GC(垃圾回收)使用1.使用默认方式使用协程2.使用自定义的 CoroutineTool 工具类来等待不同的时间 完结 前言 在 Unity 中,使用 yield return null 、yield return new WaitForEndOfFrame()等会导…

人物型Agent开发(文心智能体平台创作分享)

开发平台:文心智能体平台AgentBuilder | 想象即现实 目录 一、开发灵感 (一)打破刻板印象 (二)以古鉴今,探索人性与情感 二、角色分析与设定 (一)西门庆特质 (二&a…

我的深度学习笔记

传统观念认为:在不考虑算力的情况下,网络越深,其准确率就越高,最直接的方法就是把网络设计的越深越好。 事实上:随着网络的层数不断加深,当达到一定的书目之后,训练精度和测试精度都有下降&…

第十三届蓝桥杯真题Java c组C.纸张尺寸(持续更新)

博客主页:音符犹如代码系列专栏:蓝桥杯关注博主,后期持续更新系列文章如果有错误感谢请大家批评指出,及时修改感谢大家点赞👍收藏⭐评论✍ 【问题描述】 在 ISO 国际标准中定义了 A0 纸张的大小为 1189mm 841mm&#…

AI运用在营销领域的经典案例及解析

大家好,我是Shelly,一个专注于输出AI工具和科技前沿内容的AI应用教练,体验过300款以上的AI应用工具。关注科技及大模型领域对社会的影响10年。关注我一起驾驭AI工具,拥抱AI时代的到来。 在前面一篇文章当中,我给大家介…

[Redis][典型运用][缓存]详细讲解

目录 0.什么是缓存?1.使用Redis作为缓存1.为什么用?2.如何用? 2.缓存的更新策略0.前言1.定期生成2.实时生成 3.缓存相关问题1.缓存预热(Cache Preheating)2.缓存穿透(Cache Penetration)3.缓存雪崩(Cache Avalanche)4.缓存击穿(Cache Breakdo…

一种多版本、多人并行开发GIT分支管理规范

首发公众号: 赵侠客 引言 作为开发者每天在写代码的同时也在写BUG,所以一方面需要开发新的需求,另一方面还要填自己以前挖的坑。目前主流程序员都在使用GIT来管理自己的代码,当GIT仓库有多人维护或者项目有多个版本同时迭代开发时…

c++进阶学习--------多态

前言 需要声明的,本节课件中的代码及解释都是在vs2022下的x86程序中,涉及的指针都是4bytes。 如果要其他平台下,部分代码需要改动。 比如:如果是x64程序,则需要考虑指针是8bytes问题等等 1. 多态的概念 1.1 概念 …

.NET内网实战:白名单文件反序列化执行命令

01阅读须知 此文所节选自小报童《.NET 内网实战攻防》专栏,主要内容有.NET在各个内网渗透阶段与Windows系统交互的方式和技巧,对内网和后渗透感兴趣的朋友们可以订阅该电子报刊,解锁更多的报刊内容。 02基本介绍 本文内容部分节选自小报童…

【易社保-注册安全分析报告】

前言 由于网站注册入口容易被黑客攻击,存在如下安全问题: 1. 暴力破解密码,造成用户信息泄露 2. 短信盗刷的安全问题,影响业务及导致用户投诉 3. 带来经济损失,尤其是后付费客户,风险巨大,造…

Java泛型方法的定义和使用、泛型类、泛型接口、泛型方法、通配符、泛型的上界与下界

文章目录 一、包装类1.1、基本数据类型和对应的包装类1.2、自动装箱和自动拆箱 二、基本介绍2.1、泛型引入背景2.1、什么是泛型2.2、为什么使用泛型 三、常见泛型字母含义四、泛型的使用4.1、泛型类4.2、泛型接口4.3、泛型方法 五、泛型的继承5.1、泛型不具备继承性5.2、何为数…