Merge pull request #108 from hchen2020/master

Support rich content in UI.
This commit is contained in:
Haiping 2023-08-20 20:46:58 -05:00 committed by GitHub
commit 7cf649d73e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 260 additions and 53 deletions

View file

@ -2,5 +2,6 @@ namespace BotSharp.Abstraction.Agents;
public interface IAgentRouting
{
Task<Agent> LoadRouter();
Task<Agent> LoadCurrentAgent();
}

View file

@ -0,0 +1,9 @@
using System.Text.Json.Serialization;
namespace BotSharp.Abstraction.Agents.Models;
public class AgentRoutingArgs
{
[JsonPropertyName("agent_id")]
public string AgentId { get; set; }
}

View file

@ -20,12 +20,14 @@ public interface IConversationService
/// <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,
string conversationId,
RoleDialogModel lastDalog,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting);
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted);
List<RoleDialogModel> GetDialogHistory(string conversationId, int lastCount = 20);
Task CleanHistory(string agentId);

View file

@ -20,10 +20,16 @@ public class RoleDialogModel
public string? FunctionArgs { get; set; }
/// <summary>
/// Function execution result
/// Function execution result, this result will be seen by LLM.
/// </summary>
public string? ExecutionResult { get; set; }
/// <summary>
/// Function execution structured data, this data won't pass to LLM.
/// It's ideal to render in rich content in UI.
/// </summary>
public object ExecutionData { get; set; }
public bool IsConversationEnd { get; set; }
public bool NeedReloadAgent { get; set; }

View file

@ -5,4 +5,5 @@ public class ConversationSetting
public string DataDir { get; set; }
public string ChatCompletion { get; set; }
public bool EnableKnowledgeBase { get; set; }
public bool ShowVerboseLog { get; set; }
}

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.Services;
@ -17,11 +18,18 @@ public class AgentRouter : IAgentRouting
_settings = settings;
}
public async Task<Agent> LoadRouter()
{
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(_settings.RouterId);
return agent;
}
public async Task<Agent> LoadCurrentAgent()
{
// Load current agent from state
var state = _services.GetRequiredService<IConversationStateService>();
var currentAgentId = state.GetState("agentId");
var currentAgentId = state.GetState("agent_id");
if (string.IsNullOrEmpty(currentAgentId))
{
currentAgentId = _settings.RouterId;
@ -30,7 +38,7 @@ public class AgentRouter : IAgentRouting
var agent = await agentService.LoadAgent(currentAgentId);
// Set agent and trigger state changed
state.SetState("agentId", currentAgentId);
state.SetState("agent_id", currentAgentId);
return agent;
}

View file

@ -73,7 +73,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
<PackageReference Include="Fluid.Core" Version="2.4.0" />
<PackageReference Include="TensorFlow.Keras" Version="0.11.2" />

View file

@ -38,6 +38,7 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped<IAgentRouting, AgentRouter>();
services.AddScoped<IFunctionCallback, GoToRouterFn>();
services.AddScoped<IFunctionCallback, RouteToAgentFn>();
return services;
}

View file

@ -15,7 +15,8 @@ public partial class ConversationService
Agent agent,
List<RoleDialogModel> wholeDialogs,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting)
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
{
currentRecursiveDepth++;
if (currentRecursiveDepth > maxRecursiveDepth)
@ -23,7 +24,8 @@ public partial class ConversationService
_logger.LogError($"Exceed max current recursive depth.");
await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, "System has exception, please try later.")
{
CurrentAgentId = agent.Id
CurrentAgentId = agent.Id,
Channel = wholeDialogs.Last().Channel
}, onMessageReceived);
return false;
}
@ -38,14 +40,15 @@ public partial class ConversationService
{
var preAgentId = agent.Id;
await HandleFunctionMessage(fn, onFunctionExecuting);
await HandleFunctionMessage(fn, onFunctionExecuting, onFunctionExecuted);
// Function executed has exception
if (fn.ExecutionResult == null)
{
await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, fn.Content)
{
CurrentAgentId = fn.CurrentAgentId
CurrentAgentId = fn.CurrentAgentId,
Channel = fn.Channel
}, onMessageReceived);
return;
}
@ -58,10 +61,6 @@ public partial class ConversationService
var agentSettings = _services.GetRequiredService<AgentSettings>();
var agentService = _services.GetRequiredService<IAgentService>();
agent = await agentService.LoadAgent(fn.CurrentAgentId);
// Set state to make next conversation will go to this agent directly
// var state = _services.GetRequiredService<IConversationStateService>();
// state.SetState("agentId", fn.CurrentAgentId);
}
// Add to dialog history
@ -70,7 +69,13 @@ public partial class ConversationService
// After function is executed, pass the result to LLM to get a natural response
wholeDialogs.Add(fn);
await GetChatCompletionsAsyncRecursively(chatCompletion, conversationId, agent, wholeDialogs, onMessageReceived, onFunctionExecuting);
await GetChatCompletionsAsyncRecursively(chatCompletion,
conversationId,
agent,
wholeDialogs,
onMessageReceived,
onFunctionExecuting,
onFunctionExecuted);
});
return result;
@ -89,7 +94,9 @@ public partial class ConversationService
await onMessageReceived(msg);
}
private async Task HandleFunctionMessage(RoleDialogModel msg, Func<RoleDialogModel, Task> onFunctionExecuting)
private async Task HandleFunctionMessage(RoleDialogModel msg,
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
{
// Save states
SaveStateByArgs(msg.FunctionArgs);
@ -97,5 +104,6 @@ public partial class ConversationService
// Call functions
await onFunctionExecuting(msg);
await CallFunctions(msg);
await onFunctionExecuted(msg);
}
}

View file

@ -8,7 +8,8 @@ public partial class ConversationService
public async Task<bool> SendMessage(string agentId, string conversationId,
RoleDialogModel lastDialog,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting)
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
{
var converation = await GetConversation(conversationId);
@ -27,16 +28,19 @@ public partial class ConversationService
var stateService = _services.GetRequiredService<IConversationStateService>();
stateService.SetConversation(conversationId);
stateService.Load();
stateService.SetState("channel", lastDialog.Channel);
var router = _services.GetRequiredService<IAgentRouting>();
var agent = await router.LoadCurrentAgent();
var agent = await router.LoadRouter();
_logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
lastDialog.CurrentAgentId = agent.Id;
_storage.Append(conversationId, agent.Id, lastDialog);
var wholeDialogs = GetDialogHistory(conversationId);
wholeDialogs.Add(lastDialog);
_storage.Append(conversationId, agent.Id, lastDialog);
// Get relevant domain knowledge
/*if (_settings.EnableKnowledgeBase)
@ -67,7 +71,8 @@ public partial class ConversationService
agent,
wholeDialogs,
onMessageReceived,
onFunctionExecuting);
onFunctionExecuting,
onFunctionExecuted);
return result;
}

View file

@ -0,0 +1,36 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Functions.Models;
namespace BotSharp.Core.Functions;
public class RouteToAgentFn : IFunctionCallback
{
public string Name => "route_to_agent";
private readonly IServiceProvider _services;
public RouteToAgentFn(IServiceProvider services)
{
_services = services;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<AgentRoutingArgs>(message.FunctionArgs);
if (string.IsNullOrEmpty(args.AgentId))
{
var result = new FunctionExecutionValidationResult("false", "agent_id can't be parsed.");
message.ExecutionResult = JsonSerializer.Serialize(result);
}
else
{
var result = new FunctionExecutionValidationResult("true");
message.ExecutionResult = JsonSerializer.Serialize(result);
message.CurrentAgentId = args.AgentId;
}
return true;
}
}

View file

@ -3,7 +3,6 @@ using Microsoft.Extensions.Configuration;
using System.Drawing;
using System.IO;
using System.Reflection;
using Console = Colorful.Console;
namespace BotSharp.Core.Plugins;

View file

@ -1,8 +1,6 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.OpenAPI.ViewModels.Conversations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.OpenAPI.Controllers;
@ -50,11 +48,23 @@ public class ConversationController : ControllerBase, IApiAdapter
var stackMsg = new List<RoleDialogModel>();
await conv.SendMessage(agentId, conversationId,
new RoleDialogModel("user", input.Text),
new RoleDialogModel("user", input.Text)
{
Channel = "webapi"
},
async msg =>
stackMsg.Add(msg),
async fn
=> await Task.CompletedTask);
{
stackMsg.Add(msg);
},
async fnExecuting =>
{
},
async fnExecuted =>
{
response.Function = fnExecuted.FunctionName;
response.Data = fnExecuted.ExecutionData;
});
response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content));
return response;

View file

@ -3,4 +3,6 @@ namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class MessageResponseModel
{
public string Text { get; set; }
public string Function { get; set; }
public object Data { get; set; }
}

View file

@ -3,9 +3,11 @@ using Azure.AI.OpenAI;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Conversations.Settings;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Plugin.AzureOpenAI.Settings;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -18,12 +20,16 @@ namespace BotSharp.Plugin.AzureOpenAI.Providers;
public class ChatCompletionProvider : IChatCompletion
{
private readonly AzureOpenAiSettings _settings;
private readonly IServiceProvider _services;
private readonly ILogger _logger;
public ChatCompletionProvider(AzureOpenAiSettings settings, ILogger<ChatCompletionProvider> logger)
public ChatCompletionProvider(AzureOpenAiSettings settings,
ILogger<ChatCompletionProvider> logger,
IServiceProvider services)
{
_settings = settings;
_logger = logger;
_services = services;
}
private OpenAIClient GetClient()
@ -98,7 +104,8 @@ public class ChatCompletionProvider : IChatCompletion
{
CurrentAgentId = agent.Id,
FunctionName = message.FunctionCall.Name,
FunctionArgs = message.FunctionCall.Arguments
FunctionArgs = message.FunctionCall.Arguments,
Channel = conversations.Last().Channel
};
// Execute functions
@ -110,7 +117,8 @@ public class ChatCompletionProvider : IChatCompletion
var msg = new RoleDialogModel(AgentRole.Assistant, message.Content)
{
CurrentAgentId= agent.Id
CurrentAgentId= agent.Id,
Channel = conversations.Last().Channel
};
// Text response received
@ -215,7 +223,18 @@ public class ChatCompletionProvider : IChatCompletion
chatCompletionsOptions.Temperature = 0.5f;
chatCompletionsOptions.NucleusSamplingFactor = 0.5f;
_logger.LogInformation(string.Join("\n", chatCompletionsOptions.Messages.Select(x => $"{x.Role}: {x.Content}")));
var convSetting = _services.GetRequiredService<ConversationSetting>();
if (convSetting.ShowVerboseLog)
{
var verbose = string.Join("\n", chatCompletionsOptions.Messages.Select(x =>
{
return x.Role == ChatRole.Function ?
$"{x.Role}: {x.Name} {x.Content}" :
$"{x.Role}: {x.Content}";
}));
_logger.LogInformation(verbose);
}
return chatCompletionsOptions;
}
}

View file

@ -64,7 +64,10 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
var conversation = input.Messages
.Where(x => x.Role == AgentRole.User)
.Select(x => new RoleDialogModel(x.Role, x.Content))
.Select(x => new RoleDialogModel(x.Role, x.Content)
{
Channel = "webchat"
})
.Last();
var conversationService = _services.GetRequiredService<IConversationService>();
@ -75,6 +78,8 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
async msg =>
await OnChunkReceived(outputStream, msg),
async fn
=> await Task.CompletedTask,
async fn
=> await Task.CompletedTask);
await OnEventCompleted(outputStream);

View file

@ -15,7 +15,7 @@ using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Refit;
using BotSharp.Abstraction.Agents.Enums;
using Microsoft.Extensions.Logging;
namespace BotSharp.Plugin.MetaMessenger.Controllers;
@ -27,10 +27,12 @@ namespace BotSharp.Plugin.MetaMessenger.Controllers;
public class WebhookController : ControllerBase
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
public WebhookController(IServiceProvider services)
public WebhookController(IServiceProvider services, ILogger<WebhookController> logger)
{
_services = services;
_logger = logger;
}
[HttpGet("/messenger/webhook/{agentId}")]
@ -67,7 +69,7 @@ public class WebhookController : ControllerBase
{
var conv = _services.GetRequiredService<IConversationService>();
string content = "";
var reply = new QuickReplyMessage();
var senderId = req.Entry[0].Messaging[0].Sender.Id;
var input = req.Entry[0].Messaging[0].Message.Text;
@ -78,11 +80,13 @@ public class WebhookController : ControllerBase
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt);
// Marking seen
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{
AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt),
Recipient = recipient,
SenderAction = SenderActionEnum.MarkSeen
});
@ -90,7 +94,7 @@ public class WebhookController : ControllerBase
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{
AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt),
Recipient = recipient,
SenderAction = SenderActionEnum.TypingOn
});
@ -100,12 +104,8 @@ public class WebhookController : ControllerBase
Channel = "messenger"
}, async msg =>
{
if (msg.Role == AgentRole.Function)
{
}
content = msg.Content;
}, async fn =>
reply.Text = msg.Content;
}, async functionExecuting =>
{
/*await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{
@ -113,28 +113,39 @@ public class WebhookController : ControllerBase
Recipient = JsonSerializer.Serialize(new { Id = sessionId }, jsonOpt),
Message = JsonSerializer.Serialize(new { Text = "I'm pulling the relevent information, please wait a second ..." }, jsonOpt)
});*/
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
}, async functionExecuted =>
{
// Render structured data
if (functionExecuted.ExecutionData != null)
{
AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt),
SenderAction = SenderActionEnum.TypingOn
});
// validate data format
var json = JsonSerializer.Serialize(functionExecuted.ExecutionData, jsonOpt);
try
{
var parsed = JsonSerializer.Deserialize<QuickReplyMessageItem[]>(json, jsonOpt);
reply.QuickReplies = parsed;
}
catch(Exception ex)
{
_logger.LogError(ex, ex.Message);
}
}
});
// Response to user
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{
AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt),
Message = JsonSerializer.Serialize(new { Text = content }, jsonOpt)
Recipient = recipient,
Message = JsonSerializer.Serialize(reply, jsonOpt)
});
// Typing off
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{
AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt),
Recipient = recipient,
SenderAction = SenderActionEnum.TypingOff
});
}

View file

@ -0,0 +1,5 @@
namespace BotSharp.Plugin.MetaMessenger.Interfaces;
public interface IResponseMessage
{
}

View file

@ -0,0 +1,11 @@
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class AttachementPayload
{
[JsonPropertyName("template_type")]
public string TemplateType { get; set; }
public string Text { get; set; }
public ButtonItem[] Buttons { get; set; }
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class AttachmentBody
{
public string Type { get; set; } = "template";
public AttachementPayload Payload { get; set; }
}

View file

@ -0,0 +1,8 @@
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class ButtonItem
{
public string Type { get; set; }
public string Title { get; set; }
public string Url { get; set; }
}

View file

@ -0,0 +1,17 @@
using BotSharp.Plugin.MetaMessenger.Interfaces;
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
/// <summary>
/// Quick Replies
/// https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies
/// </summary>
public class QuickReplyMessage : IResponseMessage
{
[JsonPropertyName("text")]
public string Text { get; set; }
[JsonPropertyName("quick_replies")]
public QuickReplyMessageItem[] QuickReplies { get; set; }
}

View file

@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class QuickReplyMessageItem
{
[JsonPropertyName("content_type")]
public string ContentType { get; set; } = "text";
[JsonPropertyName("title")]
public string Title { get; set; }
[JsonPropertyName("payload")]
public string Payload { get; set; }
[JsonPropertyName("image_url")]
public string ImageUrl { get; set; }
}

View file

@ -0,0 +1,13 @@
using BotSharp.Plugin.MetaMessenger.Interfaces;
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
/// <summary>
/// https://developers.facebook.com/docs/messenger-platform/send-messages/templates
/// </summary>
public class TemplateMessage : IResponseMessage
{
[JsonPropertyName("attachment")]
public AttachmentBody Attachment { get; set; }
}

View file

@ -1,3 +1,4 @@
using BotSharp.Plugin.MetaMessenger.MessagingModels;
using System;
using System.Collections.Generic;
using System.Text;
@ -10,4 +11,6 @@ public class WebhookMessageBody
[JsonPropertyName("mid")]
public string Id { get;set; }
public string Text { get;set; }
[JsonPropertyName("quick_reply")]
public QuickReplyMessageItem QuickReply { get;set; }
}

View file

@ -59,7 +59,10 @@ namespace BotSharp.Plugin.WeChat
var result = await conversationService.SendMessage(AgentId, latestConversationId, new RoleDialogModel("user", message), async msg =>
{
await ReplyTextMessageAsync(openid, msg.Content);
}, async fn =>
}, async functionExecuting =>
{
}, async functionExecuted =>
{
});