Skip to content

Commit

Permalink
add ts sample for bedrock inline agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Corneliu Croitoru committed Nov 27, 2024
1 parent 0ef3d1c commit d67a56e
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions examples/bedrock-inline-agents/typescript/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//import { BedrockInlineAgent, BedrockInlineAgentOptions } from 'multi-agent-orchestrator';
import { BedrockInlineAgent, BedrockInlineAgentOptions } from '../../../typescript/src/agents/bedrockInlineAgent';
import {
BedrockAgentRuntimeClient,
AgentActionGroup,
KnowledgeBase
} from "@aws-sdk/client-bedrock-agent-runtime";
import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";
import { v4 as uuidv4 } from 'uuid';
import { createInterface } from 'readline';

// Define action groups
const actionGroupsList: AgentActionGroup[] = [
{
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"
}
];

// Define knowledge bases
const knowledgeBases: KnowledgeBase[] = [
{
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-03",
description: 'This is my knowledge base for documents 03',
}
];


// Initialize BedrockInlineAgent
const bedrickInlineAgent = new BedrockInlineAgent({
name: "Inline Agent Creator for Agents for Amazon Bedrock",
region: 'us-east-1',
modelId: "anthropic.claude-3-haiku-20240307-v1:0",
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: actionGroupsList,
knowledgeBases: knowledgeBases,
LOG_AGENT_DEBUG_TRACE: true
});

async function runInlineAgent(userInput: string, userId: string, sessionId: string) {
const response = await bedrickInlineAgent.processRequest(
userInput,
userId,
sessionId,
[], // empty chat history
undefined // no additional params
);
return response;
}

async function main() {
const sessionId = uuidv4();
const userId = uuidv4();

console.log("Welcome to the interactive Multi-Agent system. Type 'quit' to exit.");

const readline = createInterface({
input: process.stdin,
output: process.stdout
});

const getUserInput = () => {
return new Promise((resolve) => {
readline.question('\nYou: ', (input) => {
resolve(input.trim());
});
});
};

while (true) {
const userInput = await getUserInput() as string;

if (userInput.toLowerCase() === 'quit') {
console.log("Exiting the program. Goodbye!");
readline.close();
process.exit(0);
}

try {
const response = await runInlineAgent(userInput, userId, sessionId);
if (response && response.content && response.content.length > 0) {
const text = response.content[0]?.text;
console.log(text || 'No response content');
} else {
console.log('No response');
}
} catch (error) {
console.error('Error:', error);
}
}
}

// Run the program
main().catch(console.error);

0 comments on commit d67a56e

Please sign in to comment.