Change OnFunctionExecution return type.
This commit is contained in:
parent
de2587dcb5
commit
efbc61ec1e
|
|
@ -46,9 +46,9 @@ public abstract class ConversationCompletionHookBase : IConversationCompletionHo
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual async Task<IFunctionExecutionResult> OnFunctionExecution(string name, string args)
|
||||
public virtual async Task<string> OnFunctionExecution(string name, string args)
|
||||
{
|
||||
return new FunctionExecutionValidationResult("true", "");
|
||||
return "{}";
|
||||
}
|
||||
|
||||
public virtual Task AfterCompletion(RoleDialogModel message)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,6 @@ public interface IConversationCompletionHook
|
|||
IConversationCompletionHook SetChatCompletion(IChatCompletion chatCompletion);
|
||||
|
||||
Task BeforeCompletion();
|
||||
Task<IFunctionExecutionResult> OnFunctionExecution(string name, string args);
|
||||
Task<string> OnFunctionExecution(string name, string args);
|
||||
Task AfterCompletion(RoleDialogModel message);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,20 +2,17 @@ using System.Text.Json.Serialization;
|
|||
|
||||
namespace BotSharp.Abstraction.Conversations.Models;
|
||||
|
||||
public class FunctionExecutionValidationResult : IFunctionExecutionResult
|
||||
public class FunctionExecutionValidationResult
|
||||
{
|
||||
private string _validationStatus;
|
||||
public string _validationMessage;
|
||||
|
||||
public FunctionExecutionValidationResult(string validationStatus, string validationMessage = "")
|
||||
{
|
||||
_validationStatus = validationStatus;
|
||||
_validationMessage = validationMessage;
|
||||
ValidationStatus = validationStatus;
|
||||
ValidationMessage = validationMessage;
|
||||
}
|
||||
|
||||
[JsonPropertyName("validation_status")]
|
||||
public string ValidationStatus => _validationStatus;
|
||||
public string ValidationStatus { get; set; }
|
||||
|
||||
[JsonPropertyName("validation_message")]
|
||||
public string ValidationMessage => _validationMessage;
|
||||
public string ValidationMessage { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Abstraction.Conversations.Models;
|
||||
|
||||
public class IFunctionExecutionResult
|
||||
{
|
||||
[JsonPropertyName("execution_status")]
|
||||
public FunctionExecutionStatus ExecutionStatus { get; set; }
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ public class RoleDialogModel
|
|||
/// <summary>
|
||||
/// Function name if LLM response function call
|
||||
/// </summary>
|
||||
public string? Function { get; set; }
|
||||
public string? FunctionName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Function execution result
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ using BotSharp.Abstraction.Conversations.Models;
|
|||
using BotSharp.Abstraction.Conversations.Settings;
|
||||
using BotSharp.Abstraction.Knowledges.Models;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using MongoDB.Bson.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace BotSharp.Core.Conversations.Services;
|
||||
|
|
@ -76,16 +78,21 @@ public class ConversationService : IConversationService
|
|||
|
||||
var response = await SendMessage(agentId, conversationId, wholeDialogs, async msg =>
|
||||
{
|
||||
var content = msg.Content.Replace("\r", " ").Replace("\n", " ");
|
||||
if (msg.Role == "function")
|
||||
{
|
||||
content += $"[{msg.Function}] {content}";
|
||||
_storage.Append(agentId, conversationId, new RoleDialogModel(msg.Role, content));
|
||||
var result = msg.ExecutionResult.Replace("\r", " ").Replace("\n", " ");
|
||||
var content = $"{msg.FunctionName} {result}";
|
||||
Console.WriteLine(content);
|
||||
/*_storage.Append(agentId, conversationId, new RoleDialogModel(msg.Role, content)
|
||||
{
|
||||
FunctionName = msg.FunctionName,
|
||||
});*/
|
||||
}
|
||||
else
|
||||
{
|
||||
await onMessageReceived(msg);
|
||||
var content = msg.Content.Replace("\r", " ").Replace("\n", " ");
|
||||
_storage.Append(agentId, conversationId, new RoleDialogModel(msg.Role, content));
|
||||
await onMessageReceived(msg);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -129,8 +136,7 @@ public class ConversationService : IConversationService
|
|||
// Execute functions
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
var executionResult = await hook.OnFunctionExecution(msg.Function, msg.Content);
|
||||
msg.ExecutionResult = JsonSerializer.Serialize(executionResult);
|
||||
msg.ExecutionResult = await hook.OnFunctionExecution(msg.FunctionName, msg.Content);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -140,9 +146,8 @@ public class ConversationService : IConversationService
|
|||
{
|
||||
await hook.AfterCompletion(msg);
|
||||
}
|
||||
|
||||
await onMessageReceived(msg);
|
||||
}
|
||||
await onMessageReceived(msg);
|
||||
});
|
||||
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -105,19 +105,24 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
Console.Write(message.FunctionCall.Arguments);
|
||||
var funcContextIn = new RoleDialogModel(ChatRole.Function.ToString(), message.FunctionCall.Arguments)
|
||||
{
|
||||
Function = message.FunctionCall.Name
|
||||
FunctionName = message.FunctionCall.Name
|
||||
};
|
||||
|
||||
await onMessageReceived(funcContextIn);
|
||||
|
||||
// After function is executed, pass the result to LLM
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write(message.Content);
|
||||
await onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), message.Content));
|
||||
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.Function, funcContextIn.ExecutionResult)
|
||||
{
|
||||
Name = funcContextIn.FunctionName
|
||||
});
|
||||
response = client.GetChatCompletions(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
|
||||
}
|
||||
|
||||
choice = response.Value.Choices[0];
|
||||
message = choice.Message;
|
||||
Console.Write(message.Content);
|
||||
await onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), message.Content));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue