Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeizhou-ap committed Sep 18, 2024
1 parent 15e6449 commit 450a9dd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/exchange/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

_token_usage_collector: _TokenUsageCollector = _TokenUsageCollector()


def validate_tool_output(output: str) -> None:
"""Validate tool output for the given model"""
max_output_chars = 2**20
Expand Down
9 changes: 6 additions & 3 deletions src/exchange/token_usage_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

from exchange.providers.base import Usage


@dataclass
class TokenUsage:
model: str
input_tokens: int
output_tokens: int


class _TokenUsageCollector:
def __init__(self) -> None:
# use thread-safe queue to store usage data from multiple threads
Expand All @@ -29,6 +31,7 @@ def get_token_usage_group_by_model(self) -> List[TokenUsage]:
if output_tokens is not None:
token_count_group_by_model[model][1] += output_tokens
token_usage_list = [
TokenUsage(model, input_tokens, output_tokens)
for model, (input_tokens, output_tokens) in token_count_group_by_model.items()]
return token_usage_list
TokenUsage(model, input_tokens, output_tokens)
for model, (input_tokens, output_tokens) in token_count_group_by_model.items()
]
return token_usage_list
5 changes: 2 additions & 3 deletions tests/test_exchange_collect_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

MODEL_NAME = "test-model"


def create_exchange(mock_provider, dummy_tool):
return Exchange(
provider=mock_provider,
Expand All @@ -26,9 +27,7 @@ def test_exchange_generate_collect_usage(usage_factory, dummy_tool, monkeypatch)
mock_provider.complete.return_value = (Message.assistant("msg"), usage)
exchange = create_exchange(mock_provider, dummy_tool)

monkeypatch.setattr('exchange.exchange._token_usage_collector', mock_usage_collector)
monkeypatch.setattr("exchange.exchange._token_usage_collector", mock_usage_collector)
exchange.generate()

mock_usage_collector.collect.assert_called_once_with(MODEL_NAME, usage)


1 change: 1 addition & 0 deletions tests/test_token_usage_collector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from exchange.token_usage_collector import _TokenUsageCollector, TokenUsage


def test_collect(usage_factory):
usage_collector = _TokenUsageCollector()
usage_collector.collect("model1", usage_factory(input_tokens=100, output_tokens=1000))
Expand Down

0 comments on commit 450a9dd

Please sign in to comment.