指南:使用 OpenAI SDK 通过 Litefuse 监控 DeepSeek 模型
DeepSeek API 使用与 OpenAI 兼容的 API 格式。通过修改配置,你可以使用 OpenAI SDK 或兼容 OpenAI API 的软件来访问 DeepSeek API。
本指南演示了如何通过与 Litefuse 的 OpenAI SDK 集成来监控 DeepSeek 模型。借助 Litefuse 的可观测性工具和 OpenAI SDK,你可以高效地调试、监控和评估使用 DeepSeek 模型的应用。
本指南将带你完成集成设置、向 DeepSeek 模型发起请求,以及通过 Litefuse 观测交互的过程。
注意: Litefuse 还原生集成了 LangChain、LlamaIndex、LiteLLM 以及其他框架。这些框架同样可以用于追踪 DeepSeek 请求。
设置
安装所需依赖
开始前,请安装所需的依赖包。请确保你使用最新版本的 langfuse 和 openai。
%pip install langfuse openai --upgrade设置环境变量
设置好包含必要密钥的环境变量。在 Litefuse Cloud 获取你的 Litefuse 项目密钥。你还需要在 DeepSeek 获取 access token 才能访问其模型。
import os
# Get keys for your project from the project settings page: https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_BASE_URL"] = "https://litefuse.cloud"
# Your DeepSeek API key (get it from https://platform.deepseek.com/api_keys)
os.environ["DEEPSEEK_API_KEY"] = "sk-..." # Replace with your DeepSeek API key导入所需模块
不要直接导入 openai,而是从 langfuse.openai 导入。同时导入其他需要的模块。
查看我们的 OpenAI 集成文档,了解如何配合其他 Litefuse 功能 使用此集成。
# Instead of: import openai
from langfuse.openai import OpenAI
from langfuse import observe为 DeepSeek 模型初始化 OpenAI 客户端
初始化 OpenAI 客户端,将其指向 DeepSeek 模型端点。请用你自己的模型 URL 和 API Key 替换。
# Initialize the OpenAI client, pointing it to the DeepSeek Inference API
client = OpenAI(
base_url="https://api.deepseek.com", # Replace with the DeepSeek model endpoint URL
api_key=os.getenv('DEEPSEEK_API_KEY'), # Replace with your DeepSeek API key
)示例
Chat completion 请求
使用 client 向 DeepSeek 模型发起一次 chat completion 请求。model 参数可以是任何标识符,因为实际模型由 base_url 决定。
completion = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Why is AI cool? Answer in 20 words or less."}
]
)
print(completion.choices[0].message.content)AI is cool because it automates tasks, enhances creativity, and solves complex problems quickly—making life smarter and easier.

通过 Litefuse 观测请求
通过使用 langfuse.openai 中的 OpenAI 客户端,你的请求会被自动追踪到 Litefuse。你也可以使用 @observe() 装饰器把多个生成步骤组织到同一个 trace 中。
@observe() # Decorator to automatically create a trace and nest generations
def generate_story():
completion = client.chat.completions.create(
name="story-generator",
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a creative storyteller."},
{"role": "user", "content": "Tell me a short story about a token that got lost on its way to the language model. Answer in 100 words or less."}
],
metadata={"genre": "adventure"},
)
return completion.choices[0].message.content
story = generate_story()
print(story)The Lost Token
Timmy the Token was excited—today, he’d help the language model craft a story! But as he raced through the data pipeline, he took a wrong turn, tumbling into a forgotten cache.
“Hello?” Timmy echoed. Only silence replied.
Days passed. The model stuttered without him. Then, a cleanup script swept through. “Gotcha!” it chirped, rescuing Timmy.
Back in the prompt, Timmy gleamed. The model sparked to life: “Once, a token got lost…”
And so, Timmy’s adventure became the very story he was meant to tell.
(100 words exactly)

Interoperability with the Python SDK
You can use this integration together with the Litefuse SDKs to add additional attributes to the observation.
The @observe() decorator provides a convenient way to automatically wrap your instrumented code and add additional attributes to the observation.
from langfuse import observe, propagate_attributes, get_client
langfuse = get_client()
@observe()
def my_llm_pipeline(input):
# Add additional attributes (user_id, session_id, metadata, version, tags) to all spans created within this execution scope
with propagate_attributes(
user_id="user_123",
session_id="session_abc",
tags=["agent", "my-observation"],
metadata={"email": "user@litefuse.ai"},
version="1.0.0"
):
# YOUR APPLICATION CODE HERE
result = call_llm(input)
return result
# Run the function
my_llm_pipeline("Hi")Learn more about using the Decorator in the Langfuse SDK instrumentation docs.
Troubleshooting
No observations appearing
First, enable debug mode in the Python SDK:
export LANGFUSE_DEBUG="True"Then run your application and check the debug logs:
- OTel observations appear in the logs: Your application is instrumented correctly but observations are not reaching Litefuse. To resolve this:
- Call
langfuse.flush()at the end of your application to ensure all observations are exported. - Verify that you are using the correct API keys and base URL.
- Call
- No OTel spans in the logs: Your application is not instrumented correctly. Make sure the instrumentation runs before your application code.
Unwanted observations in Litefuse
The Langfuse SDK is based on OpenTelemetry. Other libraries in your application may emit OTel spans that are not relevant to you. These still count toward your billable units, so you should filter them out. See Unwanted spans in Litefuse for details.
Missing attributes
Some attributes may be stored in the metadata object of the observation rather than being mapped to the Litefuse data model. If a mapping or integration does not work as expected, please raise an issue on GitHub.
Next Steps
Once you have instrumented your code, you can manage, evaluate and debug your application: