概述
如https://github.com/microsoft/autogen所述,autogen是一多智能体的框架,属于微软旗下的产品。
依靠AutoGen我们可以快速构建出一个多智能体应用,以满足我们各种业务场景。
环境说明
- python,3.10
- AutoGen,0.4.2
- chainlit,2.0.2
- 大模型,deepseek
安装依赖
pip install autogen-agentchat openai autogen-ext
使用UI交互界面为Chainlit 安装chainlit命令
pip install chainlit
3.示例一,聊天机器人
应用场景:创建一个聊天机器人,让他给你回复
代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : ai_hub.py
# @Software: PyCharm
import chainlit as cl
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models.openai import OpenAIChatCompletionClient
@cl.on_chat_start
async def main():
await cl.Message(content="sir 我是贾维斯,有什么可以帮您?").send()
async def run_team(query: str):
model_client = OpenAIChatCompletionClient(model="deepseek-chat", base_url="https://api.deepseek.com",api_key="YOU DEEPSEEK API KEY", model_info={
"vision": False,
"function_calling": False,
"json_output": True,
"family": "unknown",
}, )
assistant_agent = AssistantAgent("assistant", model_client=model_client,
system_message="你是一个机器人,请根据你所会的知识与用户进行友好交流!")
team = RoundRobinGroupChat(participants=[assistant_agent], max_turns=1)
response_stream = team.run_stream(task=query)
async for msg in response_stream:
if hasattr(msg, "source") and msg.source != "user" and hasattr(msg, "content"):
msg = cl.Message(content=msg.content, author="Agent Team")
await msg.send()
@cl.on_message
async def main(message: cl.Message):
await run_team(message.content)
启动机器人
chainlit run .\ai_hub.py -w
结果及界面