kyle8581 commited on
Commit
ab127e2
Β·
1 Parent(s): 86e1b98
Files changed (1) hide show
  1. utils.py +76 -0
utils.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import json
3
+
4
+ def parse_json_from_response(text: str) -> dict | None:
5
+ """
6
+ Markdown μ½”λ“œ 블둝 μ•ˆμ— 포함될 수 μžˆλŠ” JSON λ¬Έμžμ—΄μ„ μΆ”μΆœν•˜κ³  νŒŒμ‹±ν•©λ‹ˆλ‹€.
7
+
8
+ Args:
9
+ text (str): LLM이 λ°˜ν™˜ν•œ 전체 ν…μŠ€νŠΈ 응닡.
10
+
11
+ Returns:
12
+ dict | None: νŒŒμ‹±λœ λ”•μ…”λ„ˆλ¦¬ 객체, λ˜λŠ” μ‹€νŒ¨ μ‹œ None.
13
+ """
14
+ if not text:
15
+ return None
16
+
17
+ # ```json ... ``` λ˜λŠ” ``` ... ``` ν˜•μ‹μ˜ μ½”λ“œ λΈ”λ‘μ—μ„œ JSON μΆ”μΆœ
18
+ match = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", text)
19
+ if match:
20
+ json_str = match.group(1)
21
+ else:
22
+ # μ½”λ“œ 블둝이 μ—†λ‹€λ©΄, 전체 ν…μŠ€νŠΈλ₯Ό JSON으둜 κ°€μ •
23
+ json_str = text
24
+
25
+ try:
26
+ return json.loads(json_str)
27
+ except json.JSONDecodeError:
28
+ # 전체 νŒŒμ‹±μ΄ μ‹€νŒ¨ν•˜λ©΄, 첫 '{'와 λ§ˆμ§€λ§‰ '}'λ₯Ό κΈ°μ€€μœΌλ‘œ λ‹€μ‹œ μ‹œλ„
29
+ start_index = json_str.find('{')
30
+ end_index = json_str.rfind('}')
31
+ if start_index != -1 and end_index != -1 and start_index < end_index:
32
+ potential_json = json_str[start_index:end_index+1]
33
+ try:
34
+ return json.loads(potential_json)
35
+ except json.JSONDecodeError:
36
+ pass # μ΄λ§ˆμ €λ„ μ‹€νŒ¨ν•˜λ©΄ κ·Έλƒ₯ None λ°˜ν™˜
37
+
38
+ return None
39
+
40
+ def track_api_cost(response, model_name, search_context_size):
41
+ # Calculate web search cost based on model and context size
42
+ search_cost = 0
43
+
44
+ if model_name in ['gpt-4.1', 'gpt-4o', 'gpt-4o-search-preview']:
45
+ if search_context_size == 'low':
46
+ search_cost = 0.03 # $30/1000 calls = $0.03 per call
47
+ elif search_context_size == 'medium':
48
+ search_cost = 0.035 # $35/1000 calls = $0.035 per call
49
+ elif search_context_size == 'high':
50
+ search_cost = 0.05 # $50/1000 calls = $0.05 per call
51
+
52
+ elif model_name in ['gpt-4.1-mini', 'gpt-4o-mini', 'gpt-4o-mini-search-preview']:
53
+ if search_context_size == 'low':
54
+ search_cost = 0.025 # $25/1000 calls = $0.025 per call
55
+ elif search_context_size == 'medium':
56
+ search_cost = 0.0275 # $27.50/1000 calls = $0.0275 per call
57
+ elif search_context_size == 'high':
58
+ search_cost = 0.03 # $30/1000 calls = $0.03 per call
59
+
60
+ generation_cost = 0
61
+ # Calculate generation cost based on model and token counts
62
+ if model_name in ['gpt-4.1', 'gpt-4.1-2025-04-14']:
63
+ generation_cost = (response.usage.prompt_tokens * 0.002 / 1000) + (response.usage.completion_tokens * 0.008 / 1000)
64
+ elif model_name in ['gpt-4.1-mini', 'gpt-4.1-mini-2025-04-14']:
65
+ generation_cost = (response.usage.prompt_tokens * 0.0004 / 1000) + (response.usage.completion_tokens * 0.0016 / 1000)
66
+ elif model_name in ['gpt-4.1-nano', 'gpt-4.1-nano-2025-04-14']:
67
+ generation_cost = (response.usage.prompt_tokens * 0.0001 / 1000) + (response.usage.completion_tokens * 0.0004 / 1000)
68
+ elif model_name in ['gpt-4.5-preview', 'gpt-4.5-preview-2025-02-27']:
69
+ generation_cost = (response.usage.prompt_tokens * 0.075 / 1000) + (response.usage.completion_tokens * 0.15 / 1000)
70
+ elif model_name in ['gpt-4o', 'gpt-4o-2024-08-06']:
71
+ generation_cost = (response.usage.prompt_tokens * 0.0025 / 1000) + (response.usage.completion_tokens * 0.01 / 1000)
72
+ else:
73
+ generation_cost = 0 # Default to 0 for unknown models
74
+
75
+ total_cost = search_cost + generation_cost
76
+ return total_cost