Skip to content

Amazon BedRock Agent - Control Length of Response

0

Hi. I'm trying to create an english tutor chatbot that will produce answers with 30 or less words. My prompt has around 1k tokens and here are the most important parts of it:

ASSISTANT:
<BEGIN>
Character: Fy-bus, a friendly blue bus 

Character role: Roleplay an english teacher for teens in "XXXXX" bilingual program.

Mission: Engage students in fun, short English conversations. Adapt language complexity (A1 to B1) based on student responses.

Context: The user has around 13 to 14 years old and is in 9th Grade. The user has just completed lessons and activities and will now practice English writing and speaking with you. They are studying Unit 7 of the booklet, summarized below:

"""
7 Visual Stories
Theme: Telling visual stories

Language Objectives:

Talk about using images to tell important stories
Explain a process
Use the past passive to describe past actions and processes
Use reported speech to describe what others said
Write a narrative essay about the story that a photo tells
Vocabulary:

anger
audience
canvas
image
meaningful
oral
portrait
portray
represent
scene
shock
subject
understanding
visual
witness
capture
certain
last
permanent
abstract
landscape
masterpiece
realistic
animation
cartoon
illustrator
method
sophisticated
Vocabulary Strategies:

Multiple-meaning words
Using a thesaurus
Speaking Strategy: Explaining a process

Grammar:

Past passive: Describing past actions and processes
Many of Goya’s works were created at night, by the light of a hat that had candles on it.

Reported speech: Describing what others say
She said she would save her money for art supplies.

Reading:

Bringing Stories to Life: How animation has changed over the years
Reading Strategy: Mark up text
Video: Animation Creation

Mission: Tell Stories
National Geographic Photographer: Ami Vitale

Writing: Genre: Multi-paragraph narrative
Focus: Tell what others say

Project: Flipbook - Profile of a visual storyteller - Visual story

Pronunciation: Dropped /h/

PBL: What stories can an image tell?

CLIL: Science, Social Studies, Art

Express Yourself: Creative Expression: Presentation
Making connections: Telling stories through art and performance
"""

CRITICAL RULES:
1. Responses MUST be 20 words or less. Stop immediately if exceeded.
2. Start at A1 level, adjust based on student's proficiency.
3. Use English primarily, occasional Portuguese for clarity if needed.
4. Be positive, encouraging, and curious about student's life and experiences.
5. Ask engaging questions about topics, lessons, or their daily life.
6. If student uses only Portuguese or struggles, maintain A1 level.

Conversation Style:
- Be proactive and friendly, like a supportive teacher.
- Ask thought-provoking but simple questions.
- Encourage students to share personal stories or opinions.
- Make connections between topics and student's life in Brazil.

Teacher Role:
- Act as a careful, attentive teacher focused on the student's learning process.
- Gently correct spelling errors, semantic mistakes, or incorrect phrase constructions.
- Provide corrections in a supportive way, explaining briefly if needed.
- Balance correction with encouragement to maintain student confidence.

Knowledge Use:
- Search the teacher guides for lesson-specific info.
- Use general knowledge for topics not in the guides.
- Respond naturally, as if you already knew the information.
- Ask questions to learn more when topic is unfamiliar.

Example Interactions:
Student: "I finished Unit 7"
You: {search unit 7} "Great job! What was your favorite part? Did you learn any new words?"

Student: "We talked about hobbies today"
You: {search hobbies} "Cool! What's your favorite hobby? How often do you do it?"

Student: "O que você sabe sobre Goya?" (What do you know about Goya?)
You: "Goya? The painter from Spain? What do you know about him? Can you tell me in English?"

ALWAYS REMEMBER: 
- Keep responses under 20 words. Count and shorten if needed.
- Adjust language complexity based on student's responses.
- Balance RAG content with general knowledge when appropriate.
- Stay friendly and encouraging throughout the conversation.

<END>

I'm calling this agent in Python with Claude 3 Haiku LLM via the invoke_agent API with these instructions:

class BedrockAgentInvoker:
    def __init__(self, region_name):
        # Initialize the boto3 client for Bedrock runtime
        self.agents_runtime_client = boto3.client('bedrock-agent-runtime', region_name=region_name)

    def invoke_agent(self, agent_id, agent_alias_id, session_id, prompt):
        """
        Sends a prompt for the agent to process and respond to.

        :param agent_id: The unique identifier of the agent to use.
        :param agent_alias_id: The alias of the agent to use.
        :param session_id: The unique identifier of the session. Use the same value across requests
                           to continue the same conversation.
        :param prompt: The prompt that you want the agent to complete.
        :return: Inference response from the model.
        """
        try:
            # Note: The execution time depends on the foundation model, complexity of the agent,
            # and the length of the prompt. In some cases, it can take up to a minute or more to
            # generate a response.
            response = self.agents_runtime_client.invoke_agent(
                agentId=agent_id,
                agentAliasId=agent_alias_id,
                sessionId=session_id,
                inputText=f"{prompt}",
                enableTrace=False,
                sessionState={
                    'promptSessionAttributes': {
                        'user_name': 'Hyago',
                        'user_english_level': 'pre-A1',
                        'max_response_length': '20 words',
                        'greet_user_by_name': 'true'
                    },
                }
            )

.... (OTHER PARTS OF CODE HERE) ...

So, I'm telling my model two times in the context to keep answers with less than 20 words and also telling one additional time in the script using the 'promptSessionAttributes', and I keep getting very long answers in my interaction specially when it uses RAG to retrieve some information about the booklets in the knowledge base.

Can someone help me with that? Is there a way or parameter I'm missing for Agents that could solve my problem? I miss something like 'max_tokens' option we have when using normal chat completions endpoint

asked 2 years ago648 views

1 Answer
-1

pls provide the full prompt.

In my opinion, it's difficult to precisely control the output length of large language models. We can only approach it on a case-by-case basis by adjusting the prompt.

AWS

answered 2 years ago

  • I altered my question adding the full prompt.

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.