Logging & Error Handling
How to handle errors and log AI activity
Last updated
How to handle errors and log AI activity
Last updated
All logging within Wisej.AI is managed via the . The default implementation is provided by , which forwards the log entries to according to the specified TraceLevel
.
You can set up your own logging implementation by registering a service that implements the ILoggerService
interface. The optimal location for this registration is within the static constructor of the Program
class.
When using an AI provider, several types of errors can occur: channel errors, provider errors, server errors, and model errors.
Channel Errors: These involve issues with communication, such as loss of connectivity or network problems.
Provider Errors: These are related to your account with the AI provider, such as exceeding credit or usage limits.
Server Errors: Related to issues on the server side which might affect availability or performance.
Model Errors: These generally pertain to payload construction, token limits, or other constraints specific to the AI model being used.
If an error occurs while using the AI directly through a SmartPrompt.AskAsync
call, you can handle it by wrapping the call in a try/catch block. This allows you to manage exceptions effectively as your code processes the response.
If an error occurs while using a SmartSession
instance, you can handle it by utilizing the SmartSession.Error
event to manage the error.
The error event allows you to modify the last AI response and the assistant's response, effectively allowing you to "simulate" an AI response. To achieve this, you can handle the error (or override the OnError
method) by adjusting the ReplacementMessage
or changing the Text
property of the predefined ReplacementMessage
(which defaults to the exception message). Then, set the Handled
property to true
. This indicates that the error has been managed and that your custom response should be used.
If an error occurs while using the AI through a SmartAdapter
, and the SmartAdapter
is connected to a SmartHub
(as it should be), you can manage the error using the event. However, if the adapter is not connected to a hub and you are using the RunAsync()
method (or its variations) directly, you can manage errors by wrapping the call in a try/catch block.