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结束。