From 366e1339c6190616b435bdbdd508a651fc672d13 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 5 Mar 2025 17:22:46 -0600 Subject: [PATCH] refactor instruction --- .../BotSharp.Abstraction.csproj | 2 +- .../Conversations/Models/RoleDialogModel.cs | 3 +- .../Instructs/Models/InstructLogFilter.cs | 2 +- .../Instructs/Models/InstructResponseModel.cs | 8 +- .../Loggers/Models/InstructionLogModel.cs | 28 ++++- .../MLTasks/IAudioCompletion.cs | 2 + .../MLTasks/IChatCompletion.cs | 2 + .../MLTasks/IImageCompletion.cs | 2 + .../MLTasks/IRealTimeCompletion.cs | 1 - .../MLTasks/ITextCompletion.cs | 1 + .../MLTasks/ITextEmbedding.cs | 7 +- .../Instruct/FileInstructService.Image.cs | 112 +++++++++++++++++- .../Instruct/FileInstructService.Pdf.cs | 24 +++- .../Instructs/InsturctionPlugin.cs | 11 +- .../Services/InstructService.Execute.cs | 20 ++++ .../FileRepository/FileRepository.Log.cs | 85 ++++++++++++- .../Hooks/InstructionLogHook.cs | 60 ++-------- .../BotSharp.OpenAPI/BotSharp.OpenAPI.csproj | 2 +- .../Controllers/InstructModeController.cs | 42 ++++++- .../Controllers/LoggerController.cs | 16 +++ .../Instructs/InstructMessageModel.cs | 1 + .../Providers/ChatCompletionProvider.cs | 9 +- .../Provider/NativeWhisperProvider.cs | 4 + .../Audio/AudioCompletionProvider.cs | 2 + .../Providers/Chat/ChatCompletionProvider.cs | 26 +++- .../Embedding/TextEmbeddingProvider.cs | 2 + .../Image/ImageCompletionProvider.cs | 1 + .../Providers/Text/TextCompletionProvider.cs | 1 + .../Providers/Chat/ChatCompletionProvider.cs | 24 +++- .../Providers/Text/TextCompletionProvider.cs | 1 + .../Chat/GeminiChatCompletionProvider.cs | 9 +- .../Chat/PalmChatCompletionProvider.cs | 8 +- .../Text/GeminiTextCompletionProvider.cs | 2 +- .../Text/PalmTextCompletionProvider.cs | 1 + .../Providers/ChatCompletionProvider.cs | 2 + .../Providers/ChatCompletionProvider.cs | 8 +- .../Providers/TextCompletionProvider.cs | 1 + .../Providers/TextEmbeddingProvider.cs | 3 +- .../Providers/ChatCompletionProvider.cs | 2 + .../Providers/TextCompletionProvider.cs | 2 + .../Providers/fastTextEmbeddingProvider.cs | 2 + .../Providers/ChatCompletionProvider.cs | 10 +- ...osoftExtensionsAIChatCompletionProvider.cs | 7 +- ...osoftExtensionsAITextCompletionProvider.cs | 1 + ...rosoftExtensionsAITextEmbeddingProvider.cs | 1 + .../Collections/InstructionLogBetaDocument.cs | 14 ++- .../MongoDbContext.cs | 2 +- .../Repository/MongoRepository.Log.cs | 39 +++--- .../Audio/AudioCompletionProvider.cs | 2 + .../Providers/Chat/ChatCompletionProvider.cs | 24 +++- .../Embedding/TextEmbeddingProvider.cs | 1 + .../Image/ImageCompletionProvider.cs | 1 + .../Providers/Text/TextCompletionProvider.cs | 1 + .../SemanticKernelChatCompletionProvider.cs | 4 +- .../SemanticKernelTextCompletionProvider.cs | 1 + .../SemanticKernelTextEmbeddingProvider.cs | 2 + .../Providers/ChatCompletionProvider.cs | 22 +++- 57 files changed, 537 insertions(+), 136 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj index f189d323..2266e975 100644 --- a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj +++ b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj @@ -1,4 +1,4 @@ - + $(TargetFramework) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index 585112a2..6f96eec5 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -108,7 +108,8 @@ public class RoleDialogModel : ITrackableMessage [JsonPropertyName("generated_images")] public List GeneratedImages { get; set; } = new List(); - + [JsonIgnore(Condition = JsonIgnoreCondition.Always)] + public string RenderedInstruction { get; set; } = string.Empty; private RoleDialogModel() { diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructLogFilter.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructLogFilter.cs index d8cafcee..9d154052 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructLogFilter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructLogFilter.cs @@ -7,7 +7,7 @@ public class InstructLogFilter : Pagination public List? AgentIds { get; set; } public List? Providers { get; set; } public List? Models { get; set; } - public List? States { get; set; } + public List? TemplateNames { get; set; } public static InstructLogFilter Empty() { diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructResponseModel.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructResponseModel.cs index e2ee794d..62752bf4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructResponseModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructResponseModel.cs @@ -3,6 +3,10 @@ namespace BotSharp.Abstraction.Instructs.Models; public class InstructResponseModel { public string? AgentId { get; set; } - public string Provider { get; set; } - public string Model { get; set; } + public string Provider { get; set; } = default!; + public string Model { get; set; } = default!; + public string? TemplateName { get; set; } + public string UserMessage { get; set; } = default!; + public string? SystemInstruction { get; set; } + public string CompletionText { get; set; } = default!; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/InstructionLogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/InstructionLogModel.cs index f77b08fe..844d7942 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/InstructionLogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/InstructionLogModel.cs @@ -1,24 +1,50 @@ +using System.Text.Json; + namespace BotSharp.Abstraction.Loggers.Models; public class InstructionLogModel { + [JsonPropertyName("id")] + public string Id { get; set; } = default!; + [JsonPropertyName("agent_id")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? AgentId { get; set; } + [JsonPropertyName("agent_name")] + [JsonIgnore] + public string? AgentName { get; set; } + [JsonPropertyName("provider")] public string Provider { get; set; } = default!; [JsonPropertyName("model")] public string Model { get; set; } = default!; + [JsonPropertyName("template_name")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? TemplateName { get; set; } + + [JsonPropertyName("user_message")] + public string UserMessage { get; set; } = string.Empty; + + [JsonPropertyName("system_instruction")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? SystemInstruction { get; set; } + + [JsonPropertyName("completion_text")] + public string CompletionText { get; set; } = string.Empty; + [JsonPropertyName("user_id")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? UserId { get; set; } - [JsonPropertyName("states")] + [JsonIgnore] public Dictionary States { get; set; } = []; + [JsonPropertyName("states")] + public Dictionary InnerStates { get; set; } = []; + [JsonPropertyName("created_time")] public DateTime CreatedTime { get; set; } = DateTime.UtcNow; } diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IAudioCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IAudioCompletion.cs index 54ed2d7d..85fc84f2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IAudioCompletion.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IAudioCompletion.cs @@ -6,6 +6,8 @@ public interface IAudioCompletion { string Provider { get; } + string Model { get; } + Task GenerateTextFromAudioAsync(Stream audio, string audioFileName, string? text = null); Task GenerateAudioFromTextAsync(string text); diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs index 306e72e3..7cf52fe4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs @@ -7,6 +7,8 @@ public interface IChatCompletion /// string Provider { get; } + string Model { get; } + /// /// Set model name, one provider can consume different model or version(s) /// diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IImageCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IImageCompletion.cs index 323fe643..2b74a337 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IImageCompletion.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IImageCompletion.cs @@ -9,6 +9,8 @@ public interface IImageCompletion /// string Provider { get; } + string Model { get; } + /// /// Set model name, one provider can consume different model or version(s) /// diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs index be6f8821..c5103c18 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs @@ -6,7 +6,6 @@ public interface IRealTimeCompletion { string Provider { get; } string Model { get; } - void SetModelName(string model); Task Connect(RealtimeHubConnection conn, diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextCompletion.cs index 63a15940..8fc549f7 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextCompletion.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextCompletion.cs @@ -6,6 +6,7 @@ public interface ITextCompletion /// The LLM provider like Microsoft Azure, OpenAI, ClaudAI /// string Provider { get; } + string Model { get; } /// /// Set model name, one provider can consume different model or version(s) diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs index 79b3aff8..a96dfec9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs @@ -6,9 +6,14 @@ public interface ITextEmbedding /// The Embedding provider like Microsoft Azure, OpenAI, ClaudAI /// string Provider { get; } + string Model { get; } + + void SetModelName(string model); + + Task GetVectorAsync(string text); Task> GetVectorsAsync(List texts); - void SetModelName(string model); + void SetDimension(int dimension); int GetDimension(); } diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs index 4de657c3..2976ab47 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Instructs.Models; +using BotSharp.Abstraction.Instructs; using System.IO; namespace BotSharp.Core.Files.Services; @@ -6,10 +8,11 @@ public partial class FileInstructService { public async Task ReadImages(string? provider, string? model, string text, IEnumerable images, string? agentId = null) { + var innerAgentId = agentId ?? Guid.Empty.ToString(); var completion = CompletionProvider.GetChatCompletion(_services, provider: provider ?? "openai", model: model ?? "gpt-4o", multiModal: true); var message = await completion.GetChatCompletions(new Agent() { - Id = agentId ?? Guid.Empty.ToString(), + Id = innerAgentId, }, new List { new RoleDialogModel(AgentRole.User, text) @@ -17,16 +20,55 @@ public partial class FileInstructService Files = images?.Select(x => new BotSharpFile { FileUrl = x.FileUrl, FileData = x.FileData }).ToList() ?? new List() } }); + + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) + { + continue; + } + + await hook.OnResponseGenerated(new InstructResponseModel + { + AgentId = innerAgentId, + Provider = completion.Provider, + Model = completion.Model, + UserMessage = text, + CompletionText = message.Content + }); + } + return message.Content; } public async Task GenerateImage(string? provider, string? model, string text, string? agentId = null) { + var innerAgentId = agentId ?? Guid.Empty.ToString(); var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-3"); var message = await completion.GetImageGeneration(new Agent() { - Id = agentId ?? Guid.Empty.ToString(), + Id = innerAgentId, }, new RoleDialogModel(AgentRole.User, text)); + + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) + { + continue; + } + + await hook.OnResponseGenerated(new InstructResponseModel + { + AgentId = innerAgentId, + Provider = completion.Provider, + Model = completion.Model, + UserMessage = text, + CompletionText = message.Content + }); + } + return message; } @@ -37,6 +79,7 @@ public partial class FileInstructService throw new ArgumentException($"Cannot find image url or data!"); } + var innerAgentId = agentId ?? Guid.Empty.ToString(); var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2"); var bytes = await DownloadFile(image); using var stream = new MemoryStream(); @@ -46,10 +89,29 @@ public partial class FileInstructService var fileName = $"{image.FileName ?? "image"}.{image.FileExtension ?? "png"}"; var message = await completion.GetImageVariation(new Agent() { - Id = agentId ?? Guid.Empty.ToString() + Id = innerAgentId }, new RoleDialogModel(AgentRole.User, string.Empty), stream, fileName); stream.Close(); + + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) + { + continue; + } + + await hook.OnResponseGenerated(new InstructResponseModel + { + AgentId = innerAgentId, + Provider = completion.Provider, + Model = completion.Model, + UserMessage = string.Empty, + CompletionText = message.Content + }); + } + return message; } @@ -60,6 +122,7 @@ public partial class FileInstructService throw new ArgumentException($"Cannot find image url or data!"); } + var innerAgentId = agentId ?? Guid.Empty.ToString(); var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2"); var bytes = await DownloadFile(image); using var stream = new MemoryStream(); @@ -69,10 +132,29 @@ public partial class FileInstructService var fileName = $"{image.FileName ?? "image"}.{image.FileExtension ?? "png"}"; var message = await completion.GetImageEdits(new Agent() { - Id = agentId ?? Guid.Empty.ToString() + Id = innerAgentId }, new RoleDialogModel(AgentRole.User, text), stream, fileName); stream.Close(); + + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) + { + continue; + } + + await hook.OnResponseGenerated(new InstructResponseModel + { + AgentId = innerAgentId, + Provider = completion.Provider, + Model = completion.Model, + UserMessage = text, + CompletionText = message.Content + }); + } + return message; } @@ -84,6 +166,7 @@ public partial class FileInstructService throw new ArgumentException($"Cannot find image/mask url or data"); } + var innerAgentId = agentId ?? Guid.Empty.ToString(); var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2"); var imageBytes = await DownloadFile(image); var maskBytes = await DownloadFile(mask); @@ -100,11 +183,30 @@ public partial class FileInstructService var maskName = $"{mask.FileName ?? "mask"}.{mask.FileExtension ?? "png"}"; var message = await completion.GetImageEdits(new Agent() { - Id = agentId ?? Guid.Empty.ToString() + Id = innerAgentId }, new RoleDialogModel(AgentRole.User, text), imageStream, imageName, maskStream, maskName); imageStream.Close(); maskStream.Close(); + + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) + { + continue; + } + + await hook.OnResponseGenerated(new InstructResponseModel + { + AgentId = innerAgentId, + Provider = completion.Provider, + Model = completion.Model, + UserMessage = text, + CompletionText = message.Content + }); + } + return message; } diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs index e1362455..9f2132a1 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs @@ -1,4 +1,6 @@ using BotSharp.Abstraction.Files.Converters; +using BotSharp.Abstraction.Instructs.Models; +using BotSharp.Abstraction.Instructs; namespace BotSharp.Core.Files.Services; @@ -23,11 +25,12 @@ public partial class FileInstructService var images = await ConvertPdfToImages(pdfFiles); if (images.IsNullOrEmpty()) return content; + var innerAgentId = agentId ?? Guid.Empty.ToString(); var completion = CompletionProvider.GetChatCompletion(_services, provider: provider ?? "openai", model: model, modelId: modelId ?? "gpt-4", multiModal: true); var message = await completion.GetChatCompletions(new Agent() { - Id = agentId ?? Guid.Empty.ToString(), + Id = innerAgentId, }, new List { new RoleDialogModel(AgentRole.User, prompt) @@ -35,6 +38,25 @@ public partial class FileInstructService Files = images.Select(x => new BotSharpFile { FileStorageUrl = x }).ToList() } }); + + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) + { + continue; + } + + await hook.OnResponseGenerated(new InstructResponseModel + { + AgentId = innerAgentId, + Provider = completion.Provider, + Model = completion.Model, + UserMessage = prompt, + CompletionText = message.Content + }); + } + return message.Content; } catch (Exception ex) diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs b/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs index 7b4596ff..663c710a 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins.Models; +using BotSharp.Abstraction.Users.Enums; using Microsoft.Extensions.Configuration; namespace BotSharp.Core.Instructs; @@ -17,7 +18,15 @@ public class InsturctionPlugin : IBotSharpPlugin public bool AttachMenu(List menu) { var section = menu.First(x => x.Label == "Apps"); - menu.Add(new PluginMenuDef("Instruction", link: "page/instruction", icon: "bx bx-book-content", weight: section.Weight + 5)); + menu.Add(new PluginMenuDef("Instruction", icon: "bx bx-book-content", weight: section.Weight + 5) + { + SubMenu = new List + { + new PluginMenuDef("Instruction", link: "page/instruction"), + new PluginMenuDef("Log", link: "page/instruction/log") { Roles = [UserRole.Root, UserRole.Admin] } + } + }); + return true; } } diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index 1d9dd1d0..36f5b523 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -43,6 +43,9 @@ public partial class InstructService } } + var provider = string.Empty; + var model = string.Empty; + // Render prompt var prompt = string.IsNullOrEmpty(templateName) ? agentService.RenderedInstruction(agent) : @@ -57,11 +60,18 @@ public partial class InstructService }; if (completer is ITextCompletion textCompleter) { + instruction = null; + provider = textCompleter.Provider; + model = textCompleter.Model; + var result = await textCompleter.GetCompletion(prompt, agentId, message.MessageId); response.Text = result; } else if (completer is IChatCompletion chatCompleter) { + provider = chatCompleter.Provider; + model = chatCompleter.Model; + if (instruction == "#TEMPLATE#") { instruction = prompt; @@ -93,6 +103,16 @@ public partial class InstructService } await hook.AfterCompletion(agent, response); + await hook.OnResponseGenerated(new InstructResponseModel + { + AgentId = agentId, + Provider = provider, + Model = model, + TemplateName = templateName, + UserMessage = prompt, + SystemInstruction = instruction, + CompletionText = response.Text + }); } return response; diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs index 9f7ca305..ea1f27b5 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs @@ -136,7 +136,8 @@ namespace BotSharp.Core.Repository foreach (var log in logs) { - var file = Path.Combine(baseDir, $"{Guid.NewGuid()}.log"); + var file = Path.Combine(baseDir, $"{Guid.NewGuid()}.json"); + log.InnerStates = BuildLogStates(log.States); var text = JsonSerializer.Serialize(log, _options); File.WriteAllText(file, text); } @@ -150,9 +151,67 @@ namespace BotSharp.Core.Repository filter = InstructLogFilter.Empty(); } + var baseDir = Path.Combine(_dbSettings.FileRepository, INSTRUCTION_LOG_FOLDER); + if (!Directory.Exists(baseDir)) + { + return new(); + } + + var logs = new List(); + var files = Directory.GetFiles(baseDir); + foreach (var file in files) + { + var json = File.ReadAllText(file); + var log = JsonSerializer.Deserialize(json, _options); + if (log == null) continue; + + var matched = true; + if (!filter.AgentIds.IsNullOrEmpty()) + { + matched = matched && filter.AgentIds.Contains(log.AgentId); + } + if (!filter.Providers.IsNullOrEmpty()) + { + matched = matched && filter.Providers.Contains(log.Provider); + } + if (!filter.Models.IsNullOrEmpty()) + { + matched = matched && filter.Models.Contains(log.Model); + } + if (!filter.TemplateNames.IsNullOrEmpty()) + { + matched = matched && filter.TemplateNames.Contains(log.TemplateName); + } + + if (!matched) continue; + + log.Id = Path.GetFileNameWithoutExtension(file); + logs.Add(log); + } + + var records = logs.OrderByDescending(x => x.CreatedTime).Skip(filter.Offset).Take(filter.Size); + var agentIds = records.Where(x => !string.IsNullOrEmpty(x.AgentId)).Select(x => x.AgentId).ToList(); + var agents = GetAgents(new AgentFilter + { + AgentIds = agentIds + }); + + records = records.Select(x => + { + var states = x.InnerStates.ToDictionary(p => p.Key, p => + { + var data = p.Value.RootElement.GetProperty("data"); + return data.ValueKind != JsonValueKind.Null ? data.ToString() : null; + }); + x.AgentName = !string.IsNullOrEmpty(x.AgentId) ? agents.FirstOrDefault(a => a.Id == x.AgentId)?.Name : null; + x.States = states ?? []; + return x; + }).ToList(); + return new PagedItems { - + Items = records, + Count = logs.Count() }; } #endregion @@ -176,6 +235,28 @@ namespace BotSharp.Core.Repository return logIndexes.IsNullOrEmpty() ? 0 : logIndexes.Max() + 1; } + + private Dictionary BuildLogStates(Dictionary states) + { + var dic = new Dictionary(); + foreach (var pair in states) + { + try + { + var jsonStr = JsonSerializer.Serialize(new { Data = JsonDocument.Parse(pair.Value) }, _options); + var json = JsonDocument.Parse(jsonStr); + dic[pair.Key] = json; + } + catch + { + var str = JsonSerializer.Serialize(new { Data = pair.Value }, _options); + var json = JsonDocument.Parse(str); + dic[pair.Key] = json; + } + } + + return dic; + } #endregion } } diff --git a/src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs b/src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs index 03e1f8ff..265b631a 100644 --- a/src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs +++ b/src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs @@ -1,8 +1,6 @@ using BotSharp.Abstraction.Instructs.Models; using BotSharp.Abstraction.Loggers.Models; -using BotSharp.Abstraction.Options; using BotSharp.Abstraction.Users; -using System.Text.Json; namespace BotSharp.Logger.Hooks; @@ -10,19 +8,18 @@ public class InstructionLogHook : InstructHookBase { private readonly IServiceProvider _services; private readonly ILogger _logger; - private readonly BotSharpOptions _options; private readonly IUserIdentity _user; + public override string SelfId => string.Empty; + public InstructionLogHook( IServiceProvider services, ILogger logger, - IUserIdentity user, - BotSharpOptions options) + IUserIdentity user) { _services = services; _logger = logger; _user = user; - _options = options; } public override async Task OnResponseGenerated(InstructResponseModel response) @@ -40,57 +37,14 @@ public class InstructionLogHook : InstructHookBase AgentId = response.AgentId, Provider = response.Provider, Model = response.Model, + TemplateName = response.TemplateName, + UserMessage = response.UserMessage, + SystemInstruction = response.SystemInstruction, + CompletionText = response.CompletionText, States = state.GetStates(), UserId = user?.Id } }); return; } - - private Dictionary CollectStates() - { - var res = new Dictionary(); - var state = _services.GetRequiredService(); - var curStates = state.GetStates(); - - curStates["test"] = JsonSerializer.Serialize(new - { - Number = "789", - Dummy = new - { - Id = 123, - Name = "name", - Score = 12.123 - }, - Items = new List - { - new - { - Name = "image", - Label = "before-service", - Attribute = new - { - Location = "Chicago", - Time = "afternoon" - } - }, - new - { - Name = "pdf", - Label = "after-service", - Attribute = new - { - Location = "New York", - Time = "morning" - } - }, - }, - Lists = new List - { - "abc", - "bcd" - } - }, _options.JsonSerializerOptions); - return curStates; - } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj index 58157b2d..38ec30db 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj +++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj @@ -1,4 +1,4 @@ - + $(TargetFramework) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs index 45054223..6a1c3705 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs @@ -52,8 +52,29 @@ public class InstructModeController : ControllerBase .SetState("model", input.Model, source: StateSource.External) .SetState("model_id", input.ModelId, source: StateSource.External); + var agentId = input.AgentId ?? Guid.Empty.ToString(); var textCompletion = CompletionProvider.GetTextCompletion(_services); - return await textCompletion.GetCompletion(input.Text, input.AgentId ?? Guid.Empty.ToString(), Guid.NewGuid().ToString()); + var response = await textCompletion.GetCompletion(input.Text, agentId, Guid.NewGuid().ToString()); + + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) + { + continue; + } + + await hook.OnResponseGenerated(new InstructResponseModel + { + AgentId = agentId, + Provider = textCompletion.Provider, + Model = textCompletion.Model, + TemplateName = input.Template, + UserMessage = input.Text, + CompletionText = response + }); + } + return response; } #region Chat @@ -66,10 +87,11 @@ public class InstructModeController : ControllerBase .SetState("model", input.Model, source: StateSource.External) .SetState("model_id", input.ModelId, source: StateSource.External); + var agentId = input.AgentId ?? Guid.Empty.ToString(); var completion = CompletionProvider.GetChatCompletion(_services); var message = await completion.GetChatCompletions(new Agent() { - Id = input.AgentId ?? Guid.Empty.ToString(), + Id = agentId, Instruction = input.Instruction }, new List { @@ -79,14 +101,22 @@ public class InstructModeController : ControllerBase var hooks = _services.GetServices(); foreach (var hook in hooks) { + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) + { + continue; + } + await hook.OnResponseGenerated(new InstructResponseModel { - AgentId = input.AgentId, - Provider = input.Provider, - Model = input.Model + AgentId = agentId, + Provider = completion.Provider, + Model = completion.Model, + TemplateName = input.Template, + UserMessage = input.Text, + SystemInstruction = message.RenderedInstruction, + CompletionText = message.Content }); } - return message.Content; } #endregion diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs index de07a482..e7ba7fd7 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs @@ -1,4 +1,8 @@ +using BotSharp.Abstraction.Instructs.Models; using BotSharp.Abstraction.Loggers.Models; +using BotSharp.Abstraction.Repositories; +using BotSharp.OpenAPI.ViewModels.Logs; +using EntityFrameworkCore.BootKit; using Microsoft.AspNetCore.Hosting; namespace BotSharp.OpenAPI.Controllers; @@ -45,4 +49,16 @@ public class LoggerController : ControllerBase var conversationService = _services.GetRequiredService(); return await conversationService.GetConversationStateLogs(conversationId); } + + [HttpGet("/logger/instruction/log")] + public async Task> GetInstructionLogs([FromQuery] InstructLogFilter request) + { + var db = _services.GetRequiredService(); + var logs = db.GetInstructionLogs(request); + return new PagedItems + { + Items = logs.Items.Select(x => InstructionLogViewModel.From(x)), + Count = logs.Count + }; + } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs index 3b0303fd..1ad3aead 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs @@ -15,4 +15,5 @@ public class IncomingInstructRequest : IncomingMessageModel { public string? AgentId { get; set; } public string? Instruction { get; set; } + public string? Template { get; set; } } \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs index b71306cf..178bc67b 100644 --- a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs @@ -9,10 +9,12 @@ namespace BotSharp.Plugin.AnthropicAI.Providers; public class ChatCompletionProvider : IChatCompletion { public string Provider => "anthropic"; + public string Model => _model; protected readonly AnthropicSettings _settings; protected readonly IServiceProvider _services; protected readonly ILogger _logger; + private List renderedInstructions = []; protected string _model; @@ -57,6 +59,7 @@ public class ChatCompletionProvider : IChatCompletion ToolCallId = toolResult.Id, FunctionName = toolResult.Name, FunctionArgs = JsonSerializer.Serialize(toolResult.Input), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } else @@ -66,6 +69,7 @@ public class ChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } @@ -98,12 +102,15 @@ public class ChatCompletionProvider : IChatCompletion private (string, MessageParameters) PrepareOptions(Agent agent, List conversations, LlmModelSetting settings) { var instruction = ""; + renderedInstructions = []; var agentService = _services.GetRequiredService(); if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { - instruction += agentService.RenderedInstruction(agent); + var text = agentService.RenderedInstruction(agent); + instruction += text; + renderedInstructions.Add(text); } /*var routing = _services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.AudioHandler/Provider/NativeWhisperProvider.cs b/src/Plugins/BotSharp.Plugin.AudioHandler/Provider/NativeWhisperProvider.cs index 1947ebc4..00d1da58 100644 --- a/src/Plugins/BotSharp.Plugin.AudioHandler/Provider/NativeWhisperProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AudioHandler/Provider/NativeWhisperProvider.cs @@ -15,6 +15,8 @@ public class NativeWhisperProvider : IAudioCompletion private readonly ILogger _logger; public string Provider => "native-whisper"; + public string Model => _model; + private string _model; public NativeWhisperProvider( BotSharpDatabaseSettings dbSettings, @@ -56,11 +58,13 @@ public class NativeWhisperProvider : IAudioCompletion { if (Enum.TryParse(model, true, out GgmlType ggmlType)) { + _model = model; LoadWhisperModel(ggmlType); } else { _logger.LogWarning($"Unsupported model type: {model}. Use Tiny model instead!"); + _model = "Tiny"; LoadWhisperModel(GgmlType.Tiny); } } diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Audio/AudioCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Audio/AudioCompletionProvider.cs index 8cc75ad8..2948703e 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Audio/AudioCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Audio/AudioCompletionProvider.cs @@ -5,6 +5,8 @@ public partial class AudioCompletionProvider : IAudioCompletion private readonly IServiceProvider _services; public string Provider => "openai"; + public string Model => _model; + private string _model; public AudioCompletionProvider(IServiceProvider service) diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs index 3fef7c0c..e49d91ce 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -9,10 +9,12 @@ public class ChatCompletionProvider : IChatCompletion protected readonly AzureOpenAiSettings _settings; protected readonly IServiceProvider _services; protected readonly ILogger _logger; + private List renderedInstructions = []; protected string _model; public virtual string Provider => "azure-openai"; + public string Model => _model; public ChatCompletionProvider( AzureOpenAiSettings settings, @@ -58,7 +60,8 @@ public class ChatCompletionProvider : IChatCompletion CurrentAgentId = agent.Id, MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, FunctionName = toolCall?.FunctionName, - FunctionArgs = toolCall?.FunctionArguments?.ToString() + FunctionArgs = toolCall?.FunctionArguments?.ToString(), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // Somethings LLM will generate a function name with agent name. @@ -73,6 +76,7 @@ public class ChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } } @@ -83,6 +87,7 @@ public class ChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } catch (Exception ex) @@ -92,6 +97,7 @@ public class ChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } @@ -136,7 +142,8 @@ public class ChatCompletionProvider : IChatCompletion var msg = new RoleDialogModel(AgentRole.Assistant, text) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // After chat completion hook @@ -163,7 +170,8 @@ public class ChatCompletionProvider : IChatCompletion MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, ToolCallId = toolCall?.Id, FunctionName = toolCall?.FunctionName, - FunctionArgs = toolCall?.FunctionArguments?.ToString() + FunctionArgs = toolCall?.FunctionArguments?.ToString(), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // Somethings LLM will generate a function name with agent name. @@ -199,7 +207,10 @@ public class ChatCompletionProvider : IChatCompletion var update = choice.ToolCallUpdates?.FirstOrDefault()?.FunctionArgumentsUpdate?.ToString() ?? string.Empty; Console.Write(update); - await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, update)); + await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, update) + { + RenderedInstruction = string.Join("\r\n", renderedInstructions) + }); continue; } @@ -207,7 +218,10 @@ public class ChatCompletionProvider : IChatCompletion _logger.LogInformation(choice.ContentUpdate[0]?.Text); - await onMessageReceived(new RoleDialogModel(choice.Role?.ToString() ?? ChatMessageRole.Assistant.ToString(), choice.ContentUpdate[0]?.Text ?? string.Empty)); + await onMessageReceived(new RoleDialogModel(choice.Role?.ToString() ?? ChatMessageRole.Assistant.ToString(), choice.ContentUpdate[0]?.Text ?? string.Empty) + { + RenderedInstruction = string.Join("\r\n", renderedInstructions) + }); } return true; @@ -221,6 +235,7 @@ public class ChatCompletionProvider : IChatCompletion var settingsService = _services.GetRequiredService(); var settings = settingsService.GetSetting(Provider, _model); var allowMultiModal = settings != null && settings.MultiModal; + renderedInstructions = []; var messages = new List(); @@ -251,6 +266,7 @@ public class ChatCompletionProvider : IChatCompletion if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var instruction = agentService.RenderedInstruction(agent); + renderedInstructions.Add(instruction); messages.Add(new SystemChatMessage(instruction)); } diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Embedding/TextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Embedding/TextEmbeddingProvider.cs index 6878ca3c..b89e338c 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Embedding/TextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Embedding/TextEmbeddingProvider.cs @@ -13,6 +13,7 @@ public class TextEmbeddingProvider : ITextEmbedding protected int _dimension; public virtual string Provider => "azure-openai"; + public string Model => _model; public TextEmbeddingProvider( AzureOpenAiSettings settings, @@ -49,6 +50,7 @@ public class TextEmbeddingProvider : ITextEmbedding _model = model; } + public void SetDimension(int dimension) { _dimension = dimension > 0 ? dimension : DEFAULT_DIMENSION; diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/ImageCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/ImageCompletionProvider.cs index 967f3e08..1b435d61 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/ImageCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/ImageCompletionProvider.cs @@ -14,6 +14,7 @@ public partial class ImageCompletionProvider : IImageCompletion protected string _model; public virtual string Provider => "azure-openai"; + public string Model => _model; public ImageCompletionProvider( AzureOpenAiSettings settings, diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Text/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Text/TextCompletionProvider.cs index 4953e114..2b6f3dfb 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Text/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Text/TextCompletionProvider.cs @@ -22,6 +22,7 @@ public class TextCompletionProvider : ITextCompletion }; public virtual string Provider => "azure-openai"; + public string Model => _model; public TextCompletionProvider( AzureOpenAiSettings settings, diff --git a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs index ef4d0c8a..4c21dd03 100644 --- a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs @@ -9,9 +9,11 @@ public class ChatCompletionProvider : IChatCompletion { protected readonly IServiceProvider _services; protected readonly ILogger _logger; + private List renderedInstructions = []; protected string _model; public virtual string Provider => "deepseek-ai"; + public string Model => _model; public ChatCompletionProvider( IServiceProvider services, @@ -51,7 +53,8 @@ public class ChatCompletionProvider : IChatCompletion MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, ToolCallId = toolCall?.Id, FunctionName = toolCall?.FunctionName, - FunctionArgs = toolCall?.FunctionArguments?.ToString() + FunctionArgs = toolCall?.FunctionArguments?.ToString(), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // Somethings LLM will generate a function name with agent name. @@ -66,6 +69,7 @@ public class ChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } @@ -107,7 +111,8 @@ public class ChatCompletionProvider : IChatCompletion var msg = new RoleDialogModel(AgentRole.Assistant, text) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // After chat completion hook @@ -134,7 +139,8 @@ public class ChatCompletionProvider : IChatCompletion MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, ToolCallId = toolCall?.Id, FunctionName = toolCall?.FunctionName, - FunctionArgs = toolCall?.FunctionArguments?.ToString() + FunctionArgs = toolCall?.FunctionArguments?.ToString(), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // Somethings LLM will generate a function name with agent name. @@ -170,7 +176,10 @@ public class ChatCompletionProvider : IChatCompletion var update = choice.ToolCallUpdates?.FirstOrDefault()?.FunctionArgumentsUpdate?.ToString() ?? string.Empty; _logger.LogInformation(update); - await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, update)); + await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, update) + { + RenderedInstruction = string.Join("\r\n", renderedInstructions) + }); continue; } @@ -178,7 +187,10 @@ public class ChatCompletionProvider : IChatCompletion _logger.LogInformation(choice.ContentUpdate[0]?.Text); - await onMessageReceived(new RoleDialogModel(choice.Role?.ToString() ?? ChatMessageRole.Assistant.ToString(), choice.ContentUpdate[0]?.Text ?? string.Empty)); + await onMessageReceived(new RoleDialogModel(choice.Role?.ToString() ?? ChatMessageRole.Assistant.ToString(), choice.ContentUpdate[0]?.Text ?? string.Empty) + { + RenderedInstruction = string.Join("\r\n", renderedInstructions) + }); } return true; @@ -197,6 +209,7 @@ public class ChatCompletionProvider : IChatCompletion var settingsService = _services.GetRequiredService(); var settings = settingsService.GetSetting(Provider, _model); var allowMultiModal = settings != null && settings.MultiModal; + renderedInstructions = []; var messages = new List(); @@ -226,6 +239,7 @@ public class ChatCompletionProvider : IChatCompletion if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var text = agentService.RenderedInstruction(agent); + renderedInstructions.Add(text); messages.Add(new SystemChatMessage(text)); } diff --git a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Text/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Text/TextCompletionProvider.cs index a1a88978..27198238 100644 --- a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Text/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Text/TextCompletionProvider.cs @@ -10,6 +10,7 @@ public class TextCompletionProvider : ITextCompletion protected string _model; public string Provider => "deepseek-ai"; + public string Model => _model; public TextCompletionProvider( IServiceProvider services, diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs index 9567ae57..174efcf2 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs @@ -2,7 +2,6 @@ using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Loggers; -using Google.Protobuf.WellKnownTypes; using Microsoft.Extensions.Logging; using Mscc.GenerativeAI; @@ -12,10 +11,12 @@ public class GeminiChatCompletionProvider : IChatCompletion { private readonly IServiceProvider _services; private readonly ILogger _logger; + private List renderedInstructions = []; private string _model; public string Provider => "google-ai"; + public string Model => _model; public GeminiChatCompletionProvider( IServiceProvider services, @@ -53,7 +54,8 @@ public class GeminiChatCompletionProvider : IChatCompletion MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, ToolCallId = part.FunctionCall.Name, FunctionName = part.FunctionCall.Name, - FunctionArgs = part.FunctionCall.Args?.ToString() + FunctionArgs = part.FunctionCall.Args?.ToString(), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } else @@ -62,6 +64,7 @@ public class GeminiChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } @@ -98,6 +101,7 @@ public class GeminiChatCompletionProvider : IChatCompletion { var agentService = _services.GetRequiredService(); var googleSettings = _services.GetRequiredService(); + renderedInstructions = []; // Add settings aiModel.UseGoogleSearch = googleSettings.Gemini.UseGoogleSearch; @@ -117,6 +121,7 @@ public class GeminiChatCompletionProvider : IChatCompletion Role = AgentRole.User }); + renderedInstructions.Add(instruction); systemPrompts.Add(instruction); } diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs index c62aebb8..4fe6ad17 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs @@ -13,10 +13,12 @@ public class PalmChatCompletionProvider : IChatCompletion { private readonly IServiceProvider _services; private readonly ILogger _logger; + private List renderedInstructions = []; private string _model; public string Provider => "google-palm"; + public string Model => _model; public PalmChatCompletionProvider( IServiceProvider services, @@ -61,7 +63,8 @@ public class PalmChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, FunctionName = llmResponse.FunctionName, - FunctionArgs = JsonSerializer.Serialize(llmResponse.Args) + FunctionArgs = JsonSerializer.Serialize(llmResponse.Args), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } else @@ -75,7 +78,8 @@ public class PalmChatCompletionProvider : IChatCompletion msg = new RoleDialogModel(llmResponse.Role, llmResponse.Content ?? message.Content) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/GeminiTextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/GeminiTextCompletionProvider.cs index e14071f7..ebbca98a 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/GeminiTextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/GeminiTextCompletionProvider.cs @@ -14,6 +14,7 @@ public class GeminiTextCompletionProvider : ITextCompletion private string _model; public string Provider => "google-ai"; + public string Model => _model; public GeminiTextCompletionProvider( IServiceProvider services, @@ -74,7 +75,6 @@ public class GeminiTextCompletionProvider : ITextCompletion _model = model; } - private void PrepareOptions(GenerativeModel aiModel) { var settings = _services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/PalmTextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/PalmTextCompletionProvider.cs index ebaf6c5f..7df18bb7 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/PalmTextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/PalmTextCompletionProvider.cs @@ -14,6 +14,7 @@ public class PalmTextCompletionProvider : ITextCompletion private string _model; public string Provider => "google-palm"; + public string Model => _model; public PalmTextCompletionProvider( IServiceProvider services, diff --git a/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs index 88b37dbb..460677ba 100644 --- a/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs @@ -10,10 +10,12 @@ namespace BotSharp.Plugin.HuggingFace.Providers; public class ChatCompletionProvider : IChatCompletion { public string Provider => "huggingface"; + public string Model => _model; private readonly IServiceProvider _services; private readonly HuggingFaceSettings _settings; private readonly ILogger _logger; + private List renderedInstructions = []; private string _model; public ChatCompletionProvider(IServiceProvider services, diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs index 32f7220b..45f41e95 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs @@ -8,6 +8,7 @@ public class ChatCompletionProvider : IChatCompletion private readonly IServiceProvider _services; private readonly ILogger _logger; private readonly LlamaSharpSettings _settings; + private List renderedInstructions = []; private string _model; public ChatCompletionProvider(IServiceProvider services, @@ -20,6 +21,7 @@ public class ChatCompletionProvider : IChatCompletion } public string Provider => "llama-sharp"; + public string Model => _model; public async Task GetChatCompletions(Agent agent, List conversations) { @@ -64,7 +66,8 @@ public class ChatCompletionProvider : IChatCompletion var msg = new RoleDialogModel(AgentRole.Assistant, totalResponse) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = instruction }; // After chat completion hook @@ -146,7 +149,8 @@ public class ChatCompletionProvider : IChatCompletion var msg = new RoleDialogModel(AgentRole.Assistant, totalResponse) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = agent.Instruction }; // Text response received diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs index 2a98becc..e911a8ff 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs @@ -10,6 +10,7 @@ public class TextCompletionProvider : ITextCompletion private readonly ITokenStatistics _tokenStatistics; private string _model; public string Provider => "llama-sharp"; + public string Model => _model; public TextCompletionProvider(IServiceProvider services, ILogger logger, diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextEmbeddingProvider.cs index 1ec461ae..5c58f470 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextEmbeddingProvider.cs @@ -1,6 +1,4 @@ using System.IO; -using System.Xml.Linq; -using static System.Net.Mime.MediaTypeNames; namespace BotSharp.Plugin.LLamaSharp.Providers; @@ -14,6 +12,7 @@ public class TextEmbeddingProvider : ITextEmbedding protected int _dimension = DEFAULT_DIMENSION; public string Provider => "llama-sharp"; + public string Model => string.Empty; public TextEmbeddingProvider(IServiceProvider services, LlamaSharpSettings settings) { diff --git a/src/Plugins/BotSharp.Plugin.LangChain/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.LangChain/Providers/ChatCompletionProvider.cs index 61f66119..aa6b1175 100644 --- a/src/Plugins/BotSharp.Plugin.LangChain/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LangChain/Providers/ChatCompletionProvider.cs @@ -20,6 +20,8 @@ namespace BotSharp.Plugin.VertexAI.Providers IServiceProvider services) : IChatCompletion { public string Provider => "vertexai"; + public string Model => _model; + private readonly VertexAIConfiguration _config = config; private readonly ChatSettings? _settings = settings; private readonly IServiceProvider _services = services; diff --git a/src/Plugins/BotSharp.Plugin.LangChain/Providers/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.LangChain/Providers/TextCompletionProvider.cs index 46186452..70f0cbdc 100644 --- a/src/Plugins/BotSharp.Plugin.LangChain/Providers/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LangChain/Providers/TextCompletionProvider.cs @@ -19,6 +19,8 @@ namespace BotSharp.Plugin.VertexAI.Providers IServiceProvider services) : ITextCompletion { public string Provider => "vertexai"; + public string Model => _model; + private readonly VertexAIConfiguration _config = config; private readonly ChatSettings? _settings = settings; private readonly IServiceProvider _services = services; diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs index 7d9c6141..07b5b76e 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.Models; using BotSharp.Plugin.MetaAI.Settings; using FastText.NetWrapper; using Microsoft.Extensions.DependencyInjection; @@ -17,6 +18,7 @@ public class fastTextEmbeddingProvider : ITextEmbedding private int _dimension; public string Provider => "meta-ai"; + public string Model => string.Empty; public fastTextEmbeddingProvider(IServiceProvider services) { diff --git a/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs index f99293f4..b5b19671 100644 --- a/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs @@ -5,11 +5,13 @@ namespace BotSharp.Plugin.MetaGLM.Providers; public class ChatCompletionProvider : IChatCompletion { public string Provider => "metaglm"; + public string Model => _model; private readonly MetaGLMSettings _settings; private readonly IServiceProvider _services; private readonly ILogger _logger; private readonly MetaGLMClient metaGLMClient; + private List renderedInstructions = []; private string _model; public ChatCompletionProvider(IServiceProvider services, @@ -49,7 +51,8 @@ public class ChatCompletionProvider : IChatCompletion responseMessage = new RoleDialogModel(AgentRole.Assistant, response?.choices.FirstOrDefault()?.message.content) { CurrentAgentId = agent.Id, - MessageId = conversations.Last().MessageId + MessageId = conversations.Last().MessageId, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } @@ -61,7 +64,8 @@ public class ChatCompletionProvider : IChatCompletion CurrentAgentId = agent.Id, MessageId = conversations.Last().MessageId, FunctionName = toolcall.function.name, - FunctionArgs = toolcall.function.arguments + FunctionArgs = toolcall.function.arguments, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } @@ -87,10 +91,12 @@ public class ChatCompletionProvider : IChatCompletion var agentService = _services.GetRequiredService(); List messages = new List(); List toolcalls = new List(); + renderedInstructions = []; if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var instruction = agentService.RenderedInstruction(agent); + renderedInstructions.Add(instruction); messages.Add(new MessageItem("system", instruction)); } diff --git a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs index 8619ecad..37d3877c 100644 --- a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs @@ -27,6 +27,7 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio private readonly IChatClient _client; private readonly ILogger _logger; private readonly IServiceProvider _services; + private List renderedInstructions = []; private string? _model; /// @@ -45,6 +46,7 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio /// public string Provider => "microsoft.extensions.ai"; + public string Model => _model; /// public void SetModelName(string model) => _model = model; @@ -54,6 +56,7 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio { // Before chat completion hook var hooks = _services.GetServices().ToArray(); + renderedInstructions = []; await Task.WhenAll(hooks.Select(hook => hook.BeforeGenerating(agent, conversations))); // Configure options @@ -82,6 +85,7 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio if (_services.GetRequiredService().RenderedInstruction(agent) is string instruction && instruction.Length > 0) { + renderedInstructions.Add(instruction); messages.Add(new(ChatRole.System, instruction)); } @@ -143,7 +147,8 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio RoleDialogModel result = new(AgentRole.Assistant, string.Concat(completion.Message.Contents.OfType())) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; if (completion.Message.Contents.OfType().FirstOrDefault() is { } fcc) diff --git a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextCompletionProvider.cs index 94d26678..ed0e94d9 100644 --- a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextCompletionProvider.cs @@ -24,6 +24,7 @@ public sealed class MicrosoftExtensionsAITextCompletionProvider : ITextCompletio /// public string Provider => "microsoft-extensions-ai"; + public string Model => _model; /// /// Creates an instance of the class. diff --git a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextEmbeddingProvider.cs index 958c4694..1758468a 100644 --- a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextEmbeddingProvider.cs @@ -23,6 +23,7 @@ public sealed class MicrosoftExtensionsAITextEmbeddingProvider : ITextEmbedding /// public string Provider => "microsoft-extensions-ai"; + public string Model => _model; /// public async Task GetVectorAsync(string text) => diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/InstructionLogBetaDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/InstructionLogBetaDocument.cs index 615e93af..4f70ad60 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/InstructionLogBetaDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/InstructionLogBetaDocument.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Loggers.Models; -using System.Text.Json; namespace BotSharp.Plugin.MongoStorage.Collections; @@ -8,6 +7,10 @@ public class InstructionLogBetaDocument : MongoBase public string? AgentId { get; set; } public string Provider { get; set; } = default!; public string Model { get; set; } = default!; + public string? TemplateName { get; set; } + public string UserMessage { get; set; } = default!; + public string? SystemInstruction { get; set; } + public string CompletionText { get; set; } = default!; public string? UserId { get; set; } public Dictionary States { get; set; } = new(); public DateTime CreatedTime { get; set; } @@ -19,6 +22,10 @@ public class InstructionLogBetaDocument : MongoBase AgentId = log.AgentId, Provider = log.Provider, Model = log.Model, + TemplateName = log.TemplateName, + UserMessage = log.UserMessage, + SystemInstruction = log.SystemInstruction, + CompletionText = log.CompletionText, UserId = log.UserId, CreatedTime = log.CreatedTime }; @@ -28,9 +35,14 @@ public class InstructionLogBetaDocument : MongoBase { return new InstructionLogModel { + Id = log.Id, AgentId = log.AgentId, Provider = log.Provider, Model = log.Model, + TemplateName = log.TemplateName, + UserMessage = log.UserMessage, + SystemInstruction = log.SystemInstruction, + CompletionText = log.CompletionText, UserId = log.UserId, CreatedTime = log.CreatedTime }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 9adc04b9..277f9e25 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -194,5 +194,5 @@ public class MongoDbContext => GetCollectionOrCreate("GlobalStatistics"); public IMongoCollection InstructionLogs - => GetCollectionOrCreate("InstructionLogsBeta"); + => GetCollectionOrCreate("InstructionLogs"); } \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs index c90960dd..05d5a6a9 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Instructs.Models; using BotSharp.Abstraction.Loggers.Models; +using BotSharp.Abstraction.Repositories.Filters; using System.Text.Json; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -167,28 +168,9 @@ public partial class MongoRepository { filters.Add(builder.In(x => x.Model, filter.Models)); } - - if (!filter.States.IsNullOrEmpty()) + if (!filter.TemplateNames.IsNullOrEmpty()) { - foreach (var pair in filter.States) - { - if (string.IsNullOrWhiteSpace(pair.Key)) continue; - - // Format key - var keys = pair.Key.Split(".").ToList(); - keys.Insert(1, "data"); - keys.Insert(0, "States"); - var formattedKey = string.Join(".", keys); - - if (pair.Value == null) - { - filters.Add(builder.Exists(formattedKey)); - } - else - { - filters.Add(builder.Eq(formattedKey, pair.Value)); - } - } + filters.Add(builder.In(x => x.TemplateName, filter.TemplateNames)); } var filterDef = builder.And(filters); @@ -196,10 +178,23 @@ public partial class MongoRepository var docs = _dc.InstructionLogs.Find(filterDef).Sort(sortDef).Skip(filter.Offset).Limit(filter.Size).ToList(); var count = _dc.InstructionLogs.CountDocuments(filterDef); + var agentIds = docs.Where(x => !string.IsNullOrEmpty(x.AgentId)).Select(x => x.AgentId).ToList(); + var agents = GetAgents(new AgentFilter + { + AgentIds = agentIds + }); + var logs = docs.Select(x => { var log = InstructionLogBetaDocument.ToDomainModel(x); - log.States = x.States.ToDictionary(x => x.Key, x => x.Value.GetElement("data").Value.ToString() ?? string.Empty); + log.AgentName = !string.IsNullOrEmpty(x.AgentId) ? agents.FirstOrDefault(a => a.Id == x.AgentId)?.Name : null; + log.States = x.States.ToDictionary(p => p.Key, p => + { + var jsonStr = p.Value.ToJson(); + var jsonDoc = JsonDocument.Parse(jsonStr); + var data = jsonDoc.RootElement.GetProperty("data"); + return data.ValueKind != JsonValueKind.Null ? data.ToString() : null; + }); return log; }).ToList(); diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Audio/AudioCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Audio/AudioCompletionProvider.cs index 3311fd43..338affd9 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Audio/AudioCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Audio/AudioCompletionProvider.cs @@ -7,6 +7,8 @@ public partial class AudioCompletionProvider : IAudioCompletion private readonly IServiceProvider _services; public string Provider => "openai"; + public string Model => _model; + private string _model; public AudioCompletionProvider(IServiceProvider service) diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs index 15eb04c5..031fab9c 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -10,8 +10,10 @@ public class ChatCompletionProvider : IChatCompletion protected readonly ILogger _logger; protected string _model; + private List renderedInstructions = []; public virtual string Provider => "openai"; + public string Model => _model; public ChatCompletionProvider( OpenAiSettings settings, @@ -53,7 +55,8 @@ public class ChatCompletionProvider : IChatCompletion MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, ToolCallId = toolCall?.Id, FunctionName = toolCall?.FunctionName, - FunctionArgs = toolCall?.FunctionArguments?.ToString() + FunctionArgs = toolCall?.FunctionArguments?.ToString(), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // Somethings LLM will generate a function name with agent name. @@ -68,6 +71,7 @@ public class ChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } @@ -112,7 +116,8 @@ public class ChatCompletionProvider : IChatCompletion var msg = new RoleDialogModel(AgentRole.Assistant, text) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // After chat completion hook @@ -139,7 +144,8 @@ public class ChatCompletionProvider : IChatCompletion MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty, ToolCallId = toolCall?.Id, FunctionName = toolCall?.FunctionName, - FunctionArgs = toolCall?.FunctionArguments?.ToString() + FunctionArgs = toolCall?.FunctionArguments?.ToString(), + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // Somethings LLM will generate a function name with agent name. @@ -175,7 +181,10 @@ public class ChatCompletionProvider : IChatCompletion var update = choice.ToolCallUpdates?.FirstOrDefault()?.FunctionArgumentsUpdate?.ToString() ?? string.Empty; _logger.LogInformation(update); - await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, update)); + await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, update) + { + RenderedInstruction = string.Join("\r\n", renderedInstructions) + }); continue; } @@ -183,7 +192,10 @@ public class ChatCompletionProvider : IChatCompletion _logger.LogInformation(choice.ContentUpdate[0]?.Text); - await onMessageReceived(new RoleDialogModel(choice.Role?.ToString() ?? ChatMessageRole.Assistant.ToString(), choice.ContentUpdate[0]?.Text ?? string.Empty)); + await onMessageReceived(new RoleDialogModel(choice.Role?.ToString() ?? ChatMessageRole.Assistant.ToString(), choice.ContentUpdate[0]?.Text ?? string.Empty) + { + RenderedInstruction = string.Join("\r\n", renderedInstructions) + }); } return true; @@ -198,6 +210,7 @@ public class ChatCompletionProvider : IChatCompletion var settingsService = _services.GetRequiredService(); var settings = settingsService.GetSetting(Provider, _model); var allowMultiModal = settings != null && settings.MultiModal; + renderedInstructions = []; var messages = new List(); @@ -227,6 +240,7 @@ public class ChatCompletionProvider : IChatCompletion if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var text = agentService.RenderedInstruction(agent); + renderedInstructions.Add(text); messages.Add(new SystemChatMessage(text)); } diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Embedding/TextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Embedding/TextEmbeddingProvider.cs index 5b92d896..4131b814 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Embedding/TextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Embedding/TextEmbeddingProvider.cs @@ -13,6 +13,7 @@ public class TextEmbeddingProvider : ITextEmbedding protected int _dimension = DEFAULT_DIMENSION; public virtual string Provider => "openai"; + public string Model => _model; public TextEmbeddingProvider( OpenAiSettings settings, diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Image/ImageCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Image/ImageCompletionProvider.cs index 0e45e789..7cfda0f5 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Image/ImageCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Image/ImageCompletionProvider.cs @@ -14,6 +14,7 @@ public partial class ImageCompletionProvider : IImageCompletion protected string _model; public virtual string Provider => "openai"; + public string Model => _model; public ImageCompletionProvider( OpenAiSettings settings, diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Text/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Text/TextCompletionProvider.cs index 097a32b7..91b39848 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Text/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Text/TextCompletionProvider.cs @@ -15,6 +15,7 @@ public class TextCompletionProvider : ITextCompletion protected string _model; public virtual string Provider => "openai"; + public string Model => _model; public TextCompletionProvider( OpenAiSettings settings, diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs index 156f238c..3f742777 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs @@ -26,6 +26,7 @@ namespace BotSharp.Plugin.SemanticKernel /// public string Provider => "semantic-kernel"; + public string Model => _model; /// /// Create a new instance of @@ -74,7 +75,8 @@ namespace BotSharp.Plugin.SemanticKernel var response = chatMessageContent != null ? chatMessageContent.Content :string.Empty; var msg = new RoleDialogModel(AgentRole.Assistant, response) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = instruction }; // After chat completion hook diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs index 91dec640..8b03c011 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs @@ -23,6 +23,7 @@ namespace BotSharp.Plugin.SemanticKernel /// public string Provider => "semantic-kernel"; + public string Model => _model; /// /// Create a new instance of diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextEmbeddingProvider.cs index 5486d972..64915090 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextEmbeddingProvider.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.Models; using Microsoft.Extensions.Configuration; using Microsoft.SemanticKernel.Embeddings; using System.Collections.Generic; @@ -33,6 +34,7 @@ namespace BotSharp.Plugin.SemanticKernel protected int _dimension; public string Provider => "semantic-kernel"; + public string Model => string.Empty; /// public async Task GetVectorAsync(string text) diff --git a/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs index 5edf4708..d5e64e7d 100644 --- a/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs @@ -7,10 +7,12 @@ namespace BotSharp.Plugin.SparkDesk.Providers; public class ChatCompletionProvider : IChatCompletion { public string Provider => "sparkdesk"; + public string Model => _model; private readonly SparkDeskSettings _settings; private readonly IServiceProvider _services; private readonly ILogger _logger; + private List renderedInstructions = []; private string _model; public ChatCompletionProvider(IServiceProvider services, @@ -42,7 +44,8 @@ public class ChatCompletionProvider : IChatCompletion var responseMessage = new RoleDialogModel(AgentRole.Assistant, response.Text) { CurrentAgentId = agent.Id, - MessageId = conversations.Last().MessageId + MessageId = conversations.Last().MessageId, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; if (response.FunctionCall != null) @@ -52,7 +55,8 @@ public class ChatCompletionProvider : IChatCompletion CurrentAgentId = agent.Id, MessageId = conversations.Last().MessageId, FunctionName = response.FunctionCall.Name, - FunctionArgs = response.FunctionCall.Arguments + FunctionArgs = response.FunctionCall.Arguments, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; } @@ -92,7 +96,8 @@ public class ChatCompletionProvider : IChatCompletion var msg = new RoleDialogModel(AgentRole.Assistant, response.Text) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // After chat completion hook @@ -116,7 +121,8 @@ public class ChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, FunctionName = response.FunctionCall.Name, - FunctionArgs = response.FunctionCall.Arguments + FunctionArgs = response.FunctionCall.Arguments, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }; // Somethings LLM will generate a function name with agent name. @@ -150,14 +156,16 @@ public class ChatCompletionProvider : IChatCompletion { CurrentAgentId = agent.Id, FunctionName = response.FunctionCall.Name, - FunctionArgs = response.FunctionCall.Arguments + FunctionArgs = response.FunctionCall.Arguments, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }); continue; } await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, response.Text) { - CurrentAgentId = agent.Id + CurrentAgentId = agent.Id, + RenderedInstruction = string.Join("\r\n", renderedInstructions) }); } @@ -175,10 +183,12 @@ public class ChatCompletionProvider : IChatCompletion var functions = new List(); var agentService = _services.GetRequiredService(); var messages = new List(); + renderedInstructions = []; if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var instruction = agentService.RenderedInstruction(agent); + renderedInstructions.Add(instruction); messages.Add(ChatMessage.FromSystem(instruction)); } if (!string.IsNullOrEmpty(agent.Knowledges))