make AWS Bedrock chatbot sound like person?

0

Hi everyone,

I'm currently using the code* from this AWS article https://aws.amazon.com/blogs/machine-learning/knowledge-bases-in-amazon-bedrock-now-simplifies-asking-questions-on-a-single-document/ to query my own data.

When I ask the chatbot a question such as "what makes my company better than its competitors?", it simply regurgitates word for word what the answer is from the data file provided to it. It doesn't put the answer into its own words, and it doesn't sound like a human at all (i.e., nothing at the start or end of the answer saying something like "sure, based on the data provided, the answer is.... I hope this is helpful. Let me know if there's anything else I can help with"), it literally just spits out the exact same text from the data file provided to it.

How do I make it sound more human-like and make it re-write the answer into its own words?

Thank you!!!

  • Here is the code from the article provided above: 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

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': 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': 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'])

1 Answer
1
Accepted Answer

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

profile picture
EXPERT
answered 10 months ago
profile picture
EXPERT
reviewed 10 months ago
  • thank you!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions