sherpa_ai.error_handling package#
Overview#
The error_handling
package provides robust error management capabilities for Sherpa AI applications, ensuring graceful recovery from failures and maintaining system stability during agent operations.
Key Components
AgentErrorHandler: Manages and handles errors occurring in agent operations
Error Classification: Categorizes errors for appropriate handling strategies
Recovery Mechanisms: Techniques for recovering from various error types
Example Usage#
from sherpa_ai.error_handling.agent_error_handler import AgentErrorHandler
from sherpa_ai.agents import QAAgent
# Create an error handler
error_handler = AgentErrorHandler()
# Use with an agent in a try-except block
try:
agent = QAAgent()
response = agent.get_response("What is quantum computing?")
except Exception as e:
# Handle the error
handled_response = error_handler.handle(e, fallback_msg="I couldn't process your request.")
print(handled_response)
Submodules#
Module |
Description |
---|---|
Provides the AgentErrorHandler class for managing and recovering from errors. |
sherpa_ai.error_handling.agent_error_handler module#
- class sherpa_ai.error_handling.agent_error_handler.AgentErrorHandler[source]#
Bases:
object
Handles errors that occur during agent execution.
- say#
The function to call to send a message.
- Type:
Callable[[str], None]
- thread_ts#
The thread timestamp of the message.
- Type:
str
- error_map#
The map of errors to messages.
- Type:
Dict[BaseException, str]
- default_response#
The default response to send if an error occurs.
- Type:
str
Example
>>> handler = AgentErrorHandler() >>> handler.run_with_error_handling(my_function, x=5) '10' >>> handler.run_with_error_handling(my_function, x="invalid") 'An unexpected error occurred. Please try again.'
-
say:
Callable
[[str
],None
]#
-
thread_ts:
str
#
-
error_map:
Dict
[BaseException
,str
]#
- run_with_error_handling(executable, **kwargs)[source]#
Run the executable with error handling. If an error occurs, return the error message.
- Parameters:
executable (Callable[[str, List[Dict]], Tuple[str, List[str]]]) – The executable to run with input (question, previous_messages) and returns a tuple of (response, log).
**kwargs – The input arguments for the executable.
- Returns:
The result of the function execution or an error message if an exception occurs.
- Return type:
str
Example
>>> handler = AgentErrorHandler() >>> result = handler.run_with_error_handling(my_function, x=5) >>> print(result) '10' >>> result = handler.run_with_error_handling(my_function, x="invalid") >>> print(result) 'An unexpected error occurred. Please try again.'
Module contents#
Error handling module for Sherpa AI.
This module provides error handling functionality for the Sherpa AI system. It exports the AgentErrorHandler class which handles various types of errors that may occur during agent execution.
Example
>>> from sherpa_ai.error_handling import AgentErrorHandler
>>> handler = AgentErrorHandler()
>>> result = handler.run_with_error_handling(my_function, arg1="value1")
>>> print(result)
'Success' or error message if an error occurred
- class sherpa_ai.error_handling.AgentErrorHandler[source]#
Bases:
object
Handles errors that occur during agent execution.
- say#
The function to call to send a message.
- Type:
Callable[[str], None]
- thread_ts#
The thread timestamp of the message.
- Type:
str
- error_map#
The map of errors to messages.
- Type:
Dict[BaseException, str]
- default_response#
The default response to send if an error occurs.
- Type:
str
Example
>>> handler = AgentErrorHandler() >>> handler.run_with_error_handling(my_function, x=5) '10' >>> handler.run_with_error_handling(my_function, x="invalid") 'An unexpected error occurred. Please try again.'
- run_with_error_handling(executable, **kwargs)[source]#
Run the executable with error handling. If an error occurs, return the error message.
- Parameters:
executable (Callable[[str, List[Dict]], Tuple[str, List[str]]]) – The executable to run with input (question, previous_messages) and returns a tuple of (response, log).
**kwargs – The input arguments for the executable.
- Returns:
The result of the function execution or an error message if an exception occurs.
- Return type:
str
Example
>>> handler = AgentErrorHandler() >>> result = handler.run_with_error_handling(my_function, x=5) >>> print(result) '10' >>> result = handler.run_with_error_handling(my_function, x="invalid") >>> print(result) 'An unexpected error occurred. Please try again.'
-
say:
Callable
[[str
],None
]#
-
thread_ts:
str
#
-
error_map:
Dict
[BaseException
,str
]#