Skip to content

Conversation

yosefkri
Copy link
Contributor

Overview

This PR adds support for Amazon Bedrock agents to the AWS GenAI LLM Chatbot. Users can now configure and use Bedrock agents through the chatbot interface, providing an additional AI capability alongside existing LLM providers.

Key Changes

  1. Configuration Updates

    • Extended the bedrock section in config.json to include agent configuration
    • Added CLI prompts for Bedrock agent setup (agent ID, version, alias ID)
    • Added support for non-interactive configuration via environment variables
  2. Backend Implementation

    • Created a new Bedrock agent client module in the Python SDK layer
    • Implemented a Bedrock agent adapter that extends the BaseAdapter
    • Added support for processing EventStream responses from Bedrock agents
    • Implemented fallback mechanisms for response handling
  3. Environment and Permissions

    • Added environment variables for Bedrock agent configuration
    • Updated IAM permissions to allow Lambda functions to invoke Bedrock agents
  4. Model Registration

    • Updated the direct model provider to include Bedrock agent models
    • Registered the Bedrock agent adapter with the pattern "bedrock.bedrock_agent"
  5. Error Handling and Logging

    • Added specific error handling for Bedrock agent-related errors
    • Enhanced logging for Bedrock agent interactions

Testing

  • Tested the configuration flow to ensure agent settings are properly saved
  • Verified integration with Bedrock agent using sample queries
  • Validated response handling from Bedrock agents
  • Tested error handling for various failure scenarios
  • Verified cross-region functionality works correctly

Challenges Addressed

  1. Response Format: Implemented handling for the nested EventStream structure in Bedrock agent responses
  2. Region Configuration: Added explicit region configuration for Bedrock agent clients
  3. Event Processing: Added support for dictionary events in the EventStream

Documentation

  • Added implementation plan document with detailed architecture and design decisions
  • Updated relevant documentation to include Bedrock agent configuration and usage

How to Test

  1. Configure a Bedrock agent in your AWS account
  2. Update your config.json with the agent details:
    "bedrock": {
      "enabled": true,
      "region": "your-region",
      "agent": {
        "enabled": true,
        "agentId": "your-agent-id",
        "agentVersion": "DRAFT",
        "agentAliasId": "your-agent-alias-id"
      }
    }
  3. Deploy the chatbot
  4. Create an application using the "bedrock_agent" model
  5. Test interactions with your Bedrock agent

Future Enhancements

  • Support for streaming responses from Bedrock agents
  • Enhanced error handling and recovery mechanisms
  • UI improvements for agent-specific features
  • Integration with agent action groups
  • Support for agent knowledge bases

yosefkri and others added 2 commits August 11, 2025 10:21
* Adding support using bedrock agent

* update langchain issue

* updated plan readme

---------

Co-authored-by: Yossi Kricheli <yossik@amazon.com>
logger.info(f"Completion value type: {type(completion_value)}")
# Try to extract the completion from the EventStream
try:
# Process the EventStream
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor this to reduce nesting and improve readability? I'd extract the response stream processing and extraction logics out into chunks.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add some unit tests to cover this?

return [
{
"provider": Provider.BEDROCK.value,
"name": "bedrock_agent",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we not using the agent alias here, or including it in the name?

@maryamkhidir maryamkhidir self-requested a review August 12, 2025 08:18
Copy link
Collaborator

@maryamkhidir maryamkhidir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @yosefkri.
QQ: Will there be a follow up CR for the UI Improvements e.g add Agents trace to the chatbot console?

One observation: The current approach only allows for a single pre-configured agent in the chatbot. For better flexibility, we may want to consider supporting multiple agents. Perhaps we could explore retrieving all available agents from the customer's Bedrock environment?

For reference, this CR provides some insights:

* Adding support using bedrock agent

* update langchain issue

* updated plan readme

* Making code more readable

---------

Co-authored-by: Yossi Kricheli <yossik@amazon.com>
Copy link
Contributor

@Rob-Powell Rob-Powell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file ‘bedrock-agent-implementation-plan.md’ should be in the ./docs directory of the project somewhere not in the root.
Your PR also mentions how you've updated relevant docs but I don't see any of those yet. Is this PR still a WIP?

@yosefkri
Copy link
Contributor Author

@Rob-Powell the PR is WIP since we decided to move to discover bedrock agent dynamically instead using specific agent ID.
The plan needs to be removed, I will update the documentation also.

yosefkri and others added 2 commits August 21, 2025 14:51
* Adding support using bedrock agent

* update langchain issue

* updated plan readme

* Making code more readable

* Added list agent feature

* Added support to specific agent or list agents

* Added support for bedrock agent by passing the s3 uri for file upload

---------

Co-authored-by: Yossi Kricheli <yossik@amazon.com>
@yosefkri
Copy link
Contributor Author

Pull Request Summary: Enhanced Bedrock Agent Support

This pull request adds comprehensive support for Amazon Bedrock Agents in the AWS GenAI LLM Chatbot, with two main components:

1. S3 URI-Based File Handling for Bedrock Agents

We've implemented a more efficient approach to file handling with Bedrock Agents by providing S3 URIs instead of embedding file content directly in prompts:

  • Removed base64 encoding of file content, eliminating token consumption and size limitations
  • Enhanced prompt structure with clear sections for file information, user queries, and S3 access details
  • Preserved original file keys throughout the processing pipeline to ensure correct S3 paths
  • Added conditional file information that only includes file handling instructions when files are present
  • Improved error handling and logging for better troubleshooting
  • Fixed JSON import issues and other technical bugs

This approach allows Bedrock Agents to access files of any size through their /get-file endpoint, making the system more efficient and robust.

2. Bedrock Agent Management Functionality

We've added new capabilities to discover and interact with Bedrock Agents:

  • Agent discovery: When Bedrock Agent is enabled in the config without specific agent parameters, the system lists all available agents in the account
  • Targeted agent selection: When enabled with a specific agent ID, the system lists only that agent
  • Multi-function support: The system can now work with multiple Bedrock Agents, allowing different agents to be used for different purposes

These enhancements make it easier to discover, select, and use Bedrock Agents within the chatbot framework.

Technical Implementation Details

  • Modified the BedrockAgentAdapter class to handle S3 URIs instead of file content
  • Enhanced the bedrock_agent/client.py module with agent discovery and selection capabilities
  • Added proper error handling for non-English text and other edge cases
  • Implemented comprehensive logging for debugging and monitoring
  • Ensured backward compatibility with existing configurations

This pull request significantly improves the chatbot's ability to work with Bedrock Agents and handle file-based interactions more efficiently.

Copy link
Collaborator

@maryamkhidir maryamkhidir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

  • Please add unit tests at least for new logic and we should also have some integration tests for the Agentic flow.
  • And fix linting errors - npm run vet-all run all the checks.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test coverage for this file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test coverage for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants