File size: 3,480 Bytes
ab127e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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
# ```json ... ``` λλ ``` ... ``` νμμ μ½λ λΈλ‘μμ JSON μΆμΆ
match = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", text)
if match:
json_str = match.group(1)
else:
# μ½λ λΈλ‘μ΄ μλ€λ©΄, μ 체 ν
μ€νΈλ₯Ό JSONμΌλ‘ κ°μ
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 # μ΄λ§μ λ μ€ν¨νλ©΄ κ·Έλ₯ None λ°ν
return None
def track_api_cost(response, model_name, search_context_size):
# Calculate web search cost based on model and 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 # $30/1000 calls = $0.03 per call
elif search_context_size == 'medium':
search_cost = 0.035 # $35/1000 calls = $0.035 per call
elif search_context_size == 'high':
search_cost = 0.05 # $50/1000 calls = $0.05 per call
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 # $25/1000 calls = $0.025 per call
elif search_context_size == 'medium':
search_cost = 0.0275 # $27.50/1000 calls = $0.0275 per call
elif search_context_size == 'high':
search_cost = 0.03 # $30/1000 calls = $0.03 per call
generation_cost = 0
# Calculate generation cost based on model and token counts
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 # Default to 0 for unknown models
total_cost = search_cost + generation_cost
return total_cost
|