|
| 1 | +import importlib |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from art import TrainableModel |
| 7 | +from art.costs import build_cost_calculator, get_model_pricing |
| 8 | + |
| 9 | + |
| 10 | +class _FakeUsage: |
| 11 | + def __init__(self, prompt_tokens: int, completion_tokens: int) -> None: |
| 12 | + self.prompt_tokens = prompt_tokens |
| 13 | + self.completion_tokens = completion_tokens |
| 14 | + |
| 15 | + |
| 16 | +class _FakeResponse: |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + prompt_tokens: int, |
| 20 | + completion_tokens: int, |
| 21 | + *, |
| 22 | + num_choices: int = 1, |
| 23 | + ) -> None: |
| 24 | + self.usage = _FakeUsage(prompt_tokens, completion_tokens) |
| 25 | + self.choices = [object() for _ in range(num_choices)] |
| 26 | + |
| 27 | + |
| 28 | +class _FakeCompletions: |
| 29 | + def __init__(self, response: _FakeResponse) -> None: |
| 30 | + self._response = response |
| 31 | + |
| 32 | + async def create(self, *args: Any, **kwargs: Any) -> _FakeResponse: |
| 33 | + return self._response |
| 34 | + |
| 35 | + |
| 36 | +def _patch_async_openai( |
| 37 | + monkeypatch: pytest.MonkeyPatch, response: _FakeResponse |
| 38 | +) -> None: |
| 39 | + model_module = importlib.import_module("art.model") |
| 40 | + |
| 41 | + class _FakeAsyncOpenAI: |
| 42 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 43 | + self.chat = type( |
| 44 | + "FakeChat", |
| 45 | + (), |
| 46 | + {"completions": _FakeCompletions(response)}, |
| 47 | + )() |
| 48 | + |
| 49 | + def with_options(self, *args: Any, **kwargs: Any) -> "_FakeAsyncOpenAI": |
| 50 | + return self |
| 51 | + |
| 52 | + monkeypatch.setattr(model_module, "AsyncOpenAI", _FakeAsyncOpenAI) |
| 53 | + |
| 54 | + |
| 55 | +def _build_model() -> TrainableModel: |
| 56 | + pricing = get_model_pricing("openai/gpt-oss-20b") |
| 57 | + assert pricing is not None |
| 58 | + |
| 59 | + model = TrainableModel( |
| 60 | + name="test-run", |
| 61 | + project="test-project", |
| 62 | + base_model="openai/gpt-oss-20b", |
| 63 | + ) |
| 64 | + model.inference_api_key = "test-key" |
| 65 | + model.inference_base_url = "http://example.test/v1" |
| 66 | + model.set_cost_calculator(build_cost_calculator(pricing)) |
| 67 | + return model |
| 68 | + |
| 69 | + |
| 70 | +class TestModelOpenAIClientCosts: |
| 71 | + @pytest.mark.asyncio |
| 72 | + async def test_openai_client_automatically_logs_train_tinker_costs( |
| 73 | + self, |
| 74 | + monkeypatch: pytest.MonkeyPatch, |
| 75 | + ) -> None: |
| 76 | + _patch_async_openai(monkeypatch, _FakeResponse(1_000, 2_000)) |
| 77 | + model = _build_model() |
| 78 | + builder = model.metrics_builder("train") |
| 79 | + |
| 80 | + with builder.activate_context(): |
| 81 | + await model.openai_client().chat.completions.create( |
| 82 | + model=model.get_inference_name(), |
| 83 | + messages=[{"role": "user", "content": "hello"}], |
| 84 | + ) |
| 85 | + |
| 86 | + metrics = await builder.flush() |
| 87 | + assert metrics["costs/train/tinker_prefill"] == pytest.approx(0.00012) |
| 88 | + assert metrics["costs/train/tinker_sample"] == pytest.approx(0.0006) |
| 89 | + assert metrics["costs/train"] == pytest.approx(0.00072) |
| 90 | + |
| 91 | + @pytest.mark.asyncio |
| 92 | + async def test_openai_client_automatically_logs_eval_tinker_costs( |
| 93 | + self, |
| 94 | + monkeypatch: pytest.MonkeyPatch, |
| 95 | + ) -> None: |
| 96 | + _patch_async_openai(monkeypatch, _FakeResponse(500, 250)) |
| 97 | + model = _build_model() |
| 98 | + builder = model.metrics_builder("eval") |
| 99 | + |
| 100 | + with builder.activate_context(): |
| 101 | + await model.openai_client().chat.completions.create( |
| 102 | + model=model.get_inference_name(), |
| 103 | + messages=[{"role": "user", "content": "hello"}], |
| 104 | + ) |
| 105 | + |
| 106 | + metrics = await builder.flush() |
| 107 | + assert metrics["costs/eval/tinker_prefill"] == pytest.approx(0.00006) |
| 108 | + assert metrics["costs/eval/tinker_sample"] == pytest.approx(0.000075) |
| 109 | + assert metrics["costs/eval"] == pytest.approx(0.000135) |
| 110 | + |
| 111 | + @pytest.mark.asyncio |
| 112 | + async def test_openai_client_does_not_log_costs_without_active_metrics_context( |
| 113 | + self, |
| 114 | + monkeypatch: pytest.MonkeyPatch, |
| 115 | + ) -> None: |
| 116 | + _patch_async_openai(monkeypatch, _FakeResponse(1_000, 2_000)) |
| 117 | + model = _build_model() |
| 118 | + builder = model.metrics_builder("train") |
| 119 | + |
| 120 | + await model.openai_client().chat.completions.create( |
| 121 | + model=model.get_inference_name(), |
| 122 | + messages=[{"role": "user", "content": "hello"}], |
| 123 | + ) |
| 124 | + |
| 125 | + metrics = await builder.flush() |
| 126 | + assert metrics == {} |
| 127 | + |
| 128 | + @pytest.mark.asyncio |
| 129 | + async def test_multiple_choices_scale_prefill_cost_once_per_sample( |
| 130 | + self, |
| 131 | + monkeypatch: pytest.MonkeyPatch, |
| 132 | + ) -> None: |
| 133 | + _patch_async_openai(monkeypatch, _FakeResponse(1_000, 2_000, num_choices=3)) |
| 134 | + model = _build_model() |
| 135 | + builder = model.metrics_builder("train") |
| 136 | + |
| 137 | + with builder.activate_context(): |
| 138 | + await model.openai_client().chat.completions.create( |
| 139 | + model=model.get_inference_name(), |
| 140 | + messages=[{"role": "user", "content": "hello"}], |
| 141 | + n=3, |
| 142 | + ) |
| 143 | + |
| 144 | + metrics = await builder.flush() |
| 145 | + assert metrics["costs/train/tinker_prefill"] == pytest.approx(0.00036) |
| 146 | + assert metrics["costs/train/tinker_sample"] == pytest.approx(0.0006) |
| 147 | + |
| 148 | + def test_manual_cost_calculator_still_returns_tinker_metrics(self) -> None: |
| 149 | + model = _build_model() |
| 150 | + |
| 151 | + metrics = model.cost_calculator(1_000, 2_000, "train") |
| 152 | + |
| 153 | + assert metrics["costs/train/tinker_prefill"] == pytest.approx(0.00012) |
| 154 | + assert metrics["costs/train/tinker_sample"] == pytest.approx(0.0006) |
0 commit comments