Claude Model¶
The ClaudeModel class implements the interface for Amazon's Claude language models, providing access to various Claude model versions through Amazon Bedrock.
Class Documentation¶
bedrock_swarm.models.claude.ClaudeModel(model_id: str)
¶
Bases: BedrockModel
Implementation for Claude 3.5 models.
Source code in src/bedrock_swarm/models/base.py
Functions¶
get_model_id() -> str
¶
format_request(message: str, system: Optional[str] = None, temperature: float = 0.7, max_tokens: Optional[int] = None) -> Dict[str, Any]
¶
Format a request for Claude.
| 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/claude.py
_extract_content(response: Dict[str, Any]) -> str
¶
Extract content from Claude response.
| PARAMETER | DESCRIPTION |
|---|---|
response
|
Raw response from Claude
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Extracted content as string |
| RAISES | DESCRIPTION |
|---|---|
ResponseParsingError
|
If content cannot be extracted |
Source code in src/bedrock_swarm/models/claude.py
Model Variants¶
The following Claude model variants are supported:
- Claude 3.5 Sonnet: Latest general purpose model with strong performance across tasks
- Model ID:
us.anthropic.claude-3-5-sonnet-20241022-v2:0 - Best for: Complex reasoning, analysis, and generation tasks
Request Format¶
request = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 4096, # Optional, defaults to 4096
"temperature": 0.7, # Optional, defaults to 0.7
"messages": [
{
"role": "user",
"content": "Your message here"
}
]
}
Response Format¶
The model returns responses in a streaming format with content blocks:
async for chunk in model.invoke(request):
# Each chunk contains a content block delta
print(chunk) # Process each chunk as it arrives
Error Handling¶
The Claude model implementation includes comprehensive error handling for:
- Token validation (enforces model-specific limits)
- Temperature validation (must be between 0 and 1)
- Response parsing (handles various response formats)
- API errors (with automatic retries)
Usage Example¶
from bedrock_swarm.models import ClaudeModel
model = ClaudeModel()
request = model.format_request(
message="Explain quantum computing in simple terms.",
temperature=0.7,
max_tokens=1000
)
async for chunk in model.invoke(request):
print(chunk)
Implementation Details¶
The Claude model implementation includes:
- Request formatting according to Claude's specifications
- Response streaming with chunk processing
- Automatic retry logic for transient errors
- Comprehensive error handling
- Token limit enforcement
See Also¶
- Base Model - Base model implementation details
- Model Factory - Model creation and configuration
- Titan Model - Titan model implementation
- Models Overview - Overview of model system