-
Notifications
You must be signed in to change notification settings - Fork 133
Add Noveum Trace example for LiveKit Agents #85
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
devin-codes
wants to merge
2
commits into
livekit-examples:main
Choose a base branch
from
devin-codes:docs/add-noveum-trace-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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,173 @@ | ||
| --- | ||
| title: Noveum Tracing | ||
| category: metrics | ||
| tags: [metrics, openai, deepgram, cartesia] | ||
| difficulty: intermediate | ||
| description: Shows how to use Noveum Trace, a community-maintained integration, to trace the agent session. | ||
| demonstrates: | ||
| - Using setup_livekit_tracing to trace an AgentSession with Noveum Trace. | ||
| - Wrapping STT/TTS/LLM providers for per-utterance audio and per-call LLM capture. | ||
| - Configuring record=False for privacy-sensitive deployments. | ||
| --- | ||
|
|
||
| This example shows how to trace a LiveKit agent session with | ||
| [Noveum Trace](https://github.com/Noveum/noveum-trace), a community-maintained | ||
| observability integration. It captures AgentSession events, STT/TTS/LLM data, tool | ||
| calls, conversation history, and (optionally) audio, and exports them to | ||
| [Noveum](https://noveum.ai). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Add a `.env` in this directory with your LiveKit, Noveum, and provider credentials: | ||
| ``` | ||
| LIVEKIT_URL=your_livekit_url | ||
| LIVEKIT_API_KEY=your_api_key | ||
| LIVEKIT_API_SECRET=your_api_secret | ||
| NOVEUM_API_KEY=your_noveum_api_key | ||
| DEEPGRAM_API_KEY=your_deepgram_api_key | ||
| OPENAI_API_KEY=your_openai_api_key | ||
| CARTESIA_API_KEY=your_cartesia_api_key | ||
| ``` | ||
| - Install dependencies: | ||
| ```bash | ||
| pip install "noveum-trace[livekit]" "livekit-agents[silero]" livekit-plugins-deepgram livekit-plugins-openai livekit-plugins-cartesia python-dotenv | ||
| ``` | ||
|
|
||
| ## Run it | ||
|
|
||
| ```bash | ||
| python noveum_tracing.py console | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| - `noveum_trace.init()` configures the Noveum project and API key. | ||
| - `LiveKitSTTWrapper`, `LiveKitTTSWrapper`, and `LiveKitLLMWrapper` wrap the | ||
| STT/TTS/LLM providers passed to `AgentSession`; the STT/TTS wrappers capture | ||
| per-utterance audio and transcripts, and the LLM wrapper captures full chat | ||
| context, response text, token usage, and timing per call. | ||
| - `setup_livekit_tracing(session, record=True, trace_name_prefix="livekit-example")` | ||
| attaches session-level tracing; every turn, session event, and tool call is | ||
| exported as a trace, and `record=True` uploads the full conversation audio at | ||
| session end. | ||
| - **Privacy note:** `record=True` captures full conversation audio, and the | ||
| STT/TTS wrappers capture per-utterance audio. Pass `record=False` and skip the | ||
| wrappers for privacy-sensitive deployments; text/transcript capture is likewise | ||
| configurable. | ||
|
|
||
| ## Compatibility | ||
|
|
||
| Requires Python 3.10+ and `livekit-agents >= 1.0`. Tested with released | ||
| `noveum-trace` 1.5.21. Noveum Trace is maintained by | ||
| [Noveum](https://github.com/Noveum) (community integration, not officially part of | ||
| LiveKit) — see the [integration docs](https://noveum.ai/en/docs/integration-examples/livekit/overview), | ||
| [GitHub repository](https://github.com/Noveum/noveum-trace), and | ||
| [PyPI package](https://pypi.org/project/noveum-trace/). | ||
|
|
||
| ## Full example | ||
|
|
||
| ```python | ||
| import logging | ||
| import os | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| import noveum_trace | ||
| from noveum_trace.integrations.livekit import ( | ||
| LiveKitLLMWrapper, | ||
| LiveKitSTTWrapper, | ||
| LiveKitTTSWrapper, | ||
| extract_job_context, | ||
| setup_livekit_tracing, | ||
| ) | ||
| from livekit.agents import JobContext, JobProcess, cli, Agent, AgentSession, AgentServer, RunContext, function_tool | ||
| from livekit.plugins import cartesia, deepgram, openai, silero | ||
|
|
||
| logger = logging.getLogger("noveum-trace-example") | ||
| load_dotenv() | ||
|
|
||
|
|
||
| def setup_noveum(project: str | None = None, api_key: str | None = None): | ||
| api_key = api_key or os.getenv("NOVEUM_API_KEY") | ||
| project = project or os.getenv("NOVEUM_PROJECT", "livekit-agent-example") | ||
|
|
||
| if not api_key: | ||
| logger.warning("NOVEUM_API_KEY must be set for tracing") | ||
| return | ||
|
|
||
| noveum_trace.init(project=project, api_key=api_key) | ||
|
|
||
|
|
||
| server = AgentServer() | ||
|
|
||
|
|
||
| def prewarm(proc: JobProcess): | ||
| proc.userdata["vad"] = silero.VAD.load() | ||
| setup_noveum() | ||
|
|
||
|
|
||
| server.setup_fnc = prewarm | ||
|
|
||
|
|
||
| @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 Kelly(Agent): | ||
| def __init__(self) -> None: | ||
| super().__init__( | ||
| instructions="Your name is Kelly.", | ||
| tools=[lookup_weather], | ||
| ) | ||
|
|
||
| async def on_enter(self): | ||
| logger.info("Kelly is entering the session") | ||
| self.session.generate_reply() | ||
|
|
||
|
|
||
| @server.rtc_session() | ||
| async def entrypoint(ctx: JobContext): | ||
| job_context = await extract_job_context(ctx) | ||
| session_id = ctx.job.id | ||
|
|
||
| traced_stt = LiveKitSTTWrapper( | ||
| stt=deepgram.STT(model="nova-3", language="en-US"), | ||
| session_id=session_id, | ||
| job_context=job_context, | ||
| ) | ||
| traced_llm = LiveKitLLMWrapper( | ||
| llm=openai.LLM(model="gpt-4.1-mini"), | ||
| session_id=session_id, | ||
| job_context=job_context, | ||
| ) | ||
| traced_tts = LiveKitTTSWrapper( | ||
| tts=cartesia.TTS(model="sonic-3", voice="9626c31c-bec5-4cca-baa8-f8ba9e84c8bc"), | ||
| session_id=session_id, | ||
| job_context=job_context, | ||
| ) | ||
|
|
||
| session = AgentSession( | ||
| vad=ctx.proc.userdata["vad"], | ||
| stt=traced_stt, | ||
| llm=traced_llm, | ||
| tts=traced_tts, | ||
| ) | ||
|
|
||
| setup_livekit_tracing(session, record=True, trace_name_prefix="livekit-example") | ||
|
|
||
| await session.start(agent=Kelly(), room=ctx.room) | ||
| await ctx.connect() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli.run_app(server) | ||
| ``` |
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,103 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| import noveum_trace | ||
| from noveum_trace.integrations.livekit import ( | ||
| LiveKitLLMWrapper, | ||
| LiveKitSTTWrapper, | ||
| LiveKitTTSWrapper, | ||
| extract_job_context, | ||
| setup_livekit_tracing, | ||
| ) | ||
| from livekit.agents import JobContext, JobProcess, cli, Agent, AgentSession, AgentServer, RunContext, function_tool | ||
| from livekit.plugins import cartesia, deepgram, openai, silero | ||
|
|
||
| logger = logging.getLogger("noveum-trace-example") | ||
| load_dotenv() | ||
|
|
||
|
|
||
| def setup_noveum(project: str | None = None, api_key: str | None = None): | ||
| api_key = api_key or os.getenv("NOVEUM_API_KEY") | ||
| project = project or os.getenv("NOVEUM_PROJECT", "livekit-agent-example") | ||
|
|
||
| if not api_key: | ||
| logger.warning("NOVEUM_API_KEY must be set for tracing") | ||
| return | ||
|
|
||
| noveum_trace.init(project=project, api_key=api_key) | ||
|
|
||
|
|
||
| server = AgentServer() | ||
|
|
||
|
|
||
| def prewarm(proc: JobProcess): | ||
| proc.userdata["vad"] = silero.VAD.load() | ||
| setup_noveum() | ||
|
|
||
|
|
||
| server.setup_fnc = prewarm | ||
|
|
||
|
|
||
| @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 Kelly(Agent): | ||
| def __init__(self) -> None: | ||
| super().__init__( | ||
| instructions="Your name is Kelly.", | ||
| tools=[lookup_weather], | ||
| ) | ||
|
|
||
| async def on_enter(self): | ||
| logger.info("Kelly is entering the session") | ||
| self.session.generate_reply() | ||
|
|
||
|
|
||
| @server.rtc_session() | ||
| async def entrypoint(ctx: JobContext): | ||
| job_context = await extract_job_context(ctx) | ||
| session_id = ctx.job.id | ||
|
|
||
| traced_stt = LiveKitSTTWrapper( | ||
| stt=deepgram.STT(model="nova-3", language="en-US"), | ||
| session_id=session_id, | ||
| job_context=job_context, | ||
| ) | ||
| traced_llm = LiveKitLLMWrapper( | ||
| llm=openai.LLM(model="gpt-4.1-mini"), | ||
| session_id=session_id, | ||
| job_context=job_context, | ||
| ) | ||
| traced_tts = LiveKitTTSWrapper( | ||
| tts=cartesia.TTS(model="sonic-3", voice="9626c31c-bec5-4cca-baa8-f8ba9e84c8bc"), | ||
| session_id=session_id, | ||
| job_context=job_context, | ||
| ) | ||
|
|
||
| session = AgentSession( | ||
| vad=ctx.proc.userdata["vad"], | ||
| stt=traced_stt, | ||
| llm=traced_llm, | ||
| tts=traced_tts, | ||
| ) | ||
|
|
||
| setup_livekit_tracing(session, record=True, trace_name_prefix="livekit-example") | ||
|
|
||
| await session.start(agent=Kelly(), room=ctx.room) | ||
| await ctx.connect() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli.run_app(server) | ||
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
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.