跳到主要内容

追踪 AG2🤖

AG2 Tracing via autolog

AG2 是一个用于构建和编排 AI 代理交互的开源框架。

MLflow 追踪为开源多代理框架 AG2 提供自动追踪能力。通过调用 mlflow.ag2.autolog() 函数为 AG2 启用自动追踪,MLflow 将捕获嵌套追踪并在代理执行时将其记录到活跃的 MLflow 实验中。请注意,由于 AG2 基于 AutoGen 0.2 构建,因此此集成可在您使用 AutoGen 0.2 时使用。

import mlflow

mlflow.ag2.autolog()

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

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

基本示例

import os
from typing import Annotated, Literal

from autogen import ConversableAgent

import mlflow

# Turn on auto tracing for AG2
mlflow.ag2.autolog()

# Optional: Set a tracking URI and an experiment
mlflow.set_tracking_uri("https://:5000")
mlflow.set_experiment("AG2")


# Define a simple multi-agent workflow using AG2
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.ag2.autolog(disable=True)mlflow.autolog(disable=True) 来全局禁用 AG2 的自动追踪。