跳到主要内容

超越 Autolog:为新 LLM 提供商添加 MLflow 追踪

·17 分钟阅读
Daniel Liden
Databricks 开发者布道师

在本博文中,我们将通过为 Ollama Python SDK 的 chat 方法添加 Tracing 支持,演示如何为新的 LLM 提供商添加 MLflow Tracing。

MLflow Tracing 是 MLflow 中的一个可观测性工具,它可以捕获 GenAI 应用程序和工作流的详细执行轨迹。除了单个调用的输入、输出和元数据外,MLflow Tracing 还可以捕获中间步骤,如工具调用、推理步骤、检索步骤或其他自定义步骤。

MLflow 为许多流行的 LLM 提供商和编排框架提供了内置 Tracing 支持。如果您正在使用其中一个提供商,只需一行代码即可启用 Tracing:mlflow.<provider>.autolog()。虽然 MLflow 的自动日志记录功能涵盖了许多最常用的 LLM 提供商和编排框架,但有时您可能需要为不受支持的提供商添加 Tracing,或者进行超出自动日志记录功能的 Tracing 定制。本博文将通过以下方式展示 MLflow Tracing 的灵活性和可扩展性:

  • 为不受支持的提供商(Ollama Python SDK)添加基本 Tracing 支持
  • 展示如何捕获简单的补全和更复杂的工具调用工作流
  • 说明如何以最小的代码更改来添加 Tracing

我们将以Ollama Python SDK 为例,这是一个用于 Ollama LLM 平台的开源 Python SDK。我们将逐步进行,展示如何使用 MLflow Tracing 捕获关键信息,同时保持与提供商 SDK 的干净集成。请注意,MLflow 确实支持 Ollama 的自动日志记录,但目前仅通过 OpenAI 客户端使用,而不是直接与 Ollama Python SDK 结合使用。

为新的提供商添加 MLflow Tracing:通用原则

MLflow 文档中有一篇优秀指南,介绍了如何为 MLflow Tracing 做贡献。虽然在本例中我们不直接贡献给 MLflow 本身,但我们将遵循相同的通用原则。

本博文假设您对 MLflow Tracing 是什么以及它是如何工作的有一个基本的了解。如果您是初学者,或者需要复习,请查看Tracing Concepts 指南。

为新的提供商添加 Tracing 涉及几个关键考虑因素:

  1. 了解提供商的关键功能: 我们首先需要了解需要 Tracing 哪些 API 方法才能获得我们想要的 Tracing 信息。对于 LLM 推理提供商,这通常涉及聊天补全、工具调用或嵌入生成等操作。在编排框架中,这可能涉及检索、推理、路由或各种自定义步骤等操作。在我们的 Ollama 示例中,我们将重点关注聊天补全 API。这一步将因提供商而异。

  2. 将操作映射到 span: MLflow Tracing 使用不同的span 类型来表示不同类型的操作。您可以在此处找到内置 span 类型的描述。不同的 span 类型在 MLflow UI 中显示方式不同,并能启用特定功能。在 span 内部,我们还需要将提供商的输入和输出映射到 MLflow 期望的格式。MLflow 提供了用于记录聊天和工具输入输出的实用程序,这些将显示在 MLflow UI 中格式化的消息中。

    Chat Messages

    在为新提供商添加 Tracing 时,我们的主要任务是将提供商的 API 方法映射到具有适当 span 类型的 MLflow Tracing span。

  3. 结构化和保存关键数据: 对于我们想要 Tracing 的每个操作,我们需要识别我们想要保留的关键信息,并确保它被捕获并以有用的方式显示。例如,我们可能希望捕获控制操作行为的输入和配置数据、解释结果的输出和元数据、过早终止操作的错误等。查看类似提供商的 Traces 和 Tracing 实现可以为如何结构化和保存这些数据提供一个良好的起点。

为 Ollama Python SDK 添加 Tracing

现在我们对为新提供商添加 Tracing 的关键步骤有了高层次的理解,让我们来逐步完成这个过程,并为 Ollama Python SDK 添加 Tracing。

步骤 1:安装和测试 Ollama Python SDK

首先,我们需要安装 Ollama Python SDK,并找出在添加 Tracing 支持时需要关注哪些方法。您可以使用 pip install ollama-python 来安装 Ollama Python SDK。

如果您使用过 OpenAI Python SDK,Ollama Python SDK 会让您感觉很熟悉。以下是我们如何使用它来发出聊天补全请求:

from ollama import chat
from rich import print

response = chat(model="llama3.2",
messages = [
{"role": "user", "content": "Briefly describe the components of an MLflow model"}
]
)

print(response)

这将返回:

ChatResponse(
model='llama3.2',
created_at='2025-01-30T15:57:39.097119Z',
done=True,
done_reason='stop',
total_duration=7687553708,
load_duration=823704250,
prompt_eval_count=35,
prompt_eval_duration=3414000000,
eval_count=215,
eval_duration=3447000000,
message=Message(
role='assistant',
content="In MLflow, a model consists of several key components:\n\n1. **Model Registry**: A centralized
storage for models, containing metadata such as the model's name, version, and description.\n2. **Model Version**:
A specific iteration of a model, represented by a unique version number. This can be thought of as a snapshot of
the model at a particular point in time.\n3. **Model Artifacts**: The actual model code, parameters, and data used
to train the model. These artifacts are stored in the Model Registry and can be easily deployed or reused.\n4.
**Experiment**: A collection of runs that use the same hyperparameters and model version to train and evaluate a
model. Experiments help track progress, provide reproducibility, and facilitate collaboration.\n5. **Run**: An
individual instance of training or testing a model using a specific experiment. Runs capture the output of each
run, including metrics such as accuracy, loss, and more.\n\nThese components work together to enable efficient
model management, version control, and reproducibility in machine learning workflows.",
images=None,
tool_calls=None
)
)

我们已经验证了 Ollama Python SDK 已设置并正常工作。我们也知道在添加 Tracing 支持时需要关注的方法:ollama.chat

步骤 2:编写 Tracing 装饰器

有几种方法可以为 Ollama 的 SDK 添加 Tracing — 我们可以直接修改 SDK 代码,创建一个包装类,或者使用 Python 的方法修补功能。在本例中,我们将使用一个装饰器来修补 SDK 的 chat 方法。这种方法允许我们在不修改 SDK 代码或创建额外包装类的情况下添加 Tracing,尽管它需要理解 Python 的装饰器模式和 MLflow Tracing 的工作原理。

import mlflow
from mlflow.entities import SpanType
from mlflow.tracing.utils import set_span_chat_messages
from functools import wraps
from ollama import chat as ollama_chat

def _get_span_type(task_name: str) -> str:
span_type_mapping = {
"chat": SpanType.CHAT_MODEL,
}
return span_type_mapping.get(task_name, SpanType.UNKNOWN)

def trace_ollama_chat(func):
@wraps(func)
def wrapper(*args, **kwargs):
with mlflow.start_span(
name="ollama.chat",
span_type=_get_span_type("chat"),
) as span:
# Set model name as a span attribute
model_name = kwargs.get("model", "")
span.set_attribute("model_name", model_name)

# Log the inputs
input_messages = kwargs.get("messages", [])
span.set_inputs({
"messages": input_messages,
"model": model_name,
})

# Set input messages
set_span_chat_messages(span, input_messages)

# Make the API call
response = func(*args, **kwargs)

# Log the outputs
if hasattr(response, 'to_dict'):
output = response.to_dict()
else:
output = response
span.set_outputs(output)

output_message = response.message

# Append the output message
set_span_chat_messages(span, [{"role": output_message.role, "content": output_message.content}], append=True)

return response
return wrapper

让我们分解代码并看看它是如何工作的。

  1. 我们首先定义一个辅助函数 _get_span_type,它将 Ollama 方法映射到 MLflow span 类型。这并非严格必需,因为我们目前只 Tracing chat 函数,但它展示了一种可以应用于其他方法的模式。这遵循了Anthropic provider 的参考实现,正如 Tracing 贡献指南中所推荐的那样。

  2. 我们使用functools.wraps 定义了一个装饰器 trace_ollama_chat,它修补了 chat 函数。这里有几个关键步骤:

    1. 我们使用 mlflow.start_span 开始一个新的 span。span 名称设置为 "ollama.chat",span 类型设置为 _get_span_type 返回的值。

    2. 我们使用 span.set_attributemodel_name 设置为 span 的一个属性。这并非严格必需,因为模型名称将在输入中捕获,但它演示了如何设置 span 的任意属性。

    3. 我们使用 span.set_inputs 将消息作为 span 的输入进行记录。我们通过访问 kwargs 字典从 messages 参数获取这些消息。这些消息将记录在 MLflow UI 中 span 的“inputs”部分。我们还记录了模型名称作为输入,同样是为了说明如何记录任意输入。

      Inputs

    4. 我们使用 MLflow 的 set_span_chat_messages 实用函数来格式化输入消息,使其在 MLflow UI 的 Chat 面板中显示良好。这个助手函数确保消息被正确格式化,并为每条消息角色显示适当的样式。

    5. 我们使用 func(*args, **kwargs) 调用原始函数。这是 Ollama 的 chat 函数。

    6. 我们使用 span.set_outputs 将函数输出作为 span 属性进行记录。这会获取 Ollama API 的响应,并将其设置为 span 的属性。这些输出将记录在 MLflow UI 中 span 的“outputs”部分。

      Outputs

    7. 我们从响应中提取输出消息,并再次使用 set_span_chat_messages 将其追加到聊天历史记录中,确保它出现在 MLflow UI 的 Chat 面板中。

      Messages Panel

    8. 最后,我们返回 API 调用的响应,不做任何更改。现在,当我们使用 trace_ollama_chat 修补 chat 函数时,该函数将被 Traced,但其行为将与正常情况相同。

有几点需要注意:

  • 此实现使用了一个简单的装饰器模式,它在不修改底层 Ollama SDK 代码的情况下添加 Tracing。这使其成为一种轻量级且易于维护的方法。
  • 使用 set_span_chat_messages 可以确保输入和输出消息在 MLflow UI 的 Chat 面板中以用户友好的方式显示,从而方便跟踪对话流程。
  • 我们还可以通过其他几种方式实现这种 Tracing 行为。我们可以编写一个包装类,或者使用一个简单的包装函数,用 @mlflow.trace 来装饰 chat 函数。某些编排框架可能需要更复杂的方法,例如回调或 API 钩子。有关更多详细信息,请参阅MLflow Tracing Contribution Guide

步骤 3:修补 chat 方法并进行测试

现在我们有了 Tracing 装饰器,就可以修补 Ollama 的 chat 方法并进行测试了。

original_chat = ollama_chat
chat = trace_ollama_chat(ollama_chat)

此代码有效地修补了当前作用域中的 ollama.chat 函数。我们首先将原始函数存储在 original_chat 中以供安全保管,然后将 chat 重新赋值为装饰后的版本。这意味着我们代码中任何后续对 chat() 的调用都将使用 Traced 版本,同时保留原始功能。

现在,当我们调用 chat() 时,该方法将被 Traced,结果将被记录到 MLflow UI 中。

mlflow.set_experiment("ollama-tracing")

response = chat(model="llama3.2",
messages = [
{"role": "user", "content": "Briefly describe the components of an MLflow model"}
]
)

Tracing results

Tracing 工具和工具调用

Ollama Python SDK 支持工具调用。我们希望记录两件主要事情:

  1. 提供给 LLM 的工具
  2. 实际的工具调用,包括特定的工具和传递给它的参数。

请注意,“工具调用”指的是 LLM 指定使用哪个工具以及传递什么参数 — 而不是实际执行该工具。当 LLM 进行工具调用时,它实际上是在说“这个工具应该用这些参数运行”,而不是实际运行工具本身。工具的实际执行是分开进行的,通常在应用程序代码中。

这是 Tracing 代码的更新版本,修补了 Ollama chat 方法,该版本记录了可用的工具并捕获了工具调用:

from mlflow.entities import SpanType
from mlflow.tracing.utils import set_span_chat_messages, set_span_chat_tools
from functools import wraps
from ollama import chat as ollama_chat
import json
from uuid import uuid4

def _get_span_type(task_name: str) -> str:
span_type_mapping = {
"chat": SpanType.CHAT_MODEL,
}
return span_type_mapping.get(task_name, SpanType.UNKNOWN)

def trace_ollama_chat(func):
@wraps(func)
def wrapper(*args, **kwargs):
with mlflow.start_span(
name="ollama.chat",
span_type=_get_span_type("chat"),
) as span:
# Set model name as a span attribute
model_name = kwargs.get("model", "")
span.set_attribute("model_name", model_name)

# Log the inputs
input_messages = kwargs.get("messages", [])
tools = kwargs.get("tools", [])
span.set_inputs({
"messages": input_messages,
"model": model_name,
"tools": tools,
})

# Set input messages and tools
set_span_chat_messages(span, input_messages)
if tools:
set_span_chat_tools(span, tools)

# Make the API call
response = func(*args, **kwargs)

# Log the outputs
if hasattr(response, "to_dict"):
output = response.to_dict()
else:
output = response
span.set_outputs(output)

output_message = response.message

# Prepare the output message for span
output_span_message = {
"role": output_message.role,
"content": output_message.content,
}

# Handle tool calls if present
if output_message.tool_calls:
tool_calls = []
for tool_call in output_message.tool_calls:
tool_calls.append({
"id": str(uuid4()),
"type": "function",
"function": {
"name": tool_call.function.name,
"arguments": json.dumps(tool_call.function.arguments),
}
})
output_span_message["tool_calls"] = tool_calls

# Append the output message
set_span_chat_messages(span, [output_span_message], append=True)

return response

return wrapper

这里的关键变化是:

  • 我们使用 tools = kwargs.get("tools", [])tools 参数中提取了可用工具的列表,将它们记录为输入,并使用 set_span_chat_tools 将它们捕获以包含在 Chat 面板中。
  • 我们为输出消息中的工具调用添加了特定处理,确保根据 ToolCall 规范对其进行格式化。

现在让我们用一个简单的提示计算工具来测试一下。工具的定义符合OpenAI 规范,用于工具调用。

chat = trace_ollama_chat(ollama_chat)

tools = [
{
"type": "function",
"function": {
"name": "calculate_tip",
"description": "Calculate the tip amount based on the bill amount and tip percentage",
"parameters": {
"type": "object",
"properties": {
"bill_amount": {
"type": "number",
"description": "The total bill amount"
},
"tip_percentage": {
"type": "number",
"description": "The percentage of the bill to be given as a tip, given as a whole number."
}
},
"required": ["bill_amount", "tip_percentage"]
}
}
}
]

response = chat(
model="llama3.2",
messages=[
{"role": "user", "content": "What is the tip for a $187.32 bill with a 22% tip?"}
],
tools=tools,
)

我们可以检查 MLflow UI 中的 Tracing,现在同时显示了可用的工具和工具调用的结果。

Tool Call Results

编排:构建一个工具调用循环

到目前为止,Ollama 示例在每次进行聊天补全时只生成一个 span。但是许多 GenAI 应用程序包含多个 LLM 调用、检索步骤、工具执行和其他自定义步骤。虽然我们不会在这里详细介绍如何为编排框架添加 Tracing,但我们将通过定义一个基于前面定义的工具的工具调用循环来阐明一些关键概念。

工具调用循环将遵循以下模式:

  1. 将用户提示作为输入
  2. 响应一个或多个工具调用
  3. 对于每个工具调用,执行工具并存储结果
  4. 使用 tool 角色将工具调用结果追加到消息历史记录中
  5. 使用工具调用结果再次调用 LLM,提示它为用户的提示提供最终答案。

以下是包含一个工具调用的实现。

class ToolExecutor:
def __init__(self):
self.tools = [
{
"type": "function",
"function": {
"name": "calculate_tip",
"description": "Calculate the tip amount based on the bill amount and tip percentage",
"parameters": {
"type": "object",
"properties": {
"bill_amount": {
"type": "number",
"description": "The total bill amount"
},
"tip_percentage": {
"type": "number",
"description": "The percentage of the bill to be given as a tip, represented as a whole number."
}
},
"required": ["bill_amount", "tip_percentage"]
}
}
}
]

# Map tool names to their Python implementations
self.tool_implementations = {
"calculate_tip": self._calculate_tip
}

def _calculate_tip(self, bill_amount: float, tip_percentage: float) -> float:
"""Calculate the tip amount based on the bill amount and tip percentage."""
bill_amount = float(bill_amount)
tip_percentage = float(tip_percentage)
return round(bill_amount * (tip_percentage / 100), 2)
def execute_tool_calling_loop(self, messages):
"""Execute a complete tool calling loop with tracing."""
with mlflow.start_span(
name="ToolCallingLoop",
span_type="CHAIN",
) as parent_span:
# Set initial inputs
parent_span.set_inputs({
"initial_messages": messages,
"available_tools": self.tools
})

# Set input messages
set_span_chat_messages(parent_span, messages)

# First LLM call (already traced by our chat method patch)
response = chat(
messages=messages,
model="llama3.2",
tools=self.tools,
)

messages.append(response.message)

tool_calls = response.message.tool_calls
tool_results = []

# Execute tool calls
for tool_call in tool_calls:
with mlflow.start_span(
name=f"ToolExecution_{tool_call.function.name}",
span_type="TOOL",
) as tool_span:
# Parse tool inputs
tool_inputs = tool_call.function.arguments
tool_span.set_inputs(tool_inputs)

# Execute tool
func = self.tool_implementations.get(tool_call.function.name)
if func is None:
raise ValueError(f"No implementation for tool: {tool_call.function.name}")

result = func(**tool_inputs)
tool_span.set_outputs({"result": result})

tool_results.append({
"tool_call_id": str(uuid4()),
"output": str(result)
})

messages.append({
"role": "tool",
"tool_call_id": str(uuid4()),
"content": str(result)
})

# Prepare messages for final response
messages.append({
"role": "user",
"content": "Answer the initial question based on the tool call results. Do not refer to the tool call results in your response. Just give a direct answer."
})

# Final LLM call (already traced by our chat method patch)
final_response = chat(
messages=messages,
model="llama3.2"
)

# Set the final output for the parent span
parent_span.set_outputs({
"final_response": final_response.message.content,
"tool_results": tool_results
})

print(final_response)

# set output messages
set_span_chat_messages(parent_span, [final_response.message.model_dump()], append=True)

return final_response

这是我们在此工具调用循环中处理 Tracing 的方式:

  1. 我们首先使用 mlflow.start_span 为工具调用循环设置一个父 span。我们将 span 名称设置为 "ToolCallingLoop",span 类型设置为 "CHAIN",表示一系列操作。
  2. 我们将初始消息和可用工具记录为 span 的输入。这有助于未来的调试,使我们能够验证工具是否可用并配置正确。
  3. 我们使用已修补的 chat 函数进行第一次 LLM 调用。此调用已由我们的装饰器 Traced,因此我们无需执行任何特殊操作即可 Tracing 它。
  4. 我们迭代工具调用,执行每个工具并存储结果。每次工具执行都通过一个新 span 进行 Traced,该 span 以工具函数名命名。输入和输出作为属性记录在 span 上。
  5. 我们使用 tool 角色将工具调用结果追加到消息历史记录中。这使得 LLM 可以在后续请求中看到工具调用的结果。它还允许我们在 MLflow UI 中看到工具调用的结果。
  6. 我们准备用于最终响应的消息,包括一个根据工具调用结果回答初始问题的提示。
  7. 我们使用已修补的 chat 函数进行最后一次 LLM 调用。同样,由于我们使用的是已修补的函数,因此此调用已被 Traced。
  8. 我们为父 span 设置最终输出,包括 LLM 的最终响应和工具结果。
  9. 最后,我们使用 set_span_chat_messages 将最终响应追加到 MLflow UI 的聊天历史记录中。请注意,为了保持简洁,我们只使用 set_span_chat_messages 将用户的初始查询和最终响应记录到父 span。我们可以点击嵌套 span 来查看工具调用的结果和其他详细信息。

这个过程创建了整个工具调用循环的完整 Tracing,从初始请求到工具执行和最终响应。

我们可以按如下方式执行此操作。但是,请注意,在完全了解 LLM 生成或调用的任意代码在您的系统上将做什么之前,您应运行它。

executor = ToolExecutor()
response = executor.execute_tool_calling_loop(
messages=[
{"role": "user", "content": "What is the tip for a $235.32 bill with a 22% tip?"}
]
)

结果将是以下 Tracing:

Tool Calling Loop

结论

本博文展示了如何将 MLflow Tracing 扩展到内置提供商支持之外。我们从一个简单的例子开始 — 为 Ollama Python SDK 的 chat 方法添加 Tracing — 并看到如何通过一个轻量级的修补,我们可以捕获每个聊天补全的详细信息。然后,我们在此基础上构建,Traced 了更复杂的工具执行循环。

关键要点是:

  • MLflow Tracing 是高度可定制的,可以适应没有自动日志记录支持的提供商。
  • 添加基本 Tracing 支持通常只需进行最少的代码更改。在本例中,我们修补了 Ollama Python SDK 的 chat 方法,并编写了几行代码来添加 Tracing 支持。
  • 用于简单 API 调用的相同原则可以扩展到具有多个步骤的复杂工作流。在本例中,我们 Traced 了包含多个步骤和工具调用的工具调用循环。