-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat(metrics): broadcast llm tps + ttfs + agent_false_interruption #6373
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
Draft
Bobronium
wants to merge
1
commit into
main
Choose a base branch
from
arseny/additional-metrics
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.
Draft
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
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
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,89 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from livekit.agents.llm import ChatChunk, ChatContext, ChoiceDelta, CompletionUsage | ||
| from livekit.agents.llm.tool_context import ToolContext | ||
| from livekit.agents.tokenize import blingfire | ||
| from livekit.agents.utils import aio | ||
| from livekit.agents.voice.agent import ModelSettings | ||
| from livekit.agents.voice.generation import ( | ||
| _SENTENCE_TOKENIZER, | ||
| _llm_inference_task, | ||
| _LLMGenerationData, | ||
| ) | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
|
|
||
| def _fake_node(chunks: list[ChatChunk]): | ||
| # matches the io.LLMNode signature: (chat_ctx, tools, model_settings) -> AsyncIterable | ||
| async def node(chat_ctx, tools, model_settings): # type: ignore[no-untyped-def] | ||
| for chunk in chunks: | ||
| await asyncio.sleep(0) # yield so the ttfs consumer task can run | ||
| yield chunk | ||
|
|
||
| return node | ||
|
|
||
|
|
||
| async def _run_inference(chunks: list[ChatChunk]) -> _LLMGenerationData: | ||
| data = _LLMGenerationData(text_ch=aio.Chan(), function_ch=aio.Chan()) | ||
| await _llm_inference_task( | ||
| _fake_node(chunks), | ||
| ChatContext.empty(), | ||
| ToolContext.empty(), | ||
| ModelSettings(), | ||
| data, | ||
| ) | ||
| return data | ||
|
|
||
|
|
||
| def _content(text: str) -> ChatChunk: | ||
| return ChatChunk(id="c", delta=ChoiceDelta(content=text)) | ||
|
|
||
|
|
||
| def _usage_chunk(completion_tokens: int) -> ChatChunk: | ||
| return ChatChunk( | ||
| id="c", | ||
| usage=CompletionUsage( | ||
| completion_tokens=completion_tokens, | ||
| prompt_tokens=5, | ||
| total_tokens=completion_tokens + 5, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| class TestLLMNodeTps: | ||
| async def test_tps_set_when_usage_reported(self) -> None: | ||
| data = await _run_inference([_content("Hello there, friend."), _usage_chunk(30)]) | ||
| assert data.tps is not None | ||
| assert data.tps > 0 | ||
|
|
||
| async def test_tps_zero_when_zero_usage_is_reported(self) -> None: | ||
| data = await _run_inference([_content("Hello there, friend."), _usage_chunk(0)]) | ||
| assert data.tps == 0 | ||
|
|
||
| async def test_tps_none_when_no_usage_reported(self) -> None: | ||
| data = await _run_inference([_content("Hello there, friend.")]) # no usage chunk | ||
| assert data.tps is None | ||
|
|
||
|
|
||
| class TestLLMNodeTtfs: | ||
| async def test_ttfs_set_for_nonempty_generation(self) -> None: | ||
| data = await _run_inference([_content("First sentence. "), _content("Second one.")]) | ||
| assert data.ttfs is not None | ||
| assert data.ttfs > 0 | ||
|
|
||
| async def test_ttfs_none_for_whitespace_only(self) -> None: | ||
| data = await _run_inference([_content(" ")]) | ||
| assert data.ttfs is None | ||
|
|
||
|
|
||
| class TestSentenceTokenizerConfig: | ||
| def test_short_first_sentence_is_counted(self) -> None: | ||
| # regression: ttfs must see short openers. blingfire's TTS-oriented default | ||
| # (min_sentence_len=20) drops them, which silently inflated ttfs to the whole turn. | ||
| text = "Hi there. How can I help?" | ||
| assert len(_SENTENCE_TOKENIZER.tokenize(text)) == 2 | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tokenizer configuration may actually be different than the one being really used. We should try to use the real one