-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbedrock_streaming.py
41 lines (33 loc) · 927 Bytes
/
bedrock_streaming.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
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'
)
# Define prompt and model parameters
prompt_data = """Write an essay about why someone should drink coffee"""
text_gen_config = {
"maxTokenCount": 1000,
"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 the model with a streamed response
response = bedrock.invoke_model_with_response_stream(
body=body,
modelId=model_id,
accept=accept,
contentType=content_type
)
for event in response['body']:
data = json.loads(event['chunk']['bytes'])
print(data['outputText'])