2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Settings;
|
|
|
|
|
using BotSharp.Abstraction.MLTasks;
|
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-06-27 18:31:13 +00:00
|
|
|
public async Task<List<Conversation>> GetConversations()
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
var db = _services.GetRequiredService<AgentDbContext>();
|
|
|
|
|
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-06-27 18:31:13 +00:00
|
|
|
var db = _services.GetRequiredService<AgentDbContext>();
|
|
|
|
|
|
|
|
|
|
var record = ConversationRecord.FromConversation(sess);
|
|
|
|
|
record.Id = Guid.NewGuid().ToString();
|
|
|
|
|
record.UserId = _user.Id;
|
|
|
|
|
record.Title = "New Conversation";
|
|
|
|
|
|
|
|
|
|
db.Transaction<IAgentTable>(delegate
|
|
|
|
|
{
|
|
|
|
|
db.Add<IAgentTable>(record);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_storage.InitStorage(sess.AgentId, record.Id);
|
|
|
|
|
|
|
|
|
|
return record.ToConversation();
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public async Task<string> SendMessage(string agentId, string conversationId, RoleDialogModel lastDalog)
|
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);
|
|
|
|
|
|
|
|
|
|
var response = await SendMessage(agentId, conversationId, wholeDialogs);
|
|
|
|
|
|
|
|
|
|
_storage.Append(agentId, conversationId, new RoleDialogModel
|
|
|
|
|
{
|
|
|
|
|
Role = "assistant",
|
|
|
|
|
Text = response
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return response;
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public async Task<string> SendMessage(string agentId, string conversationId, List<RoleDialogModel> wholeDialogs)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
var agent = await _services.GetRequiredService<IAgentService>().GetAgent(agentId);
|
|
|
|
|
var chat = GetChatCompletion();
|
|
|
|
|
var response = await chat.GetChatCompletionsAsync(agent, wholeDialogs);
|
|
|
|
|
|
|
|
|
|
return response;
|
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>();
|
|
|
|
|
return completions.FirstOrDefault(x => x.GetType().FullName.Contains(_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
|
|
|
}
|