Skip to content

How should the knowledge base search operation be handled within a Bedrock Agent custom orchestration Lambda?

0

According to the sample, https://github.com/aws-samples/amazon-bedrock-samples/tree/main/agents-and-function-calling/bedrock-agents/features-examples/14-create-agent-with-custom-orchestration the lambda_rewoo.py file parses the plan generated by the foundation model to determine which tools to use — which seem to correspond to the functions defined in the action groups. However, I don’t see how it handles the part related to searching the knowledge base. The sample code demonstrates using the ReWOO approach to handle questions about the menu and restaurant reservations. However, It only sets the KB config when creating the agent. I’m curious how knowledge base searching is handled in custom orchestration.

1 Answer
0

When implementing a custom orchestration Lambda for a Bedrock Agent that uses a knowledge base, the knowledge base search operation is handled through the orchestration parsed response structure.

In a custom parser Lambda function for orchestration, you need to include the agentKnowledgeBase field in the responseDetails when the agent determines that a knowledge base search is needed. This field should be compatible with the KNOWLEDGE_BASE invocation type and contain:

  1. knowledgeBaseId - The unique identifier of the knowledge base
  2. searchQuery - The query to send to the knowledge base in the value field

When the agent processes this response, it will automatically perform the knowledge base search using the provided query. After retrieving information from the knowledge base, the agent can then use this information to formulate its final response.

For the final response that includes knowledge base information, you should use the agentFinalResponse field (compatible with the FINISH invocation type) which contains:

  • responseText - The response to the user
  • citations - Citations for the response

To ensure that citations from the knowledge base are reliably included in the final response, you may need to:

  1. Structure your orchestration template to explicitly instruct the model to include citations
  2. Use XML formatting in your orchestration template to guide the model on how to properly format responses with citations
  3. Implement proper parsing in your Lambda function to extract and format citations correctly

The custom parser Lambda gives you control over how the agent processes and includes knowledge base information and citations in its responses, allowing for more reliable and consistent handling of knowledge base searches.
Sources
How to get a specific formatted output from a Bedrock Agent that uses a KB? | AWS re:Post
Write a custom parser Lambda function in Amazon Bedrock Agents - Amazon Bedrock
How to have a bedrock agent reliably include knowledge base citations in the final response of InvokeAgent for Agents for Amazon Bedrock Runtime? | AWS re:Post

answered 5 months ago
  • This answer isn’t addressing what I was asking.

    That answer is about the default orchestration strategy, which has several predefined steps — such as pre-processing, orchestration, and knowledge base response generation. I can attach a custom parser Lambda to optionally handle the FM’s output differently before passing it back to the agent.

    What I’m asking about is the custom orchestration strategy case. In that mode, should the knowledge base search be something explicitly included when the FM is first asked to create a complete plan (for example, by having it output a special command whenever a knowledge base lookup is needed)?

    And then, when the custom orchestration Lambda decides to let the agent perform that lookup, should it use the INVOKE_TOOL action as well? Because configuring a knowledge base is not the same as defining an action group with functions, I’m not sure exactly how this part should be implemented — and the sample code doesn’t seem to show how knowledge base searching is handled in custom orchestration.

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.