追踪 DeepSeek
MLflow Tracing 提供了自动跟踪能力,可以通过 OpenAI SDK 集成来跟踪DeepSeekmodels。由于DeepSeek提供了 OpenAI 兼容的 API 格式,您可以使用 mlflow.openai.autolog() 来跟踪与DeepSeekmodels 的交互。

MLflow trace 会自动捕获以下关于DeepSeekcalls 的信息
- 提示和完成响应
- 延迟
- Token 用量
- 模型名称
- 如指定的
temperature、max_completion_tokens等附加元数据。 - 如果响应中返回函数调用
- 内置工具,如网络搜索、文件搜索、计算机使用等。
- 如果抛出任何异常
快速入门
1
安装依赖项
- Python
- JS / TS
bash
pip install 'mlflow[genai]' openai
bash
npm install mlflow-openai openai
2
启动 MLflow 服务器
- 本地 (pip)
- 本地 (docker)
如果您有本地 Python 环境 >= 3.10,您可以使用 mlflow CLI 命令在本地启动 MLflow 服务器。
bash
mlflow server
MLflow 还提供了一个 Docker Compose 文件,用于启动带有 postgres 数据库和 minio 服务器的本地 MLflow 服务器。
bash
git clone --depth 1 --filter=blob:none --sparse https://github.com/mlflow/mlflow.git
cd mlflow
git sparse-checkout set docker-compose
cd docker-compose
cp .env.dev.example .env
docker compose up -d
有关更多详细信息,请参阅 说明,例如覆盖默认环境变量。
3
启用跟踪并调用 DeepSeek
- Python
- JS / TS
python
import openai
import mlflow
# Enable auto-tracing for OpenAI (works with DeepSeek)
mlflow.openai.autolog()
# Optional: Set a tracking URI and an experiment
mlflow.set_tracking_uri("https://:5000")
mlflow.set_experiment("DeepSeek")
# Initialize the OpenAI client with DeepSeek API endpoint
client = openai.OpenAI(
base_url="https://api.deepseek.com",
api_key="<your_deepseek_api_key>",
)
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"},
],
)
typescript
import { OpenAI } from "openai";
import { tracedOpenAI } from "mlflow-openai";
// Wrap the OpenAI client and point to DeepSeek endpoint
const client = tracedOpenAI(
new OpenAI({
baseURL: "https://api.deepseek.com",
apiKey: "<your_deepseek_api_key>",
})
);
const response = await client.chat.completions.create({
model: "deepseek-chat",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is the capital of France?" },
],
temperature: 0.1,
max_tokens: 100,
});
4
在 MLflow UI 中查看跟踪
浏览您的 MLflow UI(例如,https://:5000)并打开 DeepSeek 实验,以查看上述调用的跟踪记录。

-> 查看 后续步骤 以了解更多 MLflow 功能,例如用户反馈跟踪、Prompt 管理和评估。
流式传输和异步支持
MLflow 支持流式和异步DeepSeekAPI 的跟踪。访问 OpenAI Tracing 文档,其中包含通过 OpenAI SDK 跟踪流式和异步调用的示例代码片段。
与框架或手动跟踪结合使用
MLflow 中的自动跟踪功能旨在与 Manual Tracing SDK 或多框架集成无缝协同工作。下面的示例展示了 Python(手动 span)和 JS/TS(手动 span)在相同复杂级别下的应用。
- Python
- JS / TS
python
import json
from openai import OpenAI
import mlflow
from mlflow.entities import SpanType
# Initialize the OpenAI client with DeepSeek API endpoint
client = OpenAI(
base_url="https://api.deepseek.com",
api_key="<your_deepseek_api_key>",
)
# Create a parent span for the DeepSeek call
@mlflow.trace(span_type=SpanType.CHAIN)
def answer_question(question: str):
messages = [{"role": "user", "content": question}]
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
)
# Attach session/user metadata to the trace
mlflow.update_current_trace(
metadata={
"mlflow.trace.session": "session-12345",
"mlflow.trace.user": "user-a",
}
)
return response.choices[0].message.content
answer = answer_question("What is the capital of France?")
typescript
import * as mlflow from "mlflow-tracing";
import { OpenAI } from "openai";
import { tracedOpenAI } from "mlflow-openai";
mlflow.init({
trackingUri: "https://:5000",
experimentId: "<your_experiment_id>",
});
// Wrap the OpenAI client and point to DeepSeek endpoint
const client = tracedOpenAI(
new OpenAI({
baseURL: "https://api.deepseek.com",
apiKey: "<your_deepseek_api_key>",
})
);
// Create a traced function that wraps the DeepSeek call
const answerQuestion = mlflow.trace(
async (question: string) => {
const resp = await client.chat.completions.create({
model: "deepseek-chat",
messages: [{ role: "user", content: question }],
});
return resp.choices[0].message?.content;
},
{ name: "answer-question" }
);
await answerQuestion("What is the capital of France?");
运行任一示例都将生成一个包含DeepSeekLLM span 的 trace;被跟踪的函数会自动创建父 span。
