File size: 3,480 Bytes
ab127e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import json

def parse_json_from_response(text: str) -> dict | None:
    """
    Markdown μ½”λ“œ 블둝 μ•ˆμ— 포함될 수 μžˆλŠ” JSON λ¬Έμžμ—΄μ„ μΆ”μΆœν•˜κ³  νŒŒμ‹±ν•©λ‹ˆλ‹€.

    Args:
        text (str): LLM이 λ°˜ν™˜ν•œ 전체 ν…μŠ€νŠΈ 응닡.

    Returns:
        dict | None: νŒŒμ‹±λœ λ”•μ…”λ„ˆλ¦¬ 객체, λ˜λŠ” μ‹€νŒ¨ μ‹œ None.
    """
    if not text:
        return None

    # ```json ... ``` λ˜λŠ” ``` ... ``` ν˜•μ‹μ˜ μ½”λ“œ λΈ”λ‘μ—μ„œ JSON μΆ”μΆœ
    match = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", text)
    if match:
        json_str = match.group(1)
    else:
        # μ½”λ“œ 블둝이 μ—†λ‹€λ©΄, 전체 ν…μŠ€νŠΈλ₯Ό JSON으둜 κ°€μ •
        json_str = text

    try:
        return json.loads(json_str)
    except json.JSONDecodeError:
        # 전체 νŒŒμ‹±μ΄ μ‹€νŒ¨ν•˜λ©΄, 첫 '{'와 λ§ˆμ§€λ§‰ '}'λ₯Ό κΈ°μ€€μœΌλ‘œ λ‹€μ‹œ μ‹œλ„
        start_index = json_str.find('{')
        end_index = json_str.rfind('}')
        if start_index != -1 and end_index != -1 and start_index < end_index:
            potential_json = json_str[start_index:end_index+1]
            try:
                return json.loads(potential_json)
            except json.JSONDecodeError:
                pass  # μ΄λ§ˆμ €λ„ μ‹€νŒ¨ν•˜λ©΄ κ·Έλƒ₯ None λ°˜ν™˜

    return None

def track_api_cost(response, model_name, search_context_size):
    # Calculate web search cost based on model and context size
    search_cost = 0
    
    if model_name in ['gpt-4.1', 'gpt-4o', 'gpt-4o-search-preview']:
        if search_context_size == 'low':
            search_cost = 0.03  # $30/1000 calls = $0.03 per call
        elif search_context_size == 'medium':
            search_cost = 0.035 # $35/1000 calls = $0.035 per call 
        elif search_context_size == 'high':
            search_cost = 0.05  # $50/1000 calls = $0.05 per call
            
    elif model_name in ['gpt-4.1-mini', 'gpt-4o-mini', 'gpt-4o-mini-search-preview']:
        if search_context_size == 'low':
            search_cost = 0.025 # $25/1000 calls = $0.025 per call
        elif search_context_size == 'medium':
            search_cost = 0.0275 # $27.50/1000 calls = $0.0275 per call
        elif search_context_size == 'high':
            search_cost = 0.03  # $30/1000 calls = $0.03 per call
            
    generation_cost = 0
    # Calculate generation cost based on model and token counts
    if model_name in ['gpt-4.1', 'gpt-4.1-2025-04-14']:
        generation_cost = (response.usage.prompt_tokens * 0.002 / 1000) + (response.usage.completion_tokens * 0.008 / 1000)
    elif model_name in ['gpt-4.1-mini', 'gpt-4.1-mini-2025-04-14']:
        generation_cost = (response.usage.prompt_tokens * 0.0004 / 1000) + (response.usage.completion_tokens * 0.0016 / 1000)
    elif model_name in ['gpt-4.1-nano', 'gpt-4.1-nano-2025-04-14']:
        generation_cost = (response.usage.prompt_tokens * 0.0001 / 1000) + (response.usage.completion_tokens * 0.0004 / 1000)
    elif model_name in ['gpt-4.5-preview', 'gpt-4.5-preview-2025-02-27']:
        generation_cost = (response.usage.prompt_tokens * 0.075 / 1000) + (response.usage.completion_tokens * 0.15 / 1000)
    elif model_name in ['gpt-4o', 'gpt-4o-2024-08-06']:
        generation_cost = (response.usage.prompt_tokens * 0.0025 / 1000) + (response.usage.completion_tokens * 0.01 / 1000)
    else:
        generation_cost = 0  # Default to 0 for unknown models
        
    total_cost = search_cost + generation_cost
    return total_cost