-
Notifications
You must be signed in to change notification settings - Fork 3.5k
docs: Add MLflow tracing example for voice agents #4972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
B-Step62
wants to merge
1
commit into
livekit:main
Choose a base branch
from
B-Step62:add-mlflow-tracing-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| from dotenv import load_dotenv | ||
| from opentelemetry.sdk.resources import SERVICE_NAME, Resource | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
|
|
||
| from livekit.agents import ( | ||
| Agent, | ||
| AgentServer, | ||
| AgentSession, | ||
| JobContext, | ||
| RunContext, | ||
| cli, | ||
| inference, | ||
| metrics, | ||
| ) | ||
| from livekit.agents.llm import function_tool | ||
| from livekit.agents.telemetry import set_tracer_provider | ||
| from livekit.agents.voice import MetricsCollectedEvent | ||
| from livekit.plugins import silero | ||
|
|
||
| logger = logging.getLogger("mlflow-trace-example") | ||
|
|
||
| load_dotenv() | ||
|
|
||
| # This example shows how to use MLflow to trace the agent session. | ||
| # MLflow accepts OpenTelemetry traces via its OTLP endpoint. | ||
| # | ||
| # Prerequisites: | ||
| # pip install mlflow | ||
| # mlflow server --port 5000 | ||
| # | ||
| # Environment variables: | ||
| # OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:5000 | ||
| # OTEL_EXPORTER_OTLP_HEADERS=x-mlflow-experiment-id=0 | ||
| # | ||
| # Learn more: https://mlflow.org/docs/latest/genai/tracing/integrations/listing/livekit | ||
|
|
||
|
|
||
| def setup_mlflow( | ||
| *, | ||
| endpoint: str | None = None, | ||
| experiment_id: str | None = None, | ||
| service_name: str | None = None, | ||
| ) -> TracerProvider: | ||
| """Configure OpenTelemetry to send traces to MLflow. | ||
|
|
||
| Args: | ||
| endpoint: MLflow tracking server URL (default: OTEL_EXPORTER_OTLP_ENDPOINT or http://localhost:5000) | ||
| experiment_id: MLflow experiment ID (default: OTEL_EXPORTER_OTLP_HEADERS or "0") | ||
| service_name: Service name for spans (default: OTEL_SERVICE_NAME or "livekit-agent") | ||
| """ | ||
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
|
||
| endpoint = endpoint or os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:5000") | ||
| experiment_id = experiment_id or "0" | ||
| service_name = service_name or os.getenv("OTEL_SERVICE_NAME", "livekit-agent") | ||
|
|
||
| os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = endpoint | ||
| os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = os.getenv( | ||
| "OTEL_EXPORTER_OTLP_HEADERS", f"x-mlflow-experiment-id={experiment_id}" | ||
| ) | ||
|
|
||
| resource = Resource.create({SERVICE_NAME: service_name}) | ||
| trace_provider = TracerProvider(resource=resource) | ||
| trace_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) | ||
| set_tracer_provider(trace_provider) | ||
| return trace_provider | ||
|
|
||
|
|
||
| @function_tool | ||
| async def lookup_weather(context: RunContext, location: str) -> str: | ||
| """Called when the user asks for weather related information. | ||
|
|
||
| Args: | ||
| location: The location they are asking for | ||
| """ | ||
| logger.info(f"Looking up weather for {location}") | ||
| return "sunny with a temperature of 70 degrees." | ||
|
|
||
|
|
||
| class MyAgent(Agent): | ||
| def __init__(self) -> None: | ||
| super().__init__( | ||
| instructions="You are a helpful voice assistant. Keep your responses concise and conversational.", | ||
| llm=inference.LLM("openai/gpt-4.1-mini"), | ||
| stt=inference.STT("deepgram/nova-3"), | ||
| tts=inference.TTS("cartesia/sonic-3"), | ||
| tools=[lookup_weather], | ||
| ) | ||
|
|
||
| async def on_enter(self): | ||
| self.session.generate_reply() | ||
|
|
||
|
|
||
| server = AgentServer() | ||
|
|
||
|
|
||
| @server.rtc_session() | ||
| async def entrypoint(ctx: JobContext): | ||
| trace_provider = setup_mlflow() | ||
|
|
||
| async def flush_trace(): | ||
| trace_provider.force_flush() | ||
|
|
||
| ctx.add_shutdown_callback(flush_trace) | ||
|
|
||
| session = AgentSession(vad=silero.VAD.load()) | ||
|
|
||
| @session.on("metrics_collected") | ||
| def _on_metrics_collected(ev: MetricsCollectedEvent): | ||
| metrics.log_metrics(ev.metrics) | ||
|
|
||
| await session.start(agent=MyAgent(), room=ctx.room) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli.run_app(server) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.