2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Settings;
|
2023-06-29 23:14:57 +00:00
|
|
|
using BotSharp.Abstraction.Knowledges.Models;
|
2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.MLTasks;
|
2023-07-28 04:27:12 +00:00
|
|
|
using MongoDB.Bson.IO;
|
|
|
|
|
using Newtonsoft.Json;
|
2023-07-27 21:56:57 +00:00
|
|
|
using System.Text.Json;
|
2023-06-03 02:07:30 +00:00
|
|
|
|
2023-06-12 13:28:49 +00:00
|
|
|
namespace BotSharp.Core.Conversations.Services;
|
2023-06-03 02:07:30 +00:00
|
|
|
|
|
|
|
|
public class ConversationService : IConversationService
|
|
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly IUserIdentity _user;
|
|
|
|
|
private readonly ConversationSetting _settings;
|
|
|
|
|
private readonly IConversationStorage _storage;
|
2023-06-03 02:07:30 +00:00
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public ConversationService(IServiceProvider services,
|
|
|
|
|
IUserIdentity user,
|
|
|
|
|
ConversationSetting settings,
|
|
|
|
|
IConversationStorage storage)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
_services = services;
|
|
|
|
|
_user = user;
|
|
|
|
|
_settings = settings;
|
|
|
|
|
_storage = storage;
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public Task DeleteConversation(string id)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
throw new NotImplementedException();
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-19 22:30:23 +00:00
|
|
|
public async Task<Conversation> GetConversation(string id)
|
|
|
|
|
{
|
2023-07-21 21:56:14 +00:00
|
|
|
var db = _services.GetRequiredService<BotSharpDbContext>();
|
2023-07-19 22:30:23 +00:00
|
|
|
var query = from sess in db.Conversation
|
|
|
|
|
where sess.Id == id
|
|
|
|
|
orderby sess.CreatedTime descending
|
|
|
|
|
select sess.ToConversation();
|
|
|
|
|
return query.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public async Task<List<Conversation>> GetConversations()
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-07-21 21:56:14 +00:00
|
|
|
var db = _services.GetRequiredService<BotSharpDbContext>();
|
2023-06-27 18:31:13 +00:00
|
|
|
var query = from sess in db.Conversation
|
|
|
|
|
where sess.UserId == _user.Id
|
|
|
|
|
orderby sess.CreatedTime descending
|
|
|
|
|
select sess.ToConversation();
|
|
|
|
|
return query.ToList();
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public async Task<Conversation> NewConversation(Conversation sess)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-07-21 21:56:14 +00:00
|
|
|
var db = _services.GetRequiredService<BotSharpDbContext>();
|
2023-06-27 18:31:13 +00:00
|
|
|
|
|
|
|
|
var record = ConversationRecord.FromConversation(sess);
|
2023-07-21 15:15:30 +00:00
|
|
|
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
|
|
|
|
|
record.UserId = sess.UserId.IfNullOrEmptyAs(_user.Id);
|
2023-06-27 18:31:13 +00:00
|
|
|
record.Title = "New Conversation";
|
|
|
|
|
|
2023-07-21 21:56:14 +00:00
|
|
|
db.Transaction<IBotSharpTable>(delegate
|
2023-06-27 18:31:13 +00:00
|
|
|
{
|
2023-07-21 21:56:14 +00:00
|
|
|
db.Add<IBotSharpTable>(record);
|
2023-06-27 18:31:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_storage.InitStorage(sess.AgentId, record.Id);
|
|
|
|
|
|
|
|
|
|
return record.ToConversation();
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
public async Task<bool> SendMessage(string agentId, string conversationId, RoleDialogModel lastDalog, Func<RoleDialogModel, Task> onMessageReceived)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
_storage.Append(agentId, conversationId, lastDalog);
|
|
|
|
|
|
|
|
|
|
var wholeDialogs = GetDialogHistory(agentId, conversationId);
|
|
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
var response = await SendMessage(agentId, conversationId, wholeDialogs, async msg =>
|
|
|
|
|
{
|
2023-07-27 21:56:57 +00:00
|
|
|
if (msg.Role == "function")
|
|
|
|
|
{
|
2023-07-28 04:27:12 +00:00
|
|
|
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,
|
|
|
|
|
});*/
|
2023-07-27 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-07-28 04:27:12 +00:00
|
|
|
var content = msg.Content.Replace("\r", " ").Replace("\n", " ");
|
2023-07-27 21:56:57 +00:00
|
|
|
_storage.Append(agentId, conversationId, new RoleDialogModel(msg.Role, content));
|
2023-07-28 04:27:12 +00:00
|
|
|
await onMessageReceived(msg);
|
2023-07-27 21:56:57 +00:00
|
|
|
}
|
2023-07-27 15:07:39 +00:00
|
|
|
});
|
2023-06-27 18:31:13 +00:00
|
|
|
|
|
|
|
|
return response;
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
public async Task<bool> SendMessage(string agentId, string conversationId, List<RoleDialogModel> wholeDialogs, Func<RoleDialogModel, Task> onMessageReceived)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
var agent = await _services.GetRequiredService<IAgentService>().GetAgent(agentId);
|
2023-07-19 22:30:23 +00:00
|
|
|
var converation = await GetConversation(conversationId);
|
2023-06-29 23:14:57 +00:00
|
|
|
|
|
|
|
|
// Get relevant domain knowledge
|
|
|
|
|
if (_settings.EnableKnowledgeBase)
|
|
|
|
|
{
|
|
|
|
|
var knowledge = _services.GetRequiredService<IKnowledgeService>();
|
|
|
|
|
agent.Knowledges = await knowledge.GetKnowledges(new KnowledgeRetrievalModel
|
|
|
|
|
{
|
|
|
|
|
AgentId = agentId,
|
2023-07-21 20:15:09 +00:00
|
|
|
Question = string.Join("\n", wholeDialogs.Select(x => x.Content))
|
2023-06-29 23:14:57 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var chatCompletion = GetChatCompletion();
|
2023-07-19 12:08:40 +00:00
|
|
|
|
|
|
|
|
var hooks = _services.GetServices<IConversationCompletionHook>().ToList();
|
|
|
|
|
|
2023-07-27 21:56:57 +00:00
|
|
|
// Before chat completion hook
|
|
|
|
|
foreach (var hook in hooks)
|
2023-07-19 22:30:23 +00:00
|
|
|
{
|
2023-07-27 21:56:57 +00:00
|
|
|
await hook.SetAgent(agent)
|
2023-07-21 01:55:40 +00:00
|
|
|
.SetConversation(converation)
|
|
|
|
|
.SetDialogs(wholeDialogs)
|
|
|
|
|
.SetChatCompletion(chatCompletion)
|
2023-07-19 22:30:23 +00:00
|
|
|
.BeforeCompletion();
|
2023-07-27 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await chatCompletion.GetChatCompletionsAsync(agent, wholeDialogs, async msg =>
|
2023-07-19 12:08:40 +00:00
|
|
|
{
|
2023-07-27 21:56:57 +00:00
|
|
|
if (msg.Role == "function")
|
|
|
|
|
{
|
|
|
|
|
// Execute functions
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
2023-07-28 04:27:12 +00:00
|
|
|
msg.ExecutionResult = await hook.OnFunctionExecution(msg.FunctionName, msg.Content);
|
2023-07-27 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// After chat completion hook
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
await hook.AfterCompletion(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-28 04:27:12 +00:00
|
|
|
await onMessageReceived(msg);
|
2023-07-19 12:08:40 +00:00
|
|
|
});
|
|
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
return result;
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public IChatCompletion GetChatCompletion()
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
var completions = _services.GetServices<IChatCompletion>();
|
2023-06-27 19:17:53 +00:00
|
|
|
return completions.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.ChatCompletion));
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public Task CleanHistory(string agentId)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2023-06-27 18:31:13 +00:00
|
|
|
|
|
|
|
|
public List<RoleDialogModel> GetDialogHistory(string agentId, string conversationId)
|
|
|
|
|
{
|
|
|
|
|
return _storage.GetDialogs(agentId, conversationId);
|
|
|
|
|
}
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|