Logging & Error Handling
How to handle errors and log AI activity
Logging
All logging within Wisej.AI is managed via the ILoggerService. The default implementation is provided by DefaultLoggerService, which forwards the log entries to System.Diagnostics.Trace 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.
static class Program
{
static Program()
{
Application.Services.
.AddOrReplaceService<ILoggerService, MyLoggerService>();
}
}
Error Handling
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 the AI through a SmartAdapter
, and the SmartAdapter
is connected to a SmartHub
(as it should be), you can manage the error using the SmartHub.Error 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.
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.
this.hub1.Error += (s, e) =>
{
if (IsNetworkError(e.Exception)
{
e.ReplacementMessage.Text =
"We're currently experiencing a temporary communication issue. " +
"Please try again later or reach out to support at...";
e.Handled = true;
}
}
Last updated