Skip to content

Commit

Permalink
raising error all the way up
Browse files Browse the repository at this point in the history
  • Loading branch information
brnaba-aws committed Oct 15, 2024
1 parent 43363db commit 5eec25c
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 31 deletions.
12 changes: 7 additions & 5 deletions examples/python-demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ async def handle_request(_orchestrator: MultiAgentOrchestrator, _user_input:str,
# Print metadata
print("\nMetadata:")
print(f"Selected Agent: {response.metadata.agent_name}")
if response.streaming:
print('Response:', response.output.content[0]['text'])
else:
print('Response:', response.output.content[0]['text'])
if isinstance(response, AgentResponse) and response.streaming is False:
# Handle regular response
if isinstance(response.output, str):
print(response.output)
elif isinstance(response.output, ConversationMessage):
print(response.output.content[0].get('text'))

def custom_input_payload_encoder(input_text: str,
chat_history: List[Any],
Expand Down Expand Up @@ -61,7 +63,7 @@ def custom_output_payload_decoder(response: Dict[str, Any]) -> Any:
LOG_EXECUTION_TIMES=True,
MAX_RETRIES=3,
USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED=True,
MAX_MESSAGE_PAIRS_PER_AGENT=10
MAX_MESSAGE_PAIRS_PER_AGENT=10,
))

# Add some agents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,4 @@ async def process_request(

except (BotoCoreError, ClientError) as error:
Logger.error(f"Error processing request: {str(error)}")
return ConversationMessage(
role=ParticipantRole.ASSISTANT.value,
content=[{"text": "Sorry, I encountered an error while processing your request."}]
)
raise error
10 changes: 5 additions & 5 deletions python/src/multi_agent_orchestrator/agents/anthropic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ async def process_request(
)

except Exception as error:
print(f"Error processing request: {error}")
raise str(error)
Logger.error(f"Error processing request: {error}")
raise error

async def handle_single_response(self, input_data: Dict) -> Any:
try:
response = self.client.messages.create(**input_data)
return response
except Exception as error:
print(f"Error invoking Anthropic: {error}")
raise str(error)
Logger.error(f"Error invoking Anthropic: {error}")
raise error

async def handle_streaming_response(self, input) -> Any:
message = {}
Expand All @@ -211,7 +211,7 @@ async def handle_streaming_response(self, input) -> Any:

except Exception as error:
Logger.error(f"Error getting stream from Anthropic model: {str(error)}")
raise str(error)
raise error


def set_system_prompt(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async def handle_single_response(self, converse_input: Dict[str, Any]) -> Conver
)
except Exception as error:
Logger.error(f"Error invoking Bedrock model:{str(error)}")
raise
raise error

async def handle_streaming_response(self, converse_input: Dict[str, Any]) -> ConversationMessage:
try:
Expand Down Expand Up @@ -220,7 +220,7 @@ async def handle_streaming_response(self, converse_input: Dict[str, Any]) -> Con

except Exception as error:
Logger.error(f"Error getting stream from Bedrock model: {str(error)}")
raise
raise error

def set_system_prompt(self,
template: Optional[str] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def process_request(self,
raise ValueError("No valid tool use found in the response")
except Exception as error:
Logger.error(f"Error processing translation request:{str(error)}")
raise
raise error

def set_source_language(self, language: Optional[str]):
"""Set the source language for translation"""
Expand Down
2 changes: 1 addition & 1 deletion python/src/multi_agent_orchestrator/agents/chain_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def process_request(

except Exception as error:
Logger.logger.error(f"Error processing request with agent {agent.name}:{str(error)}")
return self.create_default_response()
raise f"Error processing request with agent {agent.name}:{str(error)}"

return final_response

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def process_request(self,

except Exception as error:
Logger.logger.error(f"Error in ComprehendContentFilterAgent:{str(error)}")
raise
raise error

def add_custom_check(self, check: CheckFunction):
self.custom_checks.append(check)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ async def process_request(self, input_text: str, user_id: str, session_id: str,

except (BotoCoreError, ClientError) as error:
Logger.error(f"Error processing request: {str(error)}")
raise
raise error

Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ async def process_request(self,

except (BotoCoreError, ClientError) as error:
Logger.error(f"Error processing request:{str(error)}")
raise
raise error
7 changes: 5 additions & 2 deletions python/src/multi_agent_orchestrator/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ async def route_request(self,
user_id,
session_id,
additional_params),
output=self.config.CLASSIFICATION_ERROR_MESSAGE,
output=self.config.CLASSIFICATION_ERROR_MESSAGE
if self.config.CLASSIFICATION_ERROR_MESSAGE else
str(error),
streaming=False
)

Expand Down Expand Up @@ -196,7 +198,8 @@ async def route_request(self,
user_id,
session_id,
additional_params),
output= self.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE,
output = self.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE \
if self.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE else str(error),
streaming=False
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def save_chat_message(
self.table.put_item(Item=item)
except Exception as error:
Logger.logger.error(f"Error saving conversation to DynamoDB:{str(error)}")
raise
raise error

return self._remove_timestamps(trimmed_conversation)

Expand All @@ -77,7 +77,7 @@ async def fetch_chat(
return self._remove_timestamps(stored_messages)
except Exception as error:
Logger.logger.error(f"Error getting conversation from DynamoDB:{str(error)}")
raise
raise error

async def fetch_chat_with_timestamp(
self,
Expand All @@ -94,7 +94,7 @@ async def fetch_chat_with_timestamp(
return stored_messages
except Exception as error:
Logger.logger.error(f"Error getting conversation from DynamoDB: {str(error)}")
raise
raise error

async def fetch_all_chats(self, user_id: str, session_id: str) -> List[ConversationMessage]:
try:
Expand Down Expand Up @@ -135,7 +135,7 @@ async def fetch_all_chats(self, user_id: str, session_id: str) -> List[Conversat
return self._remove_timestamps(all_chats)
except Exception as error:
Logger.logger.error(f"Error querying conversations from DynamoDB:{str(error)}")
raise
raise error

def _generate_key(self, user_id: str, session_id: str, agent_id: str) -> str:
return f"{session_id}#{agent_id}"
Expand Down
6 changes: 2 additions & 4 deletions python/src/multi_agent_orchestrator/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ class OrchestratorConfig:
LOG_EXECUTION_TIMES: bool = False # pylint: disable=invalid-name
MAX_RETRIES: int = 3 # pylint: disable=invalid-name
USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED: bool = True # pylint: disable=invalid-name
CLASSIFICATION_ERROR_MESSAGE: str = "I'm sorry, an error occurred while processing \
your request.Please try again later." # pylint: disable=invalid-name
CLASSIFICATION_ERROR_MESSAGE: str = None
NO_SELECTED_AGENT_MESSAGE: str = "I'm sorry, I couldn't determine how to handle your request.\
Could you please rephrase it?" # pylint: disable=invalid-name
GENERAL_ROUTING_ERROR_MSG_MESSAGE: str = "An error occurred while processing your request. \
Please try again later." # pylint: disable=invalid-name
GENERAL_ROUTING_ERROR_MSG_MESSAGE: str = None
MAX_MESSAGE_PAIRS_PER_AGENT: int = 100 # pylint: disable=invalid-name

0 comments on commit 5eec25c

Please sign in to comment.