跳到主要内容

MLflow OpenAI 自动日志记录

MLflow 的 OpenAI flavor 支持自动日志记录,可确保您的想法的实验、测试和验证能够被动态捕获,而无需用日志记录样板代码包装您的代码。

注意

自动日志记录仅支持版本为 1.17 及更高版本的 OpenAI SDK。

MLflow 对 OpenAI SDK 的自动日志记录支持以下接口

  • 通过 client.chat.completions.create() 进行聊天补全
  • 通过 client.completions.create() 进行补全(旧版)
  • 通过 client.embeddings.create() 进行嵌入

其中 clientopenai.OpenAI() 的实例。

在本指南中,我们将讨论自动日志记录功能中的一些关键特性。

快速入门

要开始使用 MLflow 的 OpenAI 自动日志记录,您只需在脚本或笔记本的开头调用 mlflow.openai.autolog()。在不带任何参数重写的情况下启用自动日志记录将默认启用跟踪。

提示

当激活自动日志记录时,默认启用的唯一元素是跟踪信息的记录。您可以在此处阅读有关 MLflow 跟踪的更多信息。

import os
import openai
import mlflow

# Enables trace logging by default
mlflow.openai.autolog()

openai_client = openai.OpenAI()

messages = [
{
"role": "user",
"content": "What does turning something up to 11 refer to?",
}
]

# The input messages and the response will be logged as a trace to the active experiment
answer = openai_client.chat.completions.create(
model="gpt-4o",
messages=messages,
temperature=0.99,
)
注意

使用 OpenAI SDK 时,请确保您的访问令牌已分配给环境变量 OPENAI_API_KEY

使用 OpenAI 自动日志记录的示例

import os

import mlflow
import openai

API_KEY = os.environ.get("OPENAI_API_KEY")
EXPERIMENT_NAME = "OpenAI Autologging Demonstration"

# set an active model for linking traces
mlflow.set_active_model(name="OpenAI_Model")

mlflow.openai.autolog()

mlflow.set_experiment(EXPERIMENT_NAME)

openai_client = openai.OpenAI(api_key=API_KEY)

messages = [
{
"role": "user",
"content": "State that you are responding to a test and that you are alive.",
}
]

openai_client.chat.completions.create(
model="gpt-4o",
messages=messages,
temperature=0.95,
)

在 UI 中查看日志记录的模型以及调用 OpenAI 客户端时使用的跟踪信息,如下面的图像所示

OpenAI Autologging artifacts and traces

常见问题

自动日志记录支持异步 API 吗?

MLflow OpenAI 自动日志记录功能不支持异步 API 来记录模型或跟踪。

最好通过使用来自代码的模型功能来保存您的异步实现。

如果您想为异步 OpenAI API 记录跟踪事件,下面是一个记录流式异步请求跟踪的简化示例

import openai
import mlflow
import asyncio

# Activate an experiment for logging traces to
mlflow.set_experiment("OpenAI")


async def fetch_openai_response(messages, model="gpt-4o", temperature=0.99):
"""
Asynchronously gets a response from the OpenAI API using the provided messages and streams the response.

Args:
messages (list): List of message dictionaries for the OpenAI API.
model (str): The model to use for the OpenAI API. Default is "gpt-4o".
temperature (float): The temperature to use for the OpenAI API. Default is 0.99.

Returns:
None
"""
client = openai.AsyncOpenAI()

# Create the response stream
response_stream = await client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
stream=True,
)

# Manually log traces using the tracing fluent API
with mlflow.start_span() as trace:
trace.set_inputs(messages)
full_response = []

async for chunk in response_stream:
content = chunk.choices[0].delta.content
if content is not None:
print(content, end="")
full_response.append(content)

trace.set_outputs("".join(full_response))


messages = [
{
"role": "user",
"content": "How much additional hydrogen mass would Jupiter require to ignite a sustainable fusion cycle?",
}
]

await fetch_openai_response(messages)