Skip to content

Commit

Permalink
cost = chunk.usage.model_extra.get("estimated_cost", 0) if hasattr(ch…
Browse files Browse the repository at this point in the history
…unk, "usage") and chunk.usage else 0 # Estimated costs returned by DeepInfra API

fix AttributeError: 'NoneType' object has no attribute 'model_extra'

khoj-ai#1019
  • Loading branch information
thinker007 committed Jan 6, 2025
1 parent 555d3df commit daff2ab
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions src/khoj/processor/conversation/openai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ def completion_with_backoff(
input_tokens = chunk.usage.prompt_tokens if hasattr(chunk, "usage") and chunk.usage else 0
output_tokens = chunk.usage.completion_tokens if hasattr(chunk, "usage") and chunk.usage else 0
cost = chunk.usage.model_extra.get("estimated_cost", 0) if hasattr(chunk, "usage") and chunk.usage else 0 # Estimated costs returned by DeepInfra API
else:
cost = None
tracer["usage"] = get_chat_usage_metrics(model_name, input_tokens, output_tokens, tracer.get("usage"), cost)

# Save conversation trace
Expand Down Expand Up @@ -217,10 +215,7 @@ def llm_thread(
# Calculate cost of chat
input_tokens = chunk.usage.prompt_tokens if hasattr(chunk, "usage") and chunk.usage else 0
output_tokens = chunk.usage.completion_tokens if hasattr(chunk, "usage") and chunk.usage else 0
if chunk.usage:
cost = chunk.usage.model_extra.get("estimated_cost") or 0 # Estimated costs returned by DeepInfra API
else:
cost = None
cost = chunk.usage.model_extra.get("estimated_cost", 0) if hasattr(chunk, "usage") and chunk.usage else 0 # Estimated costs returned by DeepInfra API
tracer["usage"] = get_chat_usage_metrics(model_name, input_tokens, output_tokens, tracer.get("usage"), cost)

# Save conversation trace
Expand Down

0 comments on commit daff2ab

Please sign in to comment.