response_to_user

This commit is contained in:
Haiping Chen 2025-04-23 23:10:07 -05:00
parent 99d3104674
commit fe82939733
7 changed files with 63 additions and 3 deletions

View file

@ -6,8 +6,8 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="EntityFramework" Version="6.4.4" />
<PackageVersion Include="Google_GenerativeAI" Version="2.5.5" />
<PackageVersion Include="Google_GenerativeAI.Live" Version="2.5.5" />
<PackageVersion Include="Google_GenerativeAI" Version="2.5.8" />
<PackageVersion Include="Google_GenerativeAI.Live" Version="2.5.8" />
<PackageVersion Include="LLMSharp.Google.Palm" Version="1.0.2" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageVersion Include="Microsoft.AspNetCore.StaticFiles" Version="$(AspNetCoreVersion)" />

View file

@ -17,6 +17,12 @@ public class RealtimeConversationHook : ConversationHookBase, IConversationHook
{
return;
}
if (message.FunctionName == "response_to_user")
{
return;
}
// Save states
if (message.FunctionArgs != null && message.FunctionArgs.Length > 3)
{
@ -51,6 +57,11 @@ public class RealtimeConversationHook : ConversationHookBase, IConversationHook
await hub.Completer.UpdateSession(hub.HubConn);
await hub.Completer.TriggerModelInference();
}
else if (message.FunctionName == "response_to_user")
{
await hub.Completer.InsertConversationItem(message);
await hub.Completer.TriggerModelInference();
}
else
{
// Update session for changed states

View file

@ -30,7 +30,7 @@ public class RealtimeHub : IRealtimeHub
var routing = _services.GetRequiredService<IRoutingService>();
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(_conn.CurrentAgentId);
var agent = await agentService.GetAgent(_conn.CurrentAgentId);
var storage = _services.GetRequiredService<IConversationStorage>();
var dialogs = convService.GetDialogHistory();

View file

@ -123,6 +123,9 @@
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\functions\response_to_user.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -0,0 +1,29 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core.Routing.Functions;
/// <summary>
/// Response to user if router doesn't need to route to agent.
/// </summary>
public class ResponseToUserFn : IFunctionCallback
{
public string Name => "response_to_user";
private readonly IServiceProvider _services;
private readonly IRoutingContext _context;
public ResponseToUserFn(IServiceProvider services, IRoutingContext context)
{
_services = services;
_context = context;
}
public Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
message.Content = args.Response;
message.Handled = true;
message.StopCompletion = true;
return Task.FromResult(true);
}
}

View file

@ -0,0 +1,15 @@
{
"name": "response_to_user",
"description": "Response to user without routing to any other agent",
"visibility_expression": "{% if states.routing_mode == 'lazy' %}visible{% endif %}",
"parameters": {
"type": "object",
"properties": {
"response": {
"type": "string",
"description": "Response content"
}
},
"required": [ "response" ]
}
}

View file

@ -7,6 +7,8 @@ Follow these steps to handle user request:
4. You must include all required args for the selected agent, but you must not make up any parameters when there is no exact value provided, those parameters must set value as null if not declared.
{% if routing_mode != 'lazy' %}
5. Response must be in JSON format.
{% else %}
5. If user is greeting, you can call function response_to_user with a greeting message.
{% endif %}
{% if routing_requirements and routing_requirements != empty %}