AutoGenのトレース
AutoGen は、イベント駆動型で分散型、スケーラブル、かつレジリエントな AI エージェント システムを構築するためのオープンソース フレームワークです。
MLflow Tracing は、オープンソースのマルチエージェントフレームワークである AutoGen の自動トレース機能を提供します。自動トレースを有効にする
AutoGen の場合、 mlflow.autogen.autolog
関数を呼び出すと、 MLflow はネストされたトレースをキャプチャし、エージェントの実行時にアクティブな MLflow エクスペリメントにログを記録します。
import mlflow
mlflow.autogen.autolog()
MLflow は、マルチエージェントの実行に関する次の情報をキャプチャします。
- どのエージェントがさまざまなターンで呼び出されるか
- エージェント間で渡されるメッセージ
- 各エージェントによって行われたLLMおよびツールコールは、エージェントとターンごとに編成されています
- 待ち時間
- 例外が発生した場合
前提 条件
AutoGen で MLflow Tracing を使用するには、 MLflow と pyautogen
ライブラリをインストールする必要があります。
- Development
- Production
開発環境の場合は、Databricks の追加機能と pyautogen
を含む完全な MLflow パッケージをインストールします。
pip install --upgrade "mlflow[databricks]>=3.1" pyautogen
フル mlflow[databricks]
パッケージには、Databricks でのローカル開発と実験のためのすべての機能が含まれています。
本番運用デプロイメントの場合は、 mlflow-tracing
と pyautogen
をインストールします。
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 キーを環境変数として設定します。
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?"
)
自動トレースを無効にする
AutoGen の自動トレースは、 mlflow.autogen.autolog(disable=True)
または mlflow.autolog(disable=True)
を呼び出すことで、グローバルに無効にできます。