Titan Model¶
The TitanModel class provides an implementation for Amazon's Titan language models. It handles request formatting, response processing, and error handling specific to the Titan model family.
Class Definition¶
bedrock_swarm.models.titan.TitanModel(model_id: str)
¶
Bases: BedrockModel
Implementation for Amazon Titan models.
Source code in src/bedrock_swarm/models/base.py
Functions¶
format_request(message: str, system: Optional[str] = None, temperature: float = 0.7, max_tokens: Optional[int] = None) -> Dict[str, Any]
¶
Format a request for Titan.
| 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 |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If max_tokens exceeds the model's limit or temperature is invalid |
Source code in src/bedrock_swarm/models/titan.py
_extract_content(response: Dict[str, Any]) -> str
¶
Extract content from Titan response.
| PARAMETER | DESCRIPTION |
|---|---|
response
|
Raw response from Titan
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Extracted content as string |
| RAISES | DESCRIPTION |
|---|---|
ResponseParsingError
|
If content cannot be extracted |
Source code in src/bedrock_swarm/models/titan.py
invoke(message: str, **kwargs: Any) -> Dict[str, Any]
¶
Invoke the model with a message.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
The message to send to the model
TYPE:
|
**kwargs
|
Additional arguments to pass to format_request
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Model response |
| RAISES | DESCRIPTION |
|---|---|
ModelInvokeError
|
If there is an error invoking the model |
Source code in src/bedrock_swarm/models/titan.py
Model Variants¶
The Titan model family includes several variants with different capabilities and token limits:
- Titan Text Express
- Model ID:
amazon.titan-text-express-v1 - Max Tokens: 8,000
- Default Tokens: 2,048
-
Best for: General text generation and completion
-
Titan Text Lite
- Model ID:
amazon.titan-text-lite-v1 - Max Tokens: 4,000
- Default Tokens: 2,048
-
Best for: Lightweight text generation tasks
-
Titan Text Premier
- Model ID:
amazon.titan-text-premier-v1:0 - Max Tokens: 3,072
- Default Tokens: 2,048
- Best for: High-quality text generation
Request Format¶
The Titan model accepts requests in the following format:
{
"inputText": "Your message here",
"textGenerationConfig": {
"temperature": 0.7, # 0.0 to 1.0
"topP": 1,
"maxTokenCount": 2048,
"stopSequences": [],
},
}
Parameters¶
inputText(str): The message to send to the modeltemperature(float): Controls randomness in generation (0.0 to 1.0)maxTokenCount(int): Maximum number of tokens to generatesystem(Optional[str]): System prompt to prepend to the message
Response Format¶
The model returns responses in a streaming format:
{
"body": [
{
"chunk": {
"bytes": b'{"outputText": "Model response part 1"}'
}
},
{
"chunk": {
"bytes": b'{"outputText": "Model response part 2"}'
}
}
]
}
Error Handling¶
The Titan model implementation includes comprehensive error handling:
-
Token Validation
-
Temperature Validation
-
Response Parsing
Usage Example¶
from bedrock_swarm.models.factory import ModelFactory
# Create Titan model instance
model = ModelFactory.create_model("amazon.titan-text-express-v1")
# Format request with system prompt
request = model.format_request(
message="What is machine learning?",
system="You are a helpful AI assistant.",
temperature=0.7,
max_tokens=1000
)
# Invoke model with retry handling
try:
response = model.invoke(
client=client,
message="What is machine learning?"
)
print(response["content"])
except ModelInvokeError as e:
print(f"Model invocation failed: {e}")
Implementation Details¶
Request Formatting¶
The format_request method handles:
- System prompt integration
- Temperature validation
- Token count validation
- Request structure formatting
Response Processing¶
The _extract_content method:
- Decodes response chunks
- Extracts output text
- Handles JSON parsing errors
- Validates response format
Retry Logic¶
The model uses exponential backoff for retries: - Initial delay: 1 second - Maximum retries: 5 - Handles rate limiting - Retries on throttling errors
Testing¶
The Titan model implementation includes comprehensive tests:
- Request Formatting
- Temperature validation
- Token limit validation
-
System prompt integration
-
Response Processing
- Chunk processing
- Error handling
-
Content extraction
-
Error Scenarios
- Invalid JSON
- Missing fields
- API errors
- Rate limiting
See Also¶
- Base Model - Base model implementation details
- Model Factory - Model creation and configuration
- Claude Model - Claude model implementation