- Newest
- Most votes
- Most comments
In order four you to be able to provide a custom template for Knowledge Base response generation Orchestration strategy in a Bedrock agent using Python CDK, you need to use the knowledge_base_orchestration_config property within your agent configuration:
support_agent = bedrock.CfnAgent(self, "HorizonIQSupportAgent", agent_name="support-agent", agent_resource_role_arn=agent_role.attr_arn, auto_prepare=True, description="support-agent", foundation_model=foundation_model_name, idle_session_ttl_in_seconds=600, instruction=prompt, knowledge_bases=[bedrock.CfnAgent.AgentKnowledgeBaseProperty( description=kb_description, knowledge_base_id=knowledge_base.attr_knowledge_base_id, )], knowledge_base_orchestration_config=bedrock.CfnAgent.KnowledgeBaseOrchestrationConfigProperty( knowledge_base_template=""" YOUR TEMPLATE HERE """ ) )
Key points about the implementation:
-
Add the
knowledge_base_orchestration_configparameter to your agent configuration -
Use the
KnowledgeBaseOrchestrationConfigPropertyclass to define the template -
The template uses Handlebars syntax with variables like:
{{knowledge_base_responses}}: Contains the retrieved knowledge base entries{{question}}: The user's question{{answer}}: The generated answer{{#if}}and{{else}}: For conditional rendering
-
You can customize the template with:
- Custom formatting
- Conditional logic
- Additional context or instructions
- Different response structures
Example of a more structured template:
template = """ Context: {{knowledge_base_responses}} Question: {{question}} Response: {{#if knowledge_base_responses}} Here's what I found in our knowledge base: Main Answer: {{answer}} Additional Information: - This information is based on our official documentation - For more details, please refer to the relevant documentation sections {{else}} I apologize, but I couldn't find specific information about this in our knowledge base. Consider: 1. Rephrasing your question 2. Checking our general documentation 3. Contacting support for more specific assistance {{/if}} """ # Use the template in your agent configuration support_agent = bedrock.CfnAgent(self, "HorizonIQSupportAgent", # ... other configurations ... knowledge_base_orchestration_config=bedrock.CfnAgent.KnowledgeBaseOrchestrationConfigProperty( knowledge_base_template=template ) )
- First, create a dictionary that defines your custom prompt template for the Knowledge Base response generation:
custom_kb_prompt = { "promptType": "KNOWLEDGE_BASE_RESPONSE_GENERATION", "promptState": "ENABLED", "inferenceConfiguration": { "temperature": 0.5, "topP": 0.5, "maximumLength": 1000 }, "promptTemplate": "Your custom prompt template here. You can use placeholders like $query$ and $search_results$." } 2. Then, modify your `CfnAgent` creation to include the `prompt_override_configuration` property: ```python support_agent = bedrock.CfnAgent(self, "HorizonIQSupportAgent", agent_name="support-agent", agent_resource_role_arn=agent_role.attr_arn, auto_prepare=True, description="support-agent", foundation_model=foundation_model_name, idle_session_ttl_in_seconds=600, instruction=prompt, knowledge_bases=[bedrock.CfnAgent.AgentKnowledgeBaseProperty( description=kb_description, knowledge_base_id=knowledge_base.attr_knowledge_base_id, )], prompt_override_configuration={ "promptConfigurations": [custom_kb_prompt] } )
In the custom_kb_prompt dictionary:
- Set
promptTypeto "KNOWLEDGE_BASE_RESPONSE_GENERATION" to specify that this is for the Knowledge Base response generation stage. - Use
promptStateto enable or disable the custom prompt. - Adjust the
inferenceConfigurationparameters as needed for your use case. - In the
promptTemplate, you can use placeholders like $query$ and $search_results$ which will be replaced with actual values during runtime.
Remember to customize the prompt template and inference configuration parameters according to your specific requirements. You may also need to adjust other settings based on your agent's needs and the foundation model you're using.
This approach allows you to override the default orchestration strategy with your custom template, giving you more control over how the agent generates responses using the knowledge base.
Sources
Configure and customize queries and response generation - Amazon Bedrock
Customize agent orchestration strategy - Amazon Bedrock
Configure advanced prompts - Amazon Bedrock
Relevant content
- asked a year ago
- AWS OFFICIALUpdated a year ago

this doesn't work. I do not see the prompt modifying.