|
| 1 | +import pytest |
| 2 | +from openai.types import CompletionUsage |
| 3 | + |
| 4 | +from eval_protocol.models import EvaluationRow, ExecutionMetadata, InputMetadata, CostMetrics, Message |
| 5 | +from eval_protocol.pytest.utils import add_cost_metrics |
| 6 | + |
| 7 | + |
| 8 | +class TestExecutionMetadata: |
| 9 | + """Test execution metadata tracking including cost metrics, usage statistics, and timing.""" |
| 10 | + |
| 11 | + def test_single_model_with_provider(self): |
| 12 | + """Test normal case: single model string with provider.""" |
| 13 | + row = EvaluationRow( |
| 14 | + messages=[], |
| 15 | + input_metadata=InputMetadata( |
| 16 | + completion_params={"model": "accounts/fireworks/models/gpt-oss-120b", "provider": "fireworks"} |
| 17 | + ), |
| 18 | + execution_metadata=ExecutionMetadata( |
| 19 | + usage=CompletionUsage(prompt_tokens=100, completion_tokens=50, total_tokens=150) |
| 20 | + ), |
| 21 | + ) |
| 22 | + |
| 23 | + add_cost_metrics(row) |
| 24 | + |
| 25 | + assert row.execution_metadata.cost_metrics is not None |
| 26 | + assert row.execution_metadata.cost_metrics.input_cost_usd is not None |
| 27 | + assert row.execution_metadata.cost_metrics.output_cost_usd is not None |
| 28 | + assert row.execution_metadata.cost_metrics.total_cost_usd is not None |
| 29 | + |
| 30 | + @pytest.mark.skip(reason="Revisit when we figure out how to get cost metrics for multi-agent Pydantic.") |
| 31 | + def test_pydantic_ai_multi_agent_model_dict(self): |
| 32 | + """Test Pydantic AI multi-agent case: nested dictionary with multiple models.""" |
| 33 | + row = EvaluationRow( |
| 34 | + messages=[], |
| 35 | + input_metadata=InputMetadata( |
| 36 | + completion_params={ |
| 37 | + "model": { |
| 38 | + "joke_generation_model": { |
| 39 | + "model": "accounts/fireworks/models/kimi-k2-instruct", |
| 40 | + "provider": "fireworks", |
| 41 | + }, |
| 42 | + "joke_selection_model": { |
| 43 | + "model": "accounts/fireworks/models/deepseek-v3p1", |
| 44 | + "provider": "fireworks", |
| 45 | + }, |
| 46 | + } |
| 47 | + } |
| 48 | + ), |
| 49 | + execution_metadata=ExecutionMetadata( |
| 50 | + usage=CompletionUsage(prompt_tokens=200, completion_tokens=75, total_tokens=275) |
| 51 | + ), |
| 52 | + ) |
| 53 | + |
| 54 | + add_cost_metrics(row) |
| 55 | + |
| 56 | + assert row.execution_metadata.cost_metrics is not None |
| 57 | + assert row.execution_metadata.cost_metrics.input_cost_usd is not None |
| 58 | + assert row.execution_metadata.cost_metrics.output_cost_usd is not None |
| 59 | + assert row.execution_metadata.cost_metrics.total_cost_usd is not None |
| 60 | + |
| 61 | + def test_no_usage_stats(self): |
| 62 | + """Test case with no usage statistics.""" |
| 63 | + row = EvaluationRow( |
| 64 | + messages=[], |
| 65 | + input_metadata=InputMetadata(completion_params={"model": "gpt-3.5-turbo", "provider": "openai"}), |
| 66 | + execution_metadata=ExecutionMetadata(usage=None), |
| 67 | + ) |
| 68 | + |
| 69 | + add_cost_metrics(row) |
| 70 | + |
| 71 | + assert row.execution_metadata.cost_metrics is not None |
| 72 | + assert row.execution_metadata.cost_metrics.input_cost_usd == 0.0 |
| 73 | + assert row.execution_metadata.cost_metrics.output_cost_usd == 0.0 |
| 74 | + assert row.execution_metadata.cost_metrics.total_cost_usd == 0.0 |
| 75 | + |
| 76 | + def test_no_completion_params(self): |
| 77 | + """Test case with empty completion parameters.""" |
| 78 | + row = EvaluationRow( |
| 79 | + messages=[], |
| 80 | + input_metadata=InputMetadata(completion_params={}), |
| 81 | + execution_metadata=ExecutionMetadata( |
| 82 | + usage=CompletionUsage(prompt_tokens=100, completion_tokens=50, total_tokens=150) |
| 83 | + ), |
| 84 | + ) |
| 85 | + |
| 86 | + add_cost_metrics(row) |
| 87 | + |
| 88 | + assert row.execution_metadata.cost_metrics is not None |
| 89 | + assert row.execution_metadata.cost_metrics.input_cost_usd == 0.0 |
| 90 | + assert row.execution_metadata.cost_metrics.output_cost_usd == 0.0 |
| 91 | + assert row.execution_metadata.cost_metrics.total_cost_usd == 0.0 |
| 92 | + |
| 93 | + def test_zero_tokens(self): |
| 94 | + """Test case with zero token usage.""" |
| 95 | + row = EvaluationRow( |
| 96 | + messages=[], |
| 97 | + input_metadata=InputMetadata(completion_params={"model": "gpt-3.5-turbo", "provider": "openai"}), |
| 98 | + execution_metadata=ExecutionMetadata( |
| 99 | + usage=CompletionUsage(prompt_tokens=0, completion_tokens=0, total_tokens=0) |
| 100 | + ), |
| 101 | + ) |
| 102 | + |
| 103 | + add_cost_metrics(row) |
| 104 | + |
| 105 | + assert row.execution_metadata.cost_metrics is not None |
| 106 | + assert row.execution_metadata.cost_metrics.input_cost_usd == 0.0 |
| 107 | + assert row.execution_metadata.cost_metrics.output_cost_usd == 0.0 |
| 108 | + assert row.execution_metadata.cost_metrics.total_cost_usd == 0.0 |
| 109 | + |
| 110 | + def test_provider_mapping_variations(self): |
| 111 | + """Test different provider mappings.""" |
| 112 | + providers_and_expected = [ |
| 113 | + ("openai", "gpt-3.5-turbo", "gpt-3.5-turbo"), # No prefix - known model |
| 114 | + ( |
| 115 | + "fireworks", |
| 116 | + "accounts/fireworks/models/llama-v2-7b-chat", |
| 117 | + "fireworks_ai/accounts/fireworks/models/llama-v2-7b-chat", |
| 118 | + ), |
| 119 | + ("unknown_provider", "gpt-3.5-turbo", "gpt-3.5-turbo"), # Fallback to original - use known model |
| 120 | + ] |
| 121 | + |
| 122 | + for provider, model, expected_model_id in providers_and_expected: |
| 123 | + row = EvaluationRow( |
| 124 | + messages=[], |
| 125 | + input_metadata=InputMetadata(completion_params={"model": model, "provider": provider}), |
| 126 | + execution_metadata=ExecutionMetadata( |
| 127 | + usage=CompletionUsage(prompt_tokens=10, completion_tokens=5, total_tokens=15) |
| 128 | + ), |
| 129 | + ) |
| 130 | + |
| 131 | + add_cost_metrics(row) |
| 132 | + |
| 133 | + # Should not raise an error and should set cost metrics |
| 134 | + assert row.execution_metadata.cost_metrics is not None |
| 135 | + |
| 136 | + def test_model_without_provider(self): |
| 137 | + """Test model string without provider field.""" |
| 138 | + row = EvaluationRow( |
| 139 | + messages=[], |
| 140 | + input_metadata=InputMetadata( |
| 141 | + completion_params={"model": "gpt-3.5-turbo"} # No provider field |
| 142 | + ), |
| 143 | + execution_metadata=ExecutionMetadata( |
| 144 | + usage=CompletionUsage(prompt_tokens=50, completion_tokens=25, total_tokens=75) |
| 145 | + ), |
| 146 | + ) |
| 147 | + |
| 148 | + add_cost_metrics(row) |
| 149 | + |
| 150 | + assert row.execution_metadata.cost_metrics is not None |
| 151 | + # Should still work for OpenAI models even without explicit provider |
| 152 | + |
| 153 | + def test_execution_metadata_timing_field(self): |
| 154 | + """Test that the new duration_seconds field works correctly.""" |
| 155 | + metadata = ExecutionMetadata() |
| 156 | + |
| 157 | + # Check field exists and defaults to None |
| 158 | + assert hasattr(metadata, "duration_seconds") |
| 159 | + assert metadata.duration_seconds is None |
| 160 | + |
| 161 | + # Check it can be set |
| 162 | + metadata.duration_seconds = 1.234 |
| 163 | + assert metadata.duration_seconds == 1.234 |
0 commit comments