-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbedrock_sdk.py
44 lines (35 loc) · 925 Bytes
/
bedrock_sdk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import boto3
import json
#Create the connection to Bedrock
bedrock = boto3.client(
service_name='bedrock',
region_name='us-west-2',
endpoint_url='https://bedrock.us-west-2.amazonaws.com'
)
# List all foundation models
bedrock.list_foundation_models()
# Define prompt and model parameters
prompt_data = """Write me a poem about apples"""
text_gen_config = {
"maxTokenCount": 512,
"stopSequences": [],
"temperature": 0,
"topP": 0.9
}
body = json.dumps({
"inputText": prompt_data,
"textGenerationConfig": text_gen_config
})
model_id = 'amazon.titan-tg1-large'
accept = 'application/json'
content_type = 'application/json'
# Invoke model
response = bedrock.invoke_model(
body=body,
modelId=model_id,
accept=accept,
contentType=content_type
)
# Print response
response_body = json.loads(response['body'].read())
print(response_body['results'][0]['outputText'])