Skip to content

Commit

Permalink
increased TS version for release
Browse files Browse the repository at this point in the history
  • Loading branch information
brnaba-aws committed Oct 14, 2024
1 parent ee8eb3f commit 20485d4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
38 changes: 18 additions & 20 deletions examples/chat-demo-app/lambda/multi-agent/math_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,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 @@ -127,16 +127,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 @@ -178,27 +178,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 @@ -207,13 +207,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 @@ -237,7 +237,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 @@ -265,9 +265,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;
}



25 changes: 12 additions & 13 deletions examples/chat-demo-app/lambda/multi-agent/weather_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ interface WeatherData {
message?: string;
}

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

const responseContentBlocks = response.content as any[];

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

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

if (toolUseName === "Weather_Tool") {
const response = await fetchWeatherData({latitude: toolUseBlock.input.latitude, longitude: toolUseBlock.input.longitude});
toolResults.push({
Expand All @@ -65,16 +65,15 @@ export async function weatherToolHanlder(response:ConversationMessage, conversat
"content": [{ json: { result: response } }],
}
});
}
}
}
}
// 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;
}

async function fetchWeatherData(inputData: InputData): Promise<WeatherData> {
const endpoint = "https://api.open-meteo.com/v1/forecast";
const { latitude, longitude } = inputData;
Expand All @@ -83,14 +82,14 @@ async function fetchWeatherData(inputData: InputData): Promise<WeatherData> {
longitude: longitude?.toString() || "",
current_weather: "true",
});

try {
const response = await fetch(`${endpoint}?${params}`);
const data = await response.json() as any;
const data = await response.json() as any;
if (!response.ok) {
return { error: 'Request failed', message: data.message || 'An error occurred' };
}

return { weather_data: data };
} catch (error: any) {
return { error: error.name, message: error.message };
Expand Down
2 changes: 1 addition & 1 deletion typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multi-agent-orchestrator",
"version": "0.0.16",
"version": "0.0.17",
"description": "Multi-Agent Orchestrator framework",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 20485d4

Please sign in to comment.