Skip to content

Commit 8dbceb9

Browse files
Merge pull request #159 from hchen2020/master
print functions in verbose.
2 parents 30dec8f + 7b45a27 commit 8dbceb9

File tree

22 files changed

+135
-81
lines changed

22 files changed

+135
-81
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Agents.Settings;
2+
using BotSharp.Abstraction.Functions.Models;
23

34
namespace BotSharp.Abstraction.Agents;
45

@@ -34,7 +35,7 @@ public virtual bool OnInstructionLoaded(string template, Dictionary<string, obje
3435
return true;
3536
}
3637

37-
public virtual bool OnFunctionsLoaded(ref List<string> functions)
38+
public virtual bool OnFunctionsLoaded(ref List<FunctionDef> functions)
3839
{
3940
_agent.Functions = functions;
4041
return true;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Functions.Models;
2+
13
namespace BotSharp.Abstraction.Agents;
24

35
public interface IAgentHook
@@ -15,7 +17,7 @@ public interface IAgentHook
1517

1618
bool OnInstructionLoaded(string template, Dictionary<string, object> dict);
1719

18-
bool OnFunctionsLoaded(ref List<string> functions);
20+
bool OnFunctionsLoaded(ref List<FunctionDef> functions);
1921

2022
bool OnSamplesLoaded(ref string samples);
2123

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Functions.Models;
12
using BotSharp.Abstraction.Routing.Models;
23

34
namespace BotSharp.Abstraction.Agents.Models;
@@ -32,7 +33,7 @@ public class Agent
3233
/// Functions
3334
/// </summary>
3435
[JsonIgnore]
35-
public List<string> Functions { get; set; }
36+
public List<FunctionDef> Functions { get; set; } = new List<FunctionDef>();
3637

3738
/// <summary>
3839
/// Responses
@@ -102,9 +103,9 @@ public Agent SetTemplates(List<AgentTemplate> templates)
102103
return this;
103104
}
104105

105-
public Agent SetFunctions(List<string> functions)
106+
public Agent SetFunctions(List<FunctionDef> functions)
106107
{
107-
Functions = functions ?? new List<string>();
108+
Functions = functions ?? new List<FunctionDef>();
108109
return this;
109110
}
110111

src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionDef.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using System.Text.Json;
2-
31
namespace BotSharp.Abstraction.Functions.Models;
42

53
public class FunctionDef
64
{
75
public string Name { get; set; }
86
public string Description { get; set; }
9-
public JsonDocument Parameters { get; set; }
7+
public FunctionParametersDef Parameters { get; set; } = new FunctionParametersDef();
108

119
public override string ToString()
1210
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Text.Json;
2+
3+
namespace BotSharp.Abstraction.Functions.Models;
4+
5+
public class FunctionParametersDef
6+
{
7+
[JsonPropertyName("type")]
8+
public string Type { get; set; } = "object";
9+
10+
/// <summary>
11+
/// ParameterPropertyDef
12+
/// {
13+
/// "field_name": {}
14+
/// }
15+
/// </summary>
16+
[JsonPropertyName("properties")]
17+
public JsonDocument Properties { get; set; } = JsonSerializer.Deserialize<JsonDocument>("{}");
18+
19+
[JsonPropertyName("required")]
20+
public List<string> Required { get; set; } = new List<string>();
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Abstraction.Functions.Models;
2+
3+
public class ParameterPropertyDef
4+
{
5+
public string Type { get; set; } = "string";
6+
public string Description { get; set; }
7+
}

src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHandler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
namespace BotSharp.Abstraction.Routing;
55

6+
/// <summary>
7+
/// The routing handler will be injected to Router's FUNCTIONS section of the system prompt
8+
/// So the handler will be invoked by LLM autonomously.
9+
/// </summary>
610
public interface IRoutingHandler
711
{
812
string Name { get; }

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Agents.Models;
2+
using BotSharp.Abstraction.Functions.Models;
23
using BotSharp.Abstraction.Repositories;
34
using System.IO;
45

@@ -114,14 +115,13 @@ private List<AgentTemplate> FetchTemplatesFromFile(string fileDir)
114115
return templates;
115116
}
116117

117-
private List<string> FetchFunctionsFromFile(string fileDir)
118+
private List<FunctionDef> FetchFunctionsFromFile(string fileDir)
118119
{
119120
var file = Path.Combine(fileDir, "functions.json");
120-
if (!File.Exists(file)) return new List<string>();
121+
if (!File.Exists(file)) return new List<FunctionDef>();
121122

122123
var functionsJson = File.ReadAllText(file);
123-
var functionDefs = JsonSerializer.Deserialize<List<Abstraction.Functions.Models.FunctionDef>>(functionsJson, _options);
124-
var functions = functionDefs.Select(x => JsonSerializer.Serialize(x, _options)).ToList();
124+
var functions = JsonSerializer.Deserialize<List<FunctionDef>>(functionsJson, _options);
125125
return functions;
126126
}
127127

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using BotSharp.Abstraction.Agents.Models;
2-
using BotSharp.Abstraction.Routing;
3-
using BotSharp.Abstraction.Routing.Settings;
42
using BotSharp.Abstraction.Templating;
53

64
namespace BotSharp.Core.Agents.Services;
@@ -34,7 +32,7 @@ public async Task<Agent> LoadAgent(string id)
3432
hook.OnInstructionLoaded(agent.Instruction, templateDict);
3533
}
3634

37-
if (!agent.Functions.IsNullOrEmpty())
35+
if (agent.Functions != null)
3836
{
3937
var functions = agent.Functions;
4038
hook.OnFunctionsLoaded(ref functions);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Agents.Models;
2+
using BotSharp.Abstraction.Functions.Models;
23
using BotSharp.Abstraction.Repositories;
34
using BotSharp.Abstraction.Routing.Models;
45
using System.IO;
@@ -22,7 +23,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
2223
record.Profiles = agent.Profiles ?? new List<string>();
2324
record.RoutingRules = agent.RoutingRules ?? new List<RoutingRule>();
2425
record.Instruction = agent.Instruction ?? string.Empty;
25-
record.Functions = agent.Functions ?? new List<string>();
26+
record.Functions = agent.Functions ?? new List<FunctionDef>();
2627
record.Templates = agent.Templates ?? new List<AgentTemplate>();
2728
record.Responses = agent.Responses ?? new List<AgentResponse>();
2829

0 commit comments

Comments
 (0)