Skip to content

Commit

Permalink
throw error for TS
Browse files Browse the repository at this point in the history
  • Loading branch information
brnaba-aws committed Oct 15, 2024
1 parent 709a2b8 commit 9469fa2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 46 deletions.
11 changes: 1 addition & 10 deletions typescript/src/agents/amazonBedrockAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,7 @@ export class AmazonBedrockAgent extends Agent {
} catch (err) {
// Handle errors encountered while invoking the Amazon Bedrock agent
Logger.logger.error(err);

// Return a default error message as a fallback response
return {
role: ParticipantRole.ASSISTANT,
content: [
{
text: "Sorry, I encountered an error while processing your request.",
},
],
};
throw err;
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions typescript/src/agents/chainAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export class ChainAgent extends Agent {

let currentInput = inputText;
let finalResponse: ConversationMessage | AsyncIterable<any>;

console.log(`Processing chain with ${this.agents.length} agents`);

for (let i = 0; i < this.agents.length; i++) {
const isLastAgent = i === this.agents.length - 1;
const agent = this.agents[i];

try {
console.log(`Input for agent ${i}: ${currentInput}`);
const response = await agent.processRequest(
Expand All @@ -57,7 +57,7 @@ export class ChainAgent extends Agent {
chatHistory,
additionalParams
);

if (this.isConversationMessage(response)) {
if (response.content.length > 0 && 'text' in response.content[0]) {
currentInput = response.content[0].text;
Expand All @@ -78,25 +78,25 @@ export class ChainAgent extends Agent {
Logger.logger.warn(`Agent ${agent.name} returned an invalid response type.`);
return this.createDefaultResponse();
}

// If it's not the last agent, ensure we have a non-streaming response to pass to the next agent
if (!isLastAgent && !this.isConversationMessage(finalResponse)) {
Logger.logger.error(`Expected non-streaming response from intermediate agent ${agent.name}`);
return this.createDefaultResponse();
}
} catch (error) {
Logger.logger.error(`Error processing request with agent ${agent.name}:`, error);
return this.createDefaultResponse();
throw `Error processing request with agent ${agent.name}:${String(error)}`;
}
}

return finalResponse;
}

private isAsyncIterable(obj: any): obj is AsyncIterable<any> {
return obj && typeof obj[Symbol.asyncIterator] === 'function';
}


private isConversationMessage(response: any): response is ConversationMessage {
return response && 'role' in response && 'content' in response && Array.isArray(response.content);
Expand Down
19 changes: 8 additions & 11 deletions typescript/src/agents/openAIAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class OpenAIAgent extends Agent {
this.inferenceConfig = {
maxTokens: options.inferenceConfig?.maxTokens ?? DEFAULT_MAX_TOKENS,
temperature: options.inferenceConfig?.temperature,
topP: options.inferenceConfig?.topP,
topP: options.inferenceConfig?.topP,
stopSequences: options.inferenceConfig?.stopSequences,
};
}
Expand All @@ -49,8 +49,8 @@ export class OpenAIAgent extends Agent {
chatHistory: ConversationMessage[],
additionalParams?: Record<string, string>
): Promise<ConversationMessage | AsyncIterable<any>> {


const messages = [
...chatHistory.map(msg => ({
role: msg.role.toLowerCase() as OpenAI.Chat.ChatCompletionMessageParam['role'],
Expand All @@ -71,7 +71,7 @@ export class OpenAIAgent extends Agent {
stop: stopSequences,
};



if (this.streaming) {
return this.handleStreamingResponse(requestOptions);
Expand All @@ -90,7 +90,7 @@ export class OpenAIAgent extends Agent {
}

const assistantMessage = chatCompletion.choices[0]?.message?.content;

if (typeof assistantMessage !== 'string') {
throw new Error('Unexpected response format from OpenAI API');
}
Expand All @@ -101,10 +101,7 @@ export class OpenAIAgent extends Agent {
};
} catch (error) {
Logger.logger.error('Error in OpenAI API call:', error);
return {
role: ParticipantRole.ASSISTANT,
content: [{ text: 'I encountered an error while processing your request.' }],
};
throw error;
}
}

Expand All @@ -117,8 +114,8 @@ export class OpenAIAgent extends Agent {
}
}
}






}
30 changes: 13 additions & 17 deletions typescript/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ export interface OrchestratorConfig {

/**
* The message to display when no agent is selected to handle the user's request.
*
*
* This message is shown when the classifier couldn't determine an appropriate agent
* and USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED is set to false.
*/
NO_SELECTED_AGENT_MESSAGE?: string;

/**
* The general error message to display when an error occurs during request routing.
*
*
* This message is shown when an unexpected error occurs during the processing of a user's request,
* such as errors in agent dispatch or processing.
*/
Expand Down Expand Up @@ -217,15 +217,11 @@ export class MultiAgentOrchestrator {
USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED:
options.config?.USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED ??
DEFAULT_CONFIG.USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED,
CLASSIFICATION_ERROR_MESSAGE:
options.config?.CLASSIFICATION_ERROR_MESSAGE ??
DEFAULT_CONFIG.CLASSIFICATION_ERROR_MESSAGE,
CLASSIFICATION_ERROR_MESSAGE: options.config?.CLASSIFICATION_ERROR_MESSAGE,
NO_SELECTED_AGENT_MESSAGE:
options.config?.NO_SELECTED_AGENT_MESSAGE ??
DEFAULT_CONFIG.NO_SELECTED_AGENT_MESSAGE,
GENERAL_ROUTING_ERROR_MSG_MESSAGE:
options.config?.GENERAL_ROUTING_ERROR_MSG_MESSAGE ??
DEFAULT_CONFIG.GENERAL_ROUTING_ERROR_MSG_MESSAGE,
GENERAL_ROUTING_ERROR_MSG_MESSAGE: options.config?.GENERAL_ROUTING_ERROR_MSG_MESSAGE
};

this.executionTimes = new Map();
Expand Down Expand Up @@ -349,7 +345,7 @@ export class MultiAgentOrchestrator {
this.executionTimes = new Map();
let classifierResult: ClassifierResult;
const chatHistory = (await this.storage.fetchAllChats(userId, sessionId)) || [];

try {
classifierResult = await this.measureExecutionTime(
"Classifying user intent",
Expand All @@ -361,7 +357,7 @@ export class MultiAgentOrchestrator {
this.logger.error("Error during intent classification:", error);
return {
metadata: this.createMetadata(null, userInput, userId, sessionId, additionalParams),
output: this.config.CLASSIFICATION_ERROR_MESSAGE,
output: this.config.CLASSIFICATION_ERROR_MESSAGE ? this.config.CLASSIFICATION_ERROR_MESSAGE:error,
streaming: false,
};
}
Expand All @@ -379,7 +375,7 @@ export class MultiAgentOrchestrator {
};
}
}

try {
const agentResponse = await this.dispatchToAgent({
userInput,
Expand All @@ -388,9 +384,9 @@ export class MultiAgentOrchestrator {
classifierResult,
additionalParams,
});

const metadata = this.createMetadata(classifierResult, userInput, userId, sessionId, additionalParams);

if (this.isAsyncIterable(agentResponse)) {
const accumulatorTransform = new AccumulatorTransform();
this.processStreamInBackground(
Expand All @@ -407,7 +403,7 @@ export class MultiAgentOrchestrator {
streaming: true,
};
}

// Check if we should save the conversation
if (classifierResult?.selectedAgent.saveChat) {
await saveConversationExchange(
Expand All @@ -431,7 +427,7 @@ export class MultiAgentOrchestrator {
this.logger.error("Error during agent dispatch or processing:", error);
return {
metadata: this.createMetadata(classifierResult, userInput, userId, sessionId, additionalParams),
output: this.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE,
output: this.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE ? this.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE: error,
streaming: false,
};
} finally {
Expand Down Expand Up @@ -469,7 +465,7 @@ export class MultiAgentOrchestrator {
if (fullResponse) {



if (agent.saveChat) {
await saveConversationExchange(
userInput,
Expand All @@ -480,7 +476,7 @@ export class MultiAgentOrchestrator {
agent.id
);
}

} else {
this.logger.warn("No data accumulated, messages not saved");
}
Expand Down

0 comments on commit 9469fa2

Please sign in to comment.