-
Notifications
You must be signed in to change notification settings - Fork 802
fix(bedrock): Add prompt caching support for Converse API #3390
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
Open
AlanPonnachan
wants to merge
7
commits into
traceloop:main
Choose a base branch
from
AlanPonnachan:feat-bedrock-converse-prompt-caching
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff8e343
add converse handling function in prompt_caching
AlanPonnachan 83bdb0b
update init
AlanPonnachan 4fa3792
add test
AlanPonnachan c8abf60
Update packages/opentelemetry-instrumentation-bedrock/opentelemetry/i…
AlanPonnachan 7d65ff8
correct lint test
AlanPonnachan 6cf3366
Merge branch 'feat-bedrock-converse-prompt-caching' of https://github…
AlanPonnachan a3c94e3
add token data
AlanPonnachan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...try-instrumentation-bedrock/tests/metrics/test_bedrock_converse_prompt_caching_metrics.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import pytest | ||
from opentelemetry.instrumentation.bedrock import PromptCaching | ||
from opentelemetry.instrumentation.bedrock.prompt_caching import CacheSpanAttrs | ||
|
||
|
||
def call(brt): | ||
return brt.converse( | ||
modelId="anthropic.claude-3-haiku-20240307-v1:0", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"text": "What is the capital of the USA?", | ||
} | ||
], | ||
} | ||
], | ||
inferenceConfig={"maxTokens": 50, "temperature": 0.1}, | ||
additionalModelRequestFields={"cacheControl": {"type": "ephemeral"}}, | ||
) | ||
|
||
|
||
def get_metric(resource_metrics, name): | ||
for rm in resource_metrics: | ||
for sm in rm.scope_metrics: | ||
for metric in sm.metrics: | ||
if metric.name == name: | ||
return metric | ||
raise Exception(f"No metric found with name {name}") | ||
|
||
|
||
def assert_metric(reader, usage): | ||
metrics_data = reader.get_metrics_data() | ||
resource_metrics = metrics_data.resource_metrics | ||
assert len(resource_metrics) > 0 | ||
|
||
m = get_metric(resource_metrics, PromptCaching.LLM_BEDROCK_PROMPT_CACHING) | ||
for data_point in m.data.data_points: | ||
assert data_point.attributes[CacheSpanAttrs.TYPE] in [ | ||
"read", | ||
"write", | ||
] | ||
if data_point.attributes[CacheSpanAttrs.TYPE] == "read": | ||
assert data_point.value == usage["cache_read_input_tokens"] | ||
else: | ||
assert data_point.value == usage["cache_creation_input_tokens"] | ||
|
||
|
||
@pytest.mark.vcr | ||
def test_prompt_cache_converse(test_context, brt): | ||
_, _, reader = test_context | ||
|
||
response = call(brt) | ||
# assert first prompt writes a cache | ||
usage = response["usage"] | ||
assert usage["cache_read_input_tokens"] == 0 | ||
assert usage["cache_creation_input_tokens"] > 0 | ||
cumulative_workaround = usage["cache_creation_input_tokens"] | ||
assert_metric(reader, usage) | ||
|
||
response = call(brt) | ||
# assert second prompt reads from the cache | ||
usage = response["usage"] | ||
assert usage["cache_read_input_tokens"] > 0 | ||
assert usage["cache_creation_input_tokens"] == 0 | ||
# data is stored across reads of metric data due to the cumulative behavior | ||
usage["cache_creation_input_tokens"] = cumulative_workaround | ||
assert_metric(reader, usage) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.