跳至内容

Boto3 invoking of Bedrock agent using a wrong prompt

0

I have a bedrock agent deployed which has access to a knowledge base on postgres aurora serverless. I am also overridden the knowledgebase orchestration prompt in orchestration config. When I invoke this agent in bedrock console, it's working fine but when I do the same using boto3, it doesn't work as it uses some different prompt and fails.

I am not sure why boto3 invocations are not using the same prompt which is being used when I invoke the agent in bedrock console.

below is when I invoke from bedrock console.

"inputBodyJson": {
            "anthropic_version": "bedrock-2023-05-31",
            "system": "You are a question answering agent. I will provide you with a set of search results. The user will provide you with a question.\nIf the users asks something which is not documented, it's very important that you reply only with - \"I am sorry. I do have have complete details to answer your question. Is there something else I canhelp you with ?\" everytime. Do not change the wordings to of this reply in any way for this particular case.\n\n\nYour

below is when I invoke agent usint boto3. I don't know where this prompt is coming from.

 "inputBodyJson": {
            "anthropic_version": "bedrock-2023-05-31",
            "system": "You are a query creation agent. You have been provided with a function and a description of what it searches over. The user will provide you a question, and your job is to determine the optimal query to use based on the user's question. You must call the function in the format below: <function_calls> <invoke> <tool_name>$TOOL_NAME</tool_name> <parameters> <$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NA

Below is my code using boto3

bedrock_client = session.client("bedrock-agent-runtime")

try:
    agent_id = 'XYZ'
    agent_alias_id = 'ABC'
    response = bedrock_client.invoke_agent(
        agentId=agent_id,
        agentAliasId=agent_alias_id,
        sessionId='techlead',
        endSession=False,
        inputText="""
       what is product X?
        """
    )

    completion = ""
    print(response)
    for event in response.get("completion"):
        chunk = event.get("chunk")
        print('printing chunk',chunk)
        if chunk is None:
            continue
        completion = completion + chunk["bytes"].decode()

    print(completion)

except Exception as e:
    print(json.dumps({
        "level": "ERROR",
        "message": f"Error invoking Bedrock Agent: {str(e)}"
    }))
    raise e
已提问 8 个月前103 查看次数
1 回答
0

This issue occurs because there are two different ways to interact with Bedrock Agents, each using different prompts:

  1. Bedrock Console (UI) Interaction:
  • Uses the Knowledge Base orchestration prompt you configured
  • Follows the custom prompt settings you defined in the agent configuration
  1. Boto3 API Interaction:
  • By default, uses the standard API orchestration prompt
  • Doesn't automatically use the custom prompt settings from the console

To resolve this, you have a few options:

  1. Pass the custom prompt explicitly in your boto3 call:
response = bedrock_client.invoke_agent(
    agentId=agent_id,
    agentAliasId=agent_alias_id,
    sessionId='techlead',
    endSession=False,
    inputText="what is product X?",
    orchestrationConfiguration={
        "promptOverride": "Your custom prompt here"
    }
)
  1. Use the prepare_agent API call first:
prepare_response = bedrock_client.prepare_agent(
    agentId=agent_id,
    agentAliasId=agent_alias_id,
    sessionId='techlead',
    orchestrationConfiguration={
        "promptOverride": "Your custom prompt here"
    }
)

response = bedrock_client.invoke_agent(
    agentId=agent_id,
    agentAliasId=agent_alias_id,
    sessionId='techlead',
    endSession=False,
    inputText="what is product X?"
)
  1. Update your agent's default orchestration configuration through the AWS CLI or API, so it's consistent across all interaction methods.

The key is to ensure the orchestration configuration is properly set when making API calls. The console automatically handles this for you, but with boto3, you need to explicitly manage it.

Also, make sure you're using the latest version of boto3, as earlier versions might have different behavior regarding prompt handling.

AWS
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。