get channel from conversation
This commit is contained in:
parent
096868b229
commit
e3b0a7f075
|
|
@ -16,13 +16,15 @@ public interface IConversationService
|
|||
/// Send message to LLM
|
||||
/// </summary>
|
||||
/// <param name="agentId"></param>
|
||||
/// <param name="channel"></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,
|
||||
string channel,
|
||||
RoleDialogModel lastDalog,
|
||||
Func<RoleDialogModel, Task> onMessageReceived,
|
||||
Func<RoleDialogModel, Task> onFunctionExecuting,
|
||||
|
|
|
|||
|
|
@ -9,13 +9,14 @@ namespace BotSharp.Core.Conversations.Services;
|
|||
|
||||
public partial class ConversationService
|
||||
{
|
||||
public async Task<bool> SendMessage(string agentId,
|
||||
public async Task<bool> SendMessage(string agentId,
|
||||
string channel,
|
||||
RoleDialogModel message,
|
||||
Func<RoleDialogModel, Task> onMessageReceived,
|
||||
Func<RoleDialogModel, Task> onFunctionExecuting,
|
||||
Func<RoleDialogModel, Task> onFunctionExecuted)
|
||||
{
|
||||
var conversation = await GetConversationRecord(agentId);
|
||||
var conversation = await GetConversationRecord(agentId, channel);
|
||||
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
Agent agent = await agentService.LoadAgent(agentId);
|
||||
|
|
@ -69,16 +70,17 @@ public partial class ConversationService
|
|||
return true;
|
||||
}
|
||||
|
||||
private async Task<Conversation> GetConversationRecord(string agentId)
|
||||
private async Task<Conversation> GetConversationRecord(string agentId, string channel)
|
||||
{
|
||||
var converation = await GetConversation(_conversationId);
|
||||
|
||||
// Create conversation if this conversation not exists
|
||||
// Create conversation if this conversation does not exist
|
||||
if (converation == null)
|
||||
{
|
||||
var sess = new Conversation
|
||||
{
|
||||
Id = _conversationId,
|
||||
Channel = channel,
|
||||
AgentId = agentId
|
||||
};
|
||||
converation = await NewConversation(sess);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using BotSharp.Abstraction.Conversations.Enums;
|
||||
using BotSharp.Abstraction.Evaluations;
|
||||
using BotSharp.Abstraction.Evaluations.Models;
|
||||
using BotSharp.Abstraction.Evaluations.Settings;
|
||||
|
|
@ -92,7 +93,8 @@ public class EvaluatingService : IEvaluatingService
|
|||
RoleDialogModel response = default;
|
||||
|
||||
await conv.SendMessage(agentId,
|
||||
new RoleDialogModel("user", text),
|
||||
ConversationChannel.OpenAPI,
|
||||
new RoleDialogModel(AgentRole.User, text),
|
||||
async msg => response = msg,
|
||||
fnExecuting => Task.CompletedTask,
|
||||
fnExecuted => Task.CompletedTask);
|
||||
|
|
|
|||
|
|
@ -712,7 +712,7 @@ public class FileRepository : IBotSharpRepository
|
|||
public Conversation GetConversation(string conversationId)
|
||||
{
|
||||
var convDir = FindConversationDirectory(conversationId);
|
||||
if (!string.IsNullOrEmpty(convDir)) return null;
|
||||
if (string.IsNullOrEmpty(convDir)) return null;
|
||||
|
||||
var convFile = Path.Combine(convDir, "conversation.json");
|
||||
var content = File.ReadAllText(convFile);
|
||||
|
|
|
|||
|
|
@ -143,11 +143,12 @@ 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 conversation = db.GetConversation(state.GetConversationId());
|
||||
var channel = conversation?.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;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
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 +31,7 @@ public class ConversationController : ControllerBase, IApiAdapter
|
|||
var conv = new Conversation
|
||||
{
|
||||
AgentId = agentId,
|
||||
Channel = ConversationChannel.OpenAPI,
|
||||
UserId = _user.Id
|
||||
};
|
||||
conv = await service.NewConversation(conv);
|
||||
|
|
@ -97,15 +99,14 @@ 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);
|
||||
conv.States.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);
|
||||
await conv.SendMessage(agentId, inputMsg,
|
||||
await conv.SendMessage(agentId, input.Channel, inputMsg,
|
||||
async msg =>
|
||||
{
|
||||
response.Text = msg.Content;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,12 +77,12 @@ 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);
|
||||
.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,
|
||||
input.Channel,
|
||||
message,
|
||||
async msg =>
|
||||
await OnChunkReceived(outputStream, msg),
|
||||
|
|
|
|||
|
|
@ -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>();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Conversations.Enums;
|
||||
using BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||
using BotSharp.Abstraction.Utilities;
|
||||
|
||||
|
|
@ -46,60 +48,58 @@ public class MessageHandleService
|
|||
|
||||
// Go to LLM
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
conv.SetConversationId(sender, new List<string>
|
||||
{
|
||||
"channel=messenger"
|
||||
});
|
||||
conv.SetConversationId(sender, new List<string>());
|
||||
|
||||
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, ConversationChannel.Messenger,
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
$"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,
|
||||
ConversationChannel.Phone,
|
||||
new RoleDialogModel(AgentRole.User, input.SpeechResult),
|
||||
async msg =>
|
||||
{
|
||||
response = HangUp(msg.Content);
|
||||
}, async functionExecuting =>
|
||||
{
|
||||
}, async functionExecuted =>
|
||||
{
|
||||
});
|
||||
|
||||
return TwiML(response);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue