Skip to content

Commit 18a2d88

Browse files
author
Bob Strahan
committed
Merge branch 'develop' of ssh.gitlab.aws.dev:genaiic-reusable-assets/engagement-artifacts/genaiic-idp-accelerator into develop
2 parents ac71466 + dccd0ef commit 18a2d88

File tree

1 file changed

+45
-9
lines changed
  • lib/idp_common_pkg/idp_common/bedrock

1 file changed

+45
-9
lines changed

lib/idp_common_pkg/idp_common/bedrock/client.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import random
1818
import socket
1919
from typing import Dict, Any, List, Optional, Union, Tuple
20+
from botocore.config import Config
2021
from botocore.exceptions import ClientError, ReadTimeoutError, ConnectTimeoutError, EndpointConnectionError
2122
from urllib3.exceptions import ReadTimeoutError as Urllib3ReadTimeoutError
2223
try:
@@ -73,8 +74,12 @@ def __init__(
7374
@property
7475
def client(self):
7576
"""Lazy-loaded Bedrock client."""
77+
config = Config(
78+
connect_timeout=10,
79+
read_timeout=120
80+
)
7681
if self._client is None:
77-
self._client = boto3.client('bedrock-runtime', region_name=self.region)
82+
self._client = boto3.client('bedrock-runtime', region_name=self.region, config=config)
7883
return self._client
7984

8085
def __call__(
@@ -445,10 +450,47 @@ def _invoke_with_retry(
445450

446451
return response_with_metering
447452

448-
except ClientError as e:
453+
# except ClientError as e:
454+
except ReadTimeoutError as e
455+
error_code = "ReadTimeoutError"
456+
error_message = str(e)
457+
458+
self._put_metric('BedrockThrottles', 1)
459+
460+
# Check if we've reached max retries
461+
if retry_count >= max_retries:
462+
logger.error(f"Max retries ({max_retries}) exceeded. Last error: {error_message}")
463+
self._put_metric('BedrockRequestsFailed', 1)
464+
self._put_metric('BedrockMaxRetriesExceeded', 1)
465+
raise
466+
467+
# Calculate backoff time
468+
backoff = self._calculate_backoff(retry_count)
469+
logger.warning(f"Bedrock throttling occurred (attempt {retry_count + 1}/{max_retries}). "
470+
f"Error: {error_message}. "
471+
f"Backing off for {backoff:.2f}s")
472+
473+
# Sleep for backoff period
474+
time.sleep(backoff)
475+
476+
# Recursive call with incremented retry count
477+
return self._invoke_with_retry(
478+
converse_params=converse_params,
479+
retry_count=retry_count + 1,
480+
max_retries=max_retries,
481+
request_start_time=request_start_time,
482+
last_exception=e,
483+
context=context
484+
)
485+
except Exception as e:
486+
# logger.error(f"Unexpected error invoking Bedrock: {str(e)}", exc_info=True)
487+
self._put_metric('BedrockRequestsFailed', 1)
488+
self._put_metric('BedrockUnexpectedErrors', 1)
489+
490+
# error_code = type(e)
449491
error_code = e.response['Error']['Code']
450492
error_message = e.response['Error']['Message']
451-
493+
452494
retryable_errors = [
453495
'ThrottlingException',
454496
'ServiceQuotaExceededException',
@@ -495,12 +537,6 @@ def _invoke_with_retry(
495537
self._put_metric('BedrockRequestsFailed', 1)
496538
self._put_metric('BedrockNonRetryableErrors', 1)
497539
raise
498-
499-
except Exception as e:
500-
logger.error(f"Unexpected error invoking Bedrock: {str(e)}", exc_info=True)
501-
self._put_metric('BedrockRequestsFailed', 1)
502-
self._put_metric('BedrockUnexpectedErrors', 1)
503-
raise
504540

505541
def get_guardrail_config(self) -> Optional[Dict[str, str]]:
506542
"""

0 commit comments

Comments
 (0)