Merge pull request #201 from hchen2020/master

Adjust rich content data structure.
This commit is contained in:
Haiping 2023-11-08 16:30:31 -06:00 committed by GitHub
commit c20cdd13e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 60 additions and 34 deletions

View file

@ -1,4 +1,6 @@
using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Messaging.Models;
using BotSharp.Abstraction.Messaging.Models.RichContent;
namespace BotSharp.Abstraction.Conversations.Models; namespace BotSharp.Abstraction.Conversations.Models;
@ -35,7 +37,7 @@ public class RoleDialogModel : ITrackableMessage
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object Data { get; set; } public object Data { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? RichContent { get; set; } public RichContent<IMessageTemplate>? RichContent { get; set; }
/// <summary> /// <summary>
/// Stop conversation completion /// Stop conversation completion

View file

@ -12,6 +12,8 @@ public class FunctionCallFromLlm : RoutingArgs
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public JsonDocument? Arguments { get; set; } public JsonDocument? Arguments { get; set; }
public bool IsExecutionOnce { get; set; }
public override string ToString() public override string ToString()
{ {
var route = string.IsNullOrEmpty(AgentName) ? "" : $"<Route to {AgentName.ToUpper()} because {Reason}>"; var route = string.IsNullOrEmpty(AgentName) ? "" : $"<Route to {AgentName.ToUpper()} because {Reason}>";

View file

@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Messaging.Models;
public interface IMessageTemplate
{
string Text { get; set; }
}

View file

@ -9,7 +9,6 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent
public string? Payload { get; set; } public string? Payload { get; set; }
[JsonPropertyName("image_url")] [JsonPropertyName("image_url")]
public string? ImageUrl { get; set; } public string? ImageUrl { get; set; }
[JsonPropertyName("postback_url")]
public string? PostBackUrl { get; set; } public string? PostBackUrl { get; set; }
} }
} }

View file

@ -1,8 +1,10 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent namespace BotSharp.Abstraction.Messaging.Models.RichContent
{ {
public class QuickReplyMessage : TextMessage public class QuickReplyMessage : IMessageTemplate
{ {
public string Text { get; set; } = string.Empty;
[JsonPropertyName("quick_replies")] [JsonPropertyName("quick_replies")]
public List<QuickReplyElement> QuickReplies { get; set; } = new List<QuickReplyElement>(); public List<QuickReplyElement> QuickReplies { get; set; } = new List<QuickReplyElement>();
} }

View file

@ -1,6 +1,6 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent namespace BotSharp.Abstraction.Messaging.Models.RichContent
{ {
public class RichContent<T> public class RichContent<T> where T : IMessageTemplate
{ {
public Recipient Recipient { get; set; } = new Recipient(); public Recipient Recipient { get; set; } = new Recipient();
/// <summary> /// <summary>

View file

@ -1,9 +0,0 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{
public class AttachmentTemplate<T>
{
public string Type => "template";
public T Payload { get; set; }
}
}

View file

@ -1,8 +1,10 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{ {
public class ButtonTemplate : TextMessage public class ButtonTemplate : IMessageTemplate
{ {
public string Text { get; set; } = string.Empty;
[JsonPropertyName("template_type")] [JsonPropertyName("template_type")]
public string TemplateType => "button"; public string TemplateType => "button";
public List<ButtonElement> Buttons { get; set; } = new List<ButtonElement>(); public List<ButtonElement> Buttons { get; set; } = new List<ButtonElement>();

View file

@ -1,8 +1,10 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{ {
public class MultiSelectTemplate : TextMessage public class MultiSelectTemplate : IMessageTemplate
{ {
public string Text { get; set; } = string.Empty;
[JsonPropertyName("template_type")] [JsonPropertyName("template_type")]
public string TemplateType => "multi-select"; public string TemplateType => "multi-select";
public List<OptionElement> Options { get; set; } = new List<OptionElement>(); public List<OptionElement> Options { get; set; } = new List<OptionElement>();

View file

@ -1,11 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{
public class TemplateMessage<T>
{
public T Attachment { get; set; }
}
}

View file

@ -1,8 +1,7 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent namespace BotSharp.Abstraction.Messaging.Models.RichContent;
public class TextMessage : IMessageTemplate
{ {
public class TextMessage public string Text { get; set; } = string.Empty;
{
public string Text { get; set; } = string.Empty;
}
} }

View file

@ -0,0 +1,16 @@
using System.Text.Json;
namespace BotSharp.Abstraction.Messaging.Models;
public class RichContentJsonConverter : JsonConverter<IMessageTemplate>
{
public override IMessageTemplate? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, IMessageTemplate value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, value.GetType(), options);
}
}

View file

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

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Messaging.Models;
using BotSharp.Abstraction.Messaging.Models.RichContent; using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Routing; using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings; using BotSharp.Abstraction.Routing.Settings;
@ -102,7 +103,7 @@ public partial class ConversationService
// Only read content from RichContent for UI rendering. When richContent is null, create a basic text message for richContent. // Only read content from RichContent for UI rendering. When richContent is null, create a basic text message for richContent.
var state = _services.GetRequiredService<IConversationStateService>(); var state = _services.GetRequiredService<IConversationStateService>();
response.RichContent = response.RichContent ?? new RichContent<TextMessage> response.RichContent = response.RichContent ?? new RichContent<IMessageTemplate>
{ {
Recipient = new Recipient { Id = state.GetConversationId() }, Recipient = new Recipient { Id = state.GetConversationId() },
Message = new TextMessage { Text = response.Content } Message = new TextMessage { Text = response.Content }

View file

@ -1,4 +1,6 @@
using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Messaging.Models;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Planning; using BotSharp.Abstraction.Planning;
using BotSharp.Abstraction.Routing; using BotSharp.Abstraction.Routing;
@ -33,6 +35,14 @@ public class InstructExecutor : IExecutor
response.MessageId = message.MessageId; response.MessageId = message.MessageId;
response.Instruction = inst; response.Instruction = inst;
// Process rich content
if (response.RichContent != null &&
response.RichContent is RichContent<IMessageTemplate> template &&
string.IsNullOrEmpty(template.Message.Text))
{
template.Message.Text = response.Content;
}
return response; return response;
} }
} }

View file

@ -36,6 +36,10 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
var ret = await function.Execute(message); var ret = await function.Execute(message);
var agentId = context.GetCurrentAgentId(); var agentId = context.GetCurrentAgentId();
if (inst.IsExecutionOnce)
{
message.Content = inst.Question;
}
ret = await routing.InvokeAgent(agentId, _dialogs); ret = await routing.InvokeAgent(agentId, _dialogs);
var response = _dialogs.Last(); var response = _dialogs.Last();

View file

@ -44,7 +44,9 @@ public partial class RoutingService : IRoutingService
Function = "route_to_agent", Function = "route_to_agent",
Question = message.Content, Question = message.Content,
Reason = message.Content, Reason = message.Content,
AgentName = agent.Name AgentName = agent.Name,
OriginalAgent = agent.Name,
IsExecutionOnce = true
}; };
var result = await handler.Handle(this, inst, message); var result = await handler.Handle(this, inst, message);