Skip to content

Commit 92994da

Browse files
authored
Merge branch 'SciSharp:master' into master
2 parents 01fac40 + 699f913 commit 92994da

File tree

28 files changed

+216
-73
lines changed

28 files changed

+216
-73
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public enum AgentField
1818
Sample,
1919
LlmConfig,
2020
Utility,
21+
KnowledgeBase,
2122
MaxMessageCount
2223
}
2324

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface IAgentService
1818
/// </summary>
1919
/// <param name="id"></param>
2020
/// <returns></returns>
21-
Task<Agent> LoadAgent(string id);
21+
Task<Agent> LoadAgent(string id, bool loadUtility = true);
2222

2323
/// <summary>
2424
/// Inherit from an agent

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public class Agent
9999
/// </summary>
100100
public List<AgentUtility> Utilities { get; set; } = new();
101101

102+
/// <summary>
103+
/// Agent knowledge bases
104+
/// </summary>
105+
public List<AgentKnowledgeBase> KnowledgeBases { get; set; } = [];
106+
102107
/// <summary>
103108
/// Inherit from agent
104109
/// </summary>
@@ -118,6 +123,12 @@ public class Agent
118123
[JsonIgnore]
119124
public Dictionary<string, object> TemplateDict { get; set; } = new();
120125

126+
[JsonIgnore]
127+
public List<FunctionDef> SecondaryFunctions { get; set; } = [];
128+
129+
[JsonIgnore]
130+
public List<string> SecondaryInstructions { get; set; } = [];
131+
121132
public override string ToString()
122133
=> $"{Name} {Id}";
123134

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace BotSharp.Abstraction.Agents.Models;
2+
3+
public class AgentKnowledgeBase
4+
{
5+
public string? Name { get; set; }
6+
public bool Disabled { get; set; }
7+
8+
public AgentKnowledgeBase()
9+
{
10+
11+
}
12+
13+
public AgentKnowledgeBase(string name, bool enabled)
14+
{
15+
Name = name;
16+
Disabled = enabled;
17+
}
18+
19+
public override string ToString()
20+
{
21+
return Name ?? string.Empty;
22+
}
23+
}

src/Infrastructure/BotSharp.Core/Agents/Hooks/BasicAgentHook.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public class BasicAgentHook : AgentHookBase
44
{
55
public override string SelfId => string.Empty;
66

7+
private const string UTIL_PREFIX = "util-";
8+
79
public BasicAgentHook(IServiceProvider services, AgentSettings settings)
810
: base(services, settings)
911
{
@@ -17,22 +19,23 @@ public override void OnAgentUtilityLoaded(Agent agent)
1719
var isConvMode = conv.IsConversationMode();
1820
if (!isConvMode) return;
1921

20-
agent.Functions ??= [];
22+
agent.SecondaryFunctions ??= [];
23+
agent.SecondaryInstructions ??= [];
2124
agent.Utilities ??= [];
2225

2326
var (functions, templates) = GetUtilityContent(agent);
2427

2528
foreach (var fn in functions)
2629
{
27-
if (!agent.Functions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
30+
if (!agent.SecondaryFunctions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
2831
{
29-
agent.Functions.Add(fn);
32+
agent.SecondaryFunctions.Add(fn);
3033
}
3134
}
3235

3336
foreach (var prompt in templates)
3437
{
35-
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
38+
agent.SecondaryInstructions.Add(prompt);
3639
}
3740
}
3841

@@ -67,14 +70,13 @@ public override void OnAgentUtilityLoaded(Agent agent)
6770
return ([], []);
6871
}
6972

70-
var prefix = "util-";
7173
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
7274
var functionNames = utilities.SelectMany(x => x.Functions)
73-
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
75+
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(UTIL_PREFIX))
7476
.Select(x => x.Name)
7577
.Distinct().ToList();
7678
var templateNames = utilities.SelectMany(x => x.Templates)
77-
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
79+
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(UTIL_PREFIX))
7880
.Select(x => x.Name)
7981
.Distinct().ToList();
8082

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public partial class AgentService
88
public static ConcurrentDictionary<string, Dictionary<string, string>> AgentParameterTypes = new();
99

1010
[MemoryCache(10 * 60, perInstanceCache: true)]
11-
public async Task<Agent> LoadAgent(string id)
11+
public async Task<Agent> LoadAgent(string id, bool loadUtility = true)
1212
{
1313
if (string.IsNullOrEmpty(id) || id == Guid.Empty.ToString())
1414
{
@@ -67,7 +67,11 @@ public async Task<Agent> LoadAgent(string id)
6767
hook.OnSamplesLoaded(agent.Samples);
6868
}
6969

70-
hook.OnAgentUtilityLoaded(agent);
70+
if (loadUtility)
71+
{
72+
hook.OnAgentUtilityLoaded(agent);
73+
}
74+
7175
hook.OnAgentLoaded(agent);
7276
}
7377

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ public string RenderedInstruction(Agent agent)
1111
var render = _services.GetRequiredService<ITemplateRender>();
1212
var conv = _services.GetRequiredService<IConversationService>();
1313

14+
// merge instructions
15+
var instructions = new List<string> { agent.Instruction };
16+
var secondaryInstructions = agent.SecondaryInstructions?.Where(x => !string.IsNullOrWhiteSpace(x)).ToList() ?? [];
17+
instructions.AddRange(secondaryInstructions);
18+
1419
// update states
1520
foreach (var t in conv.States.GetStates())
1621
{
1722
agent.TemplateDict[t.Key] = t.Value;
1823
}
1924

20-
var res = render.Render(agent.Instruction, agent.TemplateDict);
25+
var res = render.Render(string.Join("\r\n", instructions), agent.TemplateDict);
2126
return res;
2227
}
2328

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
3939
record.Responses = agent.Responses ?? [];
4040
record.Samples = agent.Samples ?? [];
4141
record.Utilities = agent.Utilities ?? [];
42+
record.KnowledgeBases = agent.KnowledgeBases ?? [];
4243
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
4344
{
4445
record.LlmConfig = agent.LlmConfig;

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public void UpdateAgent(Agent agent, AgentField field)
5757
case AgentField.Utility:
5858
UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities);
5959
break;
60+
case AgentField.KnowledgeBase:
61+
UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases);
62+
break;
6063
case AgentField.MaxMessageCount:
6164
UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount);
6265
break;
@@ -168,6 +171,19 @@ private void UpdateAgentUtilities(string agentId, bool mergeUtility, List<AgentU
168171
File.WriteAllText(agentFile, json);
169172
}
170173

174+
private void UpdateAgentKnowledgeBases(string agentId, List<AgentKnowledgeBase> knowledgeBases)
175+
{
176+
if (knowledgeBases == null) return;
177+
178+
var (agent, agentFile) = GetAgentFromFile(agentId);
179+
if (agent == null) return;
180+
181+
agent.KnowledgeBases = knowledgeBases;
182+
agent.UpdatedDateTime = DateTime.UtcNow;
183+
var json = JsonSerializer.Serialize(agent, _options);
184+
File.WriteAllText(agentFile, json);
185+
}
186+
171187
private void UpdateAgentRoutingRules(string agentId, List<RoutingRule> rules)
172188
{
173189
if (rules == null) return;
@@ -310,6 +326,7 @@ private void UpdateAgentAllFields(Agent inputAgent)
310326
agent.Type = inputAgent.Type;
311327
agent.Profiles = inputAgent.Profiles;
312328
agent.Utilities = inputAgent.Utilities;
329+
agent.KnowledgeBases = inputAgent.KnowledgeBases;
313330
agent.RoutingRules = inputAgent.RoutingRules;
314331
agent.LlmConfig = inputAgent.LlmConfig;
315332
agent.MaxMessageCount = inputAgent.MaxMessageCount;

src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
3131
type: "boolean",
3232
required: true),
3333
new ParameterPropertyDef("is_new_task",
34-
"whether the user is requesting a new task that is different from the previous topic.",
34+
"whether the user is requesting a new task that is different from the previous topic. Set the first round of conversation to false.",
3535
type: "boolean")
3636
};
3737

0 commit comments

Comments
 (0)