Merge pull request #115 from hchen2020/master

Catch and log function call error.
This commit is contained in:
Haiping 2023-08-24 07:20:32 -05:00 committed by GitHub
commit 88a69f3d1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 28 deletions

View file

@ -8,4 +8,5 @@ public class AgentSettings
public string RouterId { get; set; }
public string DataDir { get; set; }
public string TemplateFormat { get; set; }
public int MaxRecursiveDepth { get; set; } = 3;
}

View file

@ -1,24 +0,0 @@
using System.Text.Json.Serialization;
namespace BotSharp.Abstraction.Functions.Models;
public class FunctionExecutionValidationResult
{
public FunctionExecutionValidationResult()
{
}
public FunctionExecutionValidationResult(string validationStatus, string? validationMessage = null)
{
ValidationStatus = validationStatus;
ValidationMessage = validationMessage;
}
[JsonPropertyName("validation_status")]
public string ValidationStatus { get; set; }
[JsonPropertyName("validation_message")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string ValidationMessage { get; set; }
}

View file

@ -29,9 +29,17 @@ public partial class ConversationService
await hook.OnFunctionExecuting(msg);
}
// Execute function
await fn.Execute(msg);
try
{
// Execute function
await fn.Execute(msg);
}
catch (Exception ex)
{
msg.ExecutionResult = ex.Message;
_logger.LogError(msg.ExecutionResult);
}
// After functions have been executed
foreach (var hook in hooks)
{

View file

@ -7,13 +7,13 @@ namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
const int maxRecursiveDepth = 3;
int currentRecursiveDepth = 0;
private async Task<bool> GetChatCompletionsAsyncRecursively(IChatCompletion chatCompletion,
string conversationId,
Agent agent,
List<RoleDialogModel> wholeDialogs,
int maxRecursiveDepth,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
@ -83,6 +83,7 @@ public partial class ConversationService
conversationId,
agent,
wholeDialogs,
maxRecursiveDepth,
onMessageReceived,
onFunctionExecuting,
onFunctionExecuted);

View file

@ -65,11 +65,14 @@ public partial class ConversationService
await hook.BeforeCompletion();
}
var agentSettings = _services.GetRequiredService<AgentSettings>();
var chatCompletion = GetChatCompletion();
var result = await GetChatCompletionsAsyncRecursively(chatCompletion,
conversationId,
agent,
wholeDialogs,
agentSettings.MaxRecursiveDepth,
onMessageReceived,
onFunctionExecuting,
onFunctionExecuted);