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