AutoGen のトレース

自動ログを使用した 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 の追加機能と pyautogenを含む完全な MLflow パッケージをインストールします。

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

完全な mlflow[databricks] パッケージには、Databricks でのローカル開発と実験のためのすべての機能が含まれています。

生産

運用環境のデプロイの場合は、 mlflow-tracingpyautogenをインストールします。

pip install --upgrade mlflow-tracing pyautogen

mlflow-tracing パッケージは、運用環境で使用するために最適化されています。

AutoGen で最適なトレース エクスペリエンスを実現するには、MLflow 3 を強くお勧めします。

例を実行する前に、環境を構成する必要があります。

Databricks ノートブックの外部のユーザーの場合: Databricks 環境変数を設定します。

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

Databricks ノートブック内のユーザーの場合: これらの資格情報は自動的に設定されます。

OpenAI API キー: API キーが構成されていることを確認します。 運用環境では、環境変数ではなく 、Mosaic AI Gateway または 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?"
)

Warnung

運用環境では、ハードコーディングされた値ではなく、 常に Mosaic AI Gateway または Databricks シークレット を使用します。

自動トレースを無効にする

AutoGen の自動トレースは、 mlflow.autogen.autolog(disable=True) または mlflow.autolog(disable=True)を呼び出すことによってグローバルに無効にすることができます。