Skip to content
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
Expand Up @@ -42,6 +42,9 @@ public class ChatResponseDto : InstructResult
[JsonPropertyName("is_streaming")]
public bool IsStreaming { get; set; }

[JsonPropertyName("is_append")]
public bool IsAppend { get; set; }

[JsonPropertyName("created_at")]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ public class RoleDialogModel : ITrackableMessage
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public bool IsStreaming { get; set; }

/// <summary>
/// Additional messages that can be sent sequentially and save to db
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public ChatMessageWrapper? AdditionalMessageWrapper { get; set; }


public RoleDialogModel()
{
}
Expand Down Expand Up @@ -171,7 +178,15 @@ public static RoleDialogModel From(RoleDialogModel source,
Instruction = source.Instruction,
Data = source.Data,
IsStreaming = source.IsStreaming,
Annotations = source.Annotations
Annotations = source.Annotations,
AdditionalMessageWrapper = source.AdditionalMessageWrapper
};
}
}

public class ChatMessageWrapper
{
public int IntervalMilliSeconds { get; set; } = 1000;
public bool SaveToDb { get; set; }
public List<RoleDialogModel>? Messages { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using BotSharp.Abstraction.Hooks;
using BotSharp.Abstraction.Infrastructures.Enums;
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Models.RichContent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Options;
Expand Down Expand Up @@ -31,63 +32,21 @@ public void Append(string conversationId, IEnumerable<RoleDialogModel> dialogs)

foreach ( var dialog in dialogs)
{
if (dialog.Role == AgentRole.Function)
var innerList = new List<RoleDialogModel> { dialog };
if (dialog.AdditionalMessageWrapper != null
&& dialog.AdditionalMessageWrapper.SaveToDb
&& dialog.AdditionalMessageWrapper.Messages?.Count > 0)
{
var meta = new DialogMetaData
{
Role = dialog.Role,
AgentId = dialog.CurrentAgentId,
MessageId = dialog.MessageId,
MessageType = dialog.MessageType,
FunctionName = dialog.FunctionName,
FunctionArgs = dialog.FunctionArgs,
ToolCallId = dialog.ToolCallId,
CreatedTime = dialog.CreatedAt
};

var content = dialog.Content.RemoveNewLine();
if (string.IsNullOrEmpty(content))
{
continue;
}
dialogElements.Add(new DialogElement
{
MetaData = meta,
Content = dialog.Content,
SecondaryContent = dialog.SecondaryContent,
Payload = dialog.Payload
});
innerList.AddRange(dialog.AdditionalMessageWrapper.Messages);
}
else
{
var meta = new DialogMetaData
{
Role = dialog.Role,
AgentId = dialog.CurrentAgentId,
MessageId = dialog.MessageId,
MessageType = dialog.MessageType,
SenderId = dialog.SenderId,
FunctionName = dialog.FunctionName,
CreatedTime = dialog.CreatedAt
};

var content = dialog.Content.RemoveNewLine();
if (string.IsNullOrEmpty(content))
foreach (var item in innerList)
{
var element = BuildDialogElement(item);
if (element != null)
{
continue;
dialogElements.Add(element);
}

var richContent = dialog.RichContent != null ? JsonSerializer.Serialize(dialog.RichContent, _options.JsonSerializerOptions) : null;
var secondaryRichContent = dialog.SecondaryRichContent != null ? JsonSerializer.Serialize(dialog.SecondaryRichContent, _options.JsonSerializerOptions) : null;
dialogElements.Add(new DialogElement
{
MetaData = meta,
Content = dialog.Content,
SecondaryContent = dialog.SecondaryContent,
RichContent = richContent,
SecondaryRichContent = secondaryRichContent,
Payload = dialog.Payload
});
}
}

Expand Down Expand Up @@ -148,4 +107,67 @@ public List<RoleDialogModel> GetDialogs(string conversationId)

return results;
}

private DialogElement? BuildDialogElement(RoleDialogModel dialog)
{
DialogElement? element = null;

if (dialog.Role == AgentRole.Function)
{
var meta = new DialogMetaData
{
Role = dialog.Role,
AgentId = dialog.CurrentAgentId,
MessageId = dialog.MessageId,
MessageType = dialog.MessageType,
FunctionName = dialog.FunctionName,
FunctionArgs = dialog.FunctionArgs,
ToolCallId = dialog.ToolCallId,
CreatedTime = dialog.CreatedAt
};

var content = dialog.Content.RemoveNewLine();
if (!string.IsNullOrEmpty(content))
{
element = new DialogElement
{
MetaData = meta,
Content = dialog.Content,
SecondaryContent = dialog.SecondaryContent,
Payload = dialog.Payload
};
}
}
else
{
var meta = new DialogMetaData
{
Role = dialog.Role,
AgentId = dialog.CurrentAgentId,
MessageId = dialog.MessageId,
MessageType = dialog.MessageType,
SenderId = dialog.SenderId,
FunctionName = dialog.FunctionName,
CreatedTime = dialog.CreatedAt
};

var content = dialog.Content.RemoveNewLine();
if (!string.IsNullOrEmpty(content))
{
var richContent = dialog.RichContent != null ? JsonSerializer.Serialize(dialog.RichContent, _options.JsonSerializerOptions) : null;
var secondaryRichContent = dialog.SecondaryRichContent != null ? JsonSerializer.Serialize(dialog.SecondaryRichContent, _options.JsonSerializerOptions) : null;
element = new DialogElement
{
MetaData = meta,
Content = dialog.Content,
SecondaryContent = dialog.SecondaryContent,
RichContent = richContent,
SecondaryRichContent = secondaryRichContent,
Payload = dialog.Payload
};
}
}

return element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message, Inv
message.StopCompletion = clonedMessage.StopCompletion;
message.RichContent = clonedMessage.RichContent;
message.Data = clonedMessage.Data;
message.AdditionalMessageWrapper = clonedMessage.AdditionalMessageWrapper;
}
catch (JsonException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@

<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\BotSharp.Plugin.ChatHub\BotSharp.Plugin.ChatHub.csproj" />
</ItemGroup>
</Project>
82 changes: 25 additions & 57 deletions src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using BotSharp.Abstraction.Conversations.Dtos;
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using BotSharp.Abstraction.Users;
using BotSharp.Plugin.ChatHub.Helpers;
using System.Runtime.CompilerServices;

namespace BotSharp.Plugin.ChartHandler.Functions;

public class PlotChartFn : IFunctionCallback
{
private readonly IServiceProvider _services;
private readonly ILogger<PlotChartFn> _logger;
private readonly BotSharpOptions _options;
private readonly ChartHandlerSettings _settings;

public string Name => "util-chart-plot_chart";
public string Indication => "Plotting chart";
Expand All @@ -20,11 +15,11 @@ public class PlotChartFn : IFunctionCallback
public PlotChartFn(
IServiceProvider services,
ILogger<PlotChartFn> logger,
BotSharpOptions options)
ChartHandlerSettings settings)
{
_services = services;
_logger = logger;
_options = options;
_settings = settings;
}

public async Task<bool> Execute(RoleDialogModel message)
Expand All @@ -43,7 +38,7 @@ public async Task<bool> Execute(RoleDialogModel message)
Instruction = inst,
LlmConfig = new AgentLlmConfig
{
MaxOutputTokens = 8192
MaxOutputTokens = _settings?.ChartPlot?.MaxOutputTokens ?? 8192
},
TemplateDict = new Dictionary<string, object>
{
Expand All @@ -52,7 +47,8 @@ public async Task<bool> Execute(RoleDialogModel message)
}
};

var response = await GetChatCompletion(innerAgent, [
var response = await GetChatCompletion(innerAgent,
[
new RoleDialogModel(AgentRole.User, "Please follow the instruction to generate the javascript code.")
{
CurrentAgentId = message.CurrentAgentId,
Expand All @@ -72,59 +68,32 @@ public async Task<bool> Execute(RoleDialogModel message)
}
};

// Send report summary after 1.5 seconds if exists
if (!string.IsNullOrEmpty(obj?.ReportSummary))
{
_ = Task.Run(async () =>
message.AdditionalMessageWrapper = new()
{
var services = _services.CreateScope().ServiceProvider;
await Task.Delay(1500);
await SendDelayedMessage(services, obj.ReportSummary, convService.ConversationId, agent.Id, agent.Name);
});
IntervalMilliSeconds = 1500,
SaveToDb = true,
Messages = new List<RoleDialogModel>
{
new()
{
Role = AgentRole.Assistant,
MessageId = message.MessageId,
CurrentAgentId = message.CurrentAgentId,
Content = obj.ReportSummary,
FunctionName = message.FunctionName,
FunctionArgs = message.FunctionArgs,
CreatedAt = DateTime.UtcNow
}
}
};
}

message.StopCompletion = true;
return true;
}

private async Task SendDelayedMessage(IServiceProvider services, string text, string conversationId, string agentId, string agentName)
{
try
{
var messageId = Guid.NewGuid().ToString();
var messageData = new ChatResponseDto
{
ConversationId = conversationId,
MessageId = messageId,
Text = text,
Sender = new() { FirstName = agentName, LastName = "", Role = AgentRole.Assistant }
};

var dialogModel = new RoleDialogModel(AgentRole.Assistant, text)
{
MessageId = messageId,
CurrentAgentId = agentId,
CreatedAt = DateTime.UtcNow
};

var storage = services.GetService<IConversationStorage>();
storage?.Append(conversationId, dialogModel);
await SendEvent(services, ChatEvent.OnMessageReceivedFromAssistant, conversationId, messageData);

}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send delayed message");
}
}

private async Task SendEvent<T>(IServiceProvider services, string @event, string conversationId, T data, [CallerMemberName] string callerName = "")
{
var user = services.GetService<IUserIdentity>();
var json = JsonSerializer.Serialize(data, _options.JsonSerializerOptions);
await EventEmitter.SendChatEvent(services, _logger, @event, conversationId, user?.Id, json, nameof(PlotChartFn), callerName);
}

private async Task<string> GetChatCompletion(Agent agent, List<RoleDialogModel> dialogs)
{
try
Expand Down Expand Up @@ -169,12 +138,11 @@ private string GetChartPlotInstruction(string agentId)
var model = "gpt-5";

var state = _services.GetRequiredService<IConversationStateService>();
var settings = _services.GetRequiredService<ChartHandlerSettings>();
provider = state.GetState("chart_plot_llm_provider")
.IfNullOrEmptyAs(settings.ChartPlot?.LlmProvider)
.IfNullOrEmptyAs(_settings.ChartPlot?.LlmProvider)
.IfNullOrEmptyAs(provider);
model = state.GetState("chart_plot_llm_model")
.IfNullOrEmptyAs(settings.ChartPlot?.LlmModel)
.IfNullOrEmptyAs(_settings.ChartPlot?.LlmModel)
.IfNullOrEmptyAs(model);

return (provider, model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ChartHandlerSettings

public class ChartPlotSetting
{
public string LlmProvider { get; set; }
public string LlmModel { get; set; }
public string? LlmProvider { get; set; }
public string? LlmModel { get; set; }
public int? MaxOutputTokens { get; set; }
}
Loading
Loading