Skip to content

Commit

Permalink
convert to cost_in_cents
Browse files Browse the repository at this point in the history
  • Loading branch information
rishsriv committed Jan 23, 2025
1 parent 8e0745b commit 8814e86
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions defog_utils/utils_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LLMResponse:
input_tokens: int
output_tokens: int
output_tokens_details: Optional[Dict[str, int]] = None
cost: Optional[float] = None
cost_in_cents: Optional[float] = None

def __post_init__(self):
if self.model in LLM_COSTS_PER_TOKEN:
Expand All @@ -59,13 +59,14 @@ def __post_init__(self):
model_name = max(potential_model_names, key=len)

if model_name:
self.cost = (
self.cost_in_cents = (
self.input_tokens
/ 1000
* LLM_COSTS_PER_TOKEN[model_name]["input_cost_per1k"]
+ self.output_tokens
/ 1000
* LLM_COSTS_PER_TOKEN[model_name]["output_cost_per1k"]
* 100
)


Expand Down Expand Up @@ -289,11 +290,9 @@ async def chat_openai_async(

if "response_format" in request_params and request_params["response_format"]:
response = await client_openai.beta.chat.completions.parse(**request_params)
print(response)
content = response.choices[0].message.parsed
else:
response = await client_openai.chat.completions.create(**request_params)
print(response)
content = response.choices[0].message.content

if response.choices[0].finish_reason == "length":
Expand Down

0 comments on commit 8814e86

Please sign in to comment.