MLflow 跟踪 为 Databricks Foundation 模型提供自动跟踪功能。 由于 Databricks Foundation Models 使用与 OpenAI 兼容的 API,因此可以通过调用 mlflow.openai.autolog
函数来启用自动跟踪,MLflow 将捕获 LLM 调用的跟踪并将其记录到活动的 MLflow 试验。
import mlflow
mlflow.openai.autolog()
MLflow 跟踪自动捕获有关 Databricks Foundation 模型调用的以下信息:
- 提示和补全应答
- 潜伏期
- 模型名称和终结点
- 其他元数据,例如
temperature
,max_tokens
如果指定 - 在响应中返回时进行函数调用
- 引发的任何异常
先决条件
若要将 MLflow 跟踪与 Databricks 基础模型配合使用,需要安装 MLflow 和 OpenAI SDK(因为 Databricks Foundation Models 使用与 OpenAI 兼容的 API)。
开发
对于开发环境,请使用 Databricks Extras 和 OpenAI SDK 安装完整的 MLflow 包:
pip install --upgrade "mlflow[databricks]>=3.1" openai
完整 mlflow[databricks]
包包括用于 Databricks 的本地开发和试验的所有功能。
生产
对于生产部署,请安装 mlflow-tracing
和 OpenAI SDK。
pip install --upgrade mlflow-tracing openai
包 mlflow-tracing
已针对生产用途进行优化。
注释
强烈建议使用 MLflow 3 进行 Databricks 基础模型的最佳跟踪体验。
在运行示例之前,需要配置环境:
对于不使用 Databricks 笔记本的用户:设置 Databricks 环境变量:
export DATABRICKS_HOST="https://your-workspace.cloud.databricks.com"
export DATABRICKS_TOKEN="your-personal-access-token"
对于 Databricks 笔记本中的用户:这些凭据会自动为您设置。
受支持的 API
MLflow 支持以下 Databricks Foundation 模型 API 的自动跟踪:
聊天补全 | 函数调用 | 流媒体 | 异步 |
---|---|---|---|
✅ | ✅ | ✅ | ✅ |
若要请求对其他 API 的支持,请在 GitHub 上打开 功能请求 。
基本示例
import mlflow
import os
from openai import OpenAI
# Databricks Foundation Model APIs use Databricks authentication.
# Enable auto-tracing for OpenAI (which will trace Databricks Foundation Model API calls)
mlflow.openai.autolog()
# Set up MLflow tracking to Databricks
mlflow.set_tracking_uri("databricks")
mlflow.set_experiment("/Shared/databricks-foundation-models-demo")
# Create OpenAI client configured for Databricks
client = OpenAI(
api_key=os.environ.get("DATABRICKS_TOKEN"),
base_url=f"{os.environ.get('DATABRICKS_HOST')}/serving-endpoints"
)
messages = [
{
"role": "user",
"content": "What is the capital of France?",
}
]
response = client.chat.completions.create(
model="databricks-llama-4-maverick",
messages=messages,
temperature=0.1,
max_tokens=100,
)
流媒体
MLflow 跟踪功能支持 Databricks 基础模型系列的流式处理 API。 通过相同的自动跟踪设置,MLflow 会自动跟踪流响应,并在跨度 UI 中显示串联的输出。
import mlflow
import os
from openai import OpenAI
# Enable auto-tracing for OpenAI (which will trace Databricks Foundation Model API calls)
mlflow.openai.autolog()
# Set up MLflow tracking to Databricks if not already configured
# mlflow.set_tracking_uri("databricks")
# mlflow.set_experiment("/Shared/databricks-streaming-demo")
# Create OpenAI client configured for Databricks
client = OpenAI(
api_key=os.environ.get("DATABRICKS_TOKEN"),
base_url=f"{os.environ.get('DATABRICKS_HOST')}/serving-endpoints"
)
stream = client.chat.completions.create(
model="databricks-llama-4-maverick",
messages=[
{"role": "user", "content": "Explain the benefits of using Databricks Foundation Models"}
],
stream=True, # Enable streaming response
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
函数调用
MLflow 追踪会自动捕获来自 Databricks Foundation Models 函数调用的响应。 响应中的函数指令将在跟踪 UI 中突出显示。 此外,可以使用@mlflow.trace
修饰器为工具函数添加批注,以便为工具执行创建跨度。
以下示例使用 Databricks Foundation Models 和 MLflow 跟踪实现简单的函数调用代理。
import json
import os
from openai import OpenAI
import mlflow
from mlflow.entities import SpanType
# Enable auto-tracing for OpenAI (which will trace Databricks Foundation Model API calls)
mlflow.openai.autolog()
# Set up MLflow tracking to Databricks if not already configured
# mlflow.set_tracking_uri("databricks")
# mlflow.set_experiment("/Shared/databricks-function-agent-demo")
# Create OpenAI client configured for Databricks
client = OpenAI(
api_key=os.environ.get("DATABRICKS_TOKEN"),
base_url=f"{os.environ.get('DATABRICKS_HOST')}/serving-endpoints"
)
# Define the tool function. Decorate it with `@mlflow.trace` to create a span for its execution.
@mlflow.trace(span_type=SpanType.TOOL)
def get_weather(city: str) -> str:
if city == "Tokyo":
return "sunny"
elif city == "Paris":
return "rainy"
return "unknown"
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
},
},
}
]
_tool_functions = {"get_weather": get_weather}
# Define a simple tool calling agent
@mlflow.trace(span_type=SpanType.AGENT)
def run_tool_agent(question: str):
messages = [{"role": "user", "content": question}]
# Invoke the model with the given question and available tools
response = client.chat.completions.create(
model="databricks-llama-4-maverick",
messages=messages,
tools=tools,
)
ai_msg = response.choices[0].message
# If the model requests tool call(s), invoke the function with the specified arguments
if tool_calls := ai_msg.tool_calls:
for tool_call in tool_calls:
function_name = tool_call.function.name
if tool_func := _tool_functions.get(function_name):
args = json.loads(tool_call.function.arguments)
tool_result = tool_func(**args)
else:
raise RuntimeError("An invalid tool is returned from the assistant!")
messages.append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": tool_result,
}
)
# Send the tool results to the model and get a new response
response = client.chat.completions.create(
model="databricks-llama-4-maverick", messages=messages
)
return response.choices[0].message.content
# Run the tool calling agent
question = "What's the weather like in Paris today?"
answer = run_tool_agent(question)
可用模型
Databricks 基础模型提供了一系列最先进的基础模型访问权限,包括 Llama、Anthropic 和其他领先的基础模型。
有关可用模型及其模型 ID 的完整和最 up-to日期列表,请参阅 Databricks 基础模型文档。
禁用自动跟踪
可以通过调用 mlflow.openai.autolog(disable=True)
或 mlflow.autolog(disable=True)
全局禁用 Databricks 基础模型的自动跟踪。