BotSharp/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs

184 lines
6.6 KiB
C#
Raw Normal View History

2023-08-19 00:15:50 +00:00
using BotSharp.Abstraction.Functions;
2023-08-26 04:41:01 +00:00
using BotSharp.Abstraction.Repositories;
2023-08-28 03:50:10 +00:00
using BotSharp.Core.Routing;
using BotSharp.Core.Templating;
2023-05-27 01:58:31 +00:00
using Microsoft.AspNetCore.Builder;
2023-06-03 02:07:30 +00:00
using Microsoft.Extensions.Configuration;
using BotSharp.Abstraction.Routing.Settings;
2023-08-30 23:29:01 +00:00
using BotSharp.Abstraction.Templating;
2023-09-01 22:26:25 +00:00
using BotSharp.Core.Instructs;
using BotSharp.Abstraction.Instructs;
2023-09-03 04:11:53 +00:00
using BotSharp.Abstraction.Routing;
2023-09-27 20:49:44 +00:00
using BotSharp.Core.Routing.Hooks;
using BotSharp.Abstraction.Routing.Models;
2023-10-11 04:11:06 +00:00
using BotSharp.Core.Plugins;
2023-10-18 11:58:36 +00:00
using BotSharp.Abstraction.Evaluations.Settings;
using BotSharp.Abstraction.Evaluations;
using BotSharp.Core.Evaluatings;
2023-10-23 00:31:49 +00:00
using BotSharp.Core.Evaluations;
2023-10-23 23:20:18 +00:00
using BotSharp.Abstraction.MLTasks.Settings;
2023-10-28 20:59:26 +00:00
using BotSharp.Abstraction.Planning;
using BotSharp.Core.Planning;
2023-12-13 18:12:25 +00:00
using BotSharp.Abstraction.MLTasks;
using static Dapper.SqlMapper;
2023-07-21 22:17:16 +00:00
2023-05-27 01:58:31 +00:00
namespace BotSharp.Core;
2023-11-29 23:23:14 +00:00
public static class BotSharpCoreExtensions
2023-05-27 01:58:31 +00:00
{
2023-11-29 23:23:14 +00:00
public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config)
2023-05-27 01:58:31 +00:00
{
2023-06-11 23:46:02 +00:00
services.AddScoped<IUserService, UserService>();
2023-12-13 18:12:25 +00:00
services.AddScoped<ILlmProviderSettingService, LlmProviderSettingService>();
2023-06-11 23:46:02 +00:00
services.AddScoped<IAgentService, AgentService>();
2023-06-23 04:43:00 +00:00
2023-07-21 22:17:16 +00:00
var agentSettings = new AgentSettings();
config.Bind("Agent", agentSettings);
services.AddSingleton((IServiceProvider x) => agentSettings);
2023-06-27 18:31:13 +00:00
var convsationSettings = new ConversationSetting();
config.Bind("Conversation", convsationSettings);
services.AddSingleton((IServiceProvider x) => convsationSettings);
2023-06-23 04:43:00 +00:00
2023-06-27 18:31:13 +00:00
services.AddScoped<IConversationStorage, ConversationStorage>();
services.AddScoped<IConversationService, ConversationService>();
2023-08-09 21:49:55 +00:00
services.AddScoped<IConversationStateService, ConversationStateService>();
services.AddScoped<RoutingContext>();
2023-06-18 18:15:00 +00:00
2023-09-01 22:33:36 +00:00
var databaseSettings = new DatabaseBasicSettings();
2023-07-21 21:56:14 +00:00
config.Bind("Database", databaseSettings);
services.AddSingleton((IServiceProvider x) => databaseSettings);
2023-09-01 22:33:36 +00:00
var myDatabaseSettings = new BotSharpDatabaseSettings();
2023-07-21 21:56:14 +00:00
config.Bind("Database", myDatabaseSettings);
2023-08-10 04:53:22 +00:00
services.AddSingleton((IServiceProvider x) => myDatabaseSettings);
2023-07-21 21:56:14 +00:00
2023-12-13 18:12:25 +00:00
var llmProviders = new List<LlmProviderSetting>();
config.Bind("LlmProviders", llmProviders);
services.AddSingleton((IServiceProvider x) =>
{
foreach (var llmProvider in llmProviders)
{
Console.WriteLine($"Loaded LlmProvider {llmProvider.Provider} settings with {llmProvider.Models.Count} models.");
}
return llmProviders;
});
2023-10-23 23:20:18 +00:00
2023-08-14 18:16:35 +00:00
RegisterPlugins(services, config);
// Register template render
2023-08-30 23:29:01 +00:00
services.AddSingleton<ITemplateRender, TemplateRender>();
services.AddScoped<IResponseTemplateService, ResponseTemplateService>();
// Register router
var routingSettings = new RoutingSettings();
config.Bind("Router", routingSettings);
services.AddSingleton((IServiceProvider x) => routingSettings);
2023-10-28 20:59:26 +00:00
services.AddScoped<NaivePlanner>();
2023-10-30 16:48:18 +00:00
services.AddScoped<HFPlanner>();
2023-10-28 20:59:26 +00:00
services.AddScoped<IPlaner>(provider =>
{
2023-10-30 16:48:18 +00:00
if (routingSettings.Planner == nameof(HFPlanner))
return provider.GetRequiredService<HFPlanner>();
2023-10-29 01:54:10 +00:00
else
2023-10-28 20:59:26 +00:00
return provider.GetRequiredService<NaivePlanner>();
});
services.AddScoped<IExecutor, InstructExecutor>();
2023-09-19 12:17:50 +00:00
services.AddScoped<IRoutingService, RoutingService>();
2023-09-03 19:32:12 +00:00
if (myDatabaseSettings.Default == "FileRepository")
{
services.AddScoped<IBotSharpRepository, FileRepository>();
}
2023-09-03 04:54:22 +00:00
2023-09-01 22:26:25 +00:00
services.AddScoped<IInstructService, InstructService>();
2023-09-24 21:32:58 +00:00
services.AddScoped<ITokenStatistics, TokenStatistics>();
2023-09-01 22:26:25 +00:00
2023-09-28 03:31:58 +00:00
services.AddScoped<IAgentHook, RoutingAgentHook>();
2023-09-27 20:49:44 +00:00
2023-10-18 11:58:36 +00:00
// Evaluation
var evalSetting = new EvaluatorSetting();
config.Bind("Evaluator", evalSetting);
services.AddSingleton((IServiceProvider x) => evalSetting);
2023-10-23 00:31:49 +00:00
services.AddScoped<IConversationHook, EvaluationConversationHook>();
2023-10-18 11:58:36 +00:00
services.AddScoped<IEvaluatingService, EvaluatingService>();
2023-10-23 00:31:49 +00:00
services.AddScoped<IExecutionLogger, ExecutionLogger>();
2023-10-18 11:58:36 +00:00
2023-11-09 21:11:36 +00:00
services.AddScoped<IConversationAttachmentService, ConversationAttachmentService>();
2023-08-14 18:16:35 +00:00
return services;
}
2023-07-21 21:56:14 +00:00
2023-08-14 18:16:35 +00:00
public static IServiceCollection UsingSqlServer(this IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpRepository>(sp =>
{
2023-09-01 22:33:36 +00:00
var myDatabaseSettings = sp.GetRequiredService<BotSharpDatabaseSettings>();
2023-08-14 18:16:35 +00:00
return DataContextHelper.GetDbContext<BotSharpDbContext, DbContext4SqlServer>(myDatabaseSettings, sp);
});
return services;
}
2023-09-03 04:54:22 +00:00
//public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config)
//{
// services.AddScoped<IBotSharpRepository>(sp =>
// {
// var myDatabaseSettings = sp.GetRequiredService<BotSharpDatabaseSettings>();
// return new FileRepository(myDatabaseSettings, sp);
// });
2023-07-21 21:56:14 +00:00
2023-09-03 04:54:22 +00:00
// return services;
//}
2023-05-27 01:58:31 +00:00
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
2023-06-26 22:16:21 +00:00
app.ApplicationServices.GetRequiredService<PluginLoader>().Configure(app);
2023-05-27 01:58:31 +00:00
return app;
}
2023-06-03 02:07:30 +00:00
2023-06-10 02:56:04 +00:00
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
{
2023-10-11 04:11:06 +00:00
var pluginSettings = new PluginSettings();
2023-06-17 02:42:35 +00:00
config.Bind("PluginLoader", pluginSettings);
2023-10-11 04:11:06 +00:00
services.AddSingleton(pluginSettings);
2023-06-11 23:46:02 +00:00
2023-06-17 02:42:35 +00:00
var loader = new PluginLoader(services, config, pluginSettings);
2023-09-24 16:20:02 +00:00
loader.Load(assembly =>
{
// Register routing handlers
var handlers = assembly.GetTypes()
.Where(x => x.IsClass)
.Where(x => x.GetInterface(nameof(IRoutingHandler)) != null)
.ToArray();
foreach (var handler in handlers)
{
services.AddScoped(typeof(IRoutingHandler), handler);
}
// Register function callback
var functions = assembly.GetTypes()
.Where(x => x.IsClass)
.Where(x => x.GetInterface(nameof(IFunctionCallback)) != null)
.ToArray();
foreach (var function in functions)
{
services.AddScoped(typeof(IFunctionCallback), function);
}
});
2023-06-26 22:16:21 +00:00
services.AddSingleton(loader);
2023-06-10 02:56:04 +00:00
}
2023-05-27 01:58:31 +00:00
}