-
Notifications
You must be signed in to change notification settings - Fork 798
botocore: add basic tracing for bedrock ConverseStream #3204
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aeab196
Add tracing for ConverseStream
xrmx de33ce2
Add converse stream example
xrmx c6a7897
Add changelog
xrmx 35de237
Fix pylint warnings
xrmx 0a896f6
Pass proper parameters in conver stream test with invalid model
xrmx 2a3436a
Remove leftover print
xrmx bd8a50f
Fix typos
xrmx 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
26 changes: 26 additions & 0 deletions
26
...ntelemetry-instrumentation-botocore/examples/bedrock-runtime/zero-code/converse_stream.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,26 @@ | ||
import os | ||
|
||
import boto3 | ||
|
||
|
||
def main(): | ||
client = boto3.client("bedrock-runtime") | ||
stream = client.converse_stream( | ||
modelId=os.getenv("CHAT_MODEL", "amazon.titan-text-lite-v1"), | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [{"text": "Write a short poem on OpenTelemetry."}], | ||
}, | ||
], | ||
) | ||
|
||
response = "" | ||
for event in stream["stream"]: | ||
if "contentBlockDelta" in event: | ||
response += event["contentBlockDelta"]["delta"]["text"] | ||
print(response) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
74 changes: 74 additions & 0 deletions
74
...mentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/bedrock_utils.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,74 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Includes work from: | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from __future__ import annotations | ||
|
||
from botocore.eventstream import EventStream | ||
from wrapt import ObjectProxy | ||
|
||
|
||
# pylint: disable=abstract-method | ||
class ConverseStreamWrapper(ObjectProxy): | ||
"""Wrapper for botocore.eventstream.EventStream""" | ||
|
||
def __init__( | ||
self, | ||
stream: EventStream, | ||
stream_done_callback, | ||
): | ||
super().__init__(stream) | ||
|
||
self._stream_done_callback = stream_done_callback | ||
# accumulating things in the same shape of non-streaming version | ||
# {"usage": {"inputTokens": 0, "outputTokens": 0}, "stopReason": "finish"} | ||
self._response = {} | ||
|
||
def __iter__(self): | ||
for event in self.__wrapped__: | ||
self._process_event(event) | ||
yield event | ||
|
||
def _process_event(self, event): | ||
if "messageStart" in event: | ||
# {'messageStart': {'role': 'assistant'}} | ||
pass | ||
|
||
if "contentBlockDelta" in event: | ||
# {'contentBlockDelta': {'delta': {'text': "Hello"}, 'contentBlockIndex': 0}} | ||
pass | ||
|
||
if "contentBlockStop" in event: | ||
# {'contentBlockStop': {'contentBlockIndex': 0}} | ||
pass | ||
|
||
if "messageStop" in event: | ||
# {'messageStop': {'stopReason': 'end_turn'}} | ||
if stop_reason := event["messageStop"].get("stopReason"): | ||
self._response["stopReason"] = stop_reason | ||
|
||
if "metadata" in event: | ||
# {'metadata': {'usage': {'inputTokens': 12, 'outputTokens': 15, 'totalTokens': 27}, 'metrics': {'latencyMs': 2980}}} | ||
if usage := event["metadata"].get("usage"): | ||
self._response["usage"] = {} | ||
if input_tokens := usage.get("inputTokens"): | ||
self._response["usage"]["inputTokens"] = input_tokens | ||
|
||
if output_tokens := usage.get("outputTokens"): | ||
self._response["usage"]["outputTokens"] = output_tokens | ||
|
||
self._stream_done_callback(self._response) |
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
69 changes: 69 additions & 0 deletions
69
...telemetry-instrumentation-botocore/tests/cassettes/test_converse_stream_with_content.yaml
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,69 @@ | ||
interactions: | ||
- request: | ||
body: '{"messages": [{"role": "user", "content": [{"text": "Say this is a test"}]}], | ||
"inferenceConfig": {"maxTokens": 10, "temperature": 0.8, "topP": 1, "stopSequences": | ||
["|"]}}' | ||
headers: | ||
Content-Length: | ||
- '170' | ||
Content-Type: | ||
- !!binary | | ||
YXBwbGljYXRpb24vanNvbg== | ||
User-Agent: | ||
- !!binary | | ||
Qm90bzMvMS4zNS41NiBtZC9Cb3RvY29yZSMxLjM1LjU2IHVhLzIuMCBvcy9saW51eCM2LjEuMC0x | ||
MDM0LW9lbSBtZC9hcmNoI3g4Nl82NCBsYW5nL3B5dGhvbiMzLjEwLjEyIG1kL3B5aW1wbCNDUHl0 | ||
aG9uIGNmZy9yZXRyeS1tb2RlI2xlZ2FjeSBCb3RvY29yZS8xLjM1LjU2 | ||
X-Amz-Date: | ||
- !!binary | | ||
MjAyNTAxMjNUMDk1MTU2Wg== | ||
X-Amz-Security-Token: | ||
- test_aws_security_token | ||
X-Amzn-Trace-Id: | ||
- !!binary | | ||
Um9vdD0xLTA0YmY4MjVjLTAxMTY5NjdhYWM1NmIxM2RlMDI1N2QwMjtQYXJlbnQ9MDdkM2U3N2Rl | ||
OGFjMzJhNDtTYW1wbGVkPTE= | ||
amz-sdk-invocation-id: | ||
- !!binary | | ||
ZGQ1MTZiNTEtOGU1Yi00NGYyLTk5MzMtZjAwYzBiOGFkYWYw | ||
amz-sdk-request: | ||
- !!binary | | ||
YXR0ZW1wdD0x | ||
authorization: | ||
- Bearer test_aws_authorization | ||
method: POST | ||
uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-text-lite-v1/converse-stream | ||
response: | ||
body: | ||
string: !!binary | | ||
AAAAlAAAAFLEwW5hCzpldmVudC10eXBlBwAMbWVzc2FnZVN0YXJ0DTpjb250ZW50LXR5cGUHABBh | ||
cHBsaWNhdGlvbi9qc29uDTptZXNzYWdlLXR5cGUHAAVldmVudHsicCI6ImFiY2RlZmdoaWprbG1u | ||
b3BxcnN0dXZ3Iiwicm9sZSI6ImFzc2lzdGFudCJ9P+wfRAAAAMQAAABXjLhVJQs6ZXZlbnQtdHlw | ||
ZQcAEWNvbnRlbnRCbG9ja0RlbHRhDTpjb250ZW50LXR5cGUHABBhcHBsaWNhdGlvbi9qc29uDTpt | ||
ZXNzYWdlLXR5cGUHAAVldmVudHsiY29udGVudEJsb2NrSW5kZXgiOjAsImRlbHRhIjp7InRleHQi | ||
OiJIaSEgSG93IGNhbiBJIGhlbHAgeW91In0sInAiOiJhYmNkZWZnaGlqa2xtbm9wcXJzdHUifeBJ | ||
9mIAAACJAAAAVlvc+UsLOmV2ZW50LXR5cGUHABBjb250ZW50QmxvY2tTdG9wDTpjb250ZW50LXR5 | ||
cGUHABBhcHBsaWNhdGlvbi9qc29uDTptZXNzYWdlLXR5cGUHAAVldmVudHsiY29udGVudEJsb2Nr | ||
SW5kZXgiOjAsInAiOiJhYmNkZSJ95xzwrwAAAKcAAABRu0n9jQs6ZXZlbnQtdHlwZQcAC21lc3Nh | ||
Z2VTdG9wDTpjb250ZW50LXR5cGUHABBhcHBsaWNhdGlvbi9qc29uDTptZXNzYWdlLXR5cGUHAAVl | ||
dmVudHsicCI6ImFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6QUJDREVGR0hJSiIsInN0b3BSZWFz | ||
b24iOiJtYXhfdG9rZW5zIn1LR3pNAAAAygAAAE5X40OECzpldmVudC10eXBlBwAIbWV0YWRhdGEN | ||
OmNvbnRlbnQtdHlwZQcAEGFwcGxpY2F0aW9uL2pzb24NOm1lc3NhZ2UtdHlwZQcABWV2ZW50eyJt | ||
ZXRyaWNzIjp7ImxhdGVuY3lNcyI6NjA4fSwicCI6ImFiY2RlZmdoaWprIiwidXNhZ2UiOnsiaW5w | ||
dXRUb2tlbnMiOjgsIm91dHB1dFRva2VucyI6MTAsInRvdGFsVG9rZW5zIjoxOH19iiQr+w== | ||
headers: | ||
Connection: | ||
- keep-alive | ||
Content-Type: | ||
- application/vnd.amazon.eventstream | ||
Date: | ||
- Thu, 23 Jan 2025 09:51:56 GMT | ||
Set-Cookie: test_set_cookie | ||
Transfer-Encoding: | ||
- chunked | ||
x-amzn-RequestId: | ||
- 2b74a5d3-615a-4f81-b00f-f0b10a618e23 | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
54 changes: 54 additions & 0 deletions
54
...try-instrumentation-botocore/tests/cassettes/test_converse_stream_with_invalid_model.yaml
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,54 @@ | ||
interactions: | ||
- request: | ||
body: '{"messages": [{"role": "user", "content": [{"text": "Say this is a test"}]}]}' | ||
headers: | ||
Content-Length: | ||
- '77' | ||
Content-Type: | ||
- !!binary | | ||
YXBwbGljYXRpb24vanNvbg== | ||
User-Agent: | ||
- !!binary | | ||
Qm90bzMvMS4zNS41NiBtZC9Cb3RvY29yZSMxLjM1LjU2IHVhLzIuMCBvcy9saW51eCM2LjEuMC0x | ||
MDM0LW9lbSBtZC9hcmNoI3g4Nl82NCBsYW5nL3B5dGhvbiMzLjEwLjEyIG1kL3B5aW1wbCNDUHl0 | ||
aG9uIGNmZy9yZXRyeS1tb2RlI2xlZ2FjeSBCb3RvY29yZS8xLjM1LjU2 | ||
X-Amz-Date: | ||
- !!binary | | ||
MjAyNTAxMjNUMDk1MTU3Wg== | ||
X-Amz-Security-Token: | ||
- test_aws_security_token | ||
X-Amzn-Trace-Id: | ||
- !!binary | | ||
Um9vdD0xLTI5NzA1OTZhLTEyZWI5NDk2ODA1ZjZhYzE5YmU3ODM2NztQYXJlbnQ9Y2M0OTA0YWE2 | ||
ZjQ2NmYxYTtTYW1wbGVkPTE= | ||
amz-sdk-invocation-id: | ||
- !!binary | | ||
MjQzZWY2ZDgtNGJhNy00YTVlLWI0MGEtYThiNDE2ZDIzYjhk | ||
amz-sdk-request: | ||
- !!binary | | ||
YXR0ZW1wdD0x | ||
authorization: | ||
- Bearer test_aws_authorization | ||
method: POST | ||
uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/does-not-exist/converse-stream | ||
response: | ||
body: | ||
string: '{"message":"The provided model identifier is invalid."}' | ||
headers: | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '55' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Thu, 23 Jan 2025 09:51:57 GMT | ||
Set-Cookie: test_set_cookie | ||
x-amzn-ErrorType: | ||
- ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/ | ||
x-amzn-RequestId: | ||
- 358b122c-d045-4d8f-a5bb-b0bd8cf6ee59 | ||
status: | ||
code: 400 | ||
message: Bad Request | ||
version: 1 |
Oops, something went wrong.
Oops, something went wrong.
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.