跳到主要内容

追踪 AutoGen🤖

AutoGen Tracing via autolog

AutoGen 是一个用于构建事件驱动、分布式、可伸缩和有弹性的 AI 代理系统的开源框架。

MLflow Tracing 为 AutoGen(一个开源多代理框架)提供了自动追踪能力。通过调用 mlflow.autogen.autolog() 函数启用 AutoGen 的自动追踪,MLflow 将捕获嵌套追踪并在代理执行时将其记录到当前的 MLflow 实验中。

import mlflow

mlflow.autogen.autolog()

MLflow 捕获关于多代理执行的以下信息

  • 在不同回合调用了哪个代理
  • 代理之间传递的消息
  • 每个代理进行的 LLM 和工具调用,按代理和回合组织
  • 延迟
  • 如果发生任何异常

基本示例

import os
from typing import Annotated, Literal

from autogen import ConversableAgent

import mlflow

# Turn on auto tracing for AutoGen
mlflow.autogen.autolog()

# Optional: Set a tracking URI and an experiment
mlflow.set_tracking_uri("http://localhost:5000")
mlflow.set_experiment("AutoGen")


# Define a simple multi-agent workflow using AutoGen
config_list = [
{
"model": "gpt-4o-mini",
# Please set your OpenAI API Key to the OPENAI_API_KEY env var before running this example
"api_key": os.environ.get("OPENAI_API_KEY"),
}
]

Operator = Literal["+", "-", "*", "/"]


def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int:
if operator == "+":
return a + b
elif operator == "-":
return a - b
elif operator == "*":
return a * b
elif operator == "/":
return int(a / b)
else:
raise ValueError("Invalid operator")


# First define the assistant agent that suggests tool calls.
assistant = ConversableAgent(
name="Assistant",
system_message="You are a helpful AI assistant. "
"You can help with simple calculations. "
"Return 'TERMINATE' when the task is done.",
llm_config={"config_list": config_list},
)

# The user proxy agent is used for interacting with the assistant agent
# and executes tool calls.
user_proxy = ConversableAgent(
name="Tool Agent",
llm_config=False,
is_termination_msg=lambda msg: msg.get("content") is not None
and "TERMINATE" in msg["content"],
human_input_mode="NEVER",
)

# Register the tool signature with the assistant agent.
assistant.register_for_llm(name="calculator", description="A simple calculator")(
calculator
)
user_proxy.register_for_execution(name="calculator")(calculator)
response = user_proxy.initiate_chat(
assistant, message="What is (44231 + 13312 / (230 - 20)) * 4?"
)

禁用自动追踪

通过调用 mlflow.autogen.autolog(disable=True)mlflow.autolog(disable=True),可以全局禁用 AutoGen 的自动追踪。