diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs index b0038f35..d217d2c1 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs @@ -12,8 +12,9 @@ public partial class AgentService var conv = _services.GetRequiredService(); // merge instructions - var texts = new List { agent.Instruction }; - texts.AddRange(agent.SecondaryInstructions ?? []); + var instructions = new List { agent.Instruction }; + var secondaryInstructions = agent.SecondaryInstructions?.Where(x => !string.IsNullOrWhiteSpace(x)).ToList() ?? []; + instructions.AddRange(secondaryInstructions); // update states foreach (var t in conv.States.GetStates()) @@ -21,7 +22,7 @@ public partial class AgentService agent.TemplateDict[t.Key] = t.Value; } - var res = render.Render(string.Join("\r\n", texts), agent.TemplateDict); + var res = render.Render(string.Join("\r\n", instructions), agent.TemplateDict); return res; } diff --git a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs index 8b90d29f..45ef200e 100644 --- a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs @@ -101,7 +101,7 @@ public class ChatCompletionProvider : IChatCompletion var agentService = _services.GetRequiredService(); - if (!string.IsNullOrEmpty(agent.Instruction)) + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { instruction += agentService.RenderedInstruction(agent); } @@ -197,7 +197,8 @@ public class ChatCompletionProvider : IChatCompletion ReferenceHandler = ReferenceHandler.IgnoreCycles, }; - foreach (var fn in agent.Functions) + var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + foreach (var fn in functions) { /*var inputschema = new InputSchema() { diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs index a1684c9d..bfce9d98 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -230,7 +230,8 @@ public class ChatCompletionProvider : IChatCompletion MaxOutputTokenCount = maxTokens }; - foreach (var function in agent.Functions) + var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + foreach (var function in functions) { if (!agentService.RenderFunction(agent, function)) continue; @@ -242,7 +243,7 @@ public class ChatCompletionProvider : IChatCompletion functionParameters: BinaryData.FromObjectAsJson(property))); } - if (!string.IsNullOrEmpty(agent.Instruction)) + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var instruction = agentService.RenderedInstruction(agent); messages.Add(new SystemChatMessage(instruction)); diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs index 4419048b..c37284b0 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs @@ -107,7 +107,7 @@ public class GeminiChatCompletionProvider : IChatCompletion var funcDeclarations = new List(); var systemPrompts = new List(); - if (!string.IsNullOrEmpty(agent.Instruction)) + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var instruction = agentService.RenderedInstruction(agent); contents.Add(new Content(instruction) @@ -119,7 +119,8 @@ public class GeminiChatCompletionProvider : IChatCompletion } var funcPrompts = new List(); - foreach (var function in agent.Functions) + var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + foreach (var function in functions) { if (!agentService.RenderFunction(agent, function)) continue; diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs index 851792a6..5c507ee9 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs @@ -99,7 +99,7 @@ public class PalmChatCompletionProvider : IChatCompletion var agentService = _services.GetRequiredService(); - if (!string.IsNullOrEmpty(agent.Instruction)) + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { prompt += agentService.RenderedInstruction(agent); } @@ -110,10 +110,11 @@ public class PalmChatCompletionProvider : IChatCompletion var messages = conversations.Select(c => new PalmChatMessage(c.Content, c.Role == AgentRole.User ? "user" : "AI")) .ToList(); - if (agent.Functions != null && agent.Functions.Count > 0) + var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + if (!functions.IsNullOrEmpty()) { prompt += "\r\n\r\n[Functions] defined in JSON Schema:\r\n"; - prompt += JsonSerializer.Serialize(agent.Functions, new JsonSerializerOptions + prompt += JsonSerializer.Serialize(functions, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true diff --git a/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs index c702a7ec..f99293f4 100644 --- a/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Utilities; + namespace BotSharp.Plugin.MetaGLM.Providers; public class ChatCompletionProvider : IChatCompletion @@ -86,7 +88,7 @@ public class ChatCompletionProvider : IChatCompletion List messages = new List(); List toolcalls = new List(); - if (!string.IsNullOrEmpty(agent.Instruction)) + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var instruction = agentService.RenderedInstruction(agent); messages.Add(new MessageItem("system", instruction)); @@ -105,7 +107,8 @@ public class ChatCompletionProvider : IChatCompletion new MessageItem("assistant", message.Content)); } - foreach (var function in agent.Functions) + var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + foreach (var function in functions) { var functionTool = ConvertToFunctionTool(function); toolcalls.Add(functionTool); diff --git a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs index 67f1e513..37869050 100644 --- a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs @@ -66,7 +66,8 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio if (_services.GetService() is { } agentService) { - foreach (var function in agent.Functions) + var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + foreach (var function in functions) { if (agentService.RenderFunction(agent, function)) { diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs index 845cd978..6f424c33 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -210,7 +210,6 @@ public class ChatCompletionProvider : IChatCompletion MaxOutputTokenCount = maxTokens }; - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); foreach (var function in functions) { diff --git a/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs index d15345e6..5edf4708 100644 --- a/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs @@ -176,7 +176,7 @@ public class ChatCompletionProvider : IChatCompletion var agentService = _services.GetRequiredService(); var messages = new List(); - if (!string.IsNullOrEmpty(agent.Instruction)) + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { var instruction = agentService.RenderedInstruction(agent); messages.Add(ChatMessage.FromSystem(instruction)); @@ -193,7 +193,8 @@ public class ChatCompletionProvider : IChatCompletion ChatMessage.FromAssistant(message.Content)); } - foreach (var function in agent.Functions) + var agentFuncs = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + foreach (var function in agentFuncs) { functions.Add(ConvertToFunctionDef(function)); }