File size: 2,197 Bytes
fc7db98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Compare local model vs AI API: latency and estimated cost per query.
"""

from __future__ import annotations

from dataclasses import dataclass

LEVEL_PROMPT = """Rate the complexity of the target word in context.
Levels: Very Easy, Easy, Medium, Hard, Very Hard.
Sentence: {sentence}
Target word: {target_word}
Reply with only the level label."""

# USD per 1k queries (rough; matches 08_efficiency.py defaults)
COST_PER_1K = {
    "openai_gpt4o_mini": 0.15,
    "gemini_flash": 0.075,
}

# Typical latency when API is used (ms) — used when no live API call
TYPICAL_AI_LATENCY_MS = {
    "openai_gpt4o_mini": 1200.0,
    "gemini_flash": 900.0,
}


@dataclass
class QueryComparison:
    local_latency_ms: float
    ai_latency_ms: float
    ai_provider: str
    ai_cost_usd: float
    ai_cost_per_1k_usd: float
    local_cost_usd: float
    speedup_factor: float
    prompt_tokens_est: int


def _estimate_tokens(text: str) -> int:
    return max(1, len(text.split()) + len(text) // 4)


def estimate_ai_cost(sentence: str, target_word: str, provider: str = "openai_gpt4o_mini") -> float:
    prompt = LEVEL_PROMPT.format(sentence=sentence, target_word=target_word)
    tokens = _estimate_tokens(prompt) + 10  # short reply
    # gpt-4o-mini ballpark: ~$0.15/1M input + $0.60/1M output — ~$0.0002 per simple call
    cost_per_1k = COST_PER_1K.get(provider, 0.15)
    return (cost_per_1k / 1000.0) * (tokens / 200.0)


def compare_query(sentence: str, target_word: str, local_latency_ms: float) -> QueryComparison:
    provider = "openai_gpt4o_mini"
    prompt = LEVEL_PROMPT.format(sentence=sentence, target_word=target_word)
    tokens = _estimate_tokens(prompt)
    ai_cost = estimate_ai_cost(sentence, target_word, provider)
    ai_latency = TYPICAL_AI_LATENCY_MS[provider]
    local_cost = 0.0
    speedup = ai_latency / max(local_latency_ms, 0.1)
    return QueryComparison(
        local_latency_ms=local_latency_ms,
        ai_latency_ms=ai_latency,
        ai_provider="GPT-4o-mini (estimated)",
        ai_cost_usd=ai_cost,
        ai_cost_per_1k_usd=COST_PER_1K[provider],
        local_cost_usd=local_cost,
        speedup_factor=speedup,
        prompt_tokens_est=tokens,
    )