Skip to content

AWS BEDROCK AGENT - Make SessionAttributes parameter work!

1

I'm building a personalized chatbot with AWS Bedrock Agent and I was following this documentation for making it personalized:

https://docs.aws.amazon.com/pt_br/bedrock/latest/userguide/agents-session-state.html

So. I want that when the user is about to chat with the LLM (using Claude 3.5 Sonnet here), in his first request I use the sessionState with sessionAttributes parameters where I pass his name, age and previous conversations they had, so the LLM can 'know' the User before generating the first answer, based on past interactions and database information

Then I wrote this script:

import uuid
import boto3

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

    try:
        # Define session attributes to maintain user-specific context across interactions
        response = agents_runtime_client.invoke_agent(
                agentId=agent_id,
                agentAliasId=agent_alias_id,
                sessionId=session_id,
                sessionState={ 
                    'sessionAttributes': {
                        'userName': 'Yoran', # {user.name}
                        'userAge': '21',     # {year(today - user.birthdate)}
                        'summary': 'The user has talked a lot about Programming Languages like Python and Javascript' # {user.conversation_summary}
                    },
},
                inputText=f"{prompt}", # user_message
                enableTrace=False,
            )

        completion = ""
        for event in response.get("completion"):
            chunk = event["chunk"]
            completion += chunk["bytes"].decode()

    except Exception as e:
        print(f"Couldn't invoke agent. {str(e)}")
        raise

    return completion

# Define the chat parameters
agent_alias_id = "XXXXXXXX"
agent_id = "XXXXXXXX"
session_id = str(uuid.uuid4())
input_text = "What is my name??"


# Invoke the agent with sessionAttributes and promptSessionAttributes
response = invoke_agent(agent_id, agent_alias_id, session_id, input_text)
print(response)

So I'm passing userName as a paramater of sessionAttributes, but when I ask for my name in the first API call, the LLM return:

I'm sorry, but I don't have your name in my records. Would you like to tell me your name?

Am I using it wrong? Read the entire documentation and thought this was the best way of giving LLM a memory but I just can't get what I expect.

asked 2 years ago779 views

2 Answers
0

Instead of using sessionAttributes use promptSessionAttributes to pass the user information in your API call. Also you need to update your agent's instructions to include the $prompt_session_attributes$ variable which acts as a placeholder that gets replaced with the context you provide.

AWS
EXPERT

answered a year ago

0

Hi Ganesh, I would like to know the difference between "promptSessionAttributes" and "sessionAttributes". As I explored and understood, prompt session attributes are for short time and should be used to inject value for that particular turn or prompt. Where, sessionAttributes are long lived; to make it available during the whole conversation or session. So I think setting user details in session attributes should be encouraged rather than setting these in prompt session attribute.

answered a year ago

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.