mirror of
https://github.com/tiennm99/litellm.git
synced 2026-07-14 03:04:12 +00:00
feat(converse_transformation.py): translate converse usage block with cache creation values to openai format
This commit is contained in:
@@ -31,7 +31,7 @@ from litellm.types.llms.openai import (
|
||||
ChatCompletionUserMessage,
|
||||
OpenAIMessageContentListBlock,
|
||||
)
|
||||
from litellm.types.utils import ModelResponse, Usage
|
||||
from litellm.types.utils import ModelResponse, PromptTokensDetailsWrapper, Usage
|
||||
from litellm.utils import add_dummy_tool, has_tool_call_blocks
|
||||
|
||||
from ..common_utils import BedrockError, BedrockModelInfo, get_bedrock_tool_name
|
||||
@@ -602,6 +602,33 @@ class AmazonConverseConfig(BaseConfig):
|
||||
thinking_blocks_list.append(_thinking_block)
|
||||
return thinking_blocks_list
|
||||
|
||||
def _transform_usage(self, usage: ConverseTokenUsageBlock) -> Usage:
|
||||
input_tokens = usage["inputTokens"]
|
||||
output_tokens = usage["outputTokens"]
|
||||
total_tokens = usage["totalTokens"]
|
||||
cache_creation_input_tokens: int = 0
|
||||
cache_read_input_tokens: int = 0
|
||||
|
||||
if "cacheReadInputTokens" in usage:
|
||||
cache_read_input_tokens = usage["cacheReadInputTokens"]
|
||||
input_tokens += cache_read_input_tokens
|
||||
if "cacheCreationInputTokens" in usage:
|
||||
cache_creation_input_tokens = usage["cacheCreationInputTokens"]
|
||||
input_tokens += cache_creation_input_tokens
|
||||
|
||||
prompt_tokens_details = PromptTokensDetailsWrapper(
|
||||
cached_tokens=cache_read_input_tokens
|
||||
)
|
||||
openai_usage = Usage(
|
||||
prompt_tokens=input_tokens,
|
||||
completion_tokens=output_tokens,
|
||||
total_tokens=total_tokens,
|
||||
prompt_tokens_details=prompt_tokens_details,
|
||||
cache_creation_input_tokens=cache_creation_input_tokens,
|
||||
cache_read_input_tokens=cache_read_input_tokens,
|
||||
)
|
||||
return openai_usage
|
||||
|
||||
def _transform_response(
|
||||
self,
|
||||
model: str,
|
||||
@@ -730,9 +757,7 @@ class AmazonConverseConfig(BaseConfig):
|
||||
chat_completion_message["tool_calls"] = tools
|
||||
|
||||
## CALCULATING USAGE - bedrock returns usage in the headers
|
||||
input_tokens = completion_response["usage"]["inputTokens"]
|
||||
output_tokens = completion_response["usage"]["outputTokens"]
|
||||
total_tokens = completion_response["usage"]["totalTokens"]
|
||||
usage = self._transform_usage(completion_response["usage"])
|
||||
|
||||
model_response.choices = [
|
||||
litellm.Choices(
|
||||
@@ -743,11 +768,7 @@ class AmazonConverseConfig(BaseConfig):
|
||||
]
|
||||
model_response.created = int(time.time())
|
||||
model_response.model = model
|
||||
usage = Usage(
|
||||
prompt_tokens=input_tokens,
|
||||
completion_tokens=output_tokens,
|
||||
total_tokens=total_tokens,
|
||||
)
|
||||
|
||||
setattr(model_response, "usage", usage)
|
||||
|
||||
# Add "trace" from Bedrock guardrails - if user has opted in to returning it
|
||||
|
||||
@@ -109,6 +109,10 @@ class ConverseTokenUsageBlock(TypedDict):
|
||||
inputTokens: int
|
||||
outputTokens: int
|
||||
totalTokens: int
|
||||
cacheReadInputTokenCount: int
|
||||
cacheReadInputTokens: int
|
||||
cacheCreationInputTokenCount: int
|
||||
cacheCreationInputTokens: int
|
||||
|
||||
|
||||
class ConverseResponseBlock(TypedDict):
|
||||
@@ -400,7 +404,9 @@ class AmazonNovaCanvasTextToImageParams(TypedDict, total=False):
|
||||
conditionImage: str
|
||||
|
||||
|
||||
class AmazonNovaCanvasTextToImageRequest(AmazonNovaCanvasRequestBase, TypedDict, total=False):
|
||||
class AmazonNovaCanvasTextToImageRequest(
|
||||
AmazonNovaCanvasRequestBase, TypedDict, total=False
|
||||
):
|
||||
"""
|
||||
Request for Amazon Nova Canvas Text to Image API
|
||||
|
||||
|
||||
@@ -2948,3 +2948,12 @@ async def test_bedrock_stream_thinking_content_openwebui():
|
||||
assert (
|
||||
len(response_content) > 0
|
||||
), "There should be non-empty content after thinking tags"
|
||||
|
||||
|
||||
def test_bedrock_usage_block():
|
||||
litellm._turn_on_debug()
|
||||
response = completion(
|
||||
model="bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0",
|
||||
messages=[{"role": "user", "content": "Hello who is this?"}],
|
||||
)
|
||||
assert response.usage.total_tokens > 0
|
||||
|
||||
Reference in New Issue
Block a user