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
|