Skip to content

Commit 45341eb

Browse files
authored
Merge pull request #1030 from hchen2020/master
response_to_user
2 parents a74a8df + ab46229 commit 45341eb

7 files changed

Lines changed: 63 additions & 3 deletions

File tree

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</PropertyGroup>
77
<ItemGroup>
88
<PackageVersion Include="EntityFramework" Version="6.4.4" />
9-
<PackageVersion Include="Google_GenerativeAI" Version="2.5.5" />
10-
<PackageVersion Include="Google_GenerativeAI.Live" Version="2.5.5" />
9+
<PackageVersion Include="Google_GenerativeAI" Version="2.5.8" />
10+
<PackageVersion Include="Google_GenerativeAI.Live" Version="2.5.8" />
1111
<PackageVersion Include="LLMSharp.Google.Palm" Version="1.0.2" />
1212
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="$(AspNetCoreVersion)" />
1313
<PackageVersion Include="Microsoft.AspNetCore.StaticFiles" Version="$(AspNetCoreVersion)" />

src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public async Task OnFunctionExecuting(RoleDialogModel message)
1717
{
1818
return;
1919
}
20+
21+
if (message.FunctionName == "response_to_user")
22+
{
23+
return;
24+
}
25+
2026
// Save states
2127
if (message.FunctionArgs != null && message.FunctionArgs.Length > 3)
2228
{
@@ -51,6 +57,11 @@ public async Task OnFunctionExecuted(RoleDialogModel message)
5157
await hub.Completer.UpdateSession(hub.HubConn);
5258
await hub.Completer.TriggerModelInference();
5359
}
60+
else if (message.FunctionName == "response_to_user")
61+
{
62+
await hub.Completer.InsertConversationItem(message);
63+
await hub.Completer.TriggerModelInference();
64+
}
5465
else
5566
{
5667
// Update session for changed states

src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task ConnectToModel(Func<string, Task>? responseToUser = null, Func
3030

3131
var routing = _services.GetRequiredService<IRoutingService>();
3232
var agentService = _services.GetRequiredService<IAgentService>();
33-
var agent = await agentService.LoadAgent(_conn.CurrentAgentId);
33+
var agent = await agentService.GetAgent(_conn.CurrentAgentId);
3434

3535
var storage = _services.GetRequiredService<IConversationStorage>();
3636
var dialogs = convService.GetDialogHistory();

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\agent.json">
124124
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
125125
</Content>
126+
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\functions\response_to_user.json">
127+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
128+
</Content>
126129
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\instructions\instruction.liquid">
127130
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
128131
</Content>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using BotSharp.Abstraction.Functions;
2+
using BotSharp.Abstraction.Routing.Models;
3+
4+
namespace BotSharp.Core.Routing.Functions;
5+
6+
/// <summary>
7+
/// Response to user if router doesn't need to route to agent.
8+
/// </summary>
9+
public class ResponseToUserFn : IFunctionCallback
10+
{
11+
public string Name => "response_to_user";
12+
private readonly IServiceProvider _services;
13+
private readonly IRoutingContext _context;
14+
15+
public ResponseToUserFn(IServiceProvider services, IRoutingContext context)
16+
{
17+
_services = services;
18+
_context = context;
19+
}
20+
21+
public Task<bool> Execute(RoleDialogModel message)
22+
{
23+
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
24+
message.Content = args.Response;
25+
message.Handled = true;
26+
message.StopCompletion = true;
27+
return Task.FromResult(true);
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "response_to_user",
3+
"description": "Response to user without routing to any other agent",
4+
"visibility_expression": "{% if states.routing_mode == 'lazy' %}visible{% endif %}",
5+
"parameters": {
6+
"type": "object",
7+
"properties": {
8+
"response": {
9+
"type": "string",
10+
"description": "Response content"
11+
}
12+
},
13+
"required": [ "response" ]
14+
}
15+
}

src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/instructions/instruction.liquid

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Follow these steps to handle user request:
77
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.
88
{% if routing_mode != 'lazy' %}
99
5. Response must be in JSON format.
10+
{% else %}
11+
5. If user is greeting, you can call function response_to_user with a greeting message.
1012
{% endif %}
1113

1214
{% if routing_requirements and routing_requirements != empty %}

0 commit comments

Comments
 (0)