Merge pull request #219 from iceljc/features/add-conversation-filter

Features/add conversation filter
This commit is contained in:
Haiping 2023-11-27 10:12:48 -06:00 committed by GitHub
commit b7d9469cf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 141 additions and 154 deletions

View file

@ -16,13 +16,12 @@ public interface IConversationService
/// Send message to LLM
/// </summary>
/// <param name="agentId"></param>
/// <param name="conversationId"></param>
/// <param name="lastDalog"></param>
/// <param name="onMessageReceived"></param>
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
/// <returns></returns>
Task<bool> SendMessage(string agentId,
Task<bool> SendMessage(string agentId,
RoleDialogModel lastDalog,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting,

View file

@ -35,7 +35,7 @@ public interface IBotSharpRepository
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
void UpdateConversationStatus(string conversationId, string status);
Conversation GetConversation(string conversationId);
List<Conversation> GetConversations(string userId);
List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null);
void UpdateConversationTitle(string conversationId, string title);
List<Conversation> GetLastConversations();
void AddExectionLogs(string conversationId, List<string> logs);

View file

@ -9,7 +9,7 @@ namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
public async Task<bool> SendMessage(string agentId,
public async Task<bool> SendMessage(string agentId,
RoleDialogModel message,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting,
@ -73,12 +73,15 @@ public partial class ConversationService
{
var converation = await GetConversation(_conversationId);
// Create conversation if this conversation not exists
// Create conversation if this conversation does not exist
if (converation == null)
{
var state = _services.GetRequiredService<IConversationStateService>();
var channel = state.GetState("channel");
var sess = new Conversation
{
Id = _conversationId,
Channel = channel,
AgentId = agentId
};
converation = await NewConversation(sess);

View file

@ -57,7 +57,8 @@ public partial class ConversationService : IConversationService
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var user = db.GetUserById(_user.Id);
var conversations = db.GetConversations(user.Role == UserRole.CSR ? null : user?.Id);
var targetUserId = user.Role == UserRole.CSR ? null : user?.Id;
var conversations = db.GetConversations(userId: targetUserId);
return conversations.OrderByDescending(x => x.CreatedTime).ToList();
}

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Evaluations;
using BotSharp.Abstraction.Evaluations.Models;
using BotSharp.Abstraction.Evaluations.Settings;
@ -87,12 +88,15 @@ public class EvaluatingService : IEvaluatingService
private async Task<RoleDialogModel> SendMessage(string agentId, string conversationId, string text)
{
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(conversationId, new List<string>());
conv.SetConversationId(conversationId, new List<string>
{
$"channel={ConversationChannel.OpenAPI}"
});
RoleDialogModel response = default;
await conv.SendMessage(agentId,
new RoleDialogModel("user", text),
new RoleDialogModel(AgentRole.User, text),
async msg => response = msg,
fnExecuting => Task.CompletedTask,
fnExecuted => Task.CompletedTask);

View file

@ -131,7 +131,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
throw new NotImplementedException();
}
public List<Conversation> GetConversations(string userId)
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
{
throw new NotImplementedException();
}

View file

@ -128,12 +128,7 @@ public class FileRepository : IBotSharpRepository
public void Add<TTableInterface>(object entity)
{
if (entity is Conversation conversation)
{
_conversations.Add(conversation);
_changedTableNames.Add(nameof(Conversation));
}
else if (entity is Agent agent)
if (entity is Agent agent)
{
_agents.Add(agent);
_changedTableNames.Add(nameof(Agent));
@ -159,20 +154,7 @@ public class FileRepository : IBotSharpRepository
// Persist to disk
foreach (var table in _changedTableNames)
{
if (table == nameof(Conversation))
{
foreach (var conversation in _conversations)
{
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversation.Id);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var path = Path.Combine(dir, "conversation.json");
File.WriteAllText(path, JsonSerializer.Serialize(conversation, _options));
}
}
else if (table == nameof(Agent))
if (table == nameof(Agent))
{
foreach (var agent in _agents)
{
@ -730,32 +712,29 @@ public class FileRepository : IBotSharpRepository
public Conversation GetConversation(string conversationId)
{
var convDir = FindConversationDirectory(conversationId);
if (!string.IsNullOrEmpty(convDir))
if (string.IsNullOrEmpty(convDir)) return null;
var convFile = Path.Combine(convDir, "conversation.json");
var content = File.ReadAllText(convFile);
var record = JsonSerializer.Deserialize<Conversation>(content, _options);
var dialogFile = Path.Combine(convDir, "dialogs.txt");
if (record != null)
{
var convFile = Path.Combine(convDir, "conversation.json");
var content = File.ReadAllText(convFile);
var record = JsonSerializer.Deserialize<Conversation>(content, _options);
var dialogFile = Path.Combine(convDir, "dialogs.txt");
if (record != null)
{
record.Dialogs = CollectDialogElements(dialogFile);
}
var stateFile = Path.Combine(convDir, "state.dict");
if (record != null)
{
var states = CollectConversationStates(stateFile);
record.States = new ConversationState(states);
}
return record;
record.Dialogs = CollectDialogElements(dialogFile);
}
return null;
var stateFile = Path.Combine(convDir, "state.dict");
if (record != null)
{
var states = CollectConversationStates(stateFile);
record.States = new ConversationState(states);
}
return record;
}
public List<Conversation> GetConversations(string userId)
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
{
var records = new List<Conversation>();
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
@ -767,10 +746,16 @@ public class FileRepository : IBotSharpRepository
var json = File.ReadAllText(path);
var record = JsonSerializer.Deserialize<Conversation>(json, _options);
if (record != null && (record.UserId == userId || userId == null))
{
records.Add(record);
}
if (record == null) continue;
var matched = true;
if (!string.IsNullOrEmpty(agentId)) matched = matched && record.AgentId == agentId;
if (!string.IsNullOrEmpty(status)) matched = matched && record.Status == status;
if (!string.IsNullOrEmpty(channel)) matched = matched && record.Channel == channel;
if (!string.IsNullOrEmpty(userId)) matched = matched && record.UserId == userId;
if (!matched) continue;
records.Add(record);
}
return records;

View file

@ -143,11 +143,11 @@ public partial class RoutingService : IRoutingService
// Filter agents by profile
var state = _services.GetRequiredService<IConversationStateService>();
var name = state.GetState("channel");
var specifiedProfile = agents.FirstOrDefault(x => x.Profiles.Contains(name));
var channel = state.GetState("channel");
var specifiedProfile = agents.FirstOrDefault(x => x.Profiles.Contains(channel));
if (specifiedProfile != null)
{
records = records.Where(x => specifiedProfile.Profiles.Contains(name)).ToArray();
records = records.Where(x => specifiedProfile.Profiles.Contains(channel)).ToArray();
}
return records;

View file

@ -1,4 +1,6 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Models;
using BotSharp.OpenAPI.ViewModels.Conversations;
@ -30,6 +32,7 @@ public class ConversationController : ControllerBase, IApiAdapter
var conv = new Conversation
{
AgentId = agentId,
Channel = ConversationChannel.OpenAPI,
UserId = _user.Id
};
conv = await service.NewConversation(conv);
@ -98,13 +101,13 @@ public class ConversationController : ControllerBase, IApiAdapter
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(conversationId, input.States);
conv.States.SetState("channel", input.Channel)
.SetState("provider", input.Provider)
.SetState("model", input.Model)
.SetState("temperature", input.Temperature)
.SetState("sampling_factor", input.SamplingFactor);
.SetState("provider", input.Provider)
.SetState("model", input.Model)
.SetState("temperature", input.Temperature)
.SetState("sampling_factor", input.SamplingFactor);
var response = new ChatResponseModel();
var inputMsg = new RoleDialogModel("user", input.Text);
var inputMsg = new RoleDialogModel(AgentRole.User, input.Text);
await conv.SendMessage(agentId, inputMsg,
async msg =>
{

View file

@ -1,8 +1,9 @@
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class NewMessageModel : IncomingMessageModel
{
public override string Channel { get; set; } = "openapi";
public override string Channel { get; set; } = ConversationChannel.OpenAPI;
}

View file

@ -1,8 +1,9 @@
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.OpenAPI.ViewModels.Instructs;
public class InstructMessageModel : IncomingMessageModel
{
public override string Channel { get; set; } = "openapi";
public override string Channel { get; set; } = ConversationChannel.OpenAPI;
public string? Template { get; set; }
}

View file

@ -76,13 +76,13 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(input.ConversationId, input.States);
conv.States.SetState("provider", input.Provider)
.SetState("model", input.Model)
.SetState("channel", input.Channel)
.SetState("temperature", input.Temperature)
.SetState("sampling_factor", input.SamplingFactor);
conv.States.SetState("channel", input.Channel)
.SetState("provider", input.Provider)
.SetState("model", input.Model)
.SetState("temperature", input.Temperature)
.SetState("sampling_factor", input.SamplingFactor);
var result = await conv.SendMessage(input.AgentId,
var result = await conv.SendMessage(input.AgentId,
message,
async msg =>
await OnChunkReceived(outputStream, msg),

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
using System.Collections.Generic;
using System.Linq;
@ -9,7 +10,7 @@ public class OpenAiMessageInput : IncomingMessageModel
{
public string AgentId { get; set; } = string.Empty;
public string ConversationId { get; set; } = string.Empty;
public override string Channel { get; set; } = "webchat";
public override string Channel { get; set; } = ConversationChannel.WebChat;
public List<OpenAiMessageBody> Messages { get; set; } = new List<OpenAiMessageBody>();

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Utilities;
@ -48,58 +50,59 @@ public class MessageHandleService
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(sender, new List<string>
{
"channel=messenger"
$"channel={ConversationChannel.Messenger}"
});
var replies = new List<IRichMessage>();
var result = await conv.SendMessage(agentId, new RoleDialogModel("user", message), async msg =>
{
if (msg.RichContent != null)
var result = await conv.SendMessage(agentId,
new RoleDialogModel(AgentRole.User, message), async msg =>
{
// Official API doesn't support to show extra content above the products
if (!string.IsNullOrEmpty(msg.RichContent.Message.Text) &&
// avoid duplicated text
msg.RichContent.Message is not QuickReplyMessage)
if (msg.RichContent != null)
{
replies.Add(new TextMessage(msg.RichContent.Message.Text));
}
// Official API doesn't support to show extra content above the products
if (!string.IsNullOrEmpty(msg.RichContent.Message.Text) &&
// avoid duplicated text
msg.RichContent.Message is not QuickReplyMessage)
{
replies.Add(new TextMessage(msg.RichContent.Message.Text));
}
if (msg.RichContent.Message is GenericTemplateMessage genericTemplate)
{
replies.Add(new AttachmentMessage
if (msg.RichContent.Message is GenericTemplateMessage genericTemplate)
{
Attachment = new AttachmentBody
replies.Add(new AttachmentMessage
{
Payload = genericTemplate
}
});
}
else if (msg.RichContent.Message is CouponTemplateMessage couponTemplate)
{
replies.Add(new AttachmentMessage
Attachment = new AttachmentBody
{
Payload = genericTemplate
}
});
}
else if (msg.RichContent.Message is CouponTemplateMessage couponTemplate)
{
Attachment = new AttachmentBody
replies.Add(new AttachmentMessage
{
Payload = couponTemplate
}
});
}
else if (msg.RichContent.Message is QuickReplyMessage quickReplyMessage)
{
replies.Add(quickReplyMessage);
Attachment = new AttachmentBody
{
Payload = couponTemplate
}
});
}
else if (msg.RichContent.Message is QuickReplyMessage quickReplyMessage)
{
replies.Add(quickReplyMessage);
}
else
{
replies.Add(msg.RichContent.Message);
}
}
else
{
replies.Add(msg.RichContent.Message);
replies.Add(new TextMessage(msg.Content));
}
}
else
{
replies.Add(new TextMessage(msg.Content));
}
},
_ => Task.CompletedTask,
_ => Task.CompletedTask);
},
_ => Task.CompletedTask,
_ => Task.CompletedTask);
// Response to user
foreach(var reply in replies)

View file

@ -7,6 +7,7 @@ public class ConversationCollection : MongoBase
public string AgentId { get; set; }
public string UserId { get; set; }
public string Title { get; set; }
public string Channel { get; set; }
public string Status { get; set; }
public List<StateKeyValue> States { get; set; }
public DateTime CreatedTime { get; set; }

View file

@ -32,12 +32,7 @@ public class MongoRepository : IBotSharpRepository
public void Add<TTableInterface>(object entity)
{
if (entity is Conversation conversation)
{
_conversations.Add(conversation);
_changedTableNames.Add(nameof(Conversation));
}
else if (entity is Agent agent)
if (entity is Agent agent)
{
_agents.Add(agent);
_changedTableNames.Add(nameof(Agent));
@ -61,33 +56,7 @@ public class MongoRepository : IBotSharpRepository
foreach (var table in _changedTableNames)
{
if (table == nameof(Conversation))
{
var conversations = _conversations.Select(x => new ConversationCollection
{
Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(),
AgentId = x.AgentId,
UserId = !string.IsNullOrEmpty(x.UserId) ? x.UserId : string.Empty,
Title = x.Title,
States = x.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
foreach (var conversation in conversations)
{
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, conversation.Id);
var update = Builders<ConversationCollection>.Update
.Set(x => x.AgentId, conversation.AgentId)
.Set(x => x.UserId, conversation.UserId)
.Set(x => x.Title, conversation.Title)
.Set(x => x.States, conversation.States)
.Set(x => x.CreatedTime, conversation.CreatedTime)
.Set(x => x.UpdatedTime, conversation.UpdatedTime);
_dc.Conversations.UpdateOne(filter, update, _options);
}
}
else if (table == nameof(Agent))
if (table == nameof(Agent))
{
var agents = _agents.Select(x => new AgentCollection
{
@ -610,6 +579,7 @@ public class MongoRepository : IBotSharpRepository
AgentId = conversation.AgentId,
UserId = !string.IsNullOrEmpty(conversation.UserId) ? conversation.UserId : string.Empty,
Title = conversation.Title,
Channel = conversation.Channel,
Status = conversation.Status,
States = conversation.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
CreatedTime = DateTime.UtcNow,
@ -670,6 +640,7 @@ public class MongoRepository : IBotSharpRepository
_dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog);
_dc.Conversations.UpdateOne(filterConv, updateConv);
}
public void UpdateConversationTitle(string conversationId, string title)
{
if (string.IsNullOrEmpty(conversationId)) return;
@ -684,6 +655,7 @@ public class MongoRepository : IBotSharpRepository
_dc.Conversations.UpdateOne(filterConv, updateConv);
}
public List<StateKeyValue> GetConversationStates(string conversationId)
{
var states = new List<StateKeyValue>();
@ -745,6 +717,7 @@ public class MongoRepository : IBotSharpRepository
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Channel = conv.Channel,
Status = conv.Status,
Dialogs = dialogElements,
States = new ConversationState(conv.States ?? new List<StateKeyValue>()),
@ -753,13 +726,20 @@ public class MongoRepository : IBotSharpRepository
};
}
public List<Conversation> GetConversations(string userId)
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
{
var records = new List<Conversation>();
if (string.IsNullOrEmpty(userId)) return records;
var filterByUserId = Builders<ConversationCollection>.Filter.Eq(x => x.UserId, userId);
var conversations = _dc.Conversations.Find(filterByUserId).ToList();
var builder = Builders<ConversationCollection>.Filter;
var filters = new List<FilterDefinition<ConversationCollection>>();
if (!string.IsNullOrEmpty(agentId)) filters.Add(builder.Eq(x => x.AgentId, agentId));
if (!string.IsNullOrEmpty(status)) filters.Add(builder.Eq(x => x.Status, status));
if (!string.IsNullOrEmpty(channel)) filters.Add(builder.Eq(x => x.Channel, channel));
if (!string.IsNullOrEmpty(userId)) filters.Add(builder.Eq(x => x.UserId, userId));
var conversations = _dc.Conversations.Find(builder.And(filters)).ToList();
foreach (var conv in conversations)
{
@ -770,6 +750,7 @@ public class MongoRepository : IBotSharpRepository
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Channel = conv.Channel,
Status = conv.Status,
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
@ -791,6 +772,7 @@ public class MongoRepository : IBotSharpRepository
AgentId = c.AgentId.ToString(),
UserId = c.UserId.ToString(),
Title = c.Title,
Channel = c.Channel,
Status = c.Status,
CreatedTime = c.CreatedTime,
UpdatedTime = c.UpdatedTime

View file

@ -15,6 +15,7 @@ using Twilio.Http;
using Twilio.TwiML.Messaging;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Conversations.Enums;
namespace BotSharp.Plugin.Twilio.Controllers;
@ -46,20 +47,22 @@ public class TwilioVoiceController : TwilioController
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(sessionId, new List<string>
{
"channel=phone",
$"channel={ConversationChannel.Phone}",
$"calling_phone={input.DialCallSid}"
});
VoiceResponse response = default;
var result = await conv.SendMessage(agentId, new RoleDialogModel(AgentRole.User, input.SpeechResult), async msg =>
{
response = HangUp(msg.Content);
}, async functionExecuting =>
{
}, async functionExecuted =>
{
});
var result = await conv.SendMessage(agentId,
new RoleDialogModel(AgentRole.User, input.SpeechResult),
async msg =>
{
response = HangUp(msg.Content);
}, async functionExecuting =>
{
}, async functionExecuted =>
{
});
return TwiML(response);
}