add filter functions
This commit is contained in:
parent
5a6ac3df4e
commit
938656b1f8
|
|
@ -29,14 +29,16 @@ public interface IAgentService
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task InheritAgent(Agent agent);
|
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);
|
bool RenderFunction(Agent agent, FunctionDef def);
|
||||||
|
|
||||||
FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def);
|
FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def);
|
||||||
|
|
||||||
|
IEnumerable<FunctionDef> FilterFunctions(string instruction, Agent agent, StringComparer? comparer = null);
|
||||||
|
|
||||||
bool RenderVisibility(string? visibilityExpression, Dictionary<string, object> dict);
|
bool RenderVisibility(string? visibilityExpression, Dictionary<string, object> dict);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
using BotSharp.Abstraction.Loggers;
|
using BotSharp.Abstraction.Loggers;
|
||||||
using BotSharp.Abstraction.Templating;
|
using BotSharp.Abstraction.Templating;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace BotSharp.Core.Agents.Services;
|
namespace BotSharp.Core.Agents.Services;
|
||||||
|
|
||||||
public partial class AgentService
|
public partial class AgentService
|
||||||
{
|
{
|
||||||
public string RenderedInstruction(Agent agent)
|
public string RenderInstruction(Agent agent)
|
||||||
{
|
{
|
||||||
var render = _services.GetRequiredService<ITemplateRender>();
|
var render = _services.GetRequiredService<ITemplateRender>();
|
||||||
var conv = _services.GetRequiredService<IConversationService>();
|
var conv = _services.GetRequiredService<IConversationService>();
|
||||||
|
|
@ -106,7 +107,23 @@ public partial class AgentService
|
||||||
return parameterDef;
|
return parameterDef;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string RenderedTemplate(Agent agent, string templateName)
|
public IEnumerable<FunctionDef> 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<string>(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<IConversationService>();
|
var conv = _services.GetRequiredService<IConversationService>();
|
||||||
var render = _services.GetRequiredService<ITemplateRender>();
|
var render = _services.GetRequiredService<ITemplateRender>();
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ public partial class FileInstructService : IFileInstructService
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var instruction = agentService.RenderedTemplate(agent, templateName);
|
var instruction = agentService.RenderTemplate(agent, templateName);
|
||||||
return instruction;
|
return instruction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ public class ExecuteTemplateFn : IFunctionCallback
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
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 completion = CompletionProvider.GetChatCompletion(_services, provider: agent.LlmConfig?.Provider, model: agent.LlmConfig?.Model);
|
||||||
var response = await completion.GetChatCompletions(new Agent()
|
var response = await completion.GetChatCompletions(new Agent()
|
||||||
|
|
|
||||||
|
|
@ -45,8 +45,8 @@ public partial class InstructService
|
||||||
|
|
||||||
// Render prompt
|
// Render prompt
|
||||||
var prompt = string.IsNullOrEmpty(templateName) ?
|
var prompt = string.IsNullOrEmpty(templateName) ?
|
||||||
agentService.RenderedInstruction(agent) :
|
agentService.RenderInstruction(agent) :
|
||||||
agentService.RenderedTemplate(agent, templateName);
|
agentService.RenderTemplate(agent, templateName);
|
||||||
|
|
||||||
var completer = CompletionProvider.GetCompletion(_services,
|
var completer = CompletionProvider.GetCompletion(_services,
|
||||||
agentConfig: agent.LlmConfig);
|
agentConfig: agent.LlmConfig);
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,7 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
var text = agentService.RenderedInstruction(agent);
|
var text = agentService.RenderInstruction(agent);
|
||||||
instruction += text;
|
instruction += text;
|
||||||
renderedInstructions.Add(text);
|
renderedInstructions.Add(text);
|
||||||
}
|
}
|
||||||
|
|
@ -211,7 +211,7 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
||||||
};
|
};
|
||||||
|
|
||||||
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
|
var functions = agentService.FilterFunctions(instruction, agent);
|
||||||
foreach (var fn in functions)
|
foreach (var fn in functions)
|
||||||
{
|
{
|
||||||
/*var inputschema = new InputSchema()
|
/*var inputschema = new InputSchema()
|
||||||
|
|
|
||||||
|
|
@ -359,7 +359,14 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
MaxOutputTokenCount = maxTokens
|
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)
|
foreach (var function in functions)
|
||||||
{
|
{
|
||||||
if (!agentService.RenderFunction(agent, function)) continue;
|
if (!agentService.RenderFunction(agent, function)) continue;
|
||||||
|
|
@ -372,13 +379,6 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
functionParameters: BinaryData.FromObjectAsJson(property)));
|
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))
|
if (!string.IsNullOrEmpty(agent.Knowledges))
|
||||||
{
|
{
|
||||||
messages.Add(new SystemChatMessage(agent.Knowledges));
|
messages.Add(new SystemChatMessage(agent.Knowledges));
|
||||||
|
|
|
||||||
|
|
@ -331,7 +331,14 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
MaxOutputTokenCount = maxTokens
|
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)
|
foreach (var function in functions)
|
||||||
{
|
{
|
||||||
if (!agentService.RenderFunction(agent, function)) continue;
|
if (!agentService.RenderFunction(agent, function)) continue;
|
||||||
|
|
@ -344,13 +351,6 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
functionParameters: BinaryData.FromObjectAsJson(property)));
|
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))
|
if (!string.IsNullOrEmpty(agent.Knowledges))
|
||||||
{
|
{
|
||||||
messages.Add(new SystemChatMessage(agent.Knowledges));
|
messages.Add(new SystemChatMessage(agent.Knowledges));
|
||||||
|
|
|
||||||
|
|
@ -196,13 +196,13 @@ public class GeminiChatCompletionProvider : IChatCompletion
|
||||||
var systemPrompts = new List<string>();
|
var systemPrompts = new List<string>();
|
||||||
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
var instruction = agentService.RenderedInstruction(agent);
|
var instruction = agentService.RenderInstruction(agent);
|
||||||
renderedInstructions.Add(instruction);
|
renderedInstructions.Add(instruction);
|
||||||
systemPrompts.Add(instruction);
|
systemPrompts.Add(instruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
var funcPrompts = new List<string>();
|
var funcPrompts = new List<string>();
|
||||||
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
|
var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent);
|
||||||
foreach (var function in functions)
|
foreach (var function in functions)
|
||||||
{
|
{
|
||||||
if (!agentService.RenderFunction(agent, function)) continue;
|
if (!agentService.RenderFunction(agent, function)) continue;
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,8 @@ public class PalmChatCompletionProvider : IChatCompletion
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
prompt += agentService.RenderedInstruction(agent);
|
prompt += agentService.RenderInstruction(agent);
|
||||||
|
renderedInstructions.Add(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
var routing = _services.GetRequiredService<IRoutingService>();
|
var routing = _services.GetRequiredService<IRoutingService>();
|
||||||
|
|
@ -111,7 +112,7 @@ public class PalmChatCompletionProvider : IChatCompletion
|
||||||
var messages = conversations.Select(c => new PalmChatMessage(c.Content, c.Role == AgentRole.User ? "user" : "AI"))
|
var messages = conversations.Select(c => new PalmChatMessage(c.Content, c.Role == AgentRole.User ? "user" : "AI"))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
|
var functions = agentService.FilterFunctions(prompt, agent);
|
||||||
if (!functions.IsNullOrEmpty())
|
if (!functions.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
prompt += "\r\n\r\n[Functions] defined in JSON Schema:\r\n";
|
prompt += "\r\n\r\n[Functions] defined in JSON Schema:\r\n";
|
||||||
|
|
|
||||||
|
|
@ -492,13 +492,13 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
|
||||||
var systemPrompts = new List<string>();
|
var systemPrompts = new List<string>();
|
||||||
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
var instruction = agentService.RenderedInstruction(agent);
|
var instruction = agentService.RenderInstruction(agent);
|
||||||
renderedInstructions.Add(instruction);
|
renderedInstructions.Add(instruction);
|
||||||
systemPrompts.Add(instruction);
|
systemPrompts.Add(instruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
var funcPrompts = new List<string>();
|
var funcPrompts = new List<string>();
|
||||||
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
|
var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent);
|
||||||
foreach (var function in functions)
|
foreach (var function in functions)
|
||||||
{
|
{
|
||||||
if (!agentService.RenderFunction(agent, function)) continue;
|
if (!agentService.RenderFunction(agent, function)) continue;
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
content += $"\r\n{AgentRole.Assistant}: ";
|
content += $"\r\n{AgentRole.Assistant}: ";
|
||||||
|
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
var instruction = agentService.RenderedInstruction(agent);
|
var instruction = agentService.RenderInstruction(agent);
|
||||||
var prompt = instruction + "\r\n" + content;
|
var prompt = instruction + "\r\n" + content;
|
||||||
|
|
||||||
var api = _services.GetRequiredService<IInferenceApi>();
|
var api = _services.GetRequiredService<IInferenceApi>();
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
string totalResponse = "";
|
string totalResponse = "";
|
||||||
|
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
var instruction = agentService.RenderedInstruction(agent);
|
var instruction = agentService.RenderInstruction(agent);
|
||||||
var prompt = instruction + "\r\n" + content;
|
var prompt = instruction + "\r\n" + content;
|
||||||
|
|
||||||
await foreach(var text in Spinner(executor.InferAsync(prompt, inferenceParams)))
|
await foreach(var text in Spinner(executor.InferAsync(prompt, inferenceParams)))
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
var instruction = agentService.RenderedInstruction(agent);
|
var instruction = agentService.RenderInstruction(agent);
|
||||||
renderedInstructions.Add(instruction);
|
renderedInstructions.Add(instruction);
|
||||||
messages.Add(new MessageItem("system", instruction));
|
messages.Add(new MessageItem("system", instruction));
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +113,7 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
new MessageItem("assistant", message.Content));
|
new MessageItem("assistant", message.Content));
|
||||||
}
|
}
|
||||||
|
|
||||||
var functions = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
|
var functions = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent);
|
||||||
foreach (var function in functions)
|
foreach (var function in functions)
|
||||||
{
|
{
|
||||||
var functionTool = ConvertToFunctionTool(function);
|
var functionTool = ConvertToFunctionTool(function);
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,22 @@
|
||||||
|
using BotSharp.Abstraction.Agents;
|
||||||
using BotSharp.Abstraction.Agents.Enums;
|
using BotSharp.Abstraction.Agents.Enums;
|
||||||
using BotSharp.Abstraction.Agents.Models;
|
using BotSharp.Abstraction.Agents.Models;
|
||||||
using BotSharp.Abstraction.Agents;
|
|
||||||
using BotSharp.Abstraction.Conversations.Models;
|
|
||||||
using BotSharp.Abstraction.Conversations;
|
using BotSharp.Abstraction.Conversations;
|
||||||
|
using BotSharp.Abstraction.Conversations.Models;
|
||||||
using BotSharp.Abstraction.Files;
|
using BotSharp.Abstraction.Files;
|
||||||
using BotSharp.Abstraction.Files.Utilities;
|
using BotSharp.Abstraction.Files.Utilities;
|
||||||
using BotSharp.Abstraction.Loggers;
|
using BotSharp.Abstraction.Loggers;
|
||||||
using BotSharp.Abstraction.MLTasks;
|
using BotSharp.Abstraction.MLTasks;
|
||||||
|
using BotSharp.Abstraction.Utilities;
|
||||||
using Microsoft.Extensions.AI;
|
using Microsoft.Extensions.AI;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BotSharp.Plugin.MicrosoftExtensionsAI;
|
namespace BotSharp.Plugin.MicrosoftExtensionsAI;
|
||||||
|
|
||||||
|
|
@ -66,26 +67,25 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio
|
||||||
MaxOutputTokens = int.Parse(state.GetState("max_tokens", "1024"))
|
MaxOutputTokens = int.Parse(state.GetState("max_tokens", "1024"))
|
||||||
};
|
};
|
||||||
|
|
||||||
if (_services.GetService<IAgentService>() 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
|
// Configure messages
|
||||||
List<ChatMessage> messages = [];
|
List<ChatMessage> messages = [];
|
||||||
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
|
|
||||||
if (_services.GetRequiredService<IAgentService>().RenderedInstruction(agent) is string instruction &&
|
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
||||||
instruction.Length > 0)
|
|
||||||
{
|
{
|
||||||
renderedInstructions.Add(instruction);
|
var text = agentService.RenderInstruction(agent);
|
||||||
messages.Add(new(ChatRole.System, instruction));
|
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))
|
if (!string.IsNullOrEmpty(agent.Knowledges))
|
||||||
|
|
|
||||||
|
|
@ -321,7 +321,18 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
var messages = new List<ChatMessage>();
|
var messages = new List<ChatMessage>();
|
||||||
var options = InitChatCompletionOption(agent);
|
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)
|
foreach (var function in functions)
|
||||||
{
|
{
|
||||||
if (!agentService.RenderFunction(agent, function)) continue;
|
if (!agentService.RenderFunction(agent, function)) continue;
|
||||||
|
|
@ -334,13 +345,6 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
functionParameters: BinaryData.FromObjectAsJson(property)));
|
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))
|
if (!string.IsNullOrEmpty(agent.Knowledges))
|
||||||
{
|
{
|
||||||
messages.Add(new SystemChatMessage(agent.Knowledges));
|
messages.Add(new SystemChatMessage(agent.Knowledges));
|
||||||
|
|
|
||||||
|
|
@ -555,6 +555,7 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
|
||||||
var settings = settingsService.GetSetting(Provider, _model);
|
var settings = settingsService.GetSetting(Provider, _model);
|
||||||
var allowMultiModal = settings != null && settings.MultiModal;
|
var allowMultiModal = settings != null && settings.MultiModal;
|
||||||
|
|
||||||
|
var instruction = string.Empty;
|
||||||
var messages = new List<ChatMessage>();
|
var messages = new List<ChatMessage>();
|
||||||
|
|
||||||
var temperature = float.Parse(state.GetState("temperature", "0.0"));
|
var temperature = float.Parse(state.GetState("temperature", "0.0"));
|
||||||
|
|
@ -568,7 +569,13 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
|
||||||
MaxOutputTokenCount = maxTokens
|
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)
|
foreach (var function in functions)
|
||||||
{
|
{
|
||||||
if (!agentService.RenderFunction(agent, function)) continue;
|
if (!agentService.RenderFunction(agent, function)) continue;
|
||||||
|
|
@ -581,12 +588,6 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
|
||||||
functionParameters: BinaryData.FromObjectAsJson(property)));
|
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))
|
if (!string.IsNullOrEmpty(agent.Knowledges))
|
||||||
{
|
{
|
||||||
messages.Add(new SystemChatMessage(agent.Knowledges));
|
messages.Add(new SystemChatMessage(agent.Knowledges));
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ namespace BotSharp.Plugin.SemanticKernel
|
||||||
var completion = this._kernelChatCompletion;
|
var completion = this._kernelChatCompletion;
|
||||||
|
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
var instruction = agentService.RenderedInstruction(agent);
|
var instruction = agentService.RenderInstruction(agent);
|
||||||
|
|
||||||
ChatHistory chatHistory = new ChatHistory(instruction);
|
ChatHistory chatHistory = new ChatHistory(instruction);
|
||||||
|
|
||||||
|
|
@ -70,8 +70,8 @@ namespace BotSharp.Plugin.SemanticKernel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ChatMessage = await completion.GetChatMessageContentsAsync(chatHistory);
|
var contents = await completion.GetChatMessageContentsAsync(chatHistory);
|
||||||
var chatMessageContent = ChatMessage?.FirstOrDefault();
|
var chatMessageContent = contents?.FirstOrDefault();
|
||||||
var response = chatMessageContent != null ? chatMessageContent.Content :string.Empty;
|
var response = chatMessageContent != null ? chatMessageContent.Content :string.Empty;
|
||||||
var msg = new RoleDialogModel(AgentRole.Assistant, response)
|
var msg = new RoleDialogModel(AgentRole.Assistant, response)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -236,7 +236,7 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
if (!string.IsNullOrEmpty(agent.Instruction) || !agent.SecondaryInstructions.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
var instruction = agentService.RenderedInstruction(agent);
|
var instruction = agentService.RenderInstruction(agent);
|
||||||
renderedInstructions.Add(instruction);
|
renderedInstructions.Add(instruction);
|
||||||
messages.Add(ChatMessage.FromSystem(instruction));
|
messages.Add(ChatMessage.FromSystem(instruction));
|
||||||
}
|
}
|
||||||
|
|
@ -252,7 +252,7 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
ChatMessage.FromAssistant(message.Content));
|
ChatMessage.FromAssistant(message.Content));
|
||||||
}
|
}
|
||||||
|
|
||||||
var agentFuncs = agent.Functions.Concat(agent.SecondaryFunctions ?? []);
|
var agentFuncs = agentService.FilterFunctions(renderedInstructions.FirstOrDefault(), agent);
|
||||||
foreach (var function in agentFuncs)
|
foreach (var function in agentFuncs)
|
||||||
{
|
{
|
||||||
functions.Add(ConvertToFunctionDef(function));
|
functions.Add(ConvertToFunctionDef(function));
|
||||||
|
|
|
||||||
|
|
@ -41,12 +41,12 @@ namespace BotSharp.Plugin.Google.Core
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string RenderedInstruction(Agent agent)
|
public string RenderInstruction(Agent agent)
|
||||||
{
|
{
|
||||||
return "Fake Instruction";
|
return "Fake Instruction";
|
||||||
}
|
}
|
||||||
|
|
||||||
public string RenderedTemplate(Agent agent, string templateName)
|
public string RenderTemplate(Agent agent, string templateName)
|
||||||
{
|
{
|
||||||
return $"Rendered template for {templateName}";
|
return $"Rendered template for {templateName}";
|
||||||
}
|
}
|
||||||
|
|
@ -56,6 +56,11 @@ namespace BotSharp.Plugin.Google.Core
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<FunctionDef> FilterFunctions(string instruction, Agent agent, StringComparer? comparer = null)
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
public FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def)
|
public FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def)
|
||||||
{
|
{
|
||||||
return def.Parameters;
|
return def.Parameters;
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ namespace BotSharp.Plugin.SemanticKernel.Tests
|
||||||
_servicesMock.Setup(x => x.GetService(typeof(IEnumerable<IContentGeneratingHook>)))
|
_servicesMock.Setup(x => x.GetService(typeof(IEnumerable<IContentGeneratingHook>)))
|
||||||
.Returns(new List<IContentGeneratingHook>());
|
.Returns(new List<IContentGeneratingHook>());
|
||||||
var agentService = new Mock<IAgentService>();
|
var agentService = new Mock<IAgentService>();
|
||||||
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)))
|
_servicesMock.Setup(x => x.GetService(typeof(IAgentService)))
|
||||||
.Returns(agentService.Object);
|
.Returns(agentService.Object);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue