From 60d549a6a96a3bfbf12442fa1e85c3429b0cf71d Mon Sep 17 00:00:00 2001 From: ivanbelenky Date: Mon, 21 Oct 2024 15:24:47 -0300 Subject: [PATCH] prompt details and completion tokens details --- instructor/retry.py | 7 +++++-- instructor/utils.py | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/instructor/retry.py b/instructor/retry.py index ffc41194d..205c7843f 100644 --- a/instructor/retry.py +++ b/instructor/retry.py @@ -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, @@ -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 diff --git a/instructor/utils.py b/instructor/utils.py index e9cacac25..e477f53ad 100644 --- a/instructor/utils.py +++ b/instructor/utils.py @@ -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