-
Notifications
You must be signed in to change notification settings - Fork 2
/
bedrock.py
42 lines (34 loc) · 1.02 KB
/
bedrock.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
import boto3
from botocore.exceptions import ClientError
import os
from dotenv import load_dotenv
load_dotenv()
# Put your AWS credentials in a .env file
access_key_id = os.getenv("AWS_ACCESS_KEY_ID")
secret_access_key = os.getenv("AWS_SECRET_ACCESS_KEY")
client = boto3.client(
service_name="bedrock-runtime",
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
region_name="us-west-2",
)
# The model ID for the model you want to use
model_id = "us.meta.llama3-2-3b-instruct-v1:0"
# The message you want to send to the model
user_message = "Summarize AWS"
conversation = [
{
"role": "user",
"content": [{"text": user_message}],
}
]
try:
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9},
)
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)
except (ClientError, Exception) as e:
print(f"ERROR: {e}")