035. Python(Boto3)으로 예제 실행 (Run examples with the AWS SDK for Python (Boto3))
- 원문: English
- 위치: References/Advanced › Detailed getting started guide › Get started with the API › Run examples with the AWS SDK for Python (Boto3)
한 줄 요약
Boto3로 ListFoundationModels → InvokeModel → Converse를 실행해 권한·인증 설정을 확인하는 실습입니다. CLI 예제(034)의 Python 버전입니다.
핵심 내용
- 사전 조건: Bedrock 권한이 있는 사용자/역할(032), Boto3 설치·인증 완료
| 예제 | 클라이언트 | 호출 | 응답에서 텍스트 위치 |
|---|---|---|---|
| 모델 목록 | boto3.client("bedrock") | list_foundation_models() | response["modelSummaries"] |
| InvokeModel | boto3.client("bedrock-runtime") | invoke_model(modelId, body=json) | json.loads(body.read())["output"]["message"]["content"][0]["text"] |
| Converse | boto3.client("bedrock-runtime") | converse(modelId, messages, inferenceConfig) | response["output"]["message"]["content"][0]["text"] |
- 예제 모델:
amazon.nova-micro-v1:0, 추론 설정maxTokens=512, temperature=0.5, topP=0.9 - 목록 조회 예제는
us-east-1리전 기준입니다.
brt = boto3.client("bedrock-runtime")
response = brt.converse(
modelId="amazon.nova-micro-v1:0",
messages=[{"role": "user", "content": [{"text": "Describe the purpose of a 'hello world' program in one line."}]}],
inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9},
)
print(response["output"]["message"]["content"][0]["text"])
- 💡 지원되는 경우 Converse를 InvokeModel보다 권장합니다.
- InvokeModel은 모델 고유 요청 형식(Nova는
messages+inferenceConfig)을 JSON 문자열로 넣고, 응답body를 스트림에서 읽어 파싱해야 합니다. - 다른 언어 예제: Code examples for Amazon Bedrock using AWS SDKs
SA 관점
- InvokeModel은 모델마다 요청/응답 스키마가 달라 모델 교체 시 코드 수정이 필요합니다. 멀티 모델 전략을 가진 고객에게는 Converse 기반 구현을 권하세요.