本文主要结束AutoGen开源框架优劣点、带您一步一步搭建AutoGen开发环境、搭建本地LLM服务、并编写第一个天气服务代理;
让我们一起进入AI Agents世界遨游吧;
AutoGen框架
AutoGen 是AI Agents领域开源框架的先驱,由微软推出,用于构建复杂的Agent 系统。AutoGen 支持多种编程语言和跨语言开发(包括 Python 和.NET),以及可扩展性和分布式网络构建能力,异步消息通信能力,支持多种 LLM 集成等优势。AutoGen适用于软件开发任务领域,特别是涉及复杂多 Agent 编码工作流项目。例如,构建大型软件项目的自动化测试框架。
主要特点如下:
- AutoGen 包含用户智能体和助手智能体两个核心角色。
- 用户智能体负责提出编程需求或编写提示词,助手智能体则负责生成和执行代码。
- 助手智能体不仅负责代码生成,还包括代码执行过程,并将结果反馈给用户智能体或其他智能体。
- 该框架擅长于代码任务的多智能体编排(multi-agent orchestration),同时也具备处理其他类型任务的能力。
- 在智能体间的交互过程中,允许人工提供指导。
- 来自微软的强大、坚实的社区支持。
然而,AutoGen 也存在以下局限性:
- 对非编程背景的用户来说,操作不够直观。
- 在本地部署大语言模型(LLMs)时,配置过程较为繁琐,需要额外配置代理服务器。
- 在非软件开发领域,其表现可能不如专业工具出色。
GIT地址:github.com/microsoft/a…
既然AutoGen这么优秀,让我们快速开始体验吧!!!
搭建AutoGen开发环境
安装Python环境
# 安装python 3.12.8,下载安装包 https://www.python.org/ftp/python/3.12.8/python-3.12.8-macos11.pkg
# 双击安装
# macos配置环境变量
vim ~/.bash_profile
export PATH="/Library/Frameworks/Python.framework/Versions/3.12/bin:$PATH"
alias python='python3'
alias pip='pip3'
source ~/.bash_profile
# 查看python版本
python --version
搭建本地LLM服务
若可访问openai服务(因为强的原因),此步骤可跳过。
# 下砸ollama并双击安装(https://ollama.com/)
# 运行qwen2.5
ollama run qwen2.5
# 查看是否安装成功
ollama list
# 安装LiteLLM服务
pip install 'litellm[proxy]'
# 查看litellm版本
litellm --version
# 通过litellm启动ollama qwen2.5 模型
litellm --model ollama/qwen2.5
安装AutoGen开发环境
# github下载AutoGen源码
git clone git@github.com:microsoft/autogen.git
# 打开autogen根目录
cd /Users/james/workspace/autogen
# 通过uv创建虚拟环境(https://docs.astral.sh/uv/)
uv venv
# 激活虚拟环境
source .venv/bin/activate
# 安装autoGen依赖包
uv pip install -U "autogen-agentchat" "autogen-ext" "autogen-core"
到此为止,AutoGen开发环境已经安装完毕,让我们快速构建一个天气Agent的助理吧!
Weather-Agent 示例
使用OpenAI模型的Demo
若是您没有翻墙访问OpenAI条件,可跳过直接看使用本地Qwen2.5模型的Demo
- 基于OpenAI LLM的天气代理源码
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from aioconsole import ainput
# 定义天气工具
async def get_weather(city: str) -> str:
return f"The weather in {city} is 73 degrees and Sunny."
async def main() -> None:
# 定义一个助理
weather_agent = AssistantAgent(
name="weather_agent",
model_client=OpenAIChatCompletionClient(
model="gpt-4o-2024-08-06",
# api_key="YOUR_API_KEY",
),
tools=[get_weather],
)
# 定义结束条件
text_mention_termination = TextMentionTermination("TERMINATE")
max_messages_termination = MaxMessageTermination(max_messages=5)
termination = text_mention_termination | max_messages_termination
# 定义一个单例的Agent团队
agent_team = RoundRobinGroupChat([weather_agent], termination_condition=termination)
while True:
# 从控制台获取用户输入
user_input = await ainput("Enter a message(type 'exit' to leave): ")
if user_input == 'exit':
break
# 运行代理并在控制台输出信息
stream = agent_team.run_stream(task=user_input)
await Console(stream)
# 运行
if __name__ == "__main__":
asyncio.run(main())
使用本地Qwen2.5模型的Demo
- 修改AutoGen支持Qwen2.5模型
# 打开_model_info.py文件
vim /Users/james/workspace/autogen/python/packages/autogen-ext/src/autogen_ext/models/openai/_model_info.py
# 在_model_info.py以下3个方法中增加qwen2.5模型的支持
_MODEL_POINTERS = {
... ...
### 增加qwen2.5模型
"qwen2.5": "qwen2.5",
}
_MODEL_INFO: Dict[str, ModelInfo] = {
... ...
### 增加qwen2.5模型信息
"qwen2.5": {
"vision": False,
"function_calling": True,
"json_output": True,
},
}
_MODEL_TOKEN_LIMITS: Dict[str, int] = {
... ...
### 增加qwen2.5模型token限制
"qwen2.5": 128000,
}
# AutoGen(支持qwen2.5模型)打包构建并安装到本地开发环境
cd /Users/james/workspace/autogen/python/packages/autogen-ext
uv build
uv pip install /Users/james/workspace/autogen/python/dist/autogen_ext-0.4.1.tar.gz
# 若安装成功,运行结果如下图:
到此AutoGen支持本地LLM模型配置完成!
- 基于本地ollama/qwen2.5 LLM的天气服务代理源码
# 创建Weather-Agent.py文件
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import TextMentionTermination, MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from aioconsole import ainput
import asyncio
# 定义天气工具
async def get_weather(city: str) -> str:
return f"The weather in {city} is 73 degrees and Sunny."
async def main() -> None:
# 定义一个助理
weather_agent = AssistantAgent(
name="weather_agent",
model_client=OpenAIChatCompletionClient(
model="qwen2.5",
api_key="NULL",
base_url="http://0.0.0.0:4000"
),
tools=[get_weather],
)
# 定义结束条件
text_mention_termination = TextMentionTermination("TERMINATE")
max_messages_termination = MaxMessageTermination(max_messages=5)
termination = text_mention_termination | max_messages_termination
# 定义一个单例的Agent团队
agent_team = RoundRobinGroupChat([weather_agent], termination_condition=termination)
while True:
# 从控制台获取用户输入
user_input = await ainput("Enter a message(type 'exit' to leave): ")
if user_input == 'exit':
break
# 运行代理并在控制台输出信息
stream = agent_team.run_stream(task=user_input)
await Console(stream)
# 定义Main方法
if __name__ == "__main__":
asyncio.run(main())
运行weather-agent.py
python weaterh-agent.py
# 运行结果如下图