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 AttributeError with missing usage fields in AnthropicBedrock #1293

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 29 additions & 13 deletions instructor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,36 @@ def update_total_usage(
if isinstance(response_usage, AnthropicUsage) and isinstance(
total_usage, AnthropicUsage
):
if not total_usage.cache_creation_input_tokens:
total_usage.cache_creation_input_tokens = 0
# update input_tokens / output_tokens
if hasattr(total_usage, "input_tokens") and hasattr(
response_usage, "input_tokens"
):
total_usage.input_tokens += response_usage.input_tokens or 0
if hasattr(total_usage, "output_tokens") and hasattr(
response_usage, "output_tokens"
):
total_usage.output_tokens += response_usage.output_tokens or 0

# Update cache_creation_input_tokens if both have that field
if hasattr(total_usage, "cache_creation_input_tokens") and hasattr(
response_usage, "cache_creation_input_tokens"
):
if not total_usage.cache_creation_input_tokens:
total_usage.cache_creation_input_tokens = 0
total_usage.cache_creation_input_tokens += (
response_usage.cache_creation_input_tokens or 0
)

# Update cache_read_input_tokens if both have that field
if hasattr(total_usage, "cache_read_input_tokens") and hasattr(
response_usage, "cache_read_input_tokens"
):
if not total_usage.cache_read_input_tokens:
total_usage.cache_read_input_tokens = 0
total_usage.cache_read_input_tokens += (
response_usage.cache_read_input_tokens or 0
)

if not total_usage.cache_read_input_tokens:
total_usage.cache_read_input_tokens = 0

total_usage.input_tokens += response_usage.input_tokens or 0
total_usage.output_tokens += response_usage.output_tokens or 0
total_usage.cache_creation_input_tokens += (
response_usage.cache_creation_input_tokens or 0
)
total_usage.cache_read_input_tokens += (
response_usage.cache_read_input_tokens or 0
)
response.usage = total_usage
return response
except ImportError:
Expand Down
2 changes: 0 additions & 2 deletions tests/llm/test_new_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ def test_client_anthropic_response():
assert user.age == 10


@pytest.mark.skip(reason="Skip for now")
def test_client_anthropic_bedrock_response():
client = anthropic.AnthropicBedrock(
aws_access_key=os.getenv("AWS_ACCESS_KEY_ID"),
Expand Down Expand Up @@ -222,7 +221,6 @@ async def test_async_client_anthropic_response():
assert user.age == 10


@pytest.mark.skip(reason="Skip for now")
@pytest.mark.asyncio
async def test_async_client_anthropic_bedrock_response():
client = anthropic.AsyncAnthropicBedrock(
Expand Down