MLflow 跟踪集成

MLflow 跟踪与一系列常用的生成式 AI 库和框架集成,为所有库和框架提供 单行自动跟踪 体验。 这样,你只需少量配置即可立即获得对代理的可观测性。

这种广泛的支持意味着你可以利用你已使用的工具,在没有重大代码更改的情况下获得可观测性。 对于自定义组件或不受支持的库,MLflow 还提供强大的手动跟踪 API。

自动跟踪根据特定库或 SDK 的实现方式捕获应用程序逻辑和中间步骤,例如 LLM 调用、工具使用情况和代理交互。

注释

在无服务器计算集群上,genAI 跟踪框架的自动记录功能不会自动启用。 必须通过为要跟踪的特定集成调用适当的 mlflow.<library>.autolog() 函数来显式启用自动记录。

注释

本页的快速启动示例需要MLflow 3.14或更高版本,因为它们会在Unity Catalog中存储跟踪。

顶层集成概览

下面是一些最常用的集成快速入门示例。 单击选项卡可查看基本用法示例。 有关每个方案的详细先决条件和更高级的方案,请访问其专用集成页(从选项卡或下面的列表链接)。

OpenAI

import mlflow
from mlflow.entities.trace_location import UnityCatalog
import openai

# If running this code outside of a Databricks notebook (e.g., locally),
# uncomment and set the following environment variables to point to your Databricks workspace:
# import os
# os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
# os.environ["DATABRICKS_TOKEN"] = "your-personal-access-token"

# Enable auto-tracing for OpenAI
mlflow.openai.autolog()

# Set up MLflow tracking
mlflow.set_tracking_uri("databricks")
mlflow.set_experiment(
    experiment_name="/Shared/openai-tracing-demo",
    trace_location=UnityCatalog(
        catalog_name="<UC_CATALOG_NAME>",
        schema_name="<UC_SCHEMA_NAME>",
        table_prefix="<UC_TABLE_PREFIX>",
    ),
)

openai_client = openai.OpenAI()

messages = [
    {
        "role": "user",
        "content": "What is the capital of France?",
    }
]

response = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    temperature=0.1,
    max_tokens=100,
)
# View trace in MLflow UI

完整 OpenAI 集成指南

LangChain

import mlflow
from mlflow.entities.trace_location import UnityCatalog
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

# If running this code outside of a Databricks notebook (e.g., locally),
# uncomment and set the following environment variables to point to your Databricks workspace:
# import os
# os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
# os.environ["DATABRICKS_TOKEN"] = "your-personal-access-token"

mlflow.langchain.autolog()

mlflow.set_tracking_uri("databricks")
mlflow.set_experiment(
    experiment_name="/Shared/langchain-tracing-demo",
    trace_location=UnityCatalog(
        catalog_name="<UC_CATALOG_NAME>",
        schema_name="<UC_SCHEMA_NAME>",
        table_prefix="<UC_TABLE_PREFIX>",
    ),
)

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7, max_tokens=1000)
prompt = PromptTemplate.from_template("Tell me a joke about {topic}.")
chain = prompt | llm | StrOutputParser()

chain.invoke({"topic": "artificial intelligence"})
# View trace in MLflow UI

完整 LangChain 集成指南

LangGraph

import mlflow
from mlflow.entities.trace_location import UnityCatalog
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

# If running this code outside of a Databricks notebook (e.g., locally),
# uncomment and set the following environment variables to point to your Databricks workspace:
# import os
# os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
# os.environ["DATABRICKS_TOKEN"] = "your-personal-access-token"

mlflow.langchain.autolog() # LangGraph uses LangChain's autolog

mlflow.set_tracking_uri("databricks")
mlflow.set_experiment(
    experiment_name="/Shared/langgraph-tracing-demo",
    trace_location=UnityCatalog(
        catalog_name="<UC_CATALOG_NAME>",
        schema_name="<UC_SCHEMA_NAME>",
        table_prefix="<UC_TABLE_PREFIX>",
    ),
)

@tool
def get_weather(city: str):
    """Use this to get weather information."""
    return f"It might be cloudy in {city}"

llm = ChatOpenAI(model="gpt-4o-mini")
graph = create_react_agent(llm, [get_weather])
result = graph.invoke({"messages": [("user", "what is the weather in sf?")]})
# View trace in MLflow UI

完整 LangGraph 集成指南

Anthropic

import mlflow
from mlflow.entities.trace_location import UnityCatalog
import anthropic
import os

# If running this code outside of a Databricks notebook (e.g., locally),
# uncomment and set the following environment variables to point to your Databricks workspace:
# os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
# os.environ["DATABRICKS_TOKEN"] = "your-personal-access-token"

mlflow.anthropic.autolog()

mlflow.set_tracking_uri("databricks")
mlflow.set_experiment(
    experiment_name="/Shared/anthropic-tracing-demo",
    trace_location=UnityCatalog(
        catalog_name="<UC_CATALOG_NAME>",
        schema_name="<UC_SCHEMA_NAME>",
        table_prefix="<UC_TABLE_PREFIX>",
    ),
)

client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello, Claude"}],
)
# View trace in MLflow UI

完整人类集成指南

DSPy

import mlflow
from mlflow.entities.trace_location import UnityCatalog
import dspy

# If running this code outside of a Databricks notebook (e.g., locally),
# uncomment and set the following environment variables to point to your Databricks workspace:
# import os
# os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
# os.environ["DATABRICKS_TOKEN"] = "your-personal-access-token"

mlflow.dspy.autolog()

mlflow.set_tracking_uri("databricks")
mlflow.set_experiment(
    experiment_name="/Shared/dspy-tracing-demo",
    trace_location=UnityCatalog(
        catalog_name="<UC_CATALOG_NAME>",
        schema_name="<UC_SCHEMA_NAME>",
        table_prefix="<UC_TABLE_PREFIX>",
    ),
)

lm = dspy.LM("openai/gpt-4o-mini") # Assumes OPENAI_API_KEY is set
dspy.configure(lm=lm)

class SimpleSignature(dspy.Signature):
    input_text: str = dspy.InputField()
    output_text: str = dspy.OutputField()

program = dspy.Predict(SimpleSignature)
result = program(input_text="Summarize MLflow Tracing.")
# View trace in MLflow UI

完整 DSPy 集成指南

Databricks

import mlflow
from mlflow.entities.trace_location import UnityCatalog
import os
from openai import OpenAI # Databricks FMAPI uses OpenAI client

# If running this code outside of a Databricks notebook (e.g., locally),
# uncomment and set the following environment variables to point to your Databricks workspace:
# os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
# os.environ["DATABRICKS_TOKEN"] = "your-personal-access-token"

mlflow.openai.autolog() # Traces Databricks FMAPI using OpenAI client

mlflow.set_tracking_uri("databricks")
mlflow.set_experiment(
    experiment_name="/Shared/databricks-fmapi-tracing",
    trace_location=UnityCatalog(
        catalog_name="<UC_CATALOG_NAME>",
        schema_name="<UC_SCHEMA_NAME>",
        table_prefix="<UC_TABLE_PREFIX>",
    ),
)

client = OpenAI(
    api_key=os.environ.get("DATABRICKS_TOKEN"),
    base_url=f"{os.environ.get('DATABRICKS_HOST')}/serving-endpoints"
)
response = client.chat.completions.create(
    model="databricks-llama-4-maverick",
    messages=[{"role": "user", "content": "Key features of MLflow?"}],
)
# View trace in MLflow UI

完整 Databricks 集成指南

Bedrock

import mlflow
from mlflow.entities.trace_location import UnityCatalog
import boto3

# If running this code outside of a Databricks notebook (e.g., locally),
# uncomment and set the following environment variables to point to your Databricks workspace:
# import os
# os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
# os.environ["DATABRICKS_TOKEN"] = "your-personal-access-token"

mlflow.bedrock.autolog()

mlflow.set_tracking_uri("databricks")
mlflow.set_experiment(
    experiment_name="/Shared/bedrock-tracing-demo",
    trace_location=UnityCatalog(
        catalog_name="<UC_CATALOG_NAME>",
        schema_name="<UC_SCHEMA_NAME>",
        table_prefix="<UC_TABLE_PREFIX>",
    ),
)

bedrock = boto3.client(
    service_name="bedrock-runtime",
    region_name="us-east-1" # Replace with your region
)
response = bedrock.converse(
    modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
    messages=[{"role": "user", "content": "Hello World in one line."}]
)
# View trace in MLflow UI

完整基岩集成指南

AutoGen

import mlflow
from mlflow.entities.trace_location import UnityCatalog
from autogen import ConversableAgent
import os

# If running this code outside of a Databricks notebook (e.g., locally),
# uncomment and set the following environment variables to point to your Databricks workspace:
# os.environ["DATABRICKS_HOST"] = "https://your-workspace.cloud.databricks.com"
# os.environ["DATABRICKS_TOKEN"] = "your-personal-access-token"

mlflow.autogen.autolog()

mlflow.set_tracking_uri("databricks")
mlflow.set_experiment(
    experiment_name="/Shared/autogen-tracing-demo",
    trace_location=UnityCatalog(
        catalog_name="<UC_CATALOG_NAME>",
        schema_name="<UC_SCHEMA_NAME>",
        table_prefix="<UC_TABLE_PREFIX>",
    ),
)

config_list = [{"model": "gpt-4o-mini", "api_key": os.environ.get("OPENAI_API_KEY")}]
assistant = ConversableAgent("assistant", llm_config={"config_list": config_list})
user_proxy = ConversableAgent("user_proxy", human_input_mode="NEVER", code_execution_config=False)

user_proxy.initiate_chat(assistant, message="What is 2+2?")
# View trace in MLflow UI

完整 AutoGen 集成指南

浏览所有集成

模型提供方

Anthropic Anthropic
AWS Bedrock AWS Bedrock
BytePlus 模型提供者
科希尔 模型提供者
Databricks 基础模型 API Databricks 基础模型
DeepSeek DeepSeek
FireworksAI 模型提供者
格罗克 Groq
Mistral Mistral
Moonshot AI 模型提供者
诺维塔人工智能 模型提供者
奥拉玛 Ollama
OpenAI OpenAI
Qwen 模型提供者
Together AI 模型提供者
xAI / Grok 模型提供者

代理与应用框架

AG2 代理框架
阿格诺 代理框架
Amazon Bedrock AgentCore 代理框架
AutoGen 多智能体框架
Claude Agent SDK 代理框架
CrewAI 代理框架
DSPy 优化框架
干草堆 代理框架
库格 代理框架
LangChain 代理框架
LangChain 深度代理 代理框架
LangGraph 代理框架
LlamaIndex 数据框架
LiveKit 代理 语音代理框架
马斯特拉 TypeScript 代理框架
Microsoft代理框架 代理框架
OpenAI 代理 代理框架
Pipecat 语音代理框架
PydanticAI 代理框架
Quarkus LangChain4j Java 代理框架
语义内核 多语言框架
Smolagents 代理框架
Spring AI Java 框架
线束 代理框架
集群 代理框架
TxtAI 语义搜索框架
Vercel AI SDK TypeScript 框架
伏特特工 TypeScript 代理框架
沃森克斯管弦乐团 编排平台

代码代理与长时间运行代理

克劳德密码 编程代理
Codex CLI 编程代理
Gemini CLI 编程代理
赫尔墨斯代理 编程代理
Omnigent 编程代理
OpenCode 编程代理
OpenHands 编程代理
Qwen密码 编程代理

Gateways

Databricks AI 网关 LLM 网关
直升机 LLM 网关
Kong AI网关 LLM 网关
LiteLLM 代理 LLM 网关
MLflow AI 网关 LLM 网关
OpenRouter LLM 网关
Portkey LLM 网关
Pydantic AI 门户 LLM 网关
TrueFoundry LLM 网关
Vercel AI 网关 LLM 网关

可观测性与工具

阿里泽 / 凤凰城 可观测性平台
讲师 结构化输出库
Langfuse 可观测性平台
LiteLLM LLM 抽象库
开放遥测 可观测性标准

No-Code 平台

鹅 自治工作区代理
朗弗洛 可视化工作流程构建器
n8n 工作流程自动化平台
打开Web界面 聊天界面平台

安全 API 密钥管理

对于生产环境,Databricks 建议使用 AI 网关或 Databricks 机密来管理 API 密钥。 AI 网关是首选方法,提供其他治理功能。

警告

切勿直接在代码或笔记本中提交 API 密钥。 对于敏感凭据,始终使用 AI 网关或 Databricks 机密。

Databricks 机密信息

使用 Databricks 机密管理 API 密钥:

  1. 创建机密范围并存储 API 密钥:

    from databricks.sdk import WorkspaceClient
    
    # Set your secret scope and key names
    secret_scope_name = "llm-secrets"  # Choose an appropriate scope name
    secret_key_name = "api-key"        # Choose an appropriate key name
    
    # Create the secret scope and store your API key
    w = WorkspaceClient()
    w.secrets.create_scope(scope=secret_scope_name)
    w.secrets.put_secret(
        scope=secret_scope_name,
        key=secret_key_name,
        string_value="your-api-key-here"  # Replace with your actual API key
    )
    
  2. 检索和使用代码中的机密:

    import mlflow
    import openai
    import os
    
    # Configure your secret scope and key names
    secret_scope_name = "llm-secrets"
    secret_key_name = "api-key"
    
    # Retrieve the API key from Databricks secrets
    os.environ["OPENAI_API_KEY"] = dbutils.secrets.get(
        scope=secret_scope_name,
        key=secret_key_name
    )
    
    # Enable automatic tracing
    mlflow.openai.autolog()
    
    # Use OpenAI client with securely managed API key
    client = openai.OpenAI()
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Explain MLflow Tracing"}],
        max_tokens=100
    )
    

启用多个自动跟踪集成

由于代理经常合并多个库,MLflow 追踪允许你同时启用多个集成的自动追踪,提供统一的追踪体验。

例如,若要启用 LangChain 和直接 OpenAI 跟踪,请执行以下命令:

import mlflow

# Enable MLflow Tracing for both LangChain and OpenAI
mlflow.langchain.autolog()
mlflow.openai.autolog()

# Your code using both LangChain and OpenAI directly...
# ... an example can be found on the Automatic Tracing page ...

禁用自动跟踪

可以通过调用 mlflow.<library>.autolog(disable=True)来禁用任何特定库的自动跟踪。 若要一次性禁用所有自动记录集成,请使用 mlflow.autolog(disable=True)。

import mlflow

# Disable for a specific library
mlflow.openai.autolog(disable=True)

# Disable all autologging
mlflow.autolog(disable=True)