当前位置: 首页 > news >正文

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

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 ConversableAgentagent = 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 pprintpprint.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.mrgr.cn/news/38769.html

相关文章:

  • 【Linux】修改用户名用户家目录
  • C# Blazor Server 调用海康H5Player播放摄像头画面
  • 带链的队列,入队,退队,检测带链队列的状态
  • 虚拟环境更改gcc、g++编译器版本
  • 【经验技巧】如何做好S参数的仿测一致性
  • WeChat_DevTools 断点调试方法总结
  • 工业制氮机在食品行业的应用优势
  • 【JS】封装针对dom节点的全屏查看功能
  • 阿布量化:基于 Python 的量化交易框架
  • 嵌入式学习——进程间通信方式(4)—— 消息队列
  • Ruby 多线程
  • 基于baidu的云函数实现隐藏c2真实地址
  • 助农扶贫微信小程序+ssm论文ppt源码调试讲解
  • 服务器分类极简理解
  • Kafka快速实战与基本原理详解
  • 【数据结构】链表(2)
  • Performance Analysis Kit简介
  • (undone) 阅读 MapReduce 论文笔记
  • 【JUC并发编程系列】深入理解Java并发机制:深入剖析AbstractQueuedSynchronizer的底层机制(九、AQS底层实现原理)
  • 【图像生成大模型imagen】细节逼真富有创造力