Skip to content

Commit 4e2d006

Browse files
committed
Move router profile to liquid.
1 parent 8003fc3 commit 4e2d006

15 files changed

Lines changed: 94 additions & 91 deletions

File tree

‎src/Infrastructure/BotSharp.Abstraction/Models/NameDesc.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ namespace BotSharp.Abstraction.Models;
22

33
public class NameDesc
44
{
5+
[JsonPropertyName("name")]
56
public string Name { get; set; }
7+
8+
[JsonPropertyName("description")]
69
public string Description { get; set; }
710

811
public NameDesc(string name, string description)
912
{
1013
Name = name;
1114
Description = description;
1215
}
16+
17+
public override string ToString()
18+
=> $"{Name}: {Description}";
1319
}

‎src/Infrastructure/BotSharp.Abstraction/Routing/IRouterInstance.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public interface IRouterInstance
66
{
77
string AgentId { get; }
88
Agent Router { get; }
9+
RoutingItem[] GetRoutingItems();
910
List<RoutingHandlerDef> GetHandlers();
1011
IRouterInstance Load();
1112
IRouterInstance WithDialogs(List<RoleDialogModel> dialogs);

‎src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingHandlerDef.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ public class RoutingHandlerDef
77
public string Name { get; set; }
88
public string Description { get; set; }
99
public List<NameDesc> Parameters { get; set; }
10+
11+
public override string ToString()
12+
=> $"{Name}: {Description} ({Parameters.Count} Parameters)";
1013
}

‎src/Infrastructure/BotSharp.Abstraction/Routing/Settings/RoutingSettings.cs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ public class RoutingSettings
77
/// </summary>
88
public string RouterId { get; set; } = string.Empty;
99

10-
public string RouterName { get; set; } = "Router";
11-
12-
public string Description { get; set; } = string.Empty;
13-
1410
public bool EnableReasoning { get; set; } = false;
1511
public bool UseTextCompletion { get; set; } = false;
1612

‎src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ join u in _db.Users on ua.UserId equals u.Id
2525
#endif
2626
public async Task<Agent> GetAgent(string id)
2727
{
28-
var settings = _services.GetRequiredService<RoutingSettings>();
29-
var routerInstance = _services.GetRequiredService<IRouterInstance>();
30-
if (settings.RouterId == id)
31-
{
32-
return routerInstance.Load().Router;
33-
}
34-
3528
var profile = _db.GetAgent(id);
3629

3730
var instructionFile = profile?.Instruction;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@
6363
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
6464
</ItemGroup>
6565

66+
<ItemGroup>
67+
<Folder Include="Routing\Functions\" />
68+
</ItemGroup>
69+
6670
</Project>

‎src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private async Task HandleAssistantMessage(RoleDialogModel message, Func<RoleDial
8989

9090
var routingSetting = _services.GetRequiredService<RoutingSettings>();
9191
var agentName = routingSetting.RouterId == message.CurrentAgentId ?
92-
routingSetting.RouterName :
92+
"Router" :
9393
(await _services.GetRequiredService<IAgentService>().GetAgent(message.CurrentAgentId)).Name;
9494

9595
var text = message.Role == AgentRole.Function ?

‎src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void Append(string conversationId, RoleDialogModel dialog)
4747
else
4848
{
4949
var routingSetting = _services.GetRequiredService<RoutingSettings>();
50-
var agentName = routingSetting.RouterId == agentId ? routingSetting.RouterName : db.Agents.First(x => x.Id == agentId).Name;
50+
var agentName = routingSetting.RouterId == agentId ? "Router" : db.Agents.First(x => x.Id == agentId).Name;
5151

5252
sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{agentName}|");
5353
var content = dialog.Content.Replace("\r", " ").Replace("\n", " ").Trim();

‎src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingAgentHook.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
using BotSharp.Abstraction.Functions.Models;
2+
using BotSharp.Abstraction.Models;
3+
using BotSharp.Abstraction.Repositories;
4+
using BotSharp.Abstraction.Routing;
5+
using BotSharp.Abstraction.Routing.Models;
26

37
namespace BotSharp.Core.Routing.Hooks;
48

@@ -9,6 +13,17 @@ public RoutingAgentHook(IServiceProvider services, AgentSettings settings)
913
{
1014
}
1115

16+
public override bool OnInstructionLoaded(string template, Dictionary<string, object> dict)
17+
{
18+
dict["router"] = _agent;
19+
20+
var router = _services.GetRequiredService<IRouterInstance>();
21+
dict["routing_agents"] = router.GetRoutingItems();
22+
dict["routing_handlers"] = router.GetHandlers();
23+
24+
return base.OnInstructionLoaded(template, dict);
25+
}
26+
1227
public override bool OnFunctionsLoaded(List<FunctionDef> functions)
1328
{
1429
/*functions.Add(new FunctionDef

‎src/Infrastructure/BotSharp.Core/Routing/RouterInstance.cs‎

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -28,80 +28,8 @@ public RouterInstance(IServiceProvider services,
2828

2929
public IRouterInstance Load()
3030
{
31-
var db = _services.GetRequiredService<IBotSharpRepository>();
32-
33-
_router = new Agent()
34-
{
35-
Id = _settings.RouterId,
36-
Name = _settings.RouterName,
37-
Description = _settings.Description
38-
};
39-
var agents = db.Agents.Where(x => !x.Disabled && x.AllowRouting).ToArray();
40-
41-
// Assemble prompt
42-
var prompt = @$"You're {_settings.RouterName} ({_settings.Description}). Follow these steps to handle user's request:
43-
1. Read the CONVERSATION context.
44-
2. Select a appropriate function from FUNCTIONS.
45-
3. Determine which agent is suitable according to conversation context.
46-
4. Re-think about selected function is from FUNCTIONS to handle the request.
47-
5. Make sure agent is not in args.";
48-
49-
// Append function
50-
prompt += "\r\n";
51-
prompt += "\r\nFUNCTIONS";
52-
GetHandlers().Select((handler, i) =>
53-
{
54-
prompt += "\r\n";
55-
prompt += $"\r\n{i + 1}. {handler.Name}";
56-
prompt += $"\r\n{handler.Description}";
57-
58-
// Append parameters
59-
if (handler.Parameters.Any())
60-
{
61-
prompt += "\r\nParameters:";
62-
handler.Parameters.Select((p, i) =>
63-
{
64-
prompt += $"\r\n - {p.Name}: {p.Description}";
65-
return p;
66-
}).ToList();
67-
}
68-
69-
return handler;
70-
}).ToList();
71-
72-
prompt += "\r\n";
73-
prompt += "\r\nAGENTS";
74-
agents.Select(x => new RoutingItem
75-
{
76-
AgentId = x.Id,
77-
Description = x.Description,
78-
Name = x.Name,
79-
RequiredFields = x.RoutingRules.Where(x => x.Required)
80-
.Select(x => new NameDesc(x.Field, x.Description))
81-
.ToList()
82-
}).Select((agent, i) =>
83-
{
84-
prompt += "\r\n";
85-
prompt += $"\r\n{i + 1}. {agent.Name}";
86-
prompt += $"\r\n{agent.Description}";
87-
88-
// Append parameters
89-
if (agent.RequiredFields.Any())
90-
{
91-
prompt += $"\r\nRequired:";
92-
agent.RequiredFields.Select((field, i) =>
93-
{
94-
prompt += $"\r\n - {field.Name}: {field.Description}";
95-
return field;
96-
}).ToList();
97-
}
98-
return agent;
99-
}).ToList();
100-
101-
prompt += "\r\n";
102-
prompt += "\r\nCONVERSATION";
103-
_router.Instruction = prompt;
104-
31+
var agentService = _services.GetRequiredService<IAgentService>();
32+
_router = agentService.LoadAgent(_settings.RouterId).Result;
10533
return this;
10634
}
10735

@@ -119,7 +47,7 @@ public List<RoutingHandlerDef> GetHandlers()
11947
return _services.GetServices<IRoutingHandler>()
12048
.Where(x => x.IsReasoning == _settings.EnableReasoning)
12149
.Where(x => !string.IsNullOrEmpty(x.Description))
122-
.Select(x => new RoutingHandlerDef
50+
.Select((x, i) => new RoutingHandlerDef
12351
{
12452
Name = x.Name,
12553
Description = x.Description,
@@ -157,6 +85,25 @@ protected RoutingRule[] GetRoutingRecords()
15785
return records;
15886
}
15987

88+
#if !DEBUG
89+
[MemoryCache(10 * 60)]
90+
#endif
91+
public RoutingItem[] GetRoutingItems()
92+
{
93+
var db = _services.GetRequiredService<IBotSharpRepository>();
94+
95+
var agents = db.Agents.Where(x => !x.Disabled && x.AllowRouting).ToArray();
96+
return agents.Select(x => new RoutingItem
97+
{
98+
AgentId = x.Id,
99+
Description = x.Description,
100+
Name = x.Name,
101+
RequiredFields = x.RoutingRules.Where(x => x.Required)
102+
.Select(x => new NameDesc(x.Field, x.Description))
103+
.ToList()
104+
}).ToArray();
105+
}
106+
160107
public RoutingRule[] GetRulesByName(string name)
161108
{
162109
return GetRoutingRecords()

0 commit comments

Comments
 (0)