1 Answer
- Newest
- Most votes
- Most comments
1
Hello.
Why not try issuing instructions along with the question text when generating an answer, as shown below?
import boto3
bedrock_client = boto3.client(service_name='bedrock-agent-runtime')
model_id = "your_model_id_here" # Replace with your modelID document_uri = "your_s3_uri_here" # Replace with your S3 URI
Template = "You are a helpful assistant. When answering the following questions, please do not just list your answers from the data provided, but also express your answers in your own words. The content of the question is as follows:"
def retrieveAndGenerate(input_text, sourceType, model_id, document_s3_uri=None, data=None):
region = 'us-west-2'
model_arn = f'arn:aws:bedrock:{region}::foundation-model/{model_id}'
if sourceType == "S3":
return bedrock_client.retrieve_and_generate(
input={'text': Template + input_text},
retrieveAndGenerateConfiguration={
'type': 'EXTERNAL_SOURCES',
'externalSourcesConfiguration': {
'modelArn': model_arn,
'sources': [
{
"sourceType": sourceType,
"s3Location": {
"uri": document_s3_uri
}
}
]
}
}
)
else:
return bedrock_client.retrieve_and_generate(
input={'text': Template + input_text},
retrieveAndGenerateConfiguration={
'type': 'EXTERNAL_SOURCES',
'externalSourcesConfiguration': {
'modelArn': model_arn,
'sources': [
{
"sourceType": sourceType,
"byteContent": {
"identifier": "testFile.txt",
"contentType": "text/plain",
"data": data
}
}
]
}
}
)
response = retrieveAndGenerate( input_text="What is the main topic of this document?", sourceType="S3", model_id=model_id, document_s3_uri=document_uri )
print(response['output']['text'])
Alternatively, I think it's possible to use prompt templates to set up more detailed instructions.
https://docs.aws.amazon.com/bedrock/latest/userguide/advanced-prompts-configure.html
Relevant content
- asked 10 months ago
- asked 9 months ago
- asked 9 months ago
- AWS OFFICIALUpdated a year ago
thank you!