Skip to content

Commit

Permalink
Merge pull request #113 from awslabs/inline-bedrock-agent
Browse files Browse the repository at this point in the history
Inline bedrock agent
  • Loading branch information
brnaba-aws authored Nov 27, 2024
2 parents 28abc17 + 9b359d7 commit 84d221e
Show file tree
Hide file tree
Showing 13 changed files with 1,940 additions and 236 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Get hands-on experience with the Multi-Agent Orchestrator through our diverse se
- [`chat-chainlit-app`](https://github.com/awslabs/multi-agent-orchestrator/tree/main/examples/chat-chainlit-app): Chat application built with Chainlit
- [`fast-api-streaming`](https://github.com/awslabs/multi-agent-orchestrator/tree/main/examples/fast-api-streaming): FastAPI implementation with streaming support
- [`text-2-structured-output`](https://github.com/awslabs/multi-agent-orchestrator/tree/main/examples/text-2-structured-output): Natural Language to Structured Data
- [`bedrock-inline-agents`](https://github.com/awslabs/multi-agent-orchestrator/tree/main/examples/bedrock-inline-agents): Bedrock Inline Agents sample


All examples are available in both Python and TypeScript implementations. Check out our [documentation](https://awslabs.github.io/multi-agent-orchestrator/) for comprehensive guides on setting up and using the Multi-Agent Orchestrator!
Expand Down
3 changes: 2 additions & 1 deletion docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export default defineConfig({
{ label: 'Anthropic Agent', link: '/agents/built-in/anthropic-agent'},
{ label: 'Chain Agent', link: '/agents/built-in/chain-agent' },
{ label: 'Comprehend Filter Agent', link: '/agents/built-in/comprehend-filter-agent' },
{ label: 'Amazon Bedrock Translator Agent', link: '/agents/built-in/bedrock-translator-agent' }
{ label: 'Amazon Bedrock Translator Agent', link: '/agents/built-in/bedrock-translator-agent' },
{ label: 'Amazon Bedrock Inline Agent', link: '/agents/built-in/bedrock-inline-agent' }
]
},
{ label: 'Custom Agents', link: '/agents/custom-agents' },
Expand Down
228 changes: 228 additions & 0 deletions docs/src/content/docs/agents/built-in/bedrock-inline-agent.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
---
title: Bedrock Inline Agent
description: Documentation for the BedrockInlineAgent in the Multi-Agent Orchestrator
---

## Overview

The **Bedrock Inline Agent** represents a powerful new approach to dynamic agent creation. At its core, it leverages [Amazon Bedrock's Converse API](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html) and its tool capabilities to interact with foundation models and orchestrate agent creation. Through a specialized tool, it intelligently analyzes user requests and selects the most relevant action groups and knowledge bases from your available resources.

Once the optimal [Action Groups](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-action-create.html) and/or [Knowledge Bases](https://aws.amazon.com/bedrock/knowledge-bases/) are identified, the agent uses the [InvokeInlineAgent API](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create-inline.html) to dynamically create purpose-specific Agents for Amazon Bedrock. This eliminates the need to pre-configure static agent combinations - instead, agents are created on-demand with precisely the capabilities needed for each specific request.

This architecture removes practical limits on the number of action groups and knowledge bases you can maintain. Whether you have dozens or hundreds of different action groups and knowledge bases, the agent can efficiently select and combine just the ones needed for each query. This enables sophisticated use cases that would be impractical with traditional static agent configurations.

## Key Features

- Dynamic agent creation through InvokeInlineAgent API
- Tool-based selection of action groups and knowledge bases
- Support for multiple foundation models
- Customizable inference configuration
- Enhanced debug logging capabilities
- Support for custom logging implementations

## Creating a BedrockInlineAgent

### Basic Example

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs syncKey="runtime">
<TabItem label="TypeScript" icon="seti:typescript" color="blue">
```typescript
import { BedrockInlineAgent } from 'multi-agent-orchestrator';
import { CustomLogger } from './logger';

const actionGroups = [
{
actionGroupName: "OrderManagement",
description: "Handles order-related operations like status checks and updates"
},
{
actionGroupName: "InventoryLookup",
description: "Checks product availability and stock levels"
}
];

const knowledgeBases = [
{
knowledgeBaseId: "KB001",
description: "Product catalog and specifications"
}
];

const agent = new BedrockInlineAgent({
name: 'Inline Agent Creator for Agents for Amazon Bedrock',
description: 'Specialized in creating Agent to solve customer request dynamically. You are provided with a list of Action groups and Knowledge bases which can help you in answering customer request',
actionGroupsList: actionGroups,
knowledgeBases: knowledgeBases,
region: "us-east-1",
LOG_AGENT_DEBUG_TRACE: true,
inferenceConfig: {
maxTokens: 500,
temperature: 0.5,
topP: 0.9
}
});
```
</TabItem>
<TabItem label="Python" icon="seti:python">
```python
from multi_agent_orchestrator.agents import BedrockInlineAgent, BedrockInlineAgentOptions
from custom_logger import CustomLogger

action_groups = [
{
"actionGroupName": "OrderManagement",
"description": "Handles order-related operations like status checks and updates"
},
{
"actionGroupName": "InventoryLookup",
"description": "Checks product availability and stock levels"
}
]

knowledge_bases = [
{
"knowledgeBaseId": "KB001",
"description": "Product catalog and specifications"
}
]

agent = BedrockInlineAgent(BedrockInlineAgentOptions(
name='Inline Agent Creator for Agents for Amazon Bedrock',
description='Specialized in creating Agent to solve customer request dynamically. You are provided with a list of Action groups and Knowledge bases which can help you in answering customer request',
action_groups_list=action_groups,
knowledge_bases=knowledge_bases,
region="us-east-1",
LOG_AGENT_DEBUG_TRACE=True,
inference_config={
'maxTokens': 500,
'temperature': 0.5,
'topP': 0.9
}
))
```
</TabItem>
</Tabs>

## Debug Logging

### LOG_AGENT_DEBUG_TRACE

When enabled, this flag activates detailed debug logging that helps you understand the agent's operation. Example output:

```text
> BedrockInlineAgent
> Inline Agent Creator for Agents for Amazon Bedrock
> System Prompt
> You are a Inline Agent Creator for Agents for Amazon Bedrock...
> BedrockInlineAgent
> Inline Agent Creator for Agents for Amazon Bedrock
> Tool Handler Parameters
> {
userRequest: 'Please execute...',
actionGroupNames: ['CodeInterpreterAction'],
knowledgeBases: [],
description: 'To solve this request...',
sessionId: 'session-456'
}
> BedrockInlineAgent
> Inline Agent Creator for Agents for Amazon Bedrock
> Action Group & Knowledge Base
> {
actionGroups: [
{
actionGroupName: 'CodeInterpreterAction',
parentActionGroupSignature: 'AMAZON.CodeInterpreter'
}
],
knowledgeBases: []
}
```

### Custom Logger Implementation

You can provide your own logger implementation to customize log formatting and handling. Here's an example:

<Tabs syncKey="runtime">
<TabItem label="TypeScript" icon="seti:typescript" color="blue">
```typescript
export class CustomLogger {
private static instance: CustomLogger;

private constructor() {}

static getInstance(): CustomLogger {
if (!CustomLogger.instance) {
CustomLogger.instance = new CustomLogger();
}
return CustomLogger.instance;
}

info(message: string, ...args: any[]): void {
console.info(">>: " + message, ...args);
}

warn(message: string, ...args: any[]): void {
console.warn(">>: " + message, ...args);
}

error(message: string, ...args: any[]): void {
console.error(">>: " + message, ...args);
}

debug(message: string, ...args: any[]): void {
console.debug(">>: " + message, ...args);
}

log(message: string, ...args: any[]): void {
console.log(">>: " + message, ...args);
}
}
```
</TabItem>
<TabItem label="Python" icon="seti:python">
```python
class CustomLogger:
_instance = None

def __new__(cls):
if cls._instance is None:
cls._instance = super(CustomLogger, cls).__new__(cls)
return cls._instance

@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = CustomLogger()
return cls._instance

def info(self, message: str, *args):
print(f">>: {message}", *args)

def warn(self, message: str, *args):
print(f">>: [WARNING] {message}", *args)

def error(self, message: str, *args):
print(f">>: [ERROR] {message}", *args)

def debug(self, message: str, *args):
print(f">>: [DEBUG] {message}", *args)

def log(self, message: str, *args):
print(f">>: {message}", *args)
```
</TabItem>
</Tabs>

## Sample Code

You can find sample code for using the BedrockInlineAgent in both TypeScript and Python:

- [TypeScript Sample](https://github.com/awslabs/multi-agent-orchestrator/tree/main/examples/bedrock-inline-agents/typescript)
- [Python Sample](https://github.com/awslabs/multi-agent-orchestrator/tree/main/examples/bedrock-inline-agents/python)


The BedrockInlineAgent represents a significant advancement in agent flexibility and efficiency, enabling truly dynamic, context-aware responses while optimizing resource usage.
75 changes: 75 additions & 0 deletions examples/bedrock-inline-agents/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import asyncio
import uuid
import sys
from multi_agent_orchestrator.agents import BedrockInlineAgent, BedrockInlineAgentOptions
import boto3

action_groups_list = [
{
'actionGroupName': 'CodeInterpreterAction',
'parentActionGroupSignature': 'AMAZON.CodeInterpreter',
'description':'Use this to write and execute python code to answer questions and other tasks.'
},
{
"actionGroupExecutor": {
"lambda": "arn:aws:lambda:region:0123456789012:function:my-function-name"
},
"actionGroupName": "MyActionGroupName",
"apiSchema": {
"s3": {
"s3BucketName": "bucket-name",
"s3ObjectKey": "openapi-schema.json"
}
},
"description": "My action group for doing a specific task"
}
]

knowledge_bases = [
{
"knowledgeBaseId": "knowledge-base-id-01",
"description": 'This is my knowledge base for documents 01',
},
{
"knowledgeBaseId": "knowledge-base-id-02",
"description": 'This is my knowledge base for documents 02',
},
{
"knowledgeBaseId": "knowledge-base-id-0",
"description": 'This is my knowledge base for documents 03',
}
]

bedrock_inline_agent = BedrockInlineAgent(BedrockInlineAgentOptions(
name="Inline Agent Creator for Agents for Amazon Bedrock",
region='us-east-1',
model_id="anthropic.claude-3-haiku-20240307-v1:0",
description="Specalized in creating Agent to solve customer request dynamically. You are provided with a list of Action groups and Knowledge bases which can help you in answering customer request",
action_groups_list=action_groups_list,
bedrock_agent_client=boto3.client('bedrock-agent-runtime', region_name='us-east-1'),
client=boto3.client('bedrock-runtime', region_name='us-west-2'),
knowledge_bases=knowledge_bases,
enableTrace=True
))

async def run_inline_agent(user_input, user_id, session_id):
response = await bedrock_inline_agent.process_request(user_input, user_id, session_id, [], None)
return response

if __name__ == "__main__":

session_id = str(uuid.uuid4())
user_id = str(uuid.uuid4())
print("Welcome to the interactive Multi-Agent system. Type 'quit' to exit.")

while True:
# Get user input
user_input = input("\nYou: ").strip()

if user_input.lower() == 'quit':
print("Exiting the program. Goodbye!")
sys.exit()

# Run the async function
response = asyncio.run(run_inline_agent(user_input=user_input, user_id=user_id, session_id=session_id))
print(response.content[0].get('text','No response'))
Loading

0 comments on commit 84d221e

Please sign in to comment.