forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rag

Clone
★ 0

0 watching0 forksPython2.4 MiBd67d85fKadir Yönak3 months ago12 commits

pricing.py100 lines, 4.0 KiBHistoryRawDownload
1"""DeepSeek fiyatlandırma + maliyet hesabı (tek doğruluk kaynağı: bu dosya).
2
3Fiyatlar 1M token başına USD. Kaynak: DeepSeek resmi fiyat sayfası
4(api-docs.deepseek.com/quick_start/pricing), alındığı tarih: 2026-08-28.
5Fiyatlar DEĞİŞİR — güncellemek için yalnız `PRICING` tablosunu düzenle,
6her yerde otomatik yansır. Değiştirdiğinde `updated` tarihini de güncelle.
7
8Not: DeepSeek "peak" (standart) ve "off-peak" (indirimli ≈ yarı fiyat) uygular.
9Bütçeyi TEMKİNLİ tutmak için varsayılan tier = "peak". Off-peak penceresini
10resmi sayfadan DOĞRULA (UTC saatleri değişebilir).
11"""
12from __future__ import annotations
13from dataclasses import dataclass
14
15PRICING_UPDATED = "2026-08-28"
16PRICING_SOURCE = "https://api-docs.deepseek.com/quick_start/pricing"
17
18# Peak (standart) fiyatlar — USD / 1M token. Off-peak = peak * OFFPEAK_FACTOR.
19PRICING: dict[str, dict[str, float]] = {
20 "deepseek-v4-flash": {"in_hit": 0.014, "in_miss": 0.44, "out": 1.32},
21 "deepseek-v4-pro": {"in_hit": 0.044, "in_miss": 1.32, "out": 3.96},
22 # görsel varyant flash ile aynı fiyat (doğrula):
23 "deepseek-v4-flash-vision-exp": {"in_hit": 0.014, "in_miss": 0.44, "out": 1.32},
24}
25OFFPEAK_FACTOR = 0.5
26
27# API model id'si ile fiyat-tablosu anahtarı FARKLI olabilir. Buradan eşle.
28# DeepSeek API'nin beklediği model id'sini doğrula ve gerekirse düzelt.
29MODEL_ALIASES: dict[str, str] = {
30 "deepseek-chat": "deepseek-v4-flash",
31 "deepseek-reasoner": "deepseek-v4-pro",
32}
33
34
35def resolve(model: str) -> str:
36 """API model id'sini fiyat-tablosu anahtarına çevir."""
37 return MODEL_ALIASES.get(model, model)
38
39
40@dataclass
41class Usage:
42 """Bir LLM çağrısının token kullanımı (DeepSeek/OpenAI-uyumlu usage'dan)."""
43 input_cache_hit: int = 0 # prompt_cache_hit_tokens
44 input_cache_miss: int = 0 # prompt_cache_miss_tokens
45 output: int = 0 # completion_tokens (reasoning/thinking DAHİL)
46 reasoning: int = 0 # completion_tokens_details.reasoning_tokens (bilgi amaçlı)
47
48 @property
49 def input_total(self) -> int:
50 return self.input_cache_hit + self.input_cache_miss
51
52 @property
53 def total(self) -> int:
54 return self.input_total + self.output
55
56 @classmethod
57 def from_api(cls, usage: dict) -> "Usage":
58 """DeepSeek/OpenAI-uyumlu `usage` sözlüğünden Usage üret.
59
60 DeepSeek prompt_tokens = cache_hit + cache_miss verir. cache alanları
61 yoksa tüm prompt'u cache-miss (en pahalı) say — temkinli.
62 """
63 hit = usage.get("prompt_cache_hit_tokens")
64 miss = usage.get("prompt_cache_miss_tokens")
65 if hit is None and miss is None:
66 miss = usage.get("prompt_tokens", 0)
67 hit = 0
68 out = usage.get("completion_tokens", 0)
69 det = usage.get("completion_tokens_details") or {}
70 reasoning = det.get("reasoning_tokens", 0)
71 return cls(input_cache_hit=hit or 0, input_cache_miss=miss or 0,
72 output=out, reasoning=reasoning)
73
74
75def cost_usd(model: str, usage: Usage, tier: str = "peak") -> float:
76 """Bir çağrının USD maliyeti. tier: 'peak' | 'offpeak'."""
77 key = resolve(model)
78 if key not in PRICING:
79 raise KeyError(f"Fiyat tablosunda model yok: {model!r} (çözülen: {key!r}). "
80 f"pricing.PRICING'e ekle.")
81 p = PRICING[key]
82 factor = OFFPEAK_FACTOR if tier == "offpeak" else 1.0
83 usd = (usage.input_cache_hit * p["in_hit"]
84 + usage.input_cache_miss * p["in_miss"]
85 + usage.output * p["out"]) / 1_000_000.0
86 return usd * factor
87
88
89def price_table_rows() -> list[dict]:
90 """Maliyet.md fiyat tablosu için satırlar (peak + offpeak)."""
91 rows = []
92 for m, p in PRICING.items():
93 rows.append({
94 "model": m,
95 "in_hit": p["in_hit"], "in_miss": p["in_miss"], "out": p["out"],
96 "in_hit_off": p["in_hit"] * OFFPEAK_FACTOR,
97 "in_miss_off": p["in_miss"] * OFFPEAK_FACTOR,
98 "out_off": p["out"] * OFFPEAK_FACTOR,
99 })
100 return rows