Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ async def reset(self) -> None:


class TokenUsageTerminationConfig(BaseModel):
max_total_token: int | None
max_prompt_token: int | None
max_completion_token: int | None
max_total_token: int | None = None
max_prompt_token: int | None = None
max_completion_token: int | None = None


class TokenUsageTermination(TerminationCondition, Component[TokenUsageTerminationConfig]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ async def test_token_usage_termination() -> None:
)


@pytest.mark.asyncio
async def test_token_usage_termination_component_roundtrip() -> None:
"""A TokenUsageTermination with only some limits set must survive a dump/load roundtrip."""
termination = TokenUsageTermination(max_total_token=10)
config = termination.dump_component()
loaded = TokenUsageTermination.load_component(config)
assert isinstance(loaded, TokenUsageTermination)
# The loaded condition is not terminated and keeps the original limits.
assert not loaded.terminated
assert await loaded([]) is None
stop = await loaded(
[TextMessage(content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=10, completion_tokens=10))]
)
assert stop is not None
assert loaded.terminated


@pytest.mark.asyncio
async def test_and_termination() -> None:
termination = MaxMessageTermination(2) & TextMentionTermination("stop")
Expand Down