自学内容网 自学内容网

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
如果有任何问题,欢迎在评论区提问。


原文地址:https://blog.csdn.net/qq_41472205/article/details/144015518

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!