微软Auto-gen试用及简单示例

简单试用一下微软的多智能框架autogen

目前项目在github开源https://github.com/microsoft/autogen,使用时直接安装python库即可,要求python>=3.10.

pip install -U "autogen-agentchat" "autogen-ext[openai]"

github的readme上给了两个示例。
第一个helloworld
官方示例是用openai,我这里用的阿里云qwq模型,需要创建一个.env文件,写入OPENAI_API_KEY=key

import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_core.models import ModelFamily
from dotenv import load_dotenv
load_dotenv()
async def main() -> None:
    model_client = OpenAIChatCompletionClient(model="qwen-plus",base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",                model_info={
                    "vision": False,
                    "function_calling": False,
                    "json_output": True,
                    "family": ModelFamily.UNKNOWN,
                    "structured_output": False,
                },)
    agent = AssistantAgent("assistant", model_client=model_client)
    print(await agent.run(task="Say 'Hello World!'"))
    await model_client.close()

asyncio.run(main())

结果
在这里插入图片描述
另一个示例是要下载个web surfer的浏览器,然后让autogen自动打开浏览器搜索内容,获取搜索引擎内容然后返回结果。
需要先执行命令
playwright install
pip install -U autogen-agentchat autogen-ext[openai,web-surfer]

# pip install -U autogen-agentchat autogen-ext[openai,web-surfer]
# playwright install
import asyncio
from autogen_agentchat.agents import UserProxyAgent
from autogen_agentchat.conditions import TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.agents.web_surfer import MultimodalWebSurfer
from autogen_core.models import ModelFamily
from dotenv import load_dotenv
load_dotenv()
async def main() -> None:

    model_client = OpenAIChatCompletionClient(model="qwen-plus",base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",                model_info={
                    "vision": False,
                    "function_calling": True,
                    "json_output": True,
                    "family": ModelFamily.UNKNOWN,
                    "structured_output": False,
                },)
    # The web surfer will open a Chromium browser window to perform web browsing tasks.
    web_surfer = MultimodalWebSurfer("web_surfer", model_client, headless=False, animate_actions=True)
    # The user proxy agent is used to get user input after each step of the web surfer.
    # NOTE: you can skip input by pressing Enter.
    user_proxy = UserProxyAgent("user_proxy")
    # The termination condition is set to end the conversation when the user types 'exit'.
    termination = TextMentionTermination("exit", sources=["user_proxy"])
    # Web surfer and user proxy take turns in a round-robin fashion.
    team = RoundRobinGroupChat([web_surfer, user_proxy], termination_condition=termination)
    try:
        # Start the team and wait for it to terminate.
        await Console(team.run_stream(task="Find information about AutoGen and write a short summary."))
    finally:
        await web_surfer.close()
        await model_client.close()

asyncio.run(main())

这里使用的大模型需要有function call功能。
在这里插入图片描述

在这里插入图片描述
然后用户可以再次输入任务,智能体去执行。直到用户输入exit结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖虎技安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值