Skip to content

How to provide a custom template for Knowledge Base response generation Orchestration strategy for a Bedrock agent using python cdk ?

0

I am trying to deploy a Bedrock agent with a knowledgebase using python cdk. I want to pass a custom template for Knowledge Base response generation Orchestration strategy.

How can i do it using python cdk ? Below is my code to deploy the agent which is working fine.

        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,
            )]
        )
2 Answers
0

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:

  1. Add the knowledge_base_orchestration_config parameter to your agent configuration

  2. Use the KnowledgeBaseOrchestrationConfigProperty class to define the template

  3. 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
  4. 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
    )
)
AWS
SUPPORT ENGINEER
answered 9 months ago
-1
  1. 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 promptType to "KNOWLEDGE_BASE_RESPONSE_GENERATION" to specify that this is for the Knowledge Base response generation stage.
  • Use promptState to enable or disable the custom prompt.
  • Adjust the inferenceConfiguration parameters 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

answered 9 months ago
  • this doesn't work. I do not see the prompt modifying.

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.