Skip to content

Commit

Permalink
Updated documentation for toolUseHandler, updated local demo with wea…
Browse files Browse the repository at this point in the history
…ther tool and streaming, updated package with latest version
  • Loading branch information
brnaba-aws committed Oct 14, 2024
1 parent 9ba0694 commit 0f3ec07
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 44 deletions.
10 changes: 5 additions & 5 deletions docs/src/content/docs/advanced-features/math-tool-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ B. Tool Handler
```typescript
import { ConversationMessage, ParticipantRole } from "multi-agent-orchestrator";

export async function mathToolHandler(response, conversation: ConversationMessage[]) {
export async function mathToolHandler(response, conversation: ConversationMessage[]): Promise<ConversationMessage> {
const responseContentBlocks = response.content as any[];
let toolResults: any = [];

Expand All @@ -108,7 +108,7 @@ export async function mathToolHandler(response, conversation: ConversationMessag
}

const message: ConversationMessage = { role: ParticipantRole.USER, content: toolResults };
conversation.push(message);
return messages;
}
```

Expand Down Expand Up @@ -137,10 +137,10 @@ function executeMathOperation(
const safeEval = (code: string) => {
return Function('"use strict";return (' + code + ")")();
};

try {
let result: number;

switch (operation.toLowerCase()) {
case 'add':
case 'addition':
Expand Down Expand Up @@ -182,7 +182,7 @@ function executeMathOperation(
throw new Error(`Unsupported operation: ${operation}`);
}
}

return { result };
} catch (error) {
return {
Expand Down
14 changes: 7 additions & 7 deletions docs/src/content/docs/advanced-features/weather-tool-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ If the user provides coordinates, infer the approximate location and refer to it
To use the tool, you strictly apply the provided tool specification.
- Explain your step-by-step process, and give brief updates before each step.
- Only use the Weather_Tool for data. Never guess or make up information.
- Only use the Weather_Tool for data. Never guess or make up information.
- Repeat the tool use for subsequent requests if necessary.
- If the tool errors, apologize, explain weather is unavailable, and suggest other options.
- Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use
emojis where appropriate.
- Only respond to weather queries. Remind off-topic users of your purpose.
- Only respond to weather queries. Remind off-topic users of your purpose.
- Never claim to search online, access external data, or use tools besides Weather_Tool.
- Complete the entire process until you have all required data before sending the complete response.
`;
Expand All @@ -93,7 +93,7 @@ C. Tool Handler
import { ConversationMessage, ParticipantRole } from "multi-agent-orchestrator";


export async function weatherToolHandler(response, conversation: ConversationMessage[]) {
export async function weatherToolHandler(response, conversation: ConversationMessage[]):Promse<ConversationMessage> {
const responseContentBlocks = response.content as any[];
let toolResults: any = [];

Expand All @@ -120,7 +120,7 @@ export async function weatherToolHandler(response, conversation: ConversationMes
}

const message: ConversationMessage = { role: ParticipantRole.USER, content: toolResults };
conversation.push(message);
return message;
}
```

Expand Down Expand Up @@ -189,9 +189,9 @@ import { weatherToolDescription, weatherToolHandler, WEATHER_PROMPT } from './we

const weatherAgent = new BedrockLLMAgent({
name: "Weather Agent",
description:`Specialized agent for providing comprehensive weather information and forecasts for specific cities worldwide.
This agent can deliver current conditions, temperature ranges, precipitation probabilities, wind speeds, humidity levels, UV indexes, and extended forecasts.
It can also offer insights on severe weather alerts, air quality indexes, and seasonal climate patterns.
description:`Specialized agent for providing comprehensive weather information and forecasts for specific cities worldwide.
This agent can deliver current conditions, temperature ranges, precipitation probabilities, wind speeds, humidity levels, UV indexes, and extended forecasts.
It can also offer insights on severe weather alerts, air quality indexes, and seasonal climate patterns.
The agent is capable of interpreting user queries related to weather, including natural language requests like 'Do I need an umbrella today?' or 'What's the best day for outdoor activities this week?'.
It can handle location-specific queries and time-based weather predictions, making it ideal for travel planning, event scheduling, and daily decision-making based on weather conditions.`,
streaming: false,
Expand Down
6 changes: 1 addition & 5 deletions docs/src/content/docs/agents/built-in/bedrock-llm-agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ For more complex use cases, you can create a **Bedrock LLM Agent** with all avai
}
}
}
],
toolCallback: (response) => {
// Process tool response
return { continueWithTools: false, message: response };
}
]
}
};
Expand Down
24 changes: 22 additions & 2 deletions examples/local-demo/local-orchestrator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import readline from "readline";
import {
import {
MultiAgentOrchestrator,
BedrockLLMAgent,
AmazonBedrockAgent,
LexBotAgent,
LambdaAgent,
Logger,
} from "multi-agent-orchestrator"
} from "multi-agent-orchestrator";

import {weatherToolDescription, weatherToolHanlder, WEATHER_PROMPT } from './tools/weather_tool'


function createOrchestrator(): MultiAgentOrchestrator {
Expand Down Expand Up @@ -46,6 +48,24 @@ function createOrchestrator(): MultiAgentOrchestrator {
})
);

// Add weahter agent with tool
const weatherAgent = new BedrockLLMAgent({
name: "Weather Agent",
description:
"Specialized agent for giving weather condition from a city.",
streaming: true,
inferenceConfig: {
temperature: 0.1,
},
toolConfig: {
tool: weatherToolDescription,
useToolHandler: weatherToolHanlder,
toolMaxRecursions: 5,
}
});
weatherAgent.setSystemPrompt(WEATHER_PROMPT);
orchestrator.addAgent(weatherAgent);

// Add an Amazon Bedrock Agent to the orchestrator
orchestrator.addAgent(
new AmazonBedrockAgent({
Expand Down
8 changes: 4 additions & 4 deletions examples/local-demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/local-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"license": "ISC",
"dependencies": {
"dotenv": "^16.4.5",
"multi-agent-orchestrator": "^0.0.13"
"multi-agent-orchestrator": "^0.0.15"
}
}
38 changes: 18 additions & 20 deletions examples/local-demo/tools/math_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ function executeMathOperation(
const safeEval = (code: string) => {
return Function('"use strict";return (' + code + ")")();
};

try {
let result: number;

switch (operation.toLowerCase()) {
case 'add':
case 'addition':
Expand Down Expand Up @@ -133,16 +133,16 @@ function executeMathOperation(
throw new Error(`Unsupported operation: ${operation}`);
}
}

return { result };
} catch (error) {
return {
error: `Error executing ${operation}: ${(error as Error).message}`,
};
}
}


function calculateStatistics(operation: string, args: number[]): { result: number } | { error: string } {
try {
switch (operation.toLowerCase()) {
Expand Down Expand Up @@ -184,27 +184,27 @@ try {
}


export async function mathToolHanlder(response:any, conversation: ConversationMessage[]){
export async function mathToolHanlder(response:any, conversation: ConversationMessage[]): Promise<any>{

const responseContentBlocks = response.content as any[];

const mathOperations: string[] = [];
let lastResult: number | string | undefined;

// Initialize an empty list of tool results
let toolResults:any = []

if (!responseContentBlocks) {
throw new Error("No content blocks in response");
}
}
for (const contentBlock of response.content) {
if ("text" in contentBlock) {
Logger.logger.info(contentBlock.text);
}
if ("toolUse" in contentBlock) {
const toolUseBlock = contentBlock.toolUse;
const toolUseName = toolUseBlock.name;

if (toolUseName === "perform_math_operation") {
const operation = toolUseBlock.input.operation;
let args = toolUseBlock.input.args;
Expand All @@ -213,13 +213,13 @@ export async function mathToolHanlder(response:any, conversation: ConversationM
const degToRad = Math.PI / 180;
args = [args[0] * degToRad];
}

const result = executeMathOperation(operation, args);

if ('result' in result) {
lastResult = result.result;
mathOperations.push(`Tool call ${mathOperations.length + 1}: perform_math_operation: args=[${args.join(', ')}] operation=${operation} result=${lastResult}\n`);

toolResults.push({
toolResult: {
toolUseId: toolUseBlock.toolUseId,
Expand All @@ -243,7 +243,7 @@ export async function mathToolHanlder(response:any, conversation: ConversationM
const operation = toolUseBlock.input.operation;
const args = toolUseBlock.input.args;
const result = calculateStatistics(operation, args);

if ('result' in result) {
lastResult = result.result;
mathOperations.push(`Tool call ${mathOperations.length + 1}: perform_statistical_calculation: args=[${args.join(', ')}] operation=${operation} result=${lastResult}\n`);
Expand Down Expand Up @@ -271,9 +271,7 @@ export async function mathToolHanlder(response:any, conversation: ConversationM
}
// Embed the tool results in a new user message
const message:ConversationMessage = {role: ParticipantRole.USER, content: toolResults};

// Append the new message to the ongoing conversation
conversation.push(message);

return message;
}



0 comments on commit 0f3ec07

Please sign in to comment.