BotSharp/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs

152 lines
5.1 KiB
C#
Raw Normal View History

2023-08-28 03:50:10 +00:00
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Routing.Settings;
2023-08-28 03:50:10 +00:00
using BotSharp.Core.Routing;
namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
2023-09-06 03:19:36 +00:00
public async Task<bool> SendMessage(string agentId,
RoleDialogModel lastDialog,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
{
2023-09-06 03:19:36 +00:00
var conversation = await GetConversationRecord(agentId);
var agentService = _services.GetRequiredService<IAgentService>();
Agent agent = await agentService.LoadAgent(agentId);
_logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
lastDialog.CurrentAgentId = agent.Id;
2023-08-20 22:53:53 +00:00
2023-09-06 03:19:36 +00:00
var wholeDialogs = GetDialogHistory();
2023-08-20 22:53:53 +00:00
wholeDialogs.Add(lastDialog);
2023-09-06 03:19:36 +00:00
_storage.Append(_conversationId, agent.Id, lastDialog);
var hooks = _services.GetServices<IConversationHook>().ToList();
// Before chat completion hook
foreach (var hook in hooks)
{
hook.SetAgent(agent)
2023-09-06 03:19:36 +00:00
.SetConversation(conversation);
await hook.OnDialogsLoaded(wholeDialogs);
2023-08-31 02:09:38 +00:00
await hook.BeforeCompletion(lastDialog);
// Interrupted by hook
if (lastDialog.StopCompletion)
{
var response = new RoleDialogModel(AgentRole.Assistant, lastDialog.Content);
await onMessageReceived(response);
2023-09-06 03:19:36 +00:00
_storage.Append(_conversationId, agent.Id, response);
2023-08-31 02:09:38 +00:00
return true;
}
}
2023-08-28 03:50:10 +00:00
// reasoning
var settings = _services.GetRequiredService<RoutingSettings>();
if (settings.ReasonerId == agent.Id)
2023-08-28 03:50:10 +00:00
{
var simulator = _services.GetRequiredService<Simulator>();
var reasonedContext = await simulator.Enter(agent, wholeDialogs);
if (reasonedContext.FunctionName == "interrupt_task_execution")
{
await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, reasonedContext.Content)
{
CurrentAgentId = agent.Id,
2023-08-28 15:58:35 +00:00
Channel = lastDialog.Channel
}, onMessageReceived);
return true;
}
else if (reasonedContext.FunctionName == "response_to_user")
{
await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, reasonedContext.Content)
{
CurrentAgentId = agent.Id,
2023-08-28 03:50:10 +00:00
Channel = lastDialog.Channel
}, onMessageReceived);
return true;
}
else if (reasonedContext.FunctionName == "continue_execute_task")
{
if (reasonedContext.CurrentAgentId != agent.Id)
{
agent = await agentService.LoadAgent(reasonedContext.CurrentAgentId);
}
}
simulator.Dialogs.ForEach(x =>
{
wholeDialogs.Add(x);
2023-09-06 03:19:36 +00:00
if (x.Content != null)
{
_storage.Append(_conversationId, agent.Id, x);
}
2023-08-28 03:50:10 +00:00
});
}
2023-08-24 12:18:41 +00:00
var chatCompletion = GetChatCompletion();
var result = await GetChatCompletionsAsyncRecursively(chatCompletion,
agent,
wholeDialogs,
onMessageReceived,
onFunctionExecuting,
onFunctionExecuted);
return result;
}
2023-09-06 03:19:36 +00:00
private async Task<Conversation> GetConversationRecord(string agentId)
{
var converation = await GetConversation(_conversationId);
// Create conversation if this conversation not exists
if (converation == null)
{
var sess = new Conversation
{
Id = _conversationId,
AgentId = agentId
};
converation = await NewConversation(sess);
}
return converation;
}
private void SaveStateByArgs(string args)
{
var stateService = _services.GetRequiredService<IConversationStateService>();
var jo = JsonSerializer.Deserialize<object>(args);
if (jo is JsonElement root)
{
foreach (JsonProperty property in root.EnumerateObject())
{
2023-08-22 04:48:17 +00:00
if (!string.IsNullOrEmpty(property.Value.ToString()))
{
stateService.SetState(property.Name, property.Value.ToString());
}
}
}
}
public IChatCompletion GetChatCompletion()
{
var completions = _services.GetServices<IChatCompletion>();
return completions.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.ChatCompletion));
}
2023-08-28 03:50:10 +00:00
public IChatCompletion GetGpt4ChatCompletion()
{
var completions = _services.GetServices<IChatCompletion>();
return completions.FirstOrDefault(x => x.GetType().FullName.EndsWith("GPT4CompletionProvider"));
}
}