共用方式為


追蹤 AutoGen

使用 autolog 進行 AutoGen 追蹤

AutoGen 是開放原始碼架構,用於建置事件驅動、分散式、可調整且具彈性的 AI 代理程序系統。

MLflow 追蹤 提供 AutoGen 的自動追蹤功能,這是開放原始碼的多代理程序架構。 藉由呼叫 mlflow.autogen.autolog 函式來啟用 AutoGen 的自動追蹤,MLflow 會擷取巢狀追蹤,並在代理程式執行時將其記錄至作用中的 MLflow 實驗。

import mlflow

mlflow.autogen.autolog()

MLflow 會擷取關於多重代理程序執行的下列資訊:

  • 不同回合中被呼叫的是哪個代理程式
  • 在代理程式之間傳遞的訊息
  • 每個代理所進行的 LLM 和工具呼叫,依代理和回合組織排列
  • 潛伏期
  • 如果引發任何例外狀況

備註

在無伺服器運算叢集上,不會自動啟用自動記錄。 您必須明確呼叫 mlflow.autogen.autolog() ,才能啟用此整合的自動追蹤。

先決條件

若要與 AutoGen 一起使用 MLflow 追蹤功能,您需要安裝 MLflow 以及 pyautogen 庫。

發展

針對開發環境,請安裝包含 Databricks 附加功能的完整 MLflow 套件和 pyautogen

pip install --upgrade "mlflow[databricks]>=3.1" pyautogen

完整 mlflow[databricks] 套件包含在 Databricks 上進行本地開發和實驗的所有功能。

生產

針對生產環境部署,請安裝 mlflow-tracingpyautogen

pip install --upgrade mlflow-tracing pyautogen

套件 mlflow-tracing 已針對生產環境使用進行優化。

備註

強烈建議使用 MLflow 3,以取得 AutoGen 的最佳追蹤體驗。

執行範例之前,您必須設定環境:

針對 Databricks Notebook 外部的用戶:設定 Databricks 環境變數:

export DATABRICKS_HOST="https://your-workspace.cloud.databricks.com"
export DATABRICKS_TOKEN="your-personal-access-token"

針對 Databricks 筆記本內的用戶:系統會自動為您設定這些認證。

OpenAI API 金鑰:確定您的 API 金鑰已設定。 針對生產用途,我們建議使用 馬賽克 AI 閘道或 Databricks 秘密 ,而不是環境變數:

export OPENAI_API_KEY="your-openai-api-key"

基本範例

import os
from typing import Annotated, Literal

from autogen import ConversableAgent

import mlflow

# Ensure your OPENAI_API_KEY (or other LLM provider keys) is set in your environment
# os.environ["OPENAI_API_KEY"] = "your-openai-api-key" # Uncomment and set if not globally configured

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

# Set up MLflow tracking on Databricks
mlflow.set_tracking_uri("databricks")
mlflow.set_experiment("/Shared/autogen-tracing-demo")


# 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?"
)

警告

針對生產環境,請一律使用 馬賽克 AI 閘道或 Databricks 秘密,而非硬編碼的值。

停用自動追蹤

您可以呼叫 mlflow.autogen.autolog(disable=True)mlflow.autolog(disable=True),全域停用 AutoGen 的自動追蹤。