|
1 | 1 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | | -import json |
4 | | -import math |
5 | 3 | import os |
6 | 4 | from importlib.metadata import PackageNotFoundError |
7 | | -from io import BytesIO |
8 | 5 | from typing import Any, Dict |
9 | 6 | from unittest import TestCase |
10 | 7 | from unittest.mock import MagicMock, patch |
11 | 8 |
|
12 | 9 | import gevent.monkey |
13 | | -from botocore.response import StreamingBody |
14 | 10 |
|
15 | 11 | from amazon.opentelemetry.distro.patches._instrumentation_patch import ( |
16 | 12 | AWS_GEVENT_PATCH_MODULES, |
@@ -213,84 +209,9 @@ def _test_patched_botocore_instrumentation(self): |
213 | 209 | bedrock_agent_runtime_sucess_attributes: Dict[str, str] = _do_on_success_bedrock("bedrock-agent-runtime") |
214 | 210 | self.assertEqual(len(bedrock_agent_runtime_sucess_attributes), 0) |
215 | 211 |
|
216 | | - # BedrockRuntime - Amazon Titan |
| 212 | + # BedrockRuntime |
217 | 213 | self.assertTrue("bedrock-runtime" in _KNOWN_EXTENSIONS) |
218 | 214 |
|
219 | | - self._test_patched_bedrock_runtime_invoke_model( |
220 | | - model_id="amazon.titan-embed-text-v1", |
221 | | - max_tokens=512, |
222 | | - temperature=0.9, |
223 | | - top_p=0.75, |
224 | | - finish_reason="FINISH", |
225 | | - input_tokens=123, |
226 | | - output_tokens=456, |
227 | | - ) |
228 | | - |
229 | | - self._test_patched_bedrock_runtime_invoke_model( |
230 | | - model_id="amazon.nova-pro-v1:0", |
231 | | - max_tokens=500, |
232 | | - temperature=0.9, |
233 | | - top_p=0.7, |
234 | | - finish_reason="FINISH", |
235 | | - input_tokens=123, |
236 | | - output_tokens=456, |
237 | | - ) |
238 | | - |
239 | | - # BedrockRuntime - Anthropic Claude |
240 | | - self._test_patched_bedrock_runtime_invoke_model( |
241 | | - model_id="anthropic.claude-v2:1", |
242 | | - max_tokens=512, |
243 | | - temperature=0.5, |
244 | | - top_p=0.999, |
245 | | - finish_reason="end_turn", |
246 | | - input_tokens=23, |
247 | | - output_tokens=36, |
248 | | - ) |
249 | | - |
250 | | - # BedrockRuntime - Meta LLama |
251 | | - self._test_patched_bedrock_runtime_invoke_model( |
252 | | - model_id="meta.llama2-13b-chat-v1", |
253 | | - max_tokens=512, |
254 | | - temperature=0.5, |
255 | | - top_p=0.9, |
256 | | - finish_reason="stop", |
257 | | - input_tokens=31, |
258 | | - output_tokens=36, |
259 | | - ) |
260 | | - |
261 | | - # BedrockRuntime - Cohere Command-r |
262 | | - cohere_input = "Hello, world" |
263 | | - cohere_output = "Goodbye, world" |
264 | | - |
265 | | - self._test_patched_bedrock_runtime_invoke_model( |
266 | | - model_id="cohere.command-r-v1:0", |
267 | | - max_tokens=512, |
268 | | - temperature=0.5, |
269 | | - top_p=0.75, |
270 | | - finish_reason="COMPLETE", |
271 | | - input_tokens=math.ceil(len(cohere_input) / 6), |
272 | | - output_tokens=math.ceil(len(cohere_output) / 6), |
273 | | - input_prompt=cohere_input, |
274 | | - output_prompt=cohere_output, |
275 | | - ) |
276 | | - |
277 | | - # BedrockRuntime - Mistral |
278 | | - msg = "Hello World" |
279 | | - mistral_input = f"<s>[INST] {msg} [/INST]" |
280 | | - mistral_output = "Goodbye, World" |
281 | | - |
282 | | - self._test_patched_bedrock_runtime_invoke_model( |
283 | | - model_id="mistral.mistral-7b-instruct-v0:2", |
284 | | - max_tokens=512, |
285 | | - temperature=0.5, |
286 | | - top_p=0.9, |
287 | | - finish_reason="stop", |
288 | | - input_tokens=math.ceil(len(mistral_input) / 6), |
289 | | - output_tokens=math.ceil(len(mistral_output) / 6), |
290 | | - input_prompt=mistral_input, |
291 | | - output_prompt=mistral_output, |
292 | | - ) |
293 | | - |
294 | 215 | # SecretsManager |
295 | 216 | self.assertTrue("secretsmanager" in _KNOWN_EXTENSIONS) |
296 | 217 | secretsmanager_attributes: Dict[str, str] = _do_extract_secretsmanager_attributes() |
@@ -374,130 +295,6 @@ def _test_patched_bedrock_instrumentation(self): |
374 | 295 | self.assertEqual(len(bedrock_sucess_attributes), 1) |
375 | 296 | self.assertEqual(bedrock_sucess_attributes["aws.bedrock.guardrail.id"], _BEDROCK_GUARDRAIL_ID) |
376 | 297 |
|
377 | | - def _test_patched_bedrock_runtime_invoke_model(self, **args): |
378 | | - model_id = args.get("model_id", None) |
379 | | - max_tokens = args.get("max_tokens", None) |
380 | | - temperature = args.get("temperature", None) |
381 | | - top_p = args.get("top_p", None) |
382 | | - finish_reason = args.get("finish_reason", None) |
383 | | - input_tokens = args.get("input_tokens", None) |
384 | | - output_tokens = args.get("output_tokens", None) |
385 | | - input_prompt = args.get("input_prompt", None) |
386 | | - output_prompt = args.get("output_prompt", None) |
387 | | - |
388 | | - def get_model_response_request(): |
389 | | - request_body = {} |
390 | | - response_body = {} |
391 | | - |
392 | | - if "amazon.titan" in model_id: |
393 | | - request_body = { |
394 | | - "textGenerationConfig": { |
395 | | - "maxTokenCount": max_tokens, |
396 | | - "temperature": temperature, |
397 | | - "topP": top_p, |
398 | | - } |
399 | | - } |
400 | | - |
401 | | - response_body = { |
402 | | - "inputTextTokenCount": input_tokens, |
403 | | - "results": [ |
404 | | - { |
405 | | - "tokenCount": output_tokens, |
406 | | - "outputText": "testing", |
407 | | - "completionReason": finish_reason, |
408 | | - } |
409 | | - ], |
410 | | - } |
411 | | - |
412 | | - if "amazon.nova" in model_id: |
413 | | - request_body = { |
414 | | - "inferenceConfig": { |
415 | | - "max_new_tokens": max_tokens, |
416 | | - "temperature": temperature, |
417 | | - "topP": top_p, |
418 | | - } |
419 | | - } |
420 | | - |
421 | | - response_body = { |
422 | | - "output": {"message": {"content": [{"text": ""}], "role": "assistant"}}, |
423 | | - "stopReason": finish_reason, |
424 | | - "usage": {"inputTokens": input_tokens, "outputTokens": output_tokens}, |
425 | | - } |
426 | | - |
427 | | - if "anthropic.claude" in model_id: |
428 | | - request_body = { |
429 | | - "anthropic_version": "bedrock-2023-05-31", |
430 | | - "max_tokens": max_tokens, |
431 | | - "temperature": temperature, |
432 | | - "top_p": top_p, |
433 | | - } |
434 | | - |
435 | | - response_body = { |
436 | | - "stop_reason": finish_reason, |
437 | | - "stop_sequence": None, |
438 | | - "usage": {"input_tokens": input_tokens, "output_tokens": output_tokens}, |
439 | | - } |
440 | | - |
441 | | - if "meta.llama" in model_id: |
442 | | - request_body = { |
443 | | - "max_gen_len": max_tokens, |
444 | | - "temperature": temperature, |
445 | | - "top_p": top_p, |
446 | | - } |
447 | | - |
448 | | - response_body = { |
449 | | - "prompt_token_count": input_tokens, |
450 | | - "generation_token_count": output_tokens, |
451 | | - "stop_reason": finish_reason, |
452 | | - } |
453 | | - |
454 | | - if "cohere.command" in model_id: |
455 | | - request_body = { |
456 | | - "message": input_prompt, |
457 | | - "max_tokens": max_tokens, |
458 | | - "temperature": temperature, |
459 | | - "p": top_p, |
460 | | - } |
461 | | - |
462 | | - response_body = { |
463 | | - "text": output_prompt, |
464 | | - "finish_reason": finish_reason, |
465 | | - } |
466 | | - |
467 | | - if "mistral" in model_id: |
468 | | - request_body = { |
469 | | - "prompt": input_prompt, |
470 | | - "max_tokens": max_tokens, |
471 | | - "temperature": temperature, |
472 | | - "top_p": top_p, |
473 | | - } |
474 | | - |
475 | | - response_body = {"outputs": [{"text": output_prompt, "stop_reason": finish_reason}]} |
476 | | - |
477 | | - json_bytes = json.dumps(response_body).encode("utf-8") |
478 | | - |
479 | | - return json.dumps(request_body), StreamingBody(BytesIO(json_bytes), len(json_bytes)) |
480 | | - |
481 | | - request_body, response_body = get_model_response_request() |
482 | | - |
483 | | - bedrock_runtime_attributes: Dict[str, str] = _do_extract_attributes_bedrock( |
484 | | - "bedrock-runtime", operation="InvokeModel", model_id=model_id, request_body=request_body |
485 | | - ) |
486 | | - bedrock_runtime_success_attributes: Dict[str, str] = _do_on_success_bedrock( |
487 | | - "bedrock-runtime", operation="InvokeModel", model_id=model_id, streaming_body=response_body |
488 | | - ) |
489 | | - |
490 | | - bedrock_runtime_attributes.update(bedrock_runtime_success_attributes) |
491 | | - |
492 | | - self.assertEqual(bedrock_runtime_attributes["gen_ai.system"], _GEN_AI_SYSTEM) |
493 | | - self.assertEqual(bedrock_runtime_attributes["gen_ai.request.model"], model_id) |
494 | | - self.assertEqual(bedrock_runtime_attributes["gen_ai.request.max_tokens"], max_tokens) |
495 | | - self.assertEqual(bedrock_runtime_attributes["gen_ai.request.temperature"], temperature) |
496 | | - self.assertEqual(bedrock_runtime_attributes["gen_ai.request.top_p"], top_p) |
497 | | - self.assertEqual(bedrock_runtime_attributes["gen_ai.usage.input_tokens"], input_tokens) |
498 | | - self.assertEqual(bedrock_runtime_attributes["gen_ai.usage.output_tokens"], output_tokens) |
499 | | - self.assertEqual(bedrock_runtime_attributes["gen_ai.response.finish_reasons"], [finish_reason]) |
500 | | - |
501 | 298 | def _test_patched_bedrock_agent_instrumentation(self): |
502 | 299 | """For bedrock-agent service, both extract_attributes and on_success provides attributes, |
503 | 300 | the attributes depend on the API being invoked.""" |
|
0 commit comments