-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverse.py
47 lines (38 loc) · 1.23 KB
/
converse.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
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:
streaming_response = client.converse_stream(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9},
)
# Extract and print the streamed response text in real-time.
for chunk in streaming_response["stream"]:
if "contentBlockDelta" in chunk:
text = chunk["contentBlockDelta"]["delta"]["text"]
print(text, end="")
except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
exit(1)