Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: OpenAI prompt details and completion tokens details missing from total usage #1105

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions instructor/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from instructor.utils import update_total_usage
from instructor.validators import AsyncValidationError
from openai.types.chat import ChatCompletion
from openai.types.completion_usage import CompletionUsage
from openai.types.completion_usage import CompletionUsage, CompletionTokensDetails, PromptTokensDetails
from pydantic import BaseModel, ValidationError
from tenacity import (
AsyncRetrying,
Expand Down Expand Up @@ -71,7 +71,10 @@ def initialize_usage(mode: Mode) -> CompletionUsage | Any:
Returns:
CompletionUsage | Any: Initialized usage object.
"""
total_usage = CompletionUsage(completion_tokens=0, prompt_tokens=0, total_tokens=0)
total_usage = CompletionUsage(completion_tokens=0, prompt_tokens=0, total_tokens=0,
completion_tokens_details = CompletionTokensDetails(audio_tokens=0, reasoning_tokens=0),
prompt_token_details = PromptTokensDetails(audio_tokens=0, cached_tokens=0)
)
if mode in {Mode.ANTHROPIC_TOOLS, Mode.ANTHROPIC_JSON}:
from anthropic.types import Usage as AnthropicUsage

Expand Down
6 changes: 6 additions & 0 deletions instructor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ def update_total_usage(
total_usage.completion_tokens += response_usage.completion_tokens or 0
total_usage.prompt_tokens += response_usage.prompt_tokens or 0
total_usage.total_tokens += response_usage.total_tokens or 0
if (rtd := response_usage.completion_tokens_details) and (ttd := total_usage.completion_tokens_details):
ttd.audio_tokens = (ttd.audio_tokens or 0) + (rtd.audio_tokens or 0)
ttd.reasoning_tokens = (ttd.reasoning_tokens or 0) + (rtd.reasoning_tokens or 0)
if (rpd := response_usage.prompt_tokens_details) and (tpd := total_usage.prompt_tokens_details):
tpd.audio_tokens = (tpd.audio_tokens or 0) + (rpd.audio_tokens or 0)
tpd.cached_tokens = (tpd.cached_tokens or 0) + (rpd.cached_tokens or 0)
response.usage = total_usage # Replace each response usage with the total usage
return response

Expand Down