Integration Huggingface Openai Sdk
Cookbook: Monitor 🤗 Hugging Face Models with 🪢 Langfuse
This cookbook shows you how to monitor Hugging Face models using the OpenAI SDK integration with Langfuse. This allows you to collaboratively debug, monitor and evaluate your LLM applications.
With this integration, you can test and evaluate different models, monitor your application's cost and assign scores such as user feedback or human annotations.
Note: In this example, we use the OpenAI SDK to access the Hugging Face inference APIs. You can also use other frameworks, such as Langchain, or ingest the data via our API.
Setup
Install Required Packages
Set Environment Variables
Set up your environment variables with the necessary keys. Get keys for your Langfuse project from Langfuse Cloud. Also, obtain an access token from Hugging Face.
Import Necessary Modules
Instead of importing openai directly, import it from langfuse.openai. Also, import any other necessary modules.
Initialize the OpenAI Client for Hugging Face Models
Initialize the OpenAI client but point it to the Hugging Face model endpoint. You can use any model hosted on Hugging Face that supports the OpenAI API format. Replace the model URL and access token with your own.
For this example, we use the Meta-Llama-3-8B-Instruct model.
Examples
Chat Completion Request
Use the client to make a chat completion request to the Hugging Face model. The model parameter can be any identifier since the actual model is specified in the base_url. In this example, we set the model variable tgi, short for Text Generation Inference.
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.

