cost_calculator.py

파일 다운로드

"""가설 토큰 입력의 표준단가 계산. 실제 청구·지연 측정과 별도다."""
import argparse
from decimal import Decimal
from pathlib import Path
import json
from usage import cost_fields


def calculate(path):
    results = []
    for row in json.loads(Path(path).read_text(encoding='utf-8')):
        count = row['calls']
        if type(count) is not int or count < 0:
            raise ValueError('호출 수는 0 이상의 정수입니다.')
        fields = cost_fields(row['model'], row['usage'], 'generation')
        amount = fields['cost_usd']
        results.append({'scenario': row['scenario'], 'calls': count, **fields,
                        'total_usd': str(Decimal(amount)*count) if amount is not None else None,
                        'kind': 'HYPOTHETICAL_INPUT'})
    return results


if __name__ == '__main__':
    parser = argparse.ArgumentParser();parser.add_argument('source', type=Path)
    print(json.dumps(calculate(parser.parse_args().source), ensure_ascii=False, indent=2))