Spaces:
Sleeping
Sleeping
File size: 3,601 Bytes
905e3b7 | 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | """Cost tracking for DocIntel operations."""
import json
from typing import Dict, Any
from datetime import datetime
import os
class CostTracker:
"""Track operational costs for DocIntel (zero cost implementation)."""
def __init__(self):
self.cost_data = {
"total_tokens": 0,
"total_requests": 0,
"cost_by_model": {},
"cost_per_query": {},
"total_cost": 0.0
}
self.log_file = "cost_log.json"
self._load_data()
def _load_data(self):
"""Load existing cost data."""
if os.path.exists(self.log_file):
try:
with open(self.log_file, "r") as f:
self.cost_data = json.load(f)
except:
pass
def _save_data(self):
"""Save cost data to file."""
with open(self.log_file, "w") as f:
json.dump(self.cost_data, f, indent=2)
def track_request(self, query: str, tokens: int, model: str = "groq"):
"""Track a single request."""
self.cost_data["total_tokens"] += tokens
self.cost_data["total_requests"] += 1
# Since we're using free tier, cost is zero
# But we track it for transparency
if model not in self.cost_data["cost_by_model"]:
self.cost_data["cost_by_model"][model] = {
"tokens": 0,
"cost": 0.0
}
self.cost_data["cost_by_model"][model]["tokens"] += tokens
self.cost_data["cost_by_model"][model]["cost"] = 0.0 # Free tier
# Track per query
query_id = f"q_{self.cost_data['total_requests']}"
self.cost_data["cost_per_query"][query_id] = {
"query": query[:50] + "..." if len(query) > 50 else query,
"tokens": tokens,
"cost": 0.0, # Free tier
"model": model,
"timestamp": datetime.now().isoformat()
}
self._save_data()
def get_summary(self) -> Dict[str, Any]:
"""Get cost summary."""
return {
"total_requests": self.cost_data["total_requests"],
"total_tokens": self.cost_data["total_tokens"],
"total_cost": self.cost_data["total_cost"],
"cost_by_model": self.cost_data["cost_by_model"],
"cost_per_1000_tokens": 0.0, # Free tier
"cost_per_query": 0.0, # Free tier
"monthly_estimate": 0.0 # Free tier
}
def get_cost_breakdown(self) -> Dict[str, Any]:
"""Get detailed cost breakdown."""
return {
"summary": self.get_summary(),
"recent_queries": list(self.cost_data["cost_per_query"].values())[-10:],
"free_tier_status": "Active",
"free_tier_limit": "30 requests/minute",
"free_tier_remaining": "Unlimited (free tier)"
}
def reset(self):
"""Reset cost tracking."""
self.cost_data = {
"total_tokens": 0,
"total_requests": 0,
"cost_by_model": {},
"cost_per_query": {},
"total_cost": 0.0
}
self._save_data()
def export_report(self, filepath: str = "cost_report.json"):
"""Export cost report."""
report = {
"timestamp": datetime.now().isoformat(),
"cost_data": self.cost_data,
"summary": self.get_summary()
}
with open(filepath, "w") as f:
json.dump(report, f, indent=2)
return filepath |