2. Autogen官网教程 (Terminating Conversations Between Agents)

news/2024/11/25 13:29:29/

在这一章中,我们将探讨如何结束自动生成代理之间的对话。

导入必要的库

import osfrom autogen import ConversableAgent

配置智能体

我们需要配置智能体使用的语言模型(LLM)。以下是一个配置示例:

llm_config = {"config_list": [{"model": "GLM-4-Plus","api_key": "your api key","base_url": "https://open.bigmodel.cn/api/paas/v4/",}]
}

创建智能体

创建两个智能体cathyjoe,并设置它们的系统消息和配置:

cathy = ConversableAgent("cathy",system_message="Your name is Cathy and you are a part of a duo of comedians.",llm_config=llm_config,human_input_mode="NEVER",  # Never ask for human input.
)joe = ConversableAgent("joe",system_message="Your name is Joe and you are a part of a duo of comedians.",llm_config=llm_config,human_input_mode="NEVER",  # Never ask for human input.
)

初始化对话

使用initiate_chat方法让joecathy发起对话:

result = joe.initiate_chat(cathy, message="Cathy, tell me a joke.", max_turns=2)

输出结果如下:

[33mjoe[0m (to cathy):Cathy, tell me a joke.--------------------------------------------------------------------------------
[33mcathy[0m (to joe):Oh, absolutely! Here's one for you:Why don't scientists trust atoms?Because they make up everything!But wait, it gets better! My comedy partner and I have this routine where I say the punchline, and they come back with, "And that's why my bank account is always empty—because my money's just a bunch of atoms too!" 😄Gotta love a good science pun, right? 🤓💥--------------------------------------------------------------------------------
[33mjoe[0m (to cathy):Haha, classic! You know, Cathy, that's the kind of humor that really resonates with the crowd. It's smart, but it also has that everyday relatability. And the callback to the bank account? Brilliant! It's like we're telling the audience, "Hey, we're just like you—except we make jokes about it."Now, how about we flip it and I give you a setup? Here goes:"So, I walked into a bar, and the bartender says, 'Why the long phase shift?'"Your turn to deliver the punchline! 🍻🌌--------------------------------------------------------------------------------
[33mcathy[0m (to joe):Oh, I love this setup! Here's my take:"Well, I just came from a quantum physics convention, and let's just say, I'm still waiting for my state to collapse!"Boom! 😂🍻🌌It's like we're serving up a cocktail of humor and science—shaken, not stirred! 🥂🔬--------------------------------------------------------------------------------

控制对话轮次

可以通过max_turns参数控制对话的轮次:

result = joe.initiate_chat(cathy, message="Cathy, tell me a joke.", max_turns=1
)  # decrease the number of max turns before termination

输出结果如下:

[33mjoe[0m (to cathy):Cathy, tell me a joke.--------------------------------------------------------------------------------
[33mcathy[0m (to joe):Oh, absolutely! Here's one for you:Why don't scientists trust atoms?Because they make up everything!But wait, it gets better! My comedy partner and I have this routine where I say the punchline, and they come back with, "And that's why my bank account is always empty—because my money's just a bunch of atoms too!" 😄Gotta love a good science pun, right? 🤓💥--------------------------------------------------------------------------------

限制连续自动回复

可以通过max_consecutive_auto_reply参数限制连续自动回复的次数:

joe = ConversableAgent("joe",system_message="Your name is Joe and you are a part of a duo of comedians.",llm_config=llm_config,human_input_mode="NEVER",  # Never ask for human input.max_consecutive_auto_reply=1,  # Limit the number of consecutive auto-replies.
)result = joe.initiate_chat(cathy, message="Cathy, tell me a joke.")

输出结果与之前类似,但joe在回复一次后会停止自动回复。

自定义终止条件

可以通过is_termination_msg参数设置自定义的终止条件:

joe = ConversableAgent("joe",system_message="Your name is Joe and you are a part of a duo of comedians.",llm_config=llm_config,human_input_mode="NEVER",  # Never ask for human input.is_termination_msg=lambda msg: "good bye" in msg["content"].lower(),
)result = joe.initiate_chat(cathy, message="Cathy, tell me a joke and then say the words GOOD BYE.")

输出结果如下:

[33mjoe[0m (to cathy):Cathy, tell me a joke and then say the words GOOD BYE.--------------------------------------------------------------------------------
[33mcathy[0m (to joe):Sure thing! Here's one for you:Why don't scientists trust atoms anymore?Because they make up everything!GOOD BYE! 🌟--------------------------------------------------------------------------------

总结

通过本教程,我们学习了如何使用Autogen库创建可对话的智能体,并通过示例代码展示了如何配置智能体、初始化对话、控制对话轮次、限制连续自动回复以及设置自定义终止条件。希望这些内容对你有所帮助!
参考链接:https://microsoft.github.io/autogen/0.2/docs/tutorial/chat-termination
如果有任何问题,欢迎在评论区提问。


http://www.ppmy.cn/news/1549828.html

相关文章

初识Linux—— 基本指令(下)

前言: 本篇继续来学习Linux的基础指令,继续加油!!! 本篇文章对于图片即内容详解,已同步到本人gitee:Linux学习: Linux学习与知识讲解 Linux指令 1、查看文件内容的指令 cat ​ cat 查看文件…

【FPGA-MicroBlaze】串口收发以及相关函数讲解

前言 工具:Vivado2018.3及其所对应的SDK版本 目前网上有许多MicroBlaze 的入门教程,比如下面的这个参考文章,用串口打印一个hello world。 【FPGA】Xilinx MicroBlaze软核使用第一节:Hello World!_fpga软核microblaze-CSDN博客 个…

[Redis#0] iredis: linux上redis超好用的环境配置

目录 Features 特征 Install 安装 Pip Brew Linux的 Download Binary 下载 Binary Usage 用法 Using DSN 使用 DSN Change The Default Prompt更改默认提示 Configuration 配置 Keys Development 发展 Release Strategy 发布策略 Setup Environment 设置环境 De…

go-zero(九) 自定义拦截器

go-zero 拦截器 有时我们需要在处理请求的过程中添加一些额外的逻辑,比如身份验证、日志记录、请求限流、性能监控等,这些都可以通过拦截器实现。go zero可以设置多个拦截器 一、 服务端拦截器 服务端拦截器用于处理传入的 RPC 请求,可以在…

操作系统进程和线程——针对实习面试

目录 操作系统进程和线程什么是进程和线程?进程和线程的区别?进程有哪些状态?什么是线程安全?如何实现线程安全?什么是线程安全?如何实现线程安全? 进程间的通信有哪几种方式?什么是…

微信小程序开发指南:从基础到进阶

​🌈个人主页:前端青山 🔥系列专栏:微信小程序篇 🔖人终将被年少不可得之物困其一生 依旧青山,本期给大家带来微信小程序篇专栏内容:微信小程序开发指南:从基础到进阶 前言 随着移动互联网的快速发展&…

归并排序与逆序对问题(C语言版)

一、引言 归并排序是一种高效且稳定的排序方法,而逆序对问题是算法领域的一个经典问题,本文教大家如何实现归并排序,以及如何使用归并排序去结果逆序对问题 二、归并排序 归并排序思想 分解:将待排序的数组分成两半&#xff0c…

社交电商专业赋能高校教育与产业协同发展:定制开发AI智能名片及2+1链动商城小程序的创新驱动

摘要:本文围绕社交电商有望成为高校常态专业这一趋势展开深入探讨,剖析国家政策认可下其学科发展前景,着重阐述在专业建设进程中面临的师资短缺及实践教学难题。通过引入定制开发AI智能名片与21链动商城小程序,探究如何借助这些新…