Reliable langfuse integration for tracing in realt...
# ask-ai
m
Reliable langfuse integration for tracing in realtime models? Currently the official example doesn’t show the user speech transcription
t
Thanks for reaching out, Stanislav. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to guide you through integrating Langfuse for tracing in real-time models, including user speech transcription. Just follow these steps: 1. First, set up your Langfuse credentials as environment variables:
Copy code
LANGFUSE_PUBLIC_KEY=your_public_key
LANGFUSE_SECRET_KEY=your_secret_key
LANGFUSE_HOST=https://your-langfuse-host.com
1. Next, configure Langfuse using OpenTelemetry with a custom setup function. This will help ensure proper tracing for your real-time models:
Copy code
import base64
import os
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from livekit.agents.telemetry import set_tracer_provider

def setup_langfuse(metadata=None, host=None, public_key=None, secret_key=None):
    public_key = public_key or os.getenv("LANGFUSE_PUBLIC_KEY")
    secret_key = secret_key or os.getenv("LANGFUSE_SECRET_KEY")
    host = host or os.getenv("LANGFUSE_HOST")

    if not public_key or not secret_key or not host:
        raise ValueError("LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, and LANGFUSE_HOST must be set")

    langfuse_auth = base64.b64encode(f"{public_key}:{secret_key}".encode()).decode()
    os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = f"{host.rstrip('/')}/api/public/otel"
    os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {langfuse_auth}"

    trace_provider = TracerProvider()
    trace_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
    set_tracer_provider(trace_provider, metadata=metadata)
    return trace_provider
1. Finally, for your real-time models, you can use OpenAI's Realtime API, which conveniently handles user speech transcription. Langfuse will automatically trace: • Speech input processing • Integrated transcription • LLM inference • Audio output • Function calls If you'd like, I can provide a complete implementation example or help you troubleshoot any part of this process. Just let me know—I'm here to help! Sources: LiveKit turn detector plugin | LiveKit docs | Realtime model plugins | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | complex-agents/note-taking-assistant/README.md
m
Give me complete example
t
I'd love to walk you through a complete example of integrating Langfuse for tracing in real-time models. Here it is:
Copy code
from livekit.agents import Agent, AgentSession, WorkerOptions, cli
from livekit.plugins import openai, silero

# Function to set up Langfuse tracing
def setup_langfuse():
    import base64
    import os
    from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import BatchSpanProcessor
    from livekit.agents.telemetry import set_tracer_provider

    public_key = os.getenv("LANGFUSE_PUBLIC_KEY")
    secret_key = os.getenv("LANGFUSE_SECRET_KEY")
    host = os.getenv("LANGFUSE_HOST")

    if not public_key or not secret_key or not host:
        raise ValueError("LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, and LANGFUSE_HOST must be set")

    langfuse_auth = base64.b64encode(f"{public_key}:{secret_key}".encode()).decode()
    os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = f"{host.rstrip('/')}/api/public/otel"
    os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {langfuse_auth}"

    trace_provider = TracerProvider()
    trace_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
    set_tracer_provider(trace_provider)

# Entrypoint for the agent
async def entrypoint(ctx):
    setup_langfuse()  # Initialize Langfuse tracing

    session = AgentSession(vad=silero.VAD.load())

    @session.on("metrics_collected")
    def _on_metrics_collected(ev):
        metrics.log_metrics(ev.metrics)

    # Use real-time model with integrated transcription
    agent = Agent(
        instructions="You are a helpful assistant.",
        llm=openai.realtime.RealtimeModel(voice="alloy")
    )

    await session.start(agent=agent, room=ctx.room)

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
This example shows you how to set up Langfuse tracing, configure a real-time model for transcription, and start an agent session smoothly. If anything feels unclear or if you'd like me to provide more examples, please don’t hesitate to ask—I’m here to help!