2023-08-28 03:50:10 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
|
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2023-08-19 12:50:26 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
|
|
|
|
using BotSharp.Abstraction.MLTasks;
|
2023-08-28 03:50:10 +00:00
|
|
|
using BotSharp.Core.Routing;
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Conversations.Services;
|
|
|
|
|
|
|
|
|
|
public partial class ConversationService
|
|
|
|
|
{
|
|
|
|
|
public async Task<bool> SendMessage(string agentId, string conversationId,
|
|
|
|
|
RoleDialogModel lastDialog,
|
|
|
|
|
Func<RoleDialogModel, Task> onMessageReceived,
|
2023-08-20 19:02:59 +00:00
|
|
|
Func<RoleDialogModel, Task> onFunctionExecuting,
|
|
|
|
|
Func<RoleDialogModel, Task> onFunctionExecuted)
|
2023-08-19 12:50:26 +00:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// conversation state
|
|
|
|
|
var stateService = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
stateService.SetConversation(conversationId);
|
|
|
|
|
stateService.Load();
|
2023-08-20 22:53:53 +00:00
|
|
|
stateService.SetState("channel", lastDialog.Channel);
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
var router = _services.GetRequiredService<IAgentRouting>();
|
2023-08-28 03:50:10 +00:00
|
|
|
Agent agent = await router.LoadRouter();
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
_logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
|
|
|
|
|
|
|
|
|
|
lastDialog.CurrentAgentId = agent.Id;
|
2023-08-20 22:53:53 +00:00
|
|
|
|
2023-08-19 12:50:26 +00:00
|
|
|
var wholeDialogs = GetDialogHistory(conversationId);
|
2023-08-20 22:53:53 +00:00
|
|
|
wholeDialogs.Add(lastDialog);
|
|
|
|
|
|
|
|
|
|
_storage.Append(conversationId, agent.Id, lastDialog);
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
// Get relevant domain knowledge
|
|
|
|
|
/*if (_settings.EnableKnowledgeBase)
|
|
|
|
|
{
|
|
|
|
|
var knowledge = _services.GetRequiredService<IKnowledgeService>();
|
|
|
|
|
agent.Knowledges = await knowledge.GetKnowledges(new KnowledgeRetrievalModel
|
|
|
|
|
{
|
|
|
|
|
AgentId = agentId,
|
|
|
|
|
Question = string.Join("\n", wholeDialogs.Select(x => x.Content))
|
|
|
|
|
});
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
var hooks = _services.GetServices<IConversationHook>().ToList();
|
|
|
|
|
|
|
|
|
|
// Before chat completion hook
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
hook.SetAgent(agent)
|
|
|
|
|
.SetConversation(converation);
|
|
|
|
|
|
|
|
|
|
await hook.OnDialogsLoaded(wholeDialogs);
|
|
|
|
|
await hook.BeforeCompletion();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 03:50:10 +00:00
|
|
|
// reasoning
|
|
|
|
|
if (_settings.EnableReasoning)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
agent = await agentService.LoadAgent(reasonedContext.CurrentAgentId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simulator.Dialogs.ForEach(x =>
|
|
|
|
|
{
|
|
|
|
|
wholeDialogs.Add(x);
|
|
|
|
|
_storage.Append(conversationId, agent.Id, x);
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-08-24 12:18:41 +00:00
|
|
|
|
2023-08-19 12:50:26 +00:00
|
|
|
var chatCompletion = GetChatCompletion();
|
|
|
|
|
var result = await GetChatCompletionsAsyncRecursively(chatCompletion,
|
|
|
|
|
conversationId,
|
|
|
|
|
agent,
|
|
|
|
|
wholeDialogs,
|
|
|
|
|
onMessageReceived,
|
2023-08-20 19:02:59 +00:00
|
|
|
onFunctionExecuting,
|
|
|
|
|
onFunctionExecuted);
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
2023-08-19 12:50:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
}
|
2023-08-19 12:50:26 +00:00
|
|
|
}
|