55import {
66 BedrockRuntimeClient ,
77 InvokeModelCommand ,
8- InvokeModelCommandInput
9- } from ' @aws-sdk/client-bedrock-runtime' ;
8+ InvokeModelCommandInput ,
9+ } from " @aws-sdk/client-bedrock-runtime" ;
1010
1111export class BedrockEmbedder {
1212 private client : BedrockRuntimeClient ;
1313 private modelId : string ;
1414 private callCount : number = 0 ;
1515
16- constructor ( region : string = 'ap-south-1' , modelId : string = 'amazon.titan-embed-text-v2:0' ) {
16+ constructor ( region : string , modelId : string ) {
1717 this . client = new BedrockRuntimeClient ( { region } ) ;
1818 this . modelId = modelId ;
1919 }
@@ -33,42 +33,47 @@ export class BedrockEmbedder {
3333 try {
3434 const input : InvokeModelCommandInput = {
3535 modelId : this . modelId ,
36- contentType : ' application/json' ,
37- accept : ' application/json' ,
36+ contentType : " application/json" ,
37+ accept : " application/json" ,
3838 body : JSON . stringify ( {
3939 inputText : content ,
40- normalize : true
41- } )
40+ normalize : true ,
41+ } ) ,
4242 } ;
4343
4444 const command = new InvokeModelCommand ( input ) ;
4545 const response = await this . client . send ( command ) ;
4646
4747 const responseBody = JSON . parse (
48- new TextDecoder ( ) . decode ( response . body )
48+ new TextDecoder ( ) . decode ( response . body ) ,
4949 ) ;
5050
5151 this . callCount ++ ;
5252 return responseBody . embedding ;
5353 } catch ( error : any ) {
5454 lastError = error ;
5555 const isRetryable =
56- error ?. name === ' ThrottlingException' ||
57- error ?. name === ' ServiceUnavailableException' ||
58- error ?. name === ' ModelTimeoutException' ||
56+ error ?. name === " ThrottlingException" ||
57+ error ?. name === " ServiceUnavailableException" ||
58+ error ?. name === " ModelTimeoutException" ||
5959 error ?. $metadata ?. httpStatusCode === 429 ||
6060 error ?. $metadata ?. httpStatusCode >= 500 ;
6161
6262 if ( ! isRetryable || attempt === BedrockEmbedder . MAX_RETRIES ) {
63- console . error ( `Bedrock embedding failed (attempt ${ attempt + 1 } /${ BedrockEmbedder . MAX_RETRIES + 1 } ):` , error ) ;
63+ console . error (
64+ `Bedrock embedding failed (attempt ${ attempt + 1 } /${ BedrockEmbedder . MAX_RETRIES + 1 } ):` ,
65+ error ,
66+ ) ;
6467 throw error ;
6568 }
6669
6770 // Full jitter: randomize within [50%, 100%] of exponential delay
6871 // to prevent thundering herd when multiple concurrent calls retry
6972 const maxDelay = Math . min ( 1000 * Math . pow ( 2 , attempt ) , 16000 ) ;
70- const delay = Math . floor ( maxDelay / 2 + Math . random ( ) * maxDelay / 2 ) ;
71- console . warn ( ` Bedrock throttled (attempt ${ attempt + 1 } ), retrying in ${ delay } ms...` ) ;
73+ const delay = Math . floor ( maxDelay / 2 + ( Math . random ( ) * maxDelay ) / 2 ) ;
74+ console . warn (
75+ ` Bedrock throttled (attempt ${ attempt + 1 } ), retrying in ${ delay } ms...` ,
76+ ) ;
7277 await this . sleep ( delay ) ;
7378 }
7479 }
@@ -113,6 +118,6 @@ export class BedrockEmbedder {
113118 }
114119
115120 private sleep ( ms : number ) : Promise < void > {
116- return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
121+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
117122 }
118123}
0 commit comments