Amazon Bedrock Integration for Microsoft Agent Framework

1.0.0b260409 · active · verified Thu Apr 16

The `agent-framework-bedrock` library provides an integration for the Microsoft Agent Framework to connect with Amazon Bedrock. It enables Agent Framework applications to leverage Bedrock's foundational models for tasks such as Chat Completion, Text Generation, and Text Embeddings. This package is part of Microsoft's broader Agent Framework, which aims to provide a unified, open-source SDK for building, orchestrating, and deploying AI agents and multi-agent workflows in Python and .NET. The current version, `1.0.0b260409`, is a beta release, indicating active development within the recently released 1.0 version of the main `agent-framework`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `BedrockChatClient` and use it to create an `Agent` from the Microsoft Agent Framework. It assumes AWS credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) and the AWS region (AWS_REGION_NAME) are configured in the environment, along with a specified Bedrock model ID (BEDROCK_MODEL_ID). The agent then processes a simple prompt and prints the response.

import asyncio
import os
from agent_framework import Agent
from agent_framework.amazon import BedrockChatClient
from agent_framework.core.message import Message

async def main():
    # Ensure AWS credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    # and AWS_REGION_NAME are set as environment variables or via AWS CLI config.
    # Example Bedrock model ID, e.g., "anthropic.claude-v2" or "amazon.titan-text-express-v1"
    bedrock_model_id = os.environ.get("BEDROCK_MODEL_ID", "anthropic.claude-v2")
    aws_region = os.environ.get("AWS_REGION_NAME", "us-east-1")

    if not all(os.environ.get(var) for var in ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION_NAME"]):
        print("Warning: AWS credentials or region not fully configured via environment variables.")
        print("Please set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION_NAME.")
        # In a real application, you might raise an error or use a credential provider.

    try:
        # Initialize the Bedrock chat client
        client = BedrockChatClient(region_name=aws_region, model=bedrock_model_id)

        # Create an Agent Framework Agent with the Bedrock client
        bedrock_agent = Agent(
            client=client,
            instructions="You are a helpful AI assistant that responds concisely.",
            name="BedrockAssistant",
        )

        # Run a simple interaction
        response_messages = await bedrock_agent.run("Tell me a fun fact about Python.")

        # Print the agent's response
        for msg in response_messages:
            print(f"[{msg.author_name}]: {msg.text}")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Ensure AWS credentials and BEDROCK_MODEL_ID/AWS_REGION_NAME are correctly configured.")
        print("Also, verify that the specified Bedrock model is enabled in your AWS account and region.")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →