|
| 1 | +import os |
| 2 | +import json |
| 3 | +import boto3 |
| 4 | +from unittest.mock import patch |
| 5 | +from langtrace_python_sdk import langtrace |
| 6 | +from examples.awsbedrock_examples.converse import use_converse, use_converse_stream |
| 7 | + |
| 8 | +def mock_invoke_model(*args, **kwargs): |
| 9 | + """Mock for standard completion with vendor attribute verification.""" |
| 10 | + # Verify the request body contains all expected attributes |
| 11 | + body = json.loads(kwargs.get('body', '{}')) |
| 12 | + |
| 13 | + assert body.get('max_tokens') == 4096, f"Incorrect max_tokens: {body.get('max_tokens')}" |
| 14 | + assert body.get('temperature') == 0.7, f"Incorrect temperature: {body.get('temperature')}" |
| 15 | + assert body.get('top_p') == 0.9, f"Incorrect top_p: {body.get('top_p')}" |
| 16 | + assert body.get('top_k') == 250, f"Incorrect top_k: {body.get('top_k')}" |
| 17 | + assert body.get('stop_sequences') == ["\n\nHuman:"], f"Incorrect stop_sequences: {body.get('stop_sequences')}" |
| 18 | + assert body.get('anthropic_version') == "bedrock-2024-02-20", f"Incorrect anthropic_version: {body.get('anthropic_version')}" |
| 19 | + assert kwargs.get('modelId') == "anthropic.claude-3-haiku-20240307-v1:0", f"Incorrect modelId: {kwargs.get('modelId')}" |
| 20 | + |
| 21 | + mock_response = { |
| 22 | + "completion": "Mocked response for testing with vendor attributes", |
| 23 | + "stop_reason": "stop_sequence", |
| 24 | + "usage": { |
| 25 | + "input_tokens": 10, |
| 26 | + "output_tokens": 20, |
| 27 | + "total_tokens": 30 |
| 28 | + } |
| 29 | + } |
| 30 | + return { |
| 31 | + 'body': json.dumps(mock_response).encode() |
| 32 | + } |
| 33 | + |
| 34 | +def mock_invoke_model_with_response_stream(*args, **kwargs): |
| 35 | + """Mock for streaming completion with vendor attribute verification.""" |
| 36 | + # Verify the request body contains all expected attributes |
| 37 | + body = json.loads(kwargs.get('body', '{}')) |
| 38 | + |
| 39 | + assert body.get('max_tokens') == 4096, f"Incorrect max_tokens: {body.get('max_tokens')}" |
| 40 | + assert body.get('temperature') == 0.7, f"Incorrect temperature: {body.get('temperature')}" |
| 41 | + assert body.get('top_p') == 0.9, f"Incorrect top_p: {body.get('top_p')}" |
| 42 | + assert body.get('top_k') == 250, f"Incorrect top_k: {body.get('top_k')}" |
| 43 | + assert body.get('stop_sequences') == ["\n\nHuman:"], f"Incorrect stop_sequences: {body.get('stop_sequences')}" |
| 44 | + assert body.get('anthropic_version') == "bedrock-2024-02-20", f"Incorrect anthropic_version: {body.get('anthropic_version')}" |
| 45 | + assert kwargs.get('modelId') == "anthropic.claude-3-haiku-20240307-v1:0", f"Incorrect modelId: {kwargs.get('modelId')}" |
| 46 | + |
| 47 | + chunks = [ |
| 48 | + { |
| 49 | + 'chunk': { |
| 50 | + 'bytes': json.dumps({ |
| 51 | + "completion": "Streaming chunk 1", |
| 52 | + "stop_reason": None |
| 53 | + }).encode() |
| 54 | + } |
| 55 | + }, |
| 56 | + { |
| 57 | + 'chunk': { |
| 58 | + 'bytes': json.dumps({ |
| 59 | + "completion": "Streaming chunk 2", |
| 60 | + "stop_reason": "stop_sequence" |
| 61 | + }).encode() |
| 62 | + } |
| 63 | + } |
| 64 | + ] |
| 65 | + return {'body': chunks} |
| 66 | + |
| 67 | +def run_test(): |
| 68 | + """Run tests for both standard and streaming completion.""" |
| 69 | + # Initialize Langtrace with API key from environment |
| 70 | + langtrace.init(api_key=os.environ["LANGTRACE_API_KEY"]) |
| 71 | + |
| 72 | + with patch("boto3.client") as mock_client: |
| 73 | + mock_instance = mock_client.return_value |
| 74 | + mock_instance.invoke_model = mock_invoke_model |
| 75 | + mock_instance.invoke_model_with_response_stream = mock_invoke_model_with_response_stream |
| 76 | + |
| 77 | + print("\nTesting AWS Bedrock instrumentation...") |
| 78 | + |
| 79 | + try: |
| 80 | + # Test standard completion |
| 81 | + print("\nTesting standard completion...") |
| 82 | + response = use_converse("Tell me about OpenTelemetry") |
| 83 | + print(f"Standard completion response: {response}") |
| 84 | + print("✓ Standard completion test passed with vendor attributes") |
| 85 | + |
| 86 | + # Test streaming completion |
| 87 | + print("\nTesting streaming completion...") |
| 88 | + chunks = [] |
| 89 | + for chunk in use_converse_stream("What is distributed tracing?"): |
| 90 | + chunks.append(chunk) |
| 91 | + print(f"Streaming chunk: {chunk}") |
| 92 | + assert len(chunks) == 2, f"Expected 2 chunks, got {len(chunks)}" |
| 93 | + print(f"✓ Streaming completion test passed with {len(chunks)} chunks") |
| 94 | + |
| 95 | + print("\n✓ All tests completed successfully!") |
| 96 | + except AssertionError as e: |
| 97 | + print(f"\n❌ Test failed: {str(e)}") |
| 98 | + raise |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + run_test() |
0 commit comments