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

1. Autogen官网教程 (Introduction to AutoGen)

why autogen

The whole is greater than the sum of its parts.(整体的功能或价值往往超过单独部分简单相加的总和。)
-Aristotle

autogen 例子

1. 导入必要的库

首先,导入os库和autogen库中的ConversableAgent类。

import os
from autogen import ConversableAgent

2. 配置LLM(大型语言模型)

接下来,配置LLM的参数。这里我们使用GLM-4-Plus模型,并提供了API密钥和基础URL。

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

3. 创建对话代理

使用ConversableAgent类创建一个对话代理。这里我们创建一个名为chatbot的代理,并关闭代码执行功能和人类输入模式。

agent = ConversableAgent("chatbot",llm_config=llm_config,code_execution_config=False,  # 关闭代码执行,默认为关闭。function_map=None,  # 没有注册函数,默认为None。human_input_mode="NEVER",  # 从不请求人类输入。
)

4. 生成回复

我们可以通过调用generate_reply方法来生成对话回复。这里我们传入一个包含用户消息的列表。

reply = agent.generate_reply(messages=[{"content": "Tell me a joke.", "role": "user"}])
print(reply)

输出结果如下:

[autogen.oai.client: 11-24 16:28:32] {351} WARNING - Model GLM-4-Plus 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.
Sure! Here's one for you:Why don't skeletons fight each other?Because they don't have the guts! 🦴😄

5. 创建多个对话代理

创建多个对话代理,并为它们设置不同的系统消息。这里创建两个喜剧演员代理:Cathy和Joe。

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",  # 从不请求人类输入。
)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",  # 从不请求人类输入。
)

6. 初始化对话

使用initiate_chat方法初始化对话。这里我们让Joe向Cathy发起对话,并设置最大对话轮数为2。

result = joe.initiate_chat(cathy, message="Cathy, tell me a simple and easy-to-understand joke.", max_turns=2)

输出结果如下:

[33mjoe[0m (to cathy):Cathy, tell me a simple and easy-to-understand joke.--------------------------------------------------------------------------------
[autogen.oai.client: 11-24 16:40:20] {351} WARNING - Model GLM-4-Plus 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.
[33mcathy[0m (to joe):Sure thing! Here's one for you:Why don't eggs tell jokes?Because they might crack up! 🥚😂--------------------------------------------------------------------------------
[autogen.oai.client: 11-24 16:40:23] {351} WARNING - Model GLM-4-Plus 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.
[33mjoe[0m (to cathy):Oh, Cathy, that's a classic! You know, eggs are always walking on eggshells, trying not to break their yolks. But let me add a punchline to that:Why don't eggs tell jokes?Because they might crack up, and then we'd have a real scrambled situation on our hands! 🍳😂Now, let's get out there and egg-ceed our audience's expectations! 🎤🤣--------------------------------------------------------------------------------
[autogen.oai.client: 11-24 16:40:27] {351} WARNING - Model GLM-4-Plus 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.
[33mcathy[0m (to joe):Oh, I love it! That's the spirit! Adding a little extra zing to the punchline really takes it up a notch. 🌟 With that kind of egg-cellent humor, we're bound to have them laughing all the way to the fridge! Let's crack those jokes and whisk up some fun! 🥚🎭🤣 Ready to take the stage and egg-press our comedic genius? Let's do this! 🚀🎤

总结

通过本教程,我们学习了如何使用Autogen库创建对话代理,并展示了如何生成对话回复和初始化多代理对话。希望这能帮助你更好地理解和应用Autogen库。

参考链接:https://microsoft.github.io/autogen/0.2/docs/tutorial/introduction
如果有任何问题,欢迎在评论区提问。

http://www.lryc.cn/news/490729.html

相关文章:

  • 开源账目和账单
  • vue2面试题10|[2024-11-24]
  • c语言与c++到底有什么区别?
  • 云计算-华为HCIA-学习笔记
  • 优先算法 —— 双指针系列 - 复写零
  • 初识Linux—— 基本指令(下)
  • esayexcel进行模板下载,数据导入,验证不通过,错误信息标注在excel上进行返回下载
  • 服务器数据恢复—raid5阵列热备盘上线失败导致EXT3文件系统不可用的数据恢复案例
  • 《Qt Creator:人工智能时代的跨平台开发利器》
  • AG32既可以做MCU,也可以仅当CPLD使用
  • 51c自动驾驶~合集31
  • 2023年3月GESPC++一级真题解析
  • linux NFS
  • 查看浏览器的请求头
  • 【JavaEE进阶】 JavaScript
  • 后端接受大写参数(亲测能用)
  • Unity ShaderLab --- 实现局部透明
  • Edify 3D: Scalable High-Quality 3D Asset Generation 论文解读
  • 银河麒麟v10 x86架构二进制方式kubeadm+docker+cri-docker搭建k8s集群(证书有效期100年) —— 筑梦之路
  • Python浪漫之画明亮的月亮
  • 【前端】JavaScript 中的函数嵌套:从基础到深度应用的全面指南
  • 微信小程序条件渲染与列表渲染的全面教程
  • 全面击破工程级复杂缓存难题
  • python安装包中的一些问题(三):加载 matplotlib 的过程中,调用了 Pillow(PIL 库)时发生了错误
  • AWTK-WEB 快速入门(1) - C 语言应用程序
  • 【Spiffo】环境配置:VScode+Windows开发环境
  • 贴代码框架PasteForm特性介绍之file
  • 2024年 数模美赛 B题 潜水艇
  • ChatGPT 与其他 AI 技术在短视频营销中的技术应用与协同策略
  • H.265流媒体播放器EasyPlayer.js播放器提示MSE不支持H.265解码可能的原因