add visible property

This commit is contained in:
Jicheng Lu 2024-04-09 15:52:21 -05:00
parent 63ad880b1c
commit f6cf392267
4 changed files with 64 additions and 2 deletions

View file

@ -26,6 +26,8 @@ public interface IAgentService
bool RenderFunction(Agent agent, FunctionDef def);
FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def);
/// <summary>
/// Get agent detail without trigger any hook.
/// </summary>

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.Templating;
using Newtonsoft.Json.Linq;
namespace BotSharp.Core.Agents.Services;
@ -32,6 +33,64 @@ public partial class AgentService
return true;
}
public FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def)
{
var parameterDef = def?.Parameters;
var propertyDef = parameterDef?.Properties;
if (propertyDef == null) return null;
var visibleExpress = "visibility_expression";
var root = propertyDef.RootElement;
var iterator = root.EnumerateObject();
var list = new List<string>();
while (iterator.MoveNext())
{
var prop = iterator.Current;
var name = prop.Name;
var node = prop.Value;
var matched = true;
if (node.TryGetProperty(visibleExpress, out var element))
{
var expression = element.GetString();
var render = _services.GetRequiredService<ITemplateRender>();
var result = render.Render(expression, new Dictionary<string, object>
{
{ "states", agent.TemplateDict }
});
matched = result == "visible";
}
if (matched)
{
list.Add(name);
}
}
var rootObject = JObject.Parse(root.GetRawText());
var clonedRoot = rootObject.DeepClone() as JObject;
var required = parameterDef?.Required ?? new List<string>();
foreach (var property in rootObject.Properties())
{
if (list.Contains(property.Name))
{
var value = clonedRoot.GetValue(property.Name) as JObject;
if (value != null && value.ContainsKey(visibleExpress))
{
value.Remove(visibleExpress);
}
}
else
{
clonedRoot.Remove(property.Name);
required.Remove(property.Name);
}
}
parameterDef.Properties = JsonSerializer.Deserialize<JsonDocument>(clonedRoot.ToString());
parameterDef.Required = required;
return parameterDef; ;
}
public string RenderedTemplate(Agent agent, string templateName)
{
// render liquid template

View file

@ -221,11 +221,12 @@ public class ChatCompletionProvider : IChatCompletion
{
if (agentService.RenderFunction(agent, function))
{
var property = agentService.RenderFunctionProperty(agent, function);
chatCompletionsOptions.Functions.Add(new FunctionDefinition
{
Name = function.Name,
Description = function.Description,
Parameters = BinaryData.FromObjectAsJson(function.Parameters)
Parameters = BinaryData.FromObjectAsJson(property)
});
}
}

View file

@ -222,7 +222,7 @@ public class ChatCompletionProvider : IChatCompletion
return (prompt, messages.ToArray(), functions.ToArray());
}
private string GetPrompt(List<ChatMessage> messages,List<FunctionDef> functions)
private string GetPrompt(List<ChatMessage> messages, List<FunctionDef> functions)
{
var prompt = string.Empty;