-
Notifications
You must be signed in to change notification settings - Fork 0
/
boto3_sample.py
53 lines (43 loc) · 1.26 KB
/
boto3_sample.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
45
46
47
48
49
50
51
52
53
# https://www.youtube.com/watch?v=sSkj0C62mpw
import boto3
import json
from botocore.exceptions import ClientError
bedrock = boto3.client(service_name="bedrock-runtime", region_name='us-west-2')
modelId = "anthropic.claude-3-opus-20240229-v1:0"
accept = "application/json"
contentType = "application/json"
prompt = "こんにちは、あなたは誰ですか?"
request_body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 2048,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt,
},
],
}
],
}
try:
response = bedrock.invoke_model(
modelId=modelId,
body=json.dumps(request_body),
)
# Process and print the response
result = json.loads(response.get("body").read())
input_tokens = result["usage"]["input_tokens"]
output_tokens = result["usage"]["output_tokens"]
output_list = result.get("content", [])
for output in output_list:
print(output["text"])
except ClientError as err:
print(
"Couldn't invoke Claude 3. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise