Skip to content

Adjust rich content data structure. #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Messaging.Models;
using BotSharp.Abstraction.Messaging.Models.RichContent;

namespace BotSharp.Abstraction.Conversations.Models;

Expand Down Expand Up @@ -35,7 +37,7 @@ public class RoleDialogModel : ITrackableMessage
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object Data { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? RichContent { get; set; }
public RichContent<IMessageTemplate>? RichContent { get; set; }

/// <summary>
/// Stop conversation completion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class FunctionCallFromLlm : RoutingArgs
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public JsonDocument? Arguments { get; set; }

public bool IsExecutionOnce { get; set; }

public override string ToString()
{
var route = string.IsNullOrEmpty(AgentName) ? "" : $"<Route to {AgentName.ToUpper()} because {Reason}>";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Messaging.Models;

public interface IMessageTemplate
{
string Text { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class QuickReplyElement
public string? Payload { get; set; }
[JsonPropertyName("image_url")]
public string? ImageUrl { get; set; }
[JsonPropertyName("postback_url")]
public string? PostBackUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class QuickReplyMessage : TextMessage
public class QuickReplyMessage : IMessageTemplate
{
public string Text { get; set; } = string.Empty;

[JsonPropertyName("quick_replies")]
public List<QuickReplyElement> QuickReplies { get; set; } = new List<QuickReplyElement>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class RichContent<T>
public class RichContent<T> where T : IMessageTemplate
{
public Recipient Recipient { get; set; } = new Recipient();
/// <summary>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

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")]
public string TemplateType => "button";
public List<ButtonElement> Buttons { get; set; } = new List<ButtonElement>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

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")]
public string TemplateType => "multi-select";
public List<OptionElement> Options { get; set; } = new List<OptionElement>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 0 additions & 1 deletion src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
<PackageReference Include="Fluid.Core" Version="2.5.0" />
<PackageReference Include="TensorFlow.Keras" Version="0.11.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Messaging.Models;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;
Expand Down Expand Up @@ -102,7 +103,7 @@ private async Task HandleAssistantMessage(RoleDialogModel response, Func<RoleDia

// Only read content from RichContent for UI rendering. When richContent is null, create a basic text message for richContent.
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() },
Message = new TextMessage { Text = response.Content }
Expand Down
10 changes: 10 additions & 0 deletions src/Infrastructure/BotSharp.Core/Planning/InstructExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Messaging.Models;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Planning;
using BotSharp.Abstraction.Routing;

Expand Down Expand Up @@ -33,6 +35,14 @@ public async Task<RoleDialogModel> Execute(IRoutingService routing,
response.MessageId = message.MessageId;
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst
var ret = await function.Execute(message);

var agentId = context.GetCurrentAgentId();
if (inst.IsExecutionOnce)
{
message.Content = inst.Question;
}
ret = await routing.InvokeAgent(agentId, _dialogs);

var response = _dialogs.Last();
Expand Down
4 changes: 3 additions & 1 deletion src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public async Task<RoleDialogModel> ExecuteOnce(Agent agent, RoleDialogModel mess
Function = "route_to_agent",
Question = message.Content,
Reason = message.Content,
AgentName = agent.Name
AgentName = agent.Name,
OriginalAgent = agent.Name,
IsExecutionOnce = true
};

var result = await handler.Handle(this, inst, message);
Expand Down