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
|