Base Model¶
The BedrockModel class serves as the foundation for all model implementations in the Bedrock Swarm framework. It defines the core interface and shared functionality that all model classes must implement.
Class Documentation¶
bedrock_swarm.models.base.BedrockModel(model_id: str)
¶
Bases: ABC
Base class for Bedrock model implementations.
| PARAMETER | DESCRIPTION |
|---|---|
model_id
|
The Bedrock model ID to use
TYPE:
|
Source code in src/bedrock_swarm/models/base.py
Functions¶
get_model_id() -> str
¶
set_config(config: Dict[str, Any]) -> None
¶
Set model-specific configuration.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Configuration dictionary with model settings
TYPE:
|
validate_token_count(max_tokens: Optional[int] = None) -> int
¶
Validate and return the token count to use.
| PARAMETER | DESCRIPTION |
|---|---|
max_tokens
|
Requested maximum number of tokens. If not provided, uses the model's default token count.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The validated token count to use |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If max_tokens exceeds the model's limit |
Source code in src/bedrock_swarm/models/base.py
format_request(message: str, system: Optional[str] = None, temperature: float = 0.7, max_tokens: Optional[int] = None) -> Dict[str, Any]
abstractmethod
¶
Format a request for the model.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
The message to send to the model
TYPE:
|
system
|
Optional system prompt
TYPE:
|
temperature
|
Temperature for response generation (0.0 to 1.0)
TYPE:
|
max_tokens
|
Maximum number of tokens to generate
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Formatted request dictionary |
Source code in src/bedrock_swarm/models/base.py
process_response(response: Dict[str, Any]) -> AgentResponse
¶
Process a response from this specific model.
| PARAMETER | DESCRIPTION |
|---|---|
response
|
Raw response from the model
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AgentResponse
|
Processed response as either a message or tool call |
| RAISES | DESCRIPTION |
|---|---|
ResponseParsingError
|
If response cannot be parsed |
Source code in src/bedrock_swarm/models/base.py
_extract_content(response: Dict[str, Any]) -> str
abstractmethod
¶
Extract the content from a model response.
| PARAMETER | DESCRIPTION |
|---|---|
response
|
Raw response from the model
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Extracted content as string |
| RAISES | DESCRIPTION |
|---|---|
ResponseParsingError
|
If content cannot be extracted |
Source code in src/bedrock_swarm/models/base.py
_invoke_with_retry(client: BaseClient, request: Dict[str, Any], max_retries: int = 5, initial_delay: float = 1.0) -> Dict[str, Any]
¶
Invoke model with exponential backoff retry.
| PARAMETER | DESCRIPTION |
|---|---|
client
|
Bedrock client
TYPE:
|
request
|
Request body
TYPE:
|
max_retries
|
Maximum number of retries
TYPE:
|
initial_delay
|
Initial delay in seconds
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Model response |
| RAISES | DESCRIPTION |
|---|---|
ModelInvokeError
|
If all retries fail |
Source code in src/bedrock_swarm/models/base.py
invoke(client: BaseClient, message: str, system: Optional[str] = None, temperature: float = 0.7, max_tokens: Optional[int] = None) -> AgentResponse
¶
Invoke the model with a message.
This defines the high-level flow: 1. Format request (model-specific) 2. Call Bedrock with retry 3. Process response (model-specific)
Source code in src/bedrock_swarm/models/base.py
Usage Example¶
from bedrock_swarm.models.base import BedrockModel
class CustomModel(BedrockModel):
def __init__(self, model_id: str, max_tokens: int):
super().__init__(model_id=model_id, max_tokens=max_tokens)
async def invoke(self, request: dict) -> AsyncGenerator[str, None]:
# Custom implementation
pass
def format_request(self, request: dict) -> dict:
# Custom implementation
pass
Error Handling¶
The base model provides several error handling mechanisms:
- Token validation
- Request format validation
- Response parsing error handling
Common Methods¶
All model implementations inherit these common methods:
validate_token_count: Ensures the token count is within model limitsvalidate_temperature: Validates temperature values are within acceptable rangeparse_response: Handles response parsing with error handlingformat_request: Template method for request formattinginvoke: Abstract method for model invocation
See Also¶
- Claude Model - Claude model implementation
- Titan Model - Titan model implementation
- Model Factory - Model creation and configuration
- Models Overview - Overview of model system