|
7 | 7 | from io import BytesIO |
8 | 8 | import json |
9 | 9 | from botocore.response import StreamingBody |
| 10 | +import math |
10 | 11 |
|
11 | 12 | import gevent.monkey |
12 | 13 | import pkg_resources |
@@ -290,39 +291,42 @@ def _test_patched_botocore_instrumentation(self): |
290 | 291 | self.assertEqual(bedrock_runtime_success_attributes["gen_ai.usage.output_tokens"], 36) |
291 | 292 | self.assertEqual(bedrock_runtime_success_attributes["gen_ai.response.finish_reasons"], ["end_turn"]) |
292 | 293 |
|
293 | | - #BedrockRuntime - Cohere Command Models _testing Pending |
294 | | - # self.assertTrue("bedrock-runtime" in _KNOWN_EXTENSIONS) |
295 | | - # request_body = { |
296 | | - # "max_tokens": 512, |
297 | | - # "temperature": 0.5, |
298 | | - # "p":0.75, |
299 | | - # } |
| 294 | + #BedrockRuntime - Cohere Command Models |
| 295 | + self.assertTrue("bedrock-runtime" in _KNOWN_EXTENSIONS) |
| 296 | + request_body = { |
| 297 | + 'message': "Describe the purpose of a 'hello world' program in one line.", |
| 298 | + "max_tokens": 512, |
| 299 | + "temperature": 0.5, |
| 300 | + "p":0.75, |
| 301 | + } |
300 | 302 |
|
301 | | - # bedrock_runtime_attributes: Dict[str, str] = _do_extract_attributes_bedrock( |
302 | | - # "bedrock-runtime", |
303 | | - # model_id="cohere.command", |
304 | | - # request_body=json.dumps(request_body) |
305 | | - # ) |
306 | | - # self.assertEqual(len(bedrock_runtime_attributes), 5) |
307 | | - # self.assertEqual(bedrock_runtime_attributes["gen_ai.system"], _GEN_AI_SYSTEM) |
308 | | - # self.assertEqual(bedrock_runtime_attributes["gen_ai.request.model"], "cohere.command") |
309 | | - # self.assertEqual(bedrock_runtime_attributes["gen_ai.request.max_tokens"], 512) |
310 | | - # self.assertEqual(bedrock_runtime_attributes["gen_ai.request.temperature"], 0.5) |
311 | | - # self.assertEqual(bedrock_runtime_attributes["gen_ai.request.top_p"], 0.75) |
312 | | - # response_body = { |
313 | | - # 'finish_reason': 'COMPLETE' |
314 | | - # } |
315 | | - # json_bytes = json.dumps(response_body).encode('utf-8') |
316 | | - # body_bytes = BytesIO(json_bytes) |
317 | | - # streaming_body = StreamingBody(body_bytes, len(json_bytes)) |
318 | | - # bedrock_runtime_success_attributes: Dict[str, str] = _do_on_success_bedrock( |
319 | | - # "bedrock-runtime", |
320 | | - # model_id="cohere.command", |
321 | | - # streaming_body=streaming_body |
322 | | - # ) |
323 | | - # self.assertEqual(bedrock_runtime_success_attributes["gen_ai.usage.input_tokens"], 23) |
324 | | - # self.assertEqual(bedrock_runtime_success_attributes["gen_ai.usage.output_tokens"], 36) |
325 | | - # self.assertEqual(bedrock_runtime_success_attributes["gen_ai.response.finish_reasons"], ["COMPLETE"]) |
| 303 | + bedrock_runtime_attributes: Dict[str, str] = _do_extract_attributes_bedrock( |
| 304 | + "bedrock-runtime", |
| 305 | + model_id="cohere.command", |
| 306 | + request_body=json.dumps(request_body) |
| 307 | + ) |
| 308 | + self.assertEqual(len(bedrock_runtime_attributes), 6) |
| 309 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.system"], _GEN_AI_SYSTEM) |
| 310 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.request.model"], "cohere.command") |
| 311 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.request.max_tokens"], 512) |
| 312 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.request.temperature"], 0.5) |
| 313 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.request.top_p"], 0.75) |
| 314 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.usage.input_tokens"], math.ceil(len(request_body['message'])/6)) |
| 315 | + response_body = { |
| 316 | + 'text': 'A "hello world" program serves as a simple introduction to programming, helping developers confirm their setup and test their coding environment.', |
| 317 | + 'finish_reason': 'COMPLETE' |
| 318 | + |
| 319 | + } |
| 320 | + json_bytes = json.dumps(response_body).encode('utf-8') |
| 321 | + body_bytes = BytesIO(json_bytes) |
| 322 | + streaming_body = StreamingBody(body_bytes, len(json_bytes)) |
| 323 | + bedrock_runtime_success_attributes: Dict[str, str] = _do_on_success_bedrock( |
| 324 | + "bedrock-runtime", |
| 325 | + model_id="cohere.command", |
| 326 | + streaming_body=streaming_body |
| 327 | + ) |
| 328 | + self.assertEqual(bedrock_runtime_success_attributes["gen_ai.usage.output_tokens"], math.ceil(len(response_body['text'])/6)) |
| 329 | + self.assertEqual(bedrock_runtime_success_attributes["gen_ai.response.finish_reasons"], ["COMPLETE"]) |
326 | 330 |
|
327 | 331 | #BedrockRuntime - AI21 Jamba Models |
328 | 332 | self.assertTrue("bedrock-runtime" in _KNOWN_EXTENSIONS) |
@@ -397,6 +401,45 @@ def _test_patched_botocore_instrumentation(self): |
397 | 401 | self.assertEqual(bedrock_runtime_success_attributes["gen_ai.usage.output_tokens"], 36) |
398 | 402 | self.assertEqual(bedrock_runtime_success_attributes["gen_ai.response.finish_reasons"], ["stop"]) |
399 | 403 |
|
| 404 | + #BedrockRuntime - Mistral Models |
| 405 | + self.assertTrue("bedrock-runtime" in _KNOWN_EXTENSIONS) |
| 406 | + msg="Hello, World" |
| 407 | + formatted_prompt = f"<s>[INST] {msg} [/INST]" |
| 408 | + request_body = { |
| 409 | + 'prompt': formatted_prompt, |
| 410 | + "max_tokens": 512, |
| 411 | + "temperature": 0.5, |
| 412 | + "top_p": 0.9, |
| 413 | + } |
| 414 | + |
| 415 | + bedrock_runtime_attributes: Dict[str, str] = _do_extract_attributes_bedrock( |
| 416 | + "bedrock-runtime", |
| 417 | + model_id="mistral", |
| 418 | + request_body=json.dumps(request_body) |
| 419 | + ) |
| 420 | + self.assertEqual(len(bedrock_runtime_attributes), 6) |
| 421 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.system"], _GEN_AI_SYSTEM) |
| 422 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.request.model"], "mistral") |
| 423 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.request.max_tokens"], 512) |
| 424 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.request.temperature"], 0.5) |
| 425 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.request.top_p"], 0.9) |
| 426 | + self.assertEqual(bedrock_runtime_attributes["gen_ai.usage.input_tokens"], math.ceil(len(request_body['prompt'])/6)) |
| 427 | + response_body = { |
| 428 | + 'outputs':[{ |
| 429 | + 'text': 'Goodbye, World', |
| 430 | + 'stop_reason': 'stop'}] |
| 431 | + } |
| 432 | + json_bytes = json.dumps(response_body).encode('utf-8') |
| 433 | + body_bytes = BytesIO(json_bytes) |
| 434 | + streaming_body = StreamingBody(body_bytes, len(json_bytes)) |
| 435 | + bedrock_runtime_success_attributes: Dict[str, str] = _do_on_success_bedrock( |
| 436 | + "bedrock-runtime", |
| 437 | + model_id="mistral", |
| 438 | + streaming_body=streaming_body |
| 439 | + ) |
| 440 | + #self.assertEqual(bedrock_runtime_success_attributes["gen_ai.usage.input_tokens"], 31) Srill have concerns regarging these lines |
| 441 | + self.assertEqual(bedrock_runtime_success_attributes["gen_ai.usage.output_tokens"], math.ceil(len(response_body['outputs'][0]['text'])/6)) |
| 442 | + self.assertEqual(bedrock_runtime_success_attributes["gen_ai.response.finish_reasons"], ["stop"]) |
400 | 443 |
|
401 | 444 | # SecretsManager |
402 | 445 | self.assertTrue("secretsmanager" in _KNOWN_EXTENSIONS) |
|
0 commit comments