Skip to content

Commit 8133924

Browse files
authored
Python: Restore Titan model example. (#7621)
1 parent 32d0a18 commit 8133924

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,3 +1483,20 @@ bedrock-runtime_Scenario_GenerateVideos_NovaReel:
14831483
- bedrock-runtime.java2.NovaReel.VideoGeneration
14841484
services:
14851485
bedrock-runtime: {StartAsyncInvoke, GetAsyncInvoke}
1486+
1487+
bedrock-runtime_InvokeModel_TitanText:
1488+
title: Invoke Amazon Titan Text models on &BR; using the Invoke Model API
1489+
title_abbrev: "InvokeModel"
1490+
synopsis: send a text message to Amazon Titan Text, using the Invoke Model API.
1491+
category: Amazon Titan Text
1492+
languages:
1493+
Python:
1494+
versions:
1495+
- sdk_version: 3
1496+
github: python/example_code/bedrock-runtime
1497+
excerpts:
1498+
- description: Use the Invoke Model API to send a text message.
1499+
snippet_tags:
1500+
- python.example_code.bedrock-runtime.InvokeModel_TitanText
1501+
services:
1502+
bedrock-runtime: {InvokeModel}

python/example_code/bedrock-runtime/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ functions within the same service.
6464

6565
- [InvokeModel](models/amazon_titan_image_generator/invoke_model.py#L4)
6666

67+
### Amazon Titan Text
68+
69+
- [InvokeModel](models/amazon_titan_text/invoke_model.py#L4)
70+
6771
### Amazon Titan Text Embeddings
6872

6973
- [InvokeModel](models/amazon_titan_text_embeddings/invoke_model.py#L4)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# snippet-start:[python.example_code.bedrock-runtime.InvokeModel_TitanText]
5+
# Use the native inference API to send a text message to Amazon Titan Text.
6+
7+
import boto3
8+
import json
9+
10+
from botocore.exceptions import ClientError
11+
12+
# Create a Bedrock Runtime client in the AWS Region of your choice.
13+
client = boto3.client("bedrock-runtime", region_name="us-east-1")
14+
15+
# Set the model ID, e.g., Titan Text Premier.
16+
model_id = "amazon.titan-text-premier-v1:0"
17+
18+
# Define the prompt for the model.
19+
prompt = "Describe the purpose of a 'hello world' program in one line."
20+
21+
# Format the request payload using the model's native structure.
22+
native_request = {
23+
"inputText": prompt,
24+
"textGenerationConfig": {
25+
"maxTokenCount": 512,
26+
"temperature": 0.5,
27+
},
28+
}
29+
30+
# Convert the native request to JSON.
31+
request = json.dumps(native_request)
32+
33+
try:
34+
# Invoke the model with the request.
35+
response = client.invoke_model(modelId=model_id, body=request)
36+
37+
except (ClientError, Exception) as e:
38+
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
39+
exit(1)
40+
41+
# Decode the response body.
42+
model_response = json.loads(response["body"].read())
43+
44+
# Extract and print the response text.
45+
response_text = model_response["results"][0]["outputText"]
46+
print(response_text)
47+
48+
# snippet-end:[python.example_code.bedrock-runtime.InvokeModel_TitanText]

0 commit comments

Comments
 (0)