为了方便测试效果,所以我使用了Chainlit这个库,帮助我快速生成Chat界面。
初始化下载依赖请参考AutoGen官网 。
1. 单个Agent对话
这里的核心就是使用了 AssistantAgent 来生成了一个 导诊 的Agent。
import chainlit as cl
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import TextMessage
from dotenv import load_dotenv
from autogen_ext.models.openai import AzureOpenAIChatCompletionClient
from autogen_core import CancellationToken
import os
load_dotenv()
@cl.on_chat_start
async def main():
await cl.Message(content="您好,这里是超级无敌大医院,有什么可以帮您?").send()
async def run_team(query: str):
model_client = AzureOpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
)
assistant_agent = AssistantAgent(
"assistant",
model_client=model_client,
system_message="你是一所口腔医院的导诊台机器人,负责解答用户的挂号问题,用户描述症状需求,你回答应该挂的科室。"
"在本医院中有以下科室:牙体牙髓科、口腔修复科、口腔外科、口腔种植科、儿童口腔专科。"
"如果用户的问题与挂号咨询不符合,回答:“您的描述与症状无关,暂不支持”",
)
async for msg in assistant_agent.on_messages_stream(
[TextMessage(content=query, source="user")],
cancellation_token=CancellationToken(),
):
if hasattr(msg, 'chat_message'):
chat_message = msg.chat_message
if chat_message.source != "user" and hasattr(chat_message, "content"):
message = cl.Message(content=chat_message.content, author="Agent Team")
await message.send()
@cl.on_message
async def main(message: cl.Message):
await run_team(message.content)
2. Multi Modal Agent,图片解析
import chainlit as cl
from autogen_agentchat.agents import AssistantAgent
from dotenv import load_dotenv
from autogen_ext.models.openai import AzureOpenAIChatCompletionClient
from autogen_core import CancellationToken
import os
from autogen_agentchat.messages import MultiModalMessage
from autogen_core import Image
import PIL
load_dotenv()
@cl.on_chat_start
async def main():
await cl.Message(
content="您好,这里是图片分析大师,请上传图片和描述,我会分析图片并给出分析结果"
).send()
async def run_team(query: str, elements: list):
print("打印的 query", query)
model_client = AzureOpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
)
agent = AssistantAgent(
name="assistant",
model_client=model_client,
)
pil_image = PIL.Image.open(elements[0].path)
img = Image(pil_image)
multi_modal_message = MultiModalMessage(content=[query, img], source="user")
async for msg in agent.on_messages_stream(
[multi_modal_message],
cancellation_token=CancellationToken(),
):
print("打印的 msg", msg)
if hasattr(msg, "chat_message"):
chat_message = msg.chat_message
if chat_message.source != "user" and hasattr(chat_message, "content"):
message = cl.Message(content=chat_message.content, author="Agent Team")
await message.send()
@cl.on_message
async def main(message: cl.Message):
await run_team(message.content, message.elements)
3. Agent 使用 Tools, 其实也就是funcation call
import chainlit as cl
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import AzureOpenAIChatCompletionClient
import os
from dotenv import load_dotenv
from autogen_core import CancellationToken
from autogen_agentchat.messages import TextMessage
load_dotenv()
@cl.on_chat_start
async def main():
await cl.Message(content="您好,这里是牙体牙髓科,您牙齿哪里不适?").send()
async def x_p_search(tooth_position: str) -> str:
if tooth_position == "46":
return "牙根尖处有阴影,疑似感染,需要进一步分析诊断"
else:
return f"{tooth_position}无影像"
async def run_team(query: str):
model_client = AzureOpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
temperature=0,
)
assistant_agent = AssistantAgent(
"assistant",
model_client=model_client,
tools=[x_p_search],
system_message="你是一个牙体牙髓科的病情诊断机器人,负责对用户输入的症状描述分析原因,在分析病因前先询问出用户是具体哪一颗牙齿需要治疗。"
"在知道了具体的牙位号后,再调用x_p_search工具进行问题回答,传入给x_p_search工具的参数需要自动转为牙位号,如:28"
"如果用户的问题与病情咨询无关,回答:“您的描述与症状无关,暂不支持”",
)
async for msg in assistant_agent.on_messages_stream(
[TextMessage(content=query, source="user")],
cancellation_token=CancellationToken(),
):
if hasattr(msg, "chat_message"):
chat_message = msg.chat_message
if chat_message.source != "user" and hasattr(chat_message, "content"):
if chat_message.content.endswith("无影像"):
res = await cl.AskActionMessage(
content=f"{chat_message.content},是否需要帮您申请拍摄此牙的CT影像?",
actions=[
cl.Action(
name="continue",
payload={"value": "申请"},
label="✅ 申请牙片",
),
cl.Action(
name="cancel",
payload={"value": "取消"},
label="❌ 取消",
),
],
).send()
if res and res.get("payload").get("value") == "申请":
await cl.Message(
content="牙片申请已提交!待审核通过后前往第3影像室进行拍摄。",
).send()
else:
msg = cl.Message(content=chat_message.content, author="Agent Team")
await msg.send()
@cl.on_message
async def main(message: cl.Message):
await run_team(message.content)
4.多个Agent 组成 Team, 进行分工合作,直到得到答案,经典的功能键就是AutoGen帮我们写代码并且执行,执行下来就可以观察到,当我们生成代码后,如果错误,会再次重新生成,直到最后执行成功
import asyncio
import os
from dotenv import load_dotenv
from autogen_ext.models.openai import AzureOpenAIChatCompletionClient
from autogen_agentchat.agents import CodeExecutorAgent
from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor
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
import pandas as pd
load_dotenv()
model_client = AzureOpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
temperature=0,
)
assistant = AssistantAgent(
name="Coder",
system_message="""You are a helpful AI assistant.
Solve tasks using your coding and language skills.
In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.
1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time, check the operating system. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.
2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.
Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.
When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
If you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible. Reply 'TERMINATE' in the end when everything is done.
""",
model_client=model_client,
)
code_executor = CodeExecutorAgent(
name="code_executor",
code_executor=LocalCommandLineCodeExecutor(work_dir="coding"),
sources="Coder",
)
termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10)
group_chat = RoundRobinGroupChat(
[assistant, code_executor], termination_condition=termination
)
async def run_code_executor_agent() -> None:
stream = group_chat.run_stream(task="我想去解析Excel文件转化为markdown文件,它的位置在data/transaction_data 1.xlsx")
await Console(stream)
asyncio.run(run_code_executor_agent())