Add featuer of VisibilityExpression

This commit is contained in:
Haiping Chen 2024-03-31 14:28:16 -05:00
parent 3dd203f0da
commit f5eaa716ea
9 changed files with 128 additions and 33 deletions

View file

@ -3,3 +3,31 @@
A **calling function** is a function that is passed as an argument to another function and is executed after a specific event or action occurs. In the context of **large language models (LLMs)**, calling functions can be used to hook into various stages of an LLM application. They are useful for tasks such as logging, monitoring, streaming, and more. For example, in the **BotSharp** framework, calling functions can be used to log information, monitor the progress of an LLM application, or perform other tasks. The BotSharp provides a `callbacks` argument that allows developers to interactive with external systems.
The use of calling functions in LLM applications provides flexibility and extensibility. Developers can customize the behavior of their applications by defining callback handlers that implement specific methods. These handlers can be used for tasks like logging, error handling, or interacting with external systems. The function will be triggered by LLM based on the conversation context.
## Hide Function
In order to more flexibly control whether the Agent is allowed to use a certain function, there is a Visibility Expression property in the function definition that can be used to control display or hiding. When we input prompt into LLM, although we can use state variables in the system instruction file to control the rendering content, LLM will still take the definition of the function into consideration. If the related functions are not hidden at the same time, LLM will still be It is possible to call related functions, bringing unexpected results. Because we need to control system instruction and function definition at the same time to make them consistent.
```json
{
"name": "make_payment",
"description": "call this function to make payment",
"visibility_expression": "{% if states.order_number != empty %}visible{% endif %}",
"parameters": {
"type": "object",
"properties": {
"order_number": {
"type": "string",
"description": "order number."
},
"total_amount": {
"type": "string",
"description": "total amount."
}
},
"required": ["order_number", "total_amount"]
}
}
```
The above is an example. The system will parse the liquid template of Visibility Expression `{% if states.order_number != empty %}visible{% endif %}`. When "visible" is returned, the system will allow the Agent to use this function. In liquid In expressions, we can use `states.name` to reference the state value in the conversation.

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Repositories.Filters;
@ -23,6 +24,8 @@ public interface IAgentService
string RenderedTemplate(Agent agent, string templateName);
bool RenderFunction(Agent agent, FunctionDef def);
/// <summary>
/// Get agent detail without trigger any hook.
/// </summary>

View file

@ -5,6 +5,9 @@ public class FunctionDef
public string Name { get; set; }
public string Description { get; set; }
[JsonPropertyName("visibility_expression")]
public string? VisibilityExpression { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Impact { get; set; }

View file

@ -28,4 +28,13 @@ public interface IContentGeneratingHook
/// </summary>
/// <returns></returns>
Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats) => Task.CompletedTask;
/// <summary>
/// Rdndering template
/// </summary>
/// <param name="agent"></param>
/// <param name="name"></param>
/// <param name="content"></param>
/// <returns></returns>
Task OnRenderingTemplate(Agent agent, string name, string content) => Task.CompletedTask;
}

View file

@ -90,32 +90,6 @@ public partial class AgentService
return agent;
}
public string RenderedTemplate(Agent agent, string templateName)
{
// render liquid template
var render = _services.GetRequiredService<ITemplateRender>();
var template = agent.Templates.First(x => x.Name == templateName).Content;
// update states
var conv = _services.GetRequiredService<IConversationService>();
foreach (var t in conv.States.GetStates())
{
agent.TemplateDict[t.Key] = t.Value;
}
return render.Render(template, agent.TemplateDict);
}
public string RenderedInstruction(Agent agent)
{
var render = _services.GetRequiredService<ITemplateRender>();
// update states
var conv = _services.GetRequiredService<IConversationService>();
foreach (var t in conv.States.GetStates())
{
agent.TemplateDict[t.Key] = t.Value;
}
return render.Render(agent.Instruction, agent.TemplateDict);
}
private void PopulateState(Dictionary<string, object> dict)
{
var conv = _services.GetRequiredService<IConversationService>();

View file

@ -0,0 +1,55 @@
using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public string RenderedInstruction(Agent agent)
{
var render = _services.GetRequiredService<ITemplateRender>();
// update states
var conv = _services.GetRequiredService<IConversationService>();
foreach (var t in conv.States.GetStates())
{
agent.TemplateDict[t.Key] = t.Value;
}
return render.Render(agent.Instruction, agent.TemplateDict);
}
public bool RenderFunction(Agent agent, FunctionDef def)
{
if (!string.IsNullOrEmpty(def.VisibilityExpression))
{
var render = _services.GetRequiredService<ITemplateRender>();
var result = render.Render(def.VisibilityExpression, new Dictionary<string, object>
{
{ "states", agent.TemplateDict }
});
return result == "visible";
}
return true;
}
public string RenderedTemplate(Agent agent, string templateName)
{
// render liquid template
var render = _services.GetRequiredService<ITemplateRender>();
var template = agent.Templates.First(x => x.Name == templateName).Content;
// update states
var conv = _services.GetRequiredService<IConversationService>();
foreach (var t in conv.States.GetStates())
{
agent.TemplateDict[t.Key] = t.Value;
}
var content = render.Render(template, agent.TemplateDict);
HookEmitter.Emit<IContentGeneratingHook>(_services, async hook =>
await hook.OnRenderingTemplate(agent, templateName, content)
).Wait();
return content;
}
}

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Repositories;
using System.IO;
namespace BotSharp.Core.Agents.Services;

View file

@ -218,6 +218,8 @@ public class ChatCompletionProvider : IChatCompletion
}
foreach (var function in agent.Functions)
{
if (agentService.RenderFunction(agent, function))
{
chatCompletionsOptions.Functions.Add(new FunctionDefinition
{
@ -226,6 +228,7 @@ public class ChatCompletionProvider : IChatCompletion
Parameters = BinaryData.FromObjectAsJson(function.Parameters)
});
}
}
foreach (var message in conversations)
{

View file

@ -72,6 +72,27 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
}
public async Task OnRenderingTemplate(Agent agent, string name, string content)
{
if (!_convSettings.ShowVerboseLog) return;
var conversationId = _state.GetConversationId();
var log = $"{agent.Name} is using template {name}";
var message = new RoleDialogModel(AgentRole.System, log)
{
MessageId = _routingCtx.MessageId
};
var input = new ContentLogInputModel(conversationId, message)
{
Name = agent.Name,
Source = ContentLogSource.HardRule,
Log = log
};
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
}
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
{
if (!_convSettings.ShowVerboseLog) return;