-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add metrics utilities & use dataclasses instead of TypedDict (#1009)
Co-authored-by: Théo Monnom <[email protected]> Co-authored-by: Théo Monnom <[email protected]>
- Loading branch information
1 parent
dbf9d1d
commit 5376f08
Showing
19 changed files
with
438 additions
and
349 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"livekit-agents": patch | ||
--- | ||
|
||
Reorganized metrics, added create_metrics_logger |
This file contains 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,80 @@ | ||
import logging | ||
|
||
from dotenv import load_dotenv | ||
from livekit.agents import ( | ||
AutoSubscribe, | ||
JobContext, | ||
JobProcess, | ||
WorkerOptions, | ||
cli, | ||
llm, | ||
metrics, | ||
) | ||
from livekit.agents.pipeline import VoicePipelineAgent | ||
from livekit.plugins import deepgram, openai, silero | ||
|
||
load_dotenv() | ||
logger = logging.getLogger("metrics-example") | ||
|
||
# This example logs pipeline metrics and computes cost of the session | ||
|
||
OPENAI_LLM_INPUT_PRICE = 2.50 / (10**6) # $2.50 per million tokens | ||
OPENAI_LLM_OUTPUT_PRICE = 10 / (10**6) # $10 per million tokens | ||
OPENAI_TTS_PRICE = 15 / (10**6) # $15 per million characters | ||
DEEPGRAM_STT_PRICE = 0.0043 # $0.0043 per minute | ||
|
||
|
||
def prewarm(proc: JobProcess): | ||
proc.userdata["vad"] = silero.VAD.load() | ||
|
||
|
||
async def entrypoint(ctx: JobContext): | ||
initial_ctx = llm.ChatContext().append( | ||
role="system", | ||
text=( | ||
"You are a voice assistant created by LiveKit. Your interface with users will be voice. " | ||
"You should use short and concise responses, and avoiding usage of unpronouncable punctuation." | ||
), | ||
) | ||
|
||
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) | ||
|
||
participant = await ctx.wait_for_participant() | ||
agent = VoicePipelineAgent( | ||
vad=ctx.proc.userdata["vad"], | ||
stt=deepgram.STT(), | ||
llm=openai.LLM(), | ||
tts=openai.TTS(), | ||
chat_ctx=initial_ctx, | ||
) | ||
|
||
usage_collector = metrics.UsageCollector() | ||
|
||
@agent.on("metrics_collected") | ||
def _on_metrics_collected(metrics: metrics.AgentMetrics): | ||
metrics.log_metrics(metrics) | ||
usage_collector.add_usage(metrics) | ||
|
||
async def log_session_cost(): | ||
summary = usage_collector.get_summary() | ||
llm_cost = ( | ||
summary.llm_prompt_tokens * OPENAI_LLM_INPUT_PRICE | ||
+ summary.llm_completion_tokens * OPENAI_LLM_OUTPUT_PRICE | ||
) | ||
tts_cost = summary.tts_characters_count * OPENAI_TTS_PRICE | ||
stt_cost = summary.stt_audio_duration * DEEPGRAM_STT_PRICE / 60 | ||
|
||
total_cost = llm_cost + tts_cost + stt_cost | ||
|
||
logger.info( | ||
f"Total cost: ${total_cost:.4f} (LLM: ${llm_cost:.4f}, TTS: ${tts_cost:.4f}, STT: ${stt_cost:.4f})" | ||
) | ||
|
||
ctx.add_shutdown_callback(log_session_cost) | ||
|
||
agent.start(ctx.room, participant) | ||
await agent.say("Hey, how can I help you today?", allow_interruptions=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm)) |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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 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,30 @@ | ||
from .base import ( | ||
AgentMetrics, | ||
LLMMetrics, | ||
PipelineEOUMetrics, | ||
PipelineLLMMetrics, | ||
PipelineSTTMetrics, | ||
PipelineTTSMetrics, | ||
PipelineVADMetrics, | ||
STTMetrics, | ||
TTSMetrics, | ||
VADMetrics, | ||
) | ||
from .usage_collector import UsageCollector, UsageSummary | ||
from .utils import log_metrics | ||
|
||
__all__ = [ | ||
"LLMMetrics", | ||
"AgentMetrics", | ||
"PipelineEOUMetrics", | ||
"PipelineSTTMetrics", | ||
"PipelineTTSMetrics", | ||
"PipelineVADMetrics", | ||
"PipelineLLMMetrics", | ||
"VADMetrics", | ||
"STTMetrics", | ||
"TTSMetrics", | ||
"UsageSummary", | ||
"UsageCollector", | ||
"log_metrics", | ||
] |
Oops, something went wrong.