Files
New-planet-ai-agent/scripts/analyze_usage.py

124 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Скрипт для анализа использования токенов GigaChat."""
import argparse
import json
from collections import defaultdict
from datetime import datetime
from pathlib import Path
def calculate_cost(tokens: int, model: str = "Lite") -> float:
"""Рассчитать стоимость токенов."""
rates = {
"Lite": 0.2 / 1000,
"Pro": 1.5 / 1000,
"Max": 1.95 / 1000,
}
rate = rates.get(model, rates["Lite"])
return tokens * rate
def analyze_usage(data_file: str, month: str = None):
"""Проанализировать использование токенов."""
if not Path(data_file).exists():
print(f"Файл {data_file} не найден. Создайте файл с данными использования.")
print("\nФормат данных (JSON):")
print(json.dumps(
{
"usage": [
{
"user_id": "user_123",
"date": "2025-12-15",
"tokens": 1500,
"model": "Lite",
}
]
},
indent=2,
))
return
with open(data_file, "r", encoding="utf-8") as f:
data = json.load(f)
usage_records = data.get("usage", [])
if month:
# Фильтруем по месяцу
usage_records = [
record
for record in usage_records
if record.get("date", "").startswith(month)
]
if not usage_records:
print("Нет данных для анализа")
return
# Статистика по моделям
model_stats = defaultdict(lambda: {"tokens": 0, "requests": 0})
user_stats = defaultdict(lambda: {"tokens": 0, "requests": 0})
total_tokens = 0
for record in usage_records:
tokens = record.get("tokens", 0)
model = record.get("model", "Lite")
user_id = record.get("user_id", "unknown")
model_stats[model]["tokens"] += tokens
model_stats[model]["requests"] += 1
user_stats[user_id]["tokens"] += tokens
user_stats[user_id]["requests"] += 1
total_tokens += tokens
# Выводим отчет
print("=" * 50)
print(f"GigaChat Usage Report")
if month:
print(f"Period: {month}")
print("=" * 50)
print(f"\nTotal tokens used: {total_tokens:,}")
print("\nBy Model:")
total_cost = 0
for model, stats in sorted(model_stats.items()):
cost = calculate_cost(stats["tokens"], model)
total_cost += cost
print(f" {model}:")
print(f" Tokens: {stats['tokens']:,}")
print(f" Requests: {stats['requests']}")
print(f" Cost: ₽{cost:,.2f}")
print(f"\nTotal cost: ₽{total_cost:,.2f}")
print("\nTop Users:")
top_users = sorted(user_stats.items(), key=lambda x: x[1]["tokens"], reverse=True)[:10]
for user_id, stats in top_users:
print(f" {user_id}: {stats['tokens']:,} tokens ({stats['requests']} requests)")
def main():
"""Главная функция."""
parser = argparse.ArgumentParser(description="Анализ использования токенов GigaChat")
parser.add_argument(
"--file",
type=str,
default="usage_data.json",
help="Файл с данными использования",
)
parser.add_argument(
"--month",
type=str,
default=None,
help="Месяц для анализа (формат: YYYY-MM)",
)
args = parser.parse_args()
analyze_usage(args.file, args.month)
if __name__ == "__main__":
main()