Skip to content

Commit 4841c43

Browse files
authored
Merge branch 'SciSharp:master' into master
2 parents 8bd5713 + ad27596 commit 4841c43

File tree

119 files changed

+1387
-1305
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1387
-1305
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
using BotSharp.Abstraction.Agents.Settings;
2+
using BotSharp.Abstraction.Conversations;
23
using BotSharp.Abstraction.Functions.Models;
4+
using BotSharp.Abstraction.Repositories;
5+
using BotSharp.Abstraction.Routing;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using System.Data;
38

49
namespace BotSharp.Abstraction.Agents;
510

@@ -49,7 +54,71 @@ public virtual bool OnSamplesLoaded(List<string> samples)
4954
return true;
5055
}
5156

52-
public virtual void OnAgentLoaded(Agent agent)
57+
public virtual void OnAgentLoaded(Agent agent)
5358
{
5459
}
60+
61+
public virtual void OnAgentUtilityLoaded(Agent agent)
62+
{
63+
if (agent.Type == AgentType.Routing) return;
64+
65+
var conv = _services.GetRequiredService<IConversationService>();
66+
var isConvMode = conv.IsConversationMode();
67+
if (!isConvMode) return;
68+
69+
agent.Functions ??= [];
70+
agent.Utilities ??= [];
71+
72+
var (functions, templates) = GetUtilityContent(agent);
73+
74+
agent.Functions.AddRange(functions);
75+
foreach (var prompt in templates)
76+
{
77+
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
78+
}
79+
}
80+
81+
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(Agent agent)
82+
{
83+
var db = _services.GetRequiredService<IBotSharpRepository>();
84+
var (functionNames, templateNames) = GetUniqueContent(agent.Utilities);
85+
86+
if (agent.MergeUtility)
87+
{
88+
var routing = _services.GetRequiredService<IRoutingContext>();
89+
var entryAgentId = routing.EntryAgentId;
90+
if (!string.IsNullOrEmpty(entryAgentId))
91+
{
92+
var entryAgent = db.GetAgent(entryAgentId);
93+
var (fns, tps) = GetUniqueContent(entryAgent?.Utilities);
94+
functionNames = functionNames.Concat(fns).Distinct().ToList();
95+
templateNames = templateNames.Concat(tps).Distinct().ToList();
96+
}
97+
}
98+
99+
var ua = db.GetAgent(BuiltInAgentId.UtilityAssistant);
100+
var functions = ua?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
101+
var templates = ua?.Templates?.Where(x => templateNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.Select(x => x.Content)?.ToList() ?? [];
102+
return (functions, templates);
103+
}
104+
105+
private (IEnumerable<string>, IEnumerable<string>) GetUniqueContent(IEnumerable<AgentUtility>? utilities)
106+
{
107+
if (utilities.IsNullOrEmpty())
108+
{
109+
return ([], []);
110+
}
111+
112+
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
113+
var functionNames = utilities.SelectMany(x => x.Functions)
114+
.Where(x => !string.IsNullOrEmpty(x.Name))
115+
.Select(x => x.Name)
116+
.Distinct().ToList();
117+
var templateNames = utilities.SelectMany(x => x.Templates)
118+
.Where(x => !string.IsNullOrEmpty(x.Name))
119+
.Select(x => x.Name)
120+
.Distinct().ToList();
121+
122+
return (functionNames, templateNames);
123+
}
55124
}

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public interface IAgentHook
2525

2626
bool OnSamplesLoaded(List<string> samples);
2727

28+
void OnAgentUtilityLoaded(Agent agent);
29+
2830
/// <summary>
2931
/// Triggered when agent is loaded completely.
3032
/// </summary>

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ public interface IAgentService
5959

6060
PluginDef GetPlugin(string agentId);
6161

62-
IEnumerable<string> GetAgentUtilities();
62+
IEnumerable<AgentUtility> GetAgentUtilityOptions();
6363
}

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentUtilityHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.Agents;
22

33
public interface IAgentUtilityHook
44
{
5-
void AddUtilities(List<string> utilities);
5+
void AddUtilities(List<AgentUtility> utilities);
66
}

src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,17 @@ public class Agent
8787
/// <summary>
8888
/// Profile by channel
8989
/// </summary>
90-
public List<string> Profiles { get; set; }
91-
= new List<string>();
90+
public List<string> Profiles { get; set; } = new();
91+
92+
/// <summary>
93+
/// Merge utilities from entry agent
94+
/// </summary>
95+
public bool MergeUtility { get; set; }
9296

9397
/// <summary>
9498
/// Agent utilities
9599
/// </summary>
96-
public List<string> Utilities { get; set; } = new();
100+
public List<AgentUtility> Utilities { get; set; } = new();
97101

98102
/// <summary>
99103
/// Inherit from agent
@@ -173,9 +177,9 @@ public Agent SetSamples(List<string> samples)
173177
return this;
174178
}
175179

176-
public Agent SetUtilities(List<string> utilities)
180+
public Agent SetUtilities(List<AgentUtility> utilities)
177181
{
178-
Utilities = utilities ?? new List<string>();
182+
Utilities = utilities ?? new List<AgentUtility>();
179183
return this;
180184
}
181185

@@ -215,6 +219,12 @@ public Agent SetDisabled(bool disabled)
215219
return this;
216220
}
217221

222+
public Agent SetMergeUtility(bool merge)
223+
{
224+
MergeUtility = merge;
225+
return this;
226+
}
227+
218228
public Agent SetAgentType(string type)
219229
{
220230
Type = type;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace BotSharp.Abstraction.Agents.Models;
2+
3+
public class AgentUtility
4+
{
5+
public string Name { get; set; }
6+
public bool Disabled { get; set; }
7+
public IEnumerable<UtilityFunction> Functions { get; set; } = [];
8+
public IEnumerable<UtilityTemplate> Templates { get; set; } = [];
9+
10+
public AgentUtility()
11+
{
12+
13+
}
14+
15+
public AgentUtility(
16+
string name,
17+
IEnumerable<UtilityFunction>? functions = null,
18+
IEnumerable<UtilityTemplate>? templates = null)
19+
{
20+
Name = name;
21+
Functions = functions ?? [];
22+
Templates = templates ?? [];
23+
}
24+
}
25+
26+
27+
public class UtilityFunction : UtilityBase
28+
{
29+
public UtilityFunction()
30+
{
31+
32+
}
33+
34+
public UtilityFunction(string name)
35+
{
36+
Name = name;
37+
}
38+
}
39+
40+
public class UtilityTemplate : UtilityBase
41+
{
42+
public UtilityTemplate()
43+
{
44+
45+
}
46+
47+
public UtilityTemplate(string name)
48+
{
49+
Name = name;
50+
}
51+
}
52+
53+
public class UtilityBase
54+
{
55+
public string Name { get; set; }
56+
}

src/Infrastructure/BotSharp.Abstraction/Routing/Planning/IExecutor.cs renamed to src/Infrastructure/BotSharp.Abstraction/Planning/IExecutor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BotSharp.Abstraction.Functions.Models;
2+
using BotSharp.Abstraction.Routing;
23

3-
namespace BotSharp.Abstraction.Routing.Planning;
4+
namespace BotSharp.Abstraction.Planning;
45

56
public interface IExecutor
67
{
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
using BotSharp.Abstraction.Functions.Models;
2+
13
namespace BotSharp.Abstraction.Planning;
24

35
/// <summary>
46
/// Planning process for Task Agent
7+
/// https://www.promptingguide.ai/techniques/cot
58
/// </summary>
6-
public class ITaskPlanner
9+
public interface ITaskPlanner
710
{
8-
11+
Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs);
12+
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
13+
Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
14+
List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
15+
=> dialogs;
16+
bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
17+
=> true;
18+
int MaxLoopCount => 5;
919
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ public interface IBotSharpRepository : IHaveServiceProvider
3333
List<User> GetUserByIds(List<string> ids) => throw new NotImplementedException();
3434
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
3535
User? GetUserByUserName(string userName) => throw new NotImplementedException();
36+
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();
3637
void CreateUser(User user) => throw new NotImplementedException();
3738
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
3839
void UpdateUserVerified(string userId) => throw new NotImplementedException();
40+
void AddDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
41+
void RemoveDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
42+
void UpdateDashboardConversation(string userId, DashboardConversation dashConv) => throw new NotImplementedException();
3943
void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
4044
void UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
4145
void UpdateUserEmail(string userId, string email) => throw new NotImplementedException();

src/Infrastructure/BotSharp.Abstraction/Routing/Enums/RuleType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public class RuleType
1212
/// </summary>
1313
public const string DataValidation = "data-validation";
1414

15+
/// <summary>
16+
/// The reasoning approach name for next step
17+
/// </summary>
18+
public const string Reasoner = "reasoner";
19+
1520
/// <summary>
1621
/// The planning approach name for next step
1722
/// </summary>

0 commit comments

Comments
 (0)