Created
December 6, 2023 16:12
-
-
Save benoitMariaux/6690f19399dfdb67c05af9cbf7021cbe to your computer and use it in GitHub Desktop.
Amazon Bedrock invocation example in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
my_session = boto3.session.Session() | |
my_region = my_session.region_name | |
# check you are in us-east-1 region (for now) | |
print(my_region) | |
bedrock_runtime = boto3.client('bedrock-runtime') | |
prompt = "Hello, how are you today?" | |
payload = json.dumps({ | |
"inputText": prompt, | |
"textGenerationConfig" : { | |
"maxTokenCount": 512, | |
"stopSequences": [], | |
"temperature": 0.1, | |
"topP": 0.9 | |
} | |
}) | |
print(payload) | |
response = bedrock_runtime.invoke_model( | |
body=payload, | |
modelId="amazon.titan-tg1-large", | |
accept="application/json", | |
contentType="application/json" | |
) | |
response_body = json.loads(response['body'].read()) | |
print(response_body['results'][0]['outputText']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment