|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 6 | +# with the License. A copy of the License is located at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 11 | +# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 12 | +# and limitations under the License. |
| 13 | +# |
| 14 | +import json |
| 15 | +import logging |
| 16 | +import warnings |
| 17 | +import os |
| 18 | +import time |
| 19 | +from aws_sdk_bedrock_runtime.client import ( |
| 20 | + BedrockRuntimeClient, |
| 21 | + InvokeModelWithBidirectionalStreamOperationInput, |
| 22 | +) |
| 23 | +from aws_sdk_bedrock_runtime.models import ( |
| 24 | + InvokeModelWithBidirectionalStreamInputChunk, |
| 25 | + BidirectionalInputPayloadPart, |
| 26 | +) |
| 27 | +from aws_sdk_bedrock_runtime.config import ( |
| 28 | + Config, |
| 29 | + HTTPAuthSchemeResolver, |
| 30 | + SigV4AuthScheme, |
| 31 | +) |
| 32 | +from smithy_aws_core.credentials_resolvers.environment import ( |
| 33 | + EnvironmentCredentialsResolver, |
| 34 | +) |
| 35 | + |
| 36 | +# Configure logging |
| 37 | +logger = logging.getLogger(__name__) |
| 38 | + |
| 39 | +# Suppress warnings |
| 40 | +warnings.filterwarnings("ignore") |
| 41 | + |
| 42 | + |
| 43 | +class BedrockInteractClient: |
| 44 | + """Client for interacting with AWS Bedrock Nova Sonic model""" |
| 45 | + |
| 46 | + def __init__(self, model_id="amazon.nova-sonic-v1:0", region="us-east-1"): |
| 47 | + """Initialize the Bedrock client. |
| 48 | +
|
| 49 | + Args: |
| 50 | + model_id (str): Bedrock model ID to use |
| 51 | + region (str): AWS region |
| 52 | + """ |
| 53 | + self.model_id = model_id |
| 54 | + self.region = region |
| 55 | + self.bedrock_client = None |
| 56 | + self.last_credential_check = 0 |
| 57 | + self.credential_signal_file = "/tmp/credentials_refreshed" |
| 58 | + logger.info( |
| 59 | + f"Initializing BedrockInteractClient [model_id={model_id}, region={region}]" |
| 60 | + ) |
| 61 | + |
| 62 | + def _check_credential_refresh(self): |
| 63 | + """Check if credentials have been refreshed and recreate client if needed.""" |
| 64 | + try: |
| 65 | + if os.path.exists(self.credential_signal_file): |
| 66 | + signal_mtime = os.path.getmtime(self.credential_signal_file) |
| 67 | + |
| 68 | + if signal_mtime > self.last_credential_check: |
| 69 | + # A real credential refresh from background daemon |
| 70 | + logger.info("Credential refresh signal detected - recreating Bedrock client") |
| 71 | + self.bedrock_client = None # Force recreation |
| 72 | + self.last_credential_check = signal_mtime |
| 73 | + # Remove the signal file after processing |
| 74 | + os.remove(self.credential_signal_file) |
| 75 | + except Exception as e: |
| 76 | + logger.error(f"Error checking credential refresh signal: {e}") |
| 77 | + |
| 78 | + def initialize_client(self): |
| 79 | + """Initialize the Bedrock client.""" |
| 80 | + # Check if credentials were refreshed |
| 81 | + self._check_credential_refresh() |
| 82 | + |
| 83 | + if self.bedrock_client is not None: |
| 84 | + return True |
| 85 | + |
| 86 | + logger.info(f"Initializing Bedrock client for region {self.region}") |
| 87 | + try: |
| 88 | + config = Config( |
| 89 | + endpoint_uri=f"https://bedrock-runtime.{self.region}.amazonaws.com", |
| 90 | + region=self.region, |
| 91 | + aws_credentials_identity_resolver=EnvironmentCredentialsResolver(), |
| 92 | + http_auth_scheme_resolver=HTTPAuthSchemeResolver(), |
| 93 | + http_auth_schemes={"aws.auth#sigv4": SigV4AuthScheme()}, |
| 94 | + ) |
| 95 | + self.bedrock_client = BedrockRuntimeClient(config=config) |
| 96 | + logger.info( |
| 97 | + "Bedrock client initialized successfully with EnvironmentCredentialsResolver" |
| 98 | + ) |
| 99 | + return True |
| 100 | + except Exception as e: |
| 101 | + logger.error( |
| 102 | + f"Failed to initialize Bedrock client: {str(e)}", exc_info=True |
| 103 | + ) |
| 104 | + return False |
| 105 | + |
| 106 | + async def refresh_credentials_immediately(self): |
| 107 | + """Refresh credentials immediately by calling the container metadata endpoint""" |
| 108 | + try: |
| 109 | + logger.info("Refreshing credentials due to ExpiredToken...") |
| 110 | + # Get credentials from ECS container metadata endpoint |
| 111 | + uri = os.environ.get('AWS_CONTAINER_CREDENTIALS_RELATIVE_URI') |
| 112 | + if not uri: |
| 113 | + logger.error("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI not found in environment") |
| 114 | + return False |
| 115 | + |
| 116 | + import aiohttp |
| 117 | + try: |
| 118 | + async with aiohttp.ClientSession() as session: |
| 119 | + async with session.get(f"http://169.254.170.2{uri}", timeout=2) as response: |
| 120 | + if not response.ok: |
| 121 | + logger.error(f"Failed to fetch credentials: {response.status}") |
| 122 | + return False |
| 123 | + |
| 124 | + creds = await response.json() |
| 125 | + except ImportError: |
| 126 | + # Fall back to requests if aiohttp is not available |
| 127 | + import requests |
| 128 | + response = requests.get(f"http://169.254.170.2{uri}", timeout=2) |
| 129 | + if not response.ok: |
| 130 | + logger.error(f"Failed to fetch credentials: {response.status_code}") |
| 131 | + return False |
| 132 | + |
| 133 | + creds = response.json() |
| 134 | + |
| 135 | + os.environ['AWS_ACCESS_KEY_ID'] = creds['AccessKeyId'] |
| 136 | + os.environ['AWS_SECRET_ACCESS_KEY'] = creds['SecretAccessKey'] |
| 137 | + os.environ['AWS_SESSION_TOKEN'] = creds['Token'] |
| 138 | + |
| 139 | + logger.info(f"Successfully refreshed credentials, new key ends with: ...{creds['AccessKeyId'][-4:]}") |
| 140 | + |
| 141 | + # Force client recreation on next use |
| 142 | + self.bedrock_client = None |
| 143 | + return True |
| 144 | + except Exception as e: |
| 145 | + logger.error(f"Error refreshing credentials: {str(e)}") |
| 146 | + return False |
| 147 | + |
| 148 | + async def create_stream(self): |
| 149 | + """Create a bidirectional stream with Bedrock. |
| 150 | +
|
| 151 | + Returns: |
| 152 | + stream: Bedrock bidirectional stream |
| 153 | + """ |
| 154 | + logger.info(f"Creating bidirectional stream for model {self.model_id}") |
| 155 | + try: |
| 156 | + if not self.bedrock_client: |
| 157 | + if not self.initialize_client(): |
| 158 | + raise Exception("Failed to initialize Bedrock client") |
| 159 | + |
| 160 | + stream = await self.bedrock_client.invoke_model_with_bidirectional_stream( |
| 161 | + InvokeModelWithBidirectionalStreamOperationInput(model_id=self.model_id) |
| 162 | + ) |
| 163 | + logger.info("Stream initialized successfully") |
| 164 | + return stream |
| 165 | + except Exception as e: |
| 166 | + if "ExpiredToken" in str(e): |
| 167 | + current_key = os.environ.get('AWS_ACCESS_KEY_ID', '') |
| 168 | + logger.warning(f"ExpiredToken error occurred with credential ending: ...{current_key[-4:] if len(current_key) >= 4 else 'NONE'}") |
| 169 | + |
| 170 | + # Try to refresh and retry |
| 171 | + if await self.refresh_credentials_immediately(): |
| 172 | + logger.info("Retrying stream creation with new credentials") |
| 173 | + # Recursive retry once |
| 174 | + return await self.create_stream() |
| 175 | + |
| 176 | + logger.error(f"Failed to initialize stream: {str(e)}", exc_info=True) |
| 177 | + raise |
| 178 | + |
| 179 | + async def send_event(self, stream, event_data): |
| 180 | + """Send an event to the Bedrock stream. |
| 181 | +
|
| 182 | + Args: |
| 183 | + stream: Bedrock bidirectional stream |
| 184 | + event_data (dict): Event data to send |
| 185 | +
|
| 186 | + Returns: |
| 187 | + bool: True if successful, False otherwise |
| 188 | + """ |
| 189 | + try: |
| 190 | + list(event_data.get("event", {}).keys())[ |
| 191 | + 0 |
| 192 | + ] if "event" in event_data else "unknown" |
| 193 | + |
| 194 | + event_json = json.dumps(event_data) |
| 195 | + event = InvokeModelWithBidirectionalStreamInputChunk( |
| 196 | + value=BidirectionalInputPayloadPart(bytes_=event_json.encode("utf-8")) |
| 197 | + ) |
| 198 | + await stream.input_stream.send(event) |
| 199 | + return True |
| 200 | + except Exception as e: |
| 201 | + logger.error(f"Error sending event: {str(e)}", exc_info=True) |
| 202 | + return False |
| 203 | + |
| 204 | + async def close_stream(self, stream): |
| 205 | + """Close the Bedrock stream. |
| 206 | +
|
| 207 | + Args: |
| 208 | + stream: Bedrock bidirectional stream |
| 209 | +
|
| 210 | + Returns: |
| 211 | + bool: True if successful, False otherwise |
| 212 | + """ |
| 213 | + try: |
| 214 | + if stream: |
| 215 | + await stream.input_stream.close() |
| 216 | + logger.info("Stream closed successfully") |
| 217 | + return True |
| 218 | + return False |
| 219 | + except Exception as e: |
| 220 | + logger.error(f"Error closing stream: {str(e)}", exc_info=True) |
| 221 | + return False |
0 commit comments