diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index 87d8f415..e8fc01e3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -29,14 +29,16 @@ public interface IAgentService /// Task InheritAgent(Agent agent); - string RenderedInstruction(Agent agent); + string RenderInstruction(Agent agent); - string RenderedTemplate(Agent agent, string templateName); + string RenderTemplate(Agent agent, string templateName); bool RenderFunction(Agent agent, FunctionDef def); FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def); + IEnumerable FilterFunctions(string instruction, Agent agent, StringComparer? comparer = null); + bool RenderVisibility(string? visibilityExpression, Dictionary dict); /// diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs index 9059dbf2..ca93e201 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs @@ -1,12 +1,13 @@ using BotSharp.Abstraction.Loggers; using BotSharp.Abstraction.Templating; using Newtonsoft.Json.Linq; +using System.Text.RegularExpressions; namespace BotSharp.Core.Agents.Services; public partial class AgentService { - public string RenderedInstruction(Agent agent) + public string RenderInstruction(Agent agent) { var render = _services.GetRequiredService(); var conv = _services.GetRequiredService(); @@ -106,7 +107,23 @@ public partial class AgentService return parameterDef; } - public string RenderedTemplate(Agent agent, string templateName) + public IEnumerable FilterFunctions(string instruction, Agent agent, StringComparer? comparer = null) + { + var functions = agent.Functions.AsEnumerable(); + + if (agent.FuncVisMode.IsEqualTo(AgentFuncVisMode.Auto) && !string.IsNullOrWhiteSpace(instruction)) + { + comparer = comparer ?? StringComparer.OrdinalIgnoreCase; + var matches = Regex.Matches(instruction, @"\b[A-Za-z0-9_]+\b"); + var words = new HashSet(matches.Select(m => m.Value), comparer); + functions = functions.Where(x => words.Contains(x.Name, comparer)); + } + + functions = functions.Concat(agent.SecondaryFunctions ?? []); + return functions; + } + + public string RenderTemplate(Agent agent, string templateName) { var conv = _services.GetRequiredService(); var render = _services.GetRequiredService(); diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs index 25deecd0..9c521e6b 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs @@ -75,7 +75,7 @@ public partial class FileInstructService : IFileInstructService return null; } - var instruction = agentService.RenderedTemplate(agent, templateName); + var instruction = agentService.RenderTemplate(agent, templateName); return instruction; } diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Functions/ExecuteTemplateFn.cs b/src/Infrastructure/BotSharp.Core/Instructs/Functions/ExecuteTemplateFn.cs index 7d8bcb46..9173b587 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Functions/ExecuteTemplateFn.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Functions/ExecuteTemplateFn.cs @@ -49,7 +49,7 @@ public class ExecuteTemplateFn : IFunctionCallback try { var agentService = _services.GetRequiredService(); - var text = agentService.RenderedTemplate(agent, templateName); + var text = agentService.RenderTemplate(agent, templateName); var completion = CompletionProvider.GetChatCompletion(_services, provider: agent.LlmConfig?.Provider, model: agent.LlmConfig?.Model); var response = await completion.GetChatCompletions(new Agent() diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index a6422de6..c1b16ee5 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -45,8 +45,8 @@ public partial class InstructService // Render prompt var prompt = string.IsNullOrEmpty(templateName) ? - agentService.RenderedInstruction(agent) : - agentService.RenderedTemplate(agent, templateName); + agentService.RenderInstruction(agent) : + agentService.RenderTemplate(agent, templateName); var completer = CompletionProvider.GetCompletion(_services, agentConfig: agent.LlmConfig); diff --git a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs index 50d67a1b..f2cef397 100644 --- a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs @@ -111,7 +111,7 @@ public class ChatCompletionProvider : IChatCompletion if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { - var text = agentService.RenderedInstruction(agent); + var text = agentService.RenderInstruction(agent); instruction += text; renderedInstructions.Add(text); } @@ -211,7 +211,7 @@ public class ChatCompletionProvider : IChatCompletion ReferenceHandler = ReferenceHandler.IgnoreCycles, }; - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + var functions = agentService.FilterFunctions(instruction, agent); 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 ca2a283e..a213176b 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -359,7 +359,14 @@ public class ChatCompletionProvider : IChatCompletion MaxOutputTokenCount = maxTokens }; - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) + { + var instruction = agentService.RenderInstruction(agent); + renderedInstructions.Add(instruction); + messages.Add(new SystemChatMessage(instruction)); + } + + var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent); foreach (var function in functions) { if (!agentService.RenderFunction(agent, function)) continue; @@ -372,13 +379,6 @@ public class ChatCompletionProvider : IChatCompletion functionParameters: BinaryData.FromObjectAsJson(property))); } - if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) - { - var instruction = agentService.RenderedInstruction(agent); - renderedInstructions.Add(instruction); - messages.Add(new SystemChatMessage(instruction)); - } - if (!string.IsNullOrEmpty(agent.Knowledges)) { messages.Add(new SystemChatMessage(agent.Knowledges)); diff --git a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs index f29dd77a..600ab7ba 100644 --- a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs @@ -331,7 +331,14 @@ public class ChatCompletionProvider : IChatCompletion MaxOutputTokenCount = maxTokens }; - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) + { + var text = agentService.RenderInstruction(agent); + renderedInstructions.Add(text); + messages.Add(new SystemChatMessage(text)); + } + + var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent); foreach (var function in functions) { if (!agentService.RenderFunction(agent, function)) continue; @@ -344,13 +351,6 @@ public class ChatCompletionProvider : IChatCompletion functionParameters: BinaryData.FromObjectAsJson(property))); } - if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) - { - var text = agentService.RenderedInstruction(agent); - renderedInstructions.Add(text); - messages.Add(new SystemChatMessage(text)); - } - if (!string.IsNullOrEmpty(agent.Knowledges)) { messages.Add(new SystemChatMessage(agent.Knowledges)); diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs index 1d67eac8..fe6c04c2 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs @@ -196,13 +196,13 @@ public class GeminiChatCompletionProvider : IChatCompletion var systemPrompts = new List(); if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { - var instruction = agentService.RenderedInstruction(agent); + var instruction = agentService.RenderInstruction(agent); renderedInstructions.Add(instruction); systemPrompts.Add(instruction); } var funcPrompts = new List(); - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent); 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 e992fd60..57f1c8bf 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs @@ -102,7 +102,8 @@ public class PalmChatCompletionProvider : IChatCompletion if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { - prompt += agentService.RenderedInstruction(agent); + prompt += agentService.RenderInstruction(agent); + renderedInstructions.Add(prompt); } var routing = _services.GetRequiredService(); @@ -111,7 +112,7 @@ public class PalmChatCompletionProvider : IChatCompletion var messages = conversations.Select(c => new PalmChatMessage(c.Content, c.Role == AgentRole.User ? "user" : "AI")) .ToList(); - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + var functions = agentService.FilterFunctions(prompt, agent); if (!functions.IsNullOrEmpty()) { prompt += "\r\n\r\n[Functions] defined in JSON Schema:\r\n"; diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs index 5d2dd8ca..5a402e34 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -492,13 +492,13 @@ public class GoogleRealTimeProvider : IRealTimeCompletion var systemPrompts = new List(); if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { - var instruction = agentService.RenderedInstruction(agent); + var instruction = agentService.RenderInstruction(agent); renderedInstructions.Add(instruction); systemPrompts.Add(instruction); } var funcPrompts = new List(); - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent); foreach (var function in functions) { if (!agentService.RenderFunction(agent, function)) continue; diff --git a/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs index 5a38b0a9..4a0e9107 100644 --- a/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs @@ -100,7 +100,7 @@ public class ChatCompletionProvider : IChatCompletion content += $"\r\n{AgentRole.Assistant}: "; var agentService = _services.GetRequiredService(); - var instruction = agentService.RenderedInstruction(agent); + var instruction = agentService.RenderInstruction(agent); var prompt = instruction + "\r\n" + content; var api = _services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs index 08b6c676..1380881b 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs @@ -58,7 +58,7 @@ public class ChatCompletionProvider : IChatCompletion string totalResponse = ""; var agentService = _services.GetRequiredService(); - var instruction = agentService.RenderedInstruction(agent); + var instruction = agentService.RenderInstruction(agent); var prompt = instruction + "\r\n" + content; await foreach(var text in Spinner(executor.InferAsync(prompt, inferenceParams))) diff --git a/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs index 8b3ecea3..bf8045b9 100644 --- a/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs @@ -95,7 +95,7 @@ public class ChatCompletionProvider : IChatCompletion if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { - var instruction = agentService.RenderedInstruction(agent); + var instruction = agentService.RenderInstruction(agent); renderedInstructions.Add(instruction); messages.Add(new MessageItem("system", instruction)); } @@ -113,7 +113,7 @@ public class ChatCompletionProvider : IChatCompletion new MessageItem("assistant", message.Content)); } - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent); foreach (var function in functions) { var functionTool = ConvertToFunctionTool(function); diff --git a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs index e510a9a2..a0e7ef86 100644 --- a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs @@ -1,21 +1,22 @@ +using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Agents; -using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Files; using BotSharp.Abstraction.Files.Utilities; using BotSharp.Abstraction.Loggers; using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.Utilities; using Microsoft.Extensions.AI; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using System.Collections.Generic; -using System.Threading.Tasks; using System; +using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading; +using System.Threading.Tasks; namespace BotSharp.Plugin.MicrosoftExtensionsAI; @@ -66,26 +67,25 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio MaxOutputTokens = int.Parse(state.GetState("max_tokens", "1024")) }; - if (_services.GetService() is { } agentService) - { - foreach (var function in agent.Functions) - { - if (agentService.RenderFunction(agent, function)) - { - var property = agentService.RenderFunctionProperty(agent, function); - (options.Tools ??= []).Add(new NopAIFunction(function.Name, function.Description, JsonSerializer.SerializeToElement(property))); - } - } - } - // Configure messages List messages = []; + var agentService = _services.GetRequiredService(); - if (_services.GetRequiredService().RenderedInstruction(agent) is string instruction && - instruction.Length > 0) + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { - renderedInstructions.Add(instruction); - messages.Add(new(ChatRole.System, instruction)); + var text = agentService.RenderInstruction(agent); + renderedInstructions.Add(text); + messages.Add(new(ChatRole.System, text)); + } + + var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent); + foreach (var function in functions) + { + if (agentService.RenderFunction(agent, function)) + { + var property = agentService.RenderFunctionProperty(agent, function); + (options.Tools ??= []).Add(new NopAIFunction(function.Name, function.Description, JsonSerializer.SerializeToElement(property))); + } } if (!string.IsNullOrEmpty(agent.Knowledges)) diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs index 99de556f..1940eb03 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -321,7 +321,18 @@ public class ChatCompletionProvider : IChatCompletion var messages = new List(); var options = InitChatCompletionOption(agent); - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + // Render instructions + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) + { + var text = agentService.RenderInstruction(agent); + renderedInstructions.Add(text); + messages.Add(new SystemChatMessage(text)); + } + + // Filter functions + var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent); + + // Render functions foreach (var function in functions) { if (!agentService.RenderFunction(agent, function)) continue; @@ -334,13 +345,6 @@ public class ChatCompletionProvider : IChatCompletion functionParameters: BinaryData.FromObjectAsJson(property))); } - if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) - { - var text = agentService.RenderedInstruction(agent); - renderedInstructions.Add(text); - messages.Add(new SystemChatMessage(text)); - } - if (!string.IsNullOrEmpty(agent.Knowledges)) { messages.Add(new SystemChatMessage(agent.Knowledges)); diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs index 3a0902d6..8ae4227f 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -555,6 +555,7 @@ public class RealTimeCompletionProvider : IRealTimeCompletion var settings = settingsService.GetSetting(Provider, _model); var allowMultiModal = settings != null && settings.MultiModal; + var instruction = string.Empty; var messages = new List(); var temperature = float.Parse(state.GetState("temperature", "0.0")); @@ -568,7 +569,13 @@ public class RealTimeCompletionProvider : IRealTimeCompletion MaxOutputTokenCount = maxTokens }; - var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) + { + instruction = agentService.RenderInstruction(agent); + messages.Add(new SystemChatMessage(instruction)); + } + + var functions = agentService.FilterFunctions(instruction, agent); foreach (var function in functions) { if (!agentService.RenderFunction(agent, function)) continue; @@ -581,12 +588,6 @@ public class RealTimeCompletionProvider : IRealTimeCompletion functionParameters: BinaryData.FromObjectAsJson(property))); } - if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) - { - var text = agentService.RenderedInstruction(agent); - messages.Add(new SystemChatMessage(text)); - } - if (!string.IsNullOrEmpty(agent.Knowledges)) { messages.Add(new SystemChatMessage(agent.Knowledges)); diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs index 57277399..9633221f 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelChatCompletionProvider.cs @@ -54,7 +54,7 @@ namespace BotSharp.Plugin.SemanticKernel var completion = this._kernelChatCompletion; var agentService = _services.GetRequiredService(); - var instruction = agentService.RenderedInstruction(agent); + var instruction = agentService.RenderInstruction(agent); ChatHistory chatHistory = new ChatHistory(instruction); @@ -70,8 +70,8 @@ namespace BotSharp.Plugin.SemanticKernel } } - var ChatMessage = await completion.GetChatMessageContentsAsync(chatHistory); - var chatMessageContent = ChatMessage?.FirstOrDefault(); + var contents = await completion.GetChatMessageContentsAsync(chatHistory); + var chatMessageContent = contents?.FirstOrDefault(); var response = chatMessageContent != null ? chatMessageContent.Content :string.Empty; var msg = new RoleDialogModel(AgentRole.Assistant, response) { diff --git a/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs index a1d45dcc..2180af7a 100644 --- a/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs @@ -236,7 +236,7 @@ public class ChatCompletionProvider : IChatCompletion if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty()) { - var instruction = agentService.RenderedInstruction(agent); + var instruction = agentService.RenderInstruction(agent); renderedInstructions.Add(instruction); messages.Add(ChatMessage.FromSystem(instruction)); } @@ -252,7 +252,7 @@ public class ChatCompletionProvider : IChatCompletion ChatMessage.FromAssistant(message.Content)); } - var agentFuncs = agent.Functions.Concat(agent.SecondaryFunctions ?? []); + var agentFuncs = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent); foreach (var function in agentFuncs) { functions.Add(ConvertToFunctionDef(function)); diff --git a/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs b/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs index 5d4b07d0..6fac9271 100644 --- a/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs +++ b/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs @@ -41,12 +41,12 @@ namespace BotSharp.Plugin.Google.Core return Task.CompletedTask; } - public string RenderedInstruction(Agent agent) + public string RenderInstruction(Agent agent) { return "Fake Instruction"; } - public string RenderedTemplate(Agent agent, string templateName) + public string RenderTemplate(Agent agent, string templateName) { return $"Rendered template for {templateName}"; } @@ -56,6 +56,11 @@ namespace BotSharp.Plugin.Google.Core return true; } + public IEnumerable FilterFunctions(string instruction, Agent agent, StringComparer? comparer = null) + { + return []; + } + public FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def) { return def.Parameters; diff --git a/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs b/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs index caf4746e..0472571c 100644 --- a/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs +++ b/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs @@ -38,7 +38,7 @@ namespace BotSharp.Plugin.SemanticKernel.Tests _servicesMock.Setup(x => x.GetService(typeof(IEnumerable))) .Returns(new List()); var agentService = new Mock(); - agentService.Setup(x => x.RenderedInstruction(agent)).Returns("How can I help you?"); + agentService.Setup(x => x.RenderInstruction(agent)).Returns("How can I help you?"); _servicesMock.Setup(x => x.GetService(typeof(IAgentService))) .Returns(agentService.Object);