Skip to content

Commit 5a1ff07

Browse files
committed
add unit tests for invoke_model attributes
1 parent b7110f4 commit 5a1ff07

File tree

7 files changed

+328
-5
lines changed

7 files changed

+328
-5
lines changed

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/bedrock.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io
2222
import json
2323
import logging
24+
import math
2425
from timeit import default_timer
2526
from typing import Any
2627

@@ -514,11 +515,11 @@ def _invoke_model_on_success(
514515
span, response_body, instrumentor_context, capture_content
515516
)
516517
elif "cohere.command-r" in model_id:
517-
self._handle_command_r_response(
518+
self._handle_cohere_command_r_response(
518519
span, response_body, instrumentor_context, capture_content
519520
)
520521
elif "cohere.command" in model_id:
521-
self._handle_command_response(
522+
self._handle_cohere_command_response(
522523
span, response_body, instrumentor_context, capture_content
523524
)
524525
elif "meta.llama" in model_id:

instrumentation/opentelemetry-instrumentation-botocore/tests/bedrock_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import annotations
1616

1717
import json
18+
import math
1819
from typing import Any
1920

2021
from botocore.response import StreamingBody
@@ -54,6 +55,7 @@ def assert_completion_attributes_from_streaming_body(
5455
input_tokens = None
5556
output_tokens = None
5657
finish_reason = None
58+
request_prompt = "Say this is a test"
5759
if response is not None:
5860
original_body = response["body"]
5961
body_content = original_body.read()
@@ -89,6 +91,33 @@ def assert_completion_attributes_from_streaming_body(
8991
finish_reason = (response["stop_reason"],)
9092
else:
9193
finish_reason = None
94+
elif "cohere.command-r" in request_model:
95+
input_tokens = math.ceil(len(request_prompt) / 6)
96+
text = response.get("text")
97+
if text:
98+
output_tokens = math.ceil(len(text) / 6)
99+
finish_reason = (response["finish_reason"],)
100+
elif "cohere.command" in request_model:
101+
input_tokens = math.ceil(len(request_prompt) / 6)
102+
generations = response.get("generations")
103+
if generations:
104+
first_generation = generations[0]
105+
output_tokens = math.ceil(len(first_generation["text"]) / 6)
106+
finish_reason = (first_generation["finish_reason"],)
107+
elif "meta.llama" in request_model:
108+
if "prompt_token_count" in response:
109+
input_tokens = response.get("prompt_token_count")
110+
if "generation_token_count" in response:
111+
output_tokens = response.get("generation_token_count")
112+
if "stop_reason" in response:
113+
finish_reason = (response["stop_reason"],)
114+
elif "mistral.mistral" in request_model:
115+
input_tokens = math.ceil(len(request_prompt) / 6)
116+
outputs = response.get("outputs")
117+
if outputs:
118+
first_output = outputs[0]
119+
output_tokens = math.ceil(len(first_output["text"]) / 6)
120+
finish_reason = (first_output["stop_reason"],)
92121

93122
return assert_all_attributes(
94123
span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
interactions:
2+
- request:
3+
body: |-
4+
{
5+
"message": "Say this is a test",
6+
"max_tokens": 10,
7+
"temperature": 0.8,
8+
"p": 0.9,
9+
"stop_sequences": [
10+
"|"
11+
]
12+
}
13+
headers:
14+
Content-Length:
15+
- '106'
16+
User-Agent:
17+
- Boto3/1.35.56 md/Botocore#1.35.56 ua/2.0 os/macos#24.3.0 md/arch#arm64 lang/python#3.10.16
18+
md/pyimpl#CPython cfg/retry-mode#legacy Botocore/1.35.56
19+
X-Amz-Date:
20+
- 20250410T165615Z
21+
X-Amz-Security-Token:
22+
- test_aws_security_token
23+
X-Amzn-Trace-Id:
24+
- Root=1-d11162cc-5e3ef3bd07c5f9eb3ad25214;Parent=0a40817ca75eead7;Sampled=1
25+
amz-sdk-invocation-id:
26+
- 54acc108-181d-4437-a0a2-8293496f892d
27+
amz-sdk-request:
28+
- attempt=1
29+
authorization:
30+
- Bearer test_aws_authorization
31+
method: POST
32+
uri: https://bedrock-runtime.us-west-2.amazonaws.com/model/cohere.command-r-v1%3A0/invoke
33+
response:
34+
body:
35+
string: |-
36+
{
37+
"response_id": "379ed018/64bcb4c2-902c-4646-9e54-3e5e90c3b11e",
38+
"text": "This is a test.<",
39+
"generation_id": "dcf7f3e3-a611-4fb5-85fe-ae09e6b2eaea",
40+
"chat_history": [
41+
{
42+
"role": "USER",
43+
"message": "Say this is a test"
44+
},
45+
{
46+
"role": "CHATBOT",
47+
"message": "This is a test.<"
48+
}
49+
],
50+
"finish_reason": "STOP_SEQUENCE"
51+
}
52+
headers:
53+
Connection:
54+
- keep-alive
55+
Content-Type:
56+
- application/json
57+
Date:
58+
- Thu, 10 Apr 2025 16:56:15 GMT
59+
Set-Cookie: test_set_cookie
60+
X-Amzn-Bedrock-Input-Token-Count:
61+
- '5'
62+
X-Amzn-Bedrock-Invocation-Latency:
63+
- '141'
64+
X-Amzn-Bedrock-Output-Token-Count:
65+
- '5'
66+
x-amzn-RequestId:
67+
- 4d1ce10c-03d3-4579-8bab-a0e4eb14a8d0
68+
status:
69+
code: 200
70+
message: OK
71+
version: 1
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
interactions:
2+
- request:
3+
body: |-
4+
{
5+
"prompt": "Say this is a test",
6+
"max_tokens": 10,
7+
"temperature": 0.8,
8+
"p": 1,
9+
"stop_sequences": [
10+
"|"
11+
]
12+
}
13+
headers:
14+
Content-Length:
15+
- '103'
16+
User-Agent:
17+
- Boto3/1.35.56 md/Botocore#1.35.56 ua/2.0 os/macos#24.3.0 md/arch#arm64 lang/python#3.10.16
18+
md/pyimpl#CPython cfg/retry-mode#legacy Botocore/1.35.56
19+
X-Amz-Date:
20+
- 20250410T183021Z
21+
X-Amz-Security-Token:
22+
- test_aws_security_token
23+
X-Amzn-Trace-Id:
24+
- Root=1-668b3eaf-1927d32dd30fc4f0fedf3a02;Parent=6b49f910f73172c6;Sampled=1
25+
amz-sdk-invocation-id:
26+
- 98cb0162-a3bb-4d4e-bcbf-290664700ecf
27+
amz-sdk-request:
28+
- attempt=1
29+
authorization:
30+
- Bearer test_aws_authorization
31+
method: POST
32+
uri: https://bedrock-runtime.us-west-2.amazonaws.com/model/cohere.command-light-text-v14/invoke
33+
response:
34+
body:
35+
string: |-
36+
{
37+
"id": "b68fba0b-67f6-46dd-a659-dab9a99ee354",
38+
"generations": [
39+
{
40+
"id": "195692c2-6457-4ae0-9e5e-f0472a429cdc",
41+
"text": " I would be more than happy to assist you with",
42+
"finish_reason": "MAX_TOKENS"
43+
}
44+
],
45+
"prompt": "Say this is a test"
46+
}
47+
headers:
48+
Connection:
49+
- keep-alive
50+
Content-Type:
51+
- application/json
52+
Date:
53+
- Thu, 10 Apr 2025 18:30:21 GMT
54+
Set-Cookie: test_set_cookie
55+
X-Amzn-Bedrock-Input-Token-Count:
56+
- '5'
57+
X-Amzn-Bedrock-Invocation-Latency:
58+
- '309'
59+
X-Amzn-Bedrock-Output-Token-Count:
60+
- '10'
61+
x-amzn-RequestId:
62+
- b68fba0b-67f6-46dd-a659-dab9a99ee354
63+
status:
64+
code: 200
65+
message: OK
66+
version: 1
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
interactions:
2+
- request:
3+
body: |-
4+
{
5+
"prompt": "Say this is a test",
6+
"max_gen_len": 10,
7+
"temperature": 0.8,
8+
"top_p": 1
9+
}
10+
headers:
11+
Content-Length:
12+
- '83'
13+
User-Agent:
14+
- Boto3/1.35.56 md/Botocore#1.35.56 ua/2.0 os/macos#24.3.0 md/arch#arm64 lang/python#3.10.16
15+
md/pyimpl#CPython cfg/retry-mode#legacy Botocore/1.35.56
16+
X-Amz-Date:
17+
- 20250410T192408Z
18+
X-Amz-Security-Token:
19+
- test_aws_security_token
20+
X-Amzn-Trace-Id:
21+
- Root=1-db2cb3ce-1a9a7362f6f0e5eb7dc0ce56;Parent=a301dca505171c7f;Sampled=1
22+
amz-sdk-invocation-id:
23+
- 87acada6-b95b-4280-b002-50ef98edf99d
24+
amz-sdk-request:
25+
- attempt=1
26+
authorization:
27+
- Bearer test_aws_authorization
28+
method: POST
29+
uri: https://bedrock-runtime.us-west-2.amazonaws.com/model/meta.llama3-1-70b-instruct-v1%3A0/invoke
30+
response:
31+
body:
32+
string: |-
33+
{
34+
"generation": " and you are a test subject. You are not",
35+
"prompt_token_count": 5,
36+
"generation_token_count": 10,
37+
"stop_reason": "length"
38+
}
39+
headers:
40+
Connection:
41+
- keep-alive
42+
Content-Type:
43+
- application/json
44+
Date:
45+
- Thu, 10 Apr 2025 19:24:09 GMT
46+
Set-Cookie: test_set_cookie
47+
X-Amzn-Bedrock-Input-Token-Count:
48+
- '5'
49+
X-Amzn-Bedrock-Invocation-Latency:
50+
- '617'
51+
X-Amzn-Bedrock-Output-Token-Count:
52+
- '10'
53+
x-amzn-RequestId:
54+
- 51b421ef-fd84-4c69-b933-8c928b735d17
55+
status:
56+
code: 200
57+
message: OK
58+
version: 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
interactions:
2+
- request:
3+
body: |-
4+
{
5+
"prompt": "Say this is a test",
6+
"max_tokens": 10,
7+
"temperature": 0.8,
8+
"top_p": 1,
9+
"stop": [
10+
"|"
11+
]
12+
}
13+
headers:
14+
Content-Length:
15+
- '97'
16+
User-Agent:
17+
- Boto3/1.35.56 md/Botocore#1.35.56 ua/2.0 os/macos#24.3.0 md/arch#arm64 lang/python#3.10.16
18+
md/pyimpl#CPython cfg/retry-mode#legacy Botocore/1.35.56
19+
X-Amz-Date:
20+
- 20250410T183022Z
21+
X-Amz-Security-Token:
22+
- test_aws_security_token
23+
X-Amzn-Trace-Id:
24+
- Root=1-e325095b-1fab9945412a3dcac8474715;Parent=ae78dbba36fcf5b0;Sampled=1
25+
amz-sdk-invocation-id:
26+
- cbfbbad5-adc8-4a71-82f0-0777f6c78f83
27+
amz-sdk-request:
28+
- attempt=1
29+
authorization:
30+
- Bearer test_aws_authorization
31+
method: POST
32+
uri: https://bedrock-runtime.us-west-2.amazonaws.com/model/mistral.mistral-7b-instruct-v0%3A2/invoke
33+
response:
34+
body:
35+
string: |-
36+
{
37+
"outputs": [
38+
{
39+
"text": " to find out whether you think an animal is cute",
40+
"stop_reason": "length"
41+
}
42+
]
43+
}
44+
headers:
45+
Connection:
46+
- keep-alive
47+
Content-Type:
48+
- application/json
49+
Date:
50+
- Thu, 10 Apr 2025 18:30:22 GMT
51+
Set-Cookie: test_set_cookie
52+
X-Amzn-Bedrock-Input-Token-Count:
53+
- '6'
54+
X-Amzn-Bedrock-Invocation-Latency:
55+
- '219'
56+
X-Amzn-Bedrock-Output-Token-Count:
57+
- '10'
58+
x-amzn-RequestId:
59+
- 97084131-1cef-4927-9bab-53a36390153e
60+
status:
61+
code: 200
62+
message: OK
63+
version: 1

0 commit comments

Comments
 (0)