Skip to content

Commit 0aa52c1

Browse files
authored
Merge pull request #196 from hchen2020/master
Clean code.
2 parents efeaaed + 8ed3569 commit 0aa52c1

File tree

13 files changed

+143
-181
lines changed

13 files changed

+143
-181
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json;
2+
13
namespace BotSharp.Abstraction.Conversations;
24

35
/// <summary>
@@ -11,6 +13,7 @@ public interface IConversationStateService
1113
bool ContainsState(string name);
1214
ConversationState GetStates();
1315
IConversationStateService SetState<T>(string name, T value);
16+
void SaveStateByArgs(JsonDocument args);
1417
void CleanState();
1518
void Save();
1619
}

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
using BotSharp.Abstraction.Routing.Models;
2+
13
namespace BotSharp.Abstraction.Routing;
24

35
public interface IRoutingService
46
{
57
Agent Router { get; }
8+
RoutingItem[] GetRoutingItems();
9+
RoutingRule[] GetRulesByName(string name);
10+
RoutingRule[] GetRulesByAgentId(string id);
11+
List<RoutingHandlerDef> GetHandlers();
612
void ResetRecursiveCounter();
713
Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs);
814
Task<RoleDialogModel> InstructLoop(RoleDialogModel message);

src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
8181
});
8282

8383
services.AddScoped<IExecutor, InstructExecutor>();
84-
services.AddScoped<IRouterInstance, RouterInstance>();
8584
services.AddScoped<IRoutingService, RoutingService>();
8685

8786
if (myDatabaseSettings.Default == "FileRepository")

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,23 @@ public bool ContainsState(string name)
128128
{
129129
return _states.ContainsKey(name) && !string.IsNullOrEmpty(_states[name]);
130130
}
131+
132+
public void SaveStateByArgs(JsonDocument args)
133+
{
134+
if (args == null)
135+
{
136+
return;
137+
}
138+
139+
if (args.RootElement is JsonElement root)
140+
{
141+
foreach (JsonProperty property in root.EnumerateObject())
142+
{
143+
if (!string.IsNullOrEmpty(property.Value.ToString()))
144+
{
145+
SetState(property.Name, property.Value);
146+
}
147+
}
148+
}
149+
}
131150
}

src/Infrastructure/BotSharp.Core/Planning/HFPlanner.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using BotSharp.Abstraction.Planning;
44
using BotSharp.Abstraction.Repositories;
55
using BotSharp.Abstraction.Routing.Models;
6-
using BotSharp.Abstraction.Routing.Settings;
76
using BotSharp.Abstraction.Templating;
87

98
namespace BotSharp.Core.Planning;
@@ -36,13 +35,14 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
3635
{
3736
try
3837
{
39-
response = completion.GetChatCompletions(router, new List<RoleDialogModel>
38+
var dialogs = new List<RoleDialogModel>
4039
{
4140
new RoleDialogModel(AgentRole.User, next)
4241
{
4342
MessageId = messageId
4443
}
45-
});
44+
};
45+
response = completion.GetChatCompletions(router, dialogs);
4646

4747
inst = response.Content.JsonContent<FunctionCallFromLlm>();
4848
break;
@@ -80,18 +80,15 @@ public async Task<bool> AgentExecuting(FunctionCallFromLlm inst, RoleDialogModel
8080
public async Task<bool> AgentExecuted(FunctionCallFromLlm inst, RoleDialogModel message)
8181
{
8282
var context = _services.GetRequiredService<RoutingContext>();
83-
context.Pop();
84-
83+
context.Empty();
8584
return true;
8685
}
8786

8887
private string GetNextStepPrompt(Agent router)
8988
{
9089
var template = router.Templates.First(x => x.Name == "next_step_prompt").Content;
91-
9290
var render = _services.GetRequiredService<ITemplateRender>();
93-
return render.Render(template, new Dictionary<string, object>
94-
{
95-
});
91+
var prompt = render.Render(template, router.TemplateDict);
92+
return prompt.Trim();
9693
}
9794
}

src/Infrastructure/BotSharp.Core/Planning/NaivePlanner.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using BotSharp.Abstraction.Planning;
44
using BotSharp.Abstraction.Routing.Models;
55
using BotSharp.Abstraction.Templating;
6-
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
76

87
namespace BotSharp.Core.Planning;
98

@@ -24,26 +23,33 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
2423

2524
var inst = new FunctionCallFromLlm();
2625

27-
var agentService = _services.GetRequiredService<IAgentService>();
26+
// text completion
27+
/*var agentService = _services.GetRequiredService<IAgentService>();
2828
var instruction = agentService.RenderedInstruction(router);
2929
var content = $"{instruction}\r\n###\r\n{next}";
30-
31-
// text completion
3230
content = content + "\r\nResponse: ";
31+
var completion = CompletionProvider.GetTextCompletion(_services);*/
3332

34-
var completion = CompletionProvider.GetTextCompletion(_services);
33+
// chat completion
34+
var completion = CompletionProvider.GetChatCompletion(_services);
3535

3636
int retryCount = 0;
3737
while (retryCount < 3)
3838
{
3939
string text = string.Empty;
4040
try
4141
{
42-
text = await completion.GetCompletion(content, router.Id, messageId);
43-
var response = new RoleDialogModel(AgentRole.Assistant, text)
42+
// text completion
43+
// text = await completion.GetCompletion(content, router.Id, messageId);
44+
var dialogs = new List<RoleDialogModel>
4445
{
45-
MessageId = messageId
46+
new RoleDialogModel(AgentRole.User, next)
47+
{
48+
MessageId = messageId
49+
}
4650
};
51+
var response = completion.GetChatCompletions(router, dialogs);
52+
4753
inst = response.Content.JsonContent<FunctionCallFromLlm>();
4854
break;
4955
}

src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public async Task<bool> Execute(RoleDialogModel message)
8181
private bool HasMissingRequiredField(RoleDialogModel message, out string agentId)
8282
{
8383
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
84-
var router = _services.GetRequiredService<IRouterInstance>();
84+
var routing = _services.GetRequiredService<IRoutingService>();
8585

86-
var routingRules = router.GetRulesByName(args.AgentName);
86+
var routingRules = routing.GetRulesByName(args.AgentName);
8787

8888
if (routingRules == null || !routingRules.Any())
8989
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public override bool OnInstructionLoaded(string template, Dictionary<string, obj
1919
{
2020
dict["router"] = _agent;
2121

22-
var router = _services.GetRequiredService<IRouterInstance>();
23-
dict["routing_agents"] = router.GetRoutingItems();
24-
dict["routing_handlers"] = router.GetHandlers();
22+
var routing = _services.GetRequiredService<IRoutingService>();
23+
dict["routing_agents"] = routing.GetRoutingItems();
24+
dict["routing_handlers"] = routing.GetHandlers();
2525

2626
return base.OnInstructionLoaded(template, dict);
2727
}

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

Lines changed: 0 additions & 123 deletions
This file was deleted.

src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ private async Task<bool> InvokeFunction(Agent agent, RoleDialogModel message, Li
4141
{
4242
// execute function
4343
// Save states
44-
SaveStateByArgs(JsonSerializer.Deserialize<JsonDocument>(message.FunctionArgs));
44+
var states = _services.GetRequiredService<IConversationStateService>();
45+
states.SaveStateByArgs(message.FunctionArgs?.JsonContent<JsonDocument>());
4546

4647
var conversationService = _services.GetRequiredService<IConversationService>();
4748
// Call functions

0 commit comments

Comments
 (0)