Model Factory¶
The ModelFactory class provides a centralized way to create and manage model instances in the Bedrock Swarm framework. It handles model registration, configuration, and instantiation.
Class Documentation¶
Factory for creating Bedrock model implementations.
Functions¶
create_model(model_id: str) -> BedrockModel
classmethod
¶
Create a model implementation for the given model ID.
| PARAMETER | DESCRIPTION |
|---|---|
model_id
|
The Bedrock model ID
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BedrockModel
|
An instance of the appropriate model implementation |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the model ID is not supported |
Source code in src/bedrock_swarm/models/factory.py
register_model(family: str, version: str, model_class: Type[BedrockModel], config: Dict[str, Any]) -> None
classmethod
¶
Register a new model implementation.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
Model family (e.g., "us.anthropic.claude-3-5-sonnet")
TYPE:
|
version
|
Model version (e.g., "20241022-v2:0")
TYPE:
|
model_class
|
Model implementation class
TYPE:
|
config
|
Model configuration (max_tokens, default_tokens, etc.)
TYPE:
|
Source code in src/bedrock_swarm/models/factory.py
get_supported_models() -> Dict[str, Dict[str, Dict[str, Any]]]
classmethod
¶
Get all supported models.
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Dict[str, Dict[str, Any]]]
|
Dictionary of supported model families, versions, and their configurations |
Source code in src/bedrock_swarm/models/factory.py
Model Registry¶
The model factory maintains a registry of available models and their configurations:
BEDROCK_MODEL_REGISTRY = {
"anthropic.claude-3-sonnet-20240229-v1:0": {
"class": "ClaudeModel",
"max_tokens": 200000
},
"anthropic.claude-v2:1": {
"class": "ClaudeModel",
"max_tokens": 100000
},
"amazon.titan-text-express-v1": {
"class": "TitanModel",
"max_tokens": 8000
},
"amazon.titan-text-lite-v1": {
"class": "TitanModel",
"max_tokens": 4000
}
}
Usage Example¶
from bedrock_swarm.models import ModelFactory
# Create a model instance
model = ModelFactory.create_model("anthropic.claude-3-sonnet-20240229-v1:0")
# Use the model
request = {
"prompt": "Hello, how can I help you today?",
"temperature": 0.7
}
async for chunk in model.invoke(request):
print(chunk)
Error Handling¶
The factory includes error handling for:
- Invalid model IDs
- Missing model configurations
- Model initialization errors
Implementation Details¶
The factory implementation includes:
- Model registration system
- Configuration validation
- Dynamic model instantiation
- Error handling and logging
- Model configuration management