AWS Amazon Titan
Amazon Titan
When it comes to Amazon Titan, the format of the prompt depends on the intended purpose. As you will recall from a previous lesson, the basic elements of a prompt are as follows:
- Instruction: Describes what you are asking of the model, such as summarization or question answering.
- Context: Sets the scene for the model, such as making suggestions like this one: "The following data is a medical text."
- Input: Provides the actual input text for summaries or retrieval tasks.
- Output indicator: Constrains the output from the model. For example, you may want the model to limit the output to a certain number of words or sentences while summarizing a passage.
Review the following best practices for using Amazon Titan.
Specify output length
Be specific about the number of sentences, bullet points, and paragraphs you want the model to generate in the output. Instead of asking for a response with a certain number of words, instruct the model to return, for example, "a summary in exactly three sentences" or "a list with four bullet points." Clearly stating these format requirements will help achieve more consistent and controlled results from Amazon Titan and other language models.
Provide simple, clear, and complete instructions
Amazon Titan generates the best responses when you include an unambiguous expectation of the task in your prompt.
What is the above text about?
a) biology
b) history
c) geology
Add context to the prompt for more detailed answers
You achieve better results when you add context and provide the instruction or output indicator at the end of the prompt. The following example demonstrates this approach.
- Serving size: 10
- Ingredients: no egg
Include a recipe.
Ingredients:
- 1 1/2 cups all-purpose flour
- 1 cup granulated sugar
- 1/2 cup unsweetened cocoa powder
- 1 tsp baking powder
- 1/2 tsp baking soda
- 1/2 tsp salt
- 1 cup milk
- 1/2 cup vegetable oil
- 2 tsp vanilla extract
- 1 cup boiling water
- Preheat the oven to 350°F (175°C). Grease a 9-inch cake pan with butter or oil and dust with flour.
- In a large bowl, whisk together the flour, sugar, cocoa powder, baking powder, baking soda, and salt until well combined.
- In a separate bowl, mix together the milk, vegetable oil, and vanilla extract until well combined.
- Add the boiling water to the milk mixture and stir until combined.
- Add the wet ingredients to the dry ingredients and mix until just combined. The batter may be thin and runny.
- Pour the batter into the prepared cake pan and spread evenly.
- Bake for 30–35 minutes, or until a toothpick inserted into the center of the cake comes out clean.
- Remove the cake from the oven and let it cool in the pan for 10 minutes.
- Carefully remove the cake from the pan and let it cool completely on a wire rack.
- Serve and enjoy your delicious eggless chocolate cake!
Provide default output when necessary
You can provide default output for the model to present if it's unsure about how to answer.
This prevents the model from returning answers that sound correct but might not answer the question.
The following is an example.
If you don't know a proof, respond by saying "I don't know."
Use separator characters for API calls
For Amazon Titan models, adding \n (new line character) at the end of a prompt will likely improve the model's performance. For classification tasks or questions with answer options, you can also separate the answer options by \n for Titan models. The following example shows how newline characters between choices and at the end of a prompt help you produce the desired response with Amazon Titan.
Who was Terry Whitlock?
Choose one of the options below.
a) physicist
b) farmer
c) sailor
Personalize responses
You can personalize responses by including a role in your prompt. This technique helps you generate personalized responses. The following example demonstrates this approach.
I just found out about your new job, and I'm so happy for you! Congratulations! You deserve this opportunity since you worked so hard to get it. I know you'll do great and make a positive impact on your team. I'm here to support you every step of the way. Wishing you all the best in this exciting new chapter of your life!
Cheers,
[Your Name]
Generate code
You can generate code samples, such as SQL, Python, HTML, or Java. The following example shows this capability.
# Python
import boto3
# Creates a boto3 client object
s3 = boto3.client('s3')
def download_file(bucket_name, object_key, file_path):
try:
# Downloads the S3 file to the specified file path
s3.download_file(bucket_name, object_key, file_path)
except Exception as ex:
print(ex)
# Specifies the values of bucket_name, object_key, and file_path
bucket_name = "samplebucketname"
object_key = "sampleobjectkey"
file_path = "/samplefilepath"
download_file(bucket_name, object_key, file_path)
The given code uses the download_file function to download an S3 file to the local disk. The download_file function takes the bucket name, object key, and file path as input parameters. The bucket name is the name of the S3 bucket where the file is stored, the object key is the file identifier of the file within the bucket, and the file path is the destination path on the local disk to save the file when downloaded. The function uses the download_file method of the S3 client to download the file from the specified bucket and write it to the specified file path.