Integration Deepseek Openai Sdk
Cookbook: Monitor DeepSeek Models with Langfuse Using the OpenAI SDK
The DeepSeek API uses an API format compatible with OpenAI. By modifying the configuration, you can use the OpenAI SDK or software compatible with the OpenAI API to access the DeepSeek API.
This cookbook demonstrates how to monitor DeepSeek models using the OpenAI SDK integration with Langfuse. By leveraging Langfuse's observability tools and the OpenAI SDK, you can effectively debug, monitor, and evaluate your applications that utilize DeepSeek models.
This guide will walk you through setting up the integration, making requests to DeepSeek models, and observing the interactions with Langfuse.
Note: Langfuse is also natively integrated with LangChain, LlamaIndex, LiteLLM, and other frameworks. These frameworks can be used as well to trace DeepSeek requests.
Setup
Install Required Packages
To get started, install the necessary packages. Ensure you have the latest versions of langfuse and openai.
Set Environment Variables
Set up your environment variables with the necessary keys. Obtain your Langfuse project keys from Langfuse Cloud. You will also need an access token from DeepSeek to access their models.
Import Necessary Modules
Instead of importing openai directly, import it from langfuse.openai. Also, import any other necessary modules.
Check out our OpenAI integration docs to learn how to use this integration with other Langfuse features.
Initialize the OpenAI Client for DeepSeek Models
Initialize the OpenAI client, pointing it to the DeepSeek model endpoint. Replace the model URL and APP key with your own.
Examples
Chat Completion Request
Use the client to make a chat completion request to the DeepSeek model. The model parameter can be any identifier since the actual model is specified in the base_url.
AI is cool because it automates tasks, enhances creativity, and solves complex problems quickly—making life smarter and easier.
Observe the Request with Langfuse
By using the OpenAI client from langfuse.openai, your requests are automatically traced in Langfuse. You can also use the @observe() decorator to group multiple generations into a single trace.
**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)

