-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic tracing for InvokeModelWithResponseStream
- Loading branch information
Showing
10 changed files
with
716 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
51 changes: 51 additions & 0 deletions
51
...emetry-instrumentation-botocore/examples/bedrock-runtime/zero-code/invoke_model_stream.py
This file contains 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,51 @@ | ||
import json | ||
import os | ||
|
||
import boto3 | ||
|
||
|
||
def main(): | ||
chat_model = os.getenv("CHAT_MODEL", "amazon.titan-text-lite-v1") | ||
prompt = "Write a short poem on OpenTelemetry." | ||
if "amazon.titan" in chat_model: | ||
body = { | ||
"inputText": prompt, | ||
"textGenerationConfig": {}, | ||
} | ||
elif "amazon.nova" in chat_model: | ||
body = { | ||
"messages": [{"role": "user", "content": [{"text": prompt}]}], | ||
"schemaVersion": "messages-v1", | ||
} | ||
elif "anthropic.claude" in chat_model: | ||
body = { | ||
"messages": [ | ||
{"role": "user", "content": [{"text": prompt, "type": "text"}]} | ||
], | ||
"anthropic_version": "bedrock-2023-05-31", | ||
"max_tokens": 200, | ||
} | ||
else: | ||
raise ValueError() | ||
client = boto3.client("bedrock-runtime") | ||
response = client.invoke_model_with_response_stream( | ||
modelId=chat_model, | ||
body=json.dumps(body), | ||
) | ||
|
||
answer = "" | ||
for event in response["body"]: | ||
json_bytes = event.get("chunk", {}).get("bytes", b"") | ||
decoded = json_bytes.decode("utf-8") | ||
chunk = json.loads(decoded) | ||
if "outputText" in chunk: | ||
answer += chunk["outputText"] | ||
elif "completion" in chunk: | ||
answer += chunk["completion"] | ||
elif "contentBlockDelta" in chunk: | ||
answer += chunk["contentBlockDelta"]["delta"]["text"] | ||
print(answer) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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 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 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
51 changes: 51 additions & 0 deletions
51
...tation-botocore/tests/cassettes/test_invoke_model_with_response_stream_invalid_model.yaml
This file contains 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,51 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Content-Length: | ||
- '0' | ||
User-Agent: | ||
- !!binary | | ||
Qm90bzMvMS4zNS41NiBtZC9Cb3RvY29yZSMxLjM1LjU2IHVhLzIuMCBvcy9saW51eCM2LjEuMC0x | ||
MDM0LW9lbSBtZC9hcmNoI3g4Nl82NCBsYW5nL3B5dGhvbiMzLjEwLjEyIG1kL3B5aW1wbCNDUHl0 | ||
aG9uIGNmZy9yZXRyeS1tb2RlI2xlZ2FjeSBCb3RvY29yZS8xLjM1LjU2 | ||
X-Amz-Date: | ||
- !!binary | | ||
MjAyNTAxMjRUMTM0NDM5Wg== | ||
X-Amz-Security-Token: | ||
- test_aws_security_token | ||
X-Amzn-Trace-Id: | ||
- !!binary | | ||
Um9vdD0xLTFlMjljM2Y1LTU2MzZhOWI4MmViYTYxOTFiOTcwOTI2YTtQYXJlbnQ9NzA1NzBlZjUy | ||
YzJkZjliYjtTYW1wbGVkPTE= | ||
amz-sdk-invocation-id: | ||
- !!binary | | ||
ZDg2MjFlMzAtNTk3Yi00ZWM3LWJlNGEtMThkMDQwZTRhMzcw | ||
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/invoke-with-response-stream | ||
response: | ||
body: | ||
string: '{"message":"The provided model identifier is invalid."}' | ||
headers: | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '55' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 24 Jan 2025 13:44:40 GMT | ||
Set-Cookie: test_set_cookie | ||
x-amzn-ErrorType: | ||
- ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/ | ||
x-amzn-RequestId: | ||
- 6460a108-875d-4e26-bcdf-f03c4c815f74 | ||
status: | ||
code: 400 | ||
message: Bad Request | ||
version: 1 |
Oops, something went wrong.