8
8
from fmcore .experimental .proxy .rate_limit_proxy import RateLimitedProxy
9
9
from fmcore .experimental .types .provider_types import BedrockAccountConfig
10
10
11
- # Clearer alias
12
11
BedrockClientProxy : TypeAlias = RateLimitedProxy [ChatBedrockConverse ]
13
12
14
13
15
14
class BedrockFactory :
16
- """Factory class for creating Bedrock clients with additional functionalities like rate limiting."""
15
+ """Factory class for creating Bedrock clients with additional functionalities like rate limiting.
16
+
17
+ This class provides static methods to create and configure Amazon Bedrock clients
18
+ with built-in rate limiting capabilities. It handles the creation of both single
19
+ and multiple clients based on provided configurations.
20
+
21
+ The factory supports multiple AWS accounts and automatically configures rate limiting
22
+ based on account-specific parameters.
23
+ """
17
24
18
25
@staticmethod
19
- def create_bedrock_clients (* , llm_config : LLMConfig ) -> List [BedrockClientProxy ]:
20
- """Creates multiple Bedrock clients based on the provided configuration."""
26
+ def create_bedrock_clients (llm_config : LLMConfig ) -> List [BedrockClientProxy ]:
27
+ """Creates multiple Bedrock clients based on the provided configuration.
28
+
29
+ Args:
30
+ llm_config (LLMConfig): Configuration object containing LLM settings and provider parameters,
31
+ including account configurations and model parameters.
32
+
33
+ Returns:
34
+ List[BedrockClientProxy]: A list of rate-limited Bedrock client proxies, one for each
35
+ account specified in the configuration.
36
+
37
+ Example:
38
+ llm_config = LLMConfig(...)
39
+ clients = BedrockFactory.create_bedrock_clients(llm_config)
40
+ """
21
41
return [
22
- BedrockFactory ._create_bedrock_client_with_converse (account , llm_config )
42
+ BedrockFactory ._create_bedrock_client_with_converse (
43
+ account_config = account , llm_config = llm_config
44
+ )
23
45
for account in llm_config .provider_params .accounts
24
46
]
25
47
26
48
@staticmethod
27
49
def _create_bedrock_client_with_converse (
28
- account_config : BedrockAccountConfig , llm_config : LLMConfig
50
+ account_config : BedrockAccountConfig , llm_config : LLMConfig
29
51
) -> BedrockClientProxy :
30
- """Helper method to create a single Bedrock client with rate limiting."""
52
+ """Creates a single Bedrock client with rate limiting capabilities.
53
+
54
+ Args:
55
+ account_config (BedrockAccountConfig): Configuration for a specific AWS account,
56
+ including region, role ARN, and rate limits.
57
+ llm_config (LLMConfig): Configuration containing model settings and parameters.
58
+
59
+ Returns:
60
+ BedrockClientProxy: A rate-limited proxy wrapper around the Bedrock client.
61
+
62
+ Note:
63
+ The method configures rate limiting based on the account's specified rate limit
64
+ and wraps the ChatBedrockConverse client in a proxy for controlled access.
65
+ """
31
66
boto_client = BotoFactory .get_client (
32
67
service_name = "bedrock-runtime" ,
33
68
region = account_config .region ,
@@ -37,11 +72,11 @@ def _create_bedrock_client_with_converse(
37
72
converse_client = ChatBedrockConverse (
38
73
model_id = llm_config .model_id ,
39
74
client = boto_client ,
40
- ** llm_config .model_params .dict (exclude_none = True ),
75
+ ** llm_config .model_params .model_dump (exclude_none = True ),
41
76
)
42
77
43
- # Currently, We are using the off the shelf rate limiters provided by aiolimiter
44
- # TODO Implement custom rate limiters
78
+ # Create rate limiter based on account config
45
79
rate_limiter = AsyncLimiter (max_rate = account_config .rate_limit )
46
80
81
+ # Create proxy without weight
47
82
return BedrockClientProxy (client = converse_client , rate_limiter = rate_limiter )
0 commit comments