Skip to content

Clean code. #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json;

namespace BotSharp.Abstraction.Conversations;

/// <summary>
Expand All @@ -11,6 +13,7 @@ public interface IConversationStateService
bool ContainsState(string name);
ConversationState GetStates();
IConversationStateService SetState<T>(string name, T value);
void SaveStateByArgs(JsonDocument args);
void CleanState();
void Save();
}
13 changes: 0 additions & 13 deletions src/Infrastructure/BotSharp.Abstraction/Routing/IRouterInstance.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using BotSharp.Abstraction.Routing.Models;

namespace BotSharp.Abstraction.Routing;

public interface IRoutingService
{
Agent Router { get; }
RoutingItem[] GetRoutingItems();
RoutingRule[] GetRulesByName(string name);
RoutingRule[] GetRulesByAgentId(string id);
List<RoutingHandlerDef> GetHandlers();
void ResetRecursiveCounter();
Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs);
Task<RoleDialogModel> InstructLoop(RoleDialogModel message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
});

services.AddScoped<IExecutor, InstructExecutor>();
services.AddScoped<IRouterInstance, RouterInstance>();
services.AddScoped<IRoutingService, RoutingService>();

if (myDatabaseSettings.Default == "FileRepository")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,23 @@ public bool ContainsState(string name)
{
return _states.ContainsKey(name) && !string.IsNullOrEmpty(_states[name]);
}

public void SaveStateByArgs(JsonDocument args)
{
if (args == null)
{
return;
}

if (args.RootElement is JsonElement root)
{
foreach (JsonProperty property in root.EnumerateObject())
{
if (!string.IsNullOrEmpty(property.Value.ToString()))
{
SetState(property.Name, property.Value);
}
}
}
}
}
15 changes: 6 additions & 9 deletions src/Infrastructure/BotSharp.Core/Planning/HFPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BotSharp.Abstraction.Planning;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Abstraction.Templating;

namespace BotSharp.Core.Planning;
Expand Down Expand Up @@ -36,13 +35,14 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
{
try
{
response = completion.GetChatCompletions(router, new List<RoleDialogModel>
var dialogs = new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, next)
{
MessageId = messageId
}
});
};
response = completion.GetChatCompletions(router, dialogs);

inst = response.Content.JsonContent<FunctionCallFromLlm>();
break;
Expand Down Expand Up @@ -80,18 +80,15 @@ public async Task<bool> AgentExecuting(FunctionCallFromLlm inst, RoleDialogModel
public async Task<bool> AgentExecuted(FunctionCallFromLlm inst, RoleDialogModel message)
{
var context = _services.GetRequiredService<RoutingContext>();
context.Pop();

context.Empty();
return true;
}

private string GetNextStepPrompt(Agent router)
{
var template = router.Templates.First(x => x.Name == "next_step_prompt").Content;

var render = _services.GetRequiredService<ITemplateRender>();
return render.Render(template, new Dictionary<string, object>
{
});
var prompt = render.Render(template, router.TemplateDict);
return prompt.Trim();
}
}
22 changes: 14 additions & 8 deletions src/Infrastructure/BotSharp.Core/Planning/NaivePlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BotSharp.Abstraction.Planning;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;

namespace BotSharp.Core.Planning;

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

var inst = new FunctionCallFromLlm();

var agentService = _services.GetRequiredService<IAgentService>();
// text completion
/*var agentService = _services.GetRequiredService<IAgentService>();
var instruction = agentService.RenderedInstruction(router);
var content = $"{instruction}\r\n###\r\n{next}";

// text completion
content = content + "\r\nResponse: ";
var completion = CompletionProvider.GetTextCompletion(_services);*/

var completion = CompletionProvider.GetTextCompletion(_services);
// chat completion
var completion = CompletionProvider.GetChatCompletion(_services);

int retryCount = 0;
while (retryCount < 3)
{
string text = string.Empty;
try
{
text = await completion.GetCompletion(content, router.Id, messageId);
var response = new RoleDialogModel(AgentRole.Assistant, text)
// text completion
// text = await completion.GetCompletion(content, router.Id, messageId);
var dialogs = new List<RoleDialogModel>
{
MessageId = messageId
new RoleDialogModel(AgentRole.User, next)
{
MessageId = messageId
}
};
var response = completion.GetChatCompletions(router, dialogs);

inst = response.Content.JsonContent<FunctionCallFromLlm>();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public async Task<bool> Execute(RoleDialogModel message)
private bool HasMissingRequiredField(RoleDialogModel message, out string agentId)
{
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
var router = _services.GetRequiredService<IRouterInstance>();
var routing = _services.GetRequiredService<IRoutingService>();

var routingRules = router.GetRulesByName(args.AgentName);
var routingRules = routing.GetRulesByName(args.AgentName);

if (routingRules == null || !routingRules.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public override bool OnInstructionLoaded(string template, Dictionary<string, obj
{
dict["router"] = _agent;

var router = _services.GetRequiredService<IRouterInstance>();
dict["routing_agents"] = router.GetRoutingItems();
dict["routing_handlers"] = router.GetHandlers();
var routing = _services.GetRequiredService<IRoutingService>();
dict["routing_agents"] = routing.GetRoutingItems();
dict["routing_handlers"] = routing.GetHandlers();

return base.OnInstructionLoaded(template, dict);
}
Expand Down
123 changes: 0 additions & 123 deletions src/Infrastructure/BotSharp.Core/Routing/RouterInstance.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private async Task<bool> InvokeFunction(Agent agent, RoleDialogModel message, Li
{
// execute function
// Save states
SaveStateByArgs(JsonSerializer.Deserialize<JsonDocument>(message.FunctionArgs));
var states = _services.GetRequiredService<IConversationStateService>();
states.SaveStateByArgs(message.FunctionArgs?.JsonContent<JsonDocument>());

var conversationService = _services.GetRequiredService<IConversationService>();
// Call functions
Expand Down
Loading