This commit is contained in:
Haiping Chen 2023-11-11 23:16:52 -06:00
parent f12f6c63c3
commit 6a2343c757
5 changed files with 148 additions and 0 deletions

View file

@ -3,7 +3,21 @@ namespace BotSharp.Abstraction.Agents.Enums;
public class AgentRole
{
public const string System = "system";
/// <summary>
/// AI Assistant
/// </summary>
public const string Assistant = "assistant";
/// <summary>
/// Client
/// </summary>
public const string User = "user";
public const string Function = "function";
/// <summary>
/// Customer service representative (CSR)
/// </summary>
public const string CSR = "csr";
}

View file

@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Planning;
/// <summary>
/// Task breakdown and execution plan
/// https://www.promptingguide.ai/techniques/cot
/// </summary>
public interface IPlaner
{

View file

@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,47 @@
using Microsoft.AspNetCore.SignalR;
namespace BotSharp.OpenAPI;
public class ChatHub : Hub
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
private readonly IUserIdentity _user;
public ChatHub(IServiceProvider services,
ILogger<ChatHub> logger,
IUserIdentity user)
{
_services = services;
_logger = logger;
_user = user;
}
public async Task OnMessageReceivedFromClient(string message)
{
// await Clients.User(_user.Id).SendAsync("ReceiveMessage", message);
}
/// <summary>
/// Received message from client
/// </summary>
/// <param name="user"></param>
/// <param name="message"></param>
/// <returns></returns>
public async Task SendMessage(string user, string message)
{
// await Clients.User(_user.Id).SendAsync("ReceiveMessage", message);
}
public async Task SendMessageToCaller(string user, string message)
=> await Clients.Caller.SendAsync("ReceiveMessage", user, message);
public async Task SendMessageToGroup(string user, string message)
=> await Clients.Group("SignalR Users").SendAsync("ReceiveMessage", user, message);
public override Task OnConnectedAsync()
{
Console.WriteLine($"ChatHub: {""} connected in {Context.ConnectionId}");
return base.OnConnectedAsync();
}
}

View file

@ -0,0 +1,85 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.OpenAPI.ViewModels.Conversations;
using Microsoft.AspNetCore.SignalR;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class ChatHubController : ControllerBase, IApiAdapter
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
private readonly IHubContext<ChatHub> _chatHub;
private readonly IUserIdentity _user;
public ChatHubController(IServiceProvider services,
ILogger<ChatHubController> logger,
IHubContext<ChatHub> chatHub,
IUserIdentity user)
{
_services = services;
_logger = logger;
_chatHub = chatHub;
_user = user;
}
[HttpPost("/chat-hub/client/{agentId}/{conversationId}")]
public async Task OnMessageReceivedFromClient([FromRoute] string agentId,
[FromRoute] string conversationId,
[FromBody] NewMessageModel input)
{
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(conversationId, input.States);
conv.States.SetState("channel", input.Channel);
var response = new MessageResponseModel();
var inputMsg = new RoleDialogModel("user", input.Text);
await conv.SendMessage(agentId, inputMsg,
async msg =>
{
response.Text = msg.Content;
response.Function = msg.FunctionName;
response.RichContent = msg.RichContent;
response.Instruction = msg.Instruction;
response.Data = msg.Data;
},
async fnExecuting =>
{
},
async fnExecuted =>
{
});
var state = _services.GetRequiredService<IConversationStateService>();
response.States = state.GetStates();
response.MessageId = inputMsg.MessageId;
// Update console conversation UI for CSR
await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromClient", new RoleDialogModel(AgentRole.User, input.Text)
{
});
await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromAssistant", new RoleDialogModel(AgentRole.Assistant, response.Text)
{
});
}
[HttpPost("/chat-hub/csr/{agentId}/{conversationId}")]
public async Task OnMessageReceivedFromCsr([FromRoute] string agentId,
[FromRoute] string conversationId,
[FromBody] NewMessageModel input)
{
// Update console conversation UI for User
await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromCsr", new RoleDialogModel(AgentRole.CSR, input.Text)
{
});
}
}