|
|
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 |
|
|
|
|
|
|
|
|
match = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", text) |
|
|
if match: |
|
|
json_str = match.group(1) |
|
|
else: |
|
|
|
|
|
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 |
|
|
|
|
|
return None |
|
|
|
|
|
def track_api_cost(response, model_name, search_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 |
|
|
elif search_context_size == 'medium': |
|
|
search_cost = 0.035 |
|
|
elif search_context_size == 'high': |
|
|
search_cost = 0.05 |
|
|
|
|
|
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 |
|
|
elif search_context_size == 'medium': |
|
|
search_cost = 0.0275 |
|
|
elif search_context_size == 'high': |
|
|
search_cost = 0.03 |
|
|
|
|
|
generation_cost = 0 |
|
|
|
|
|
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 |
|
|
|
|
|
total_cost = search_cost + generation_cost |
|
|
return total_cost |
|
|
|