From 79728ddba07265792903f558c44513b302495e6d Mon Sep 17 00:00:00 2001 From: Kasper Marstal Date: Thu, 21 Nov 2024 22:37:13 +0100 Subject: [PATCH] refactor: Clean up Cellm/AddIn --- .../AddIn/{Functions.cs => CellmFunctions.cs} | 27 ++++----------- ...umentParser.cs => PromptArgumentParser.cs} | 33 ++++++++++--------- .../{AddIn => Prompts}/SystemMessages.cs | 2 +- src/Cellm/Services/ServiceLocator.cs | 3 +- 4 files changed, 27 insertions(+), 38 deletions(-) rename src/Cellm/AddIn/{Functions.cs => CellmFunctions.cs} (90%) rename src/Cellm/AddIn/{PromptWithArgumentParser.cs => PromptArgumentParser.cs} (82%) rename src/Cellm/{AddIn => Prompts}/SystemMessages.cs (94%) diff --git a/src/Cellm/AddIn/Functions.cs b/src/Cellm/AddIn/CellmFunctions.cs similarity index 90% rename from src/Cellm/AddIn/Functions.cs rename to src/Cellm/AddIn/CellmFunctions.cs index 30e2dd3..ab995ae 100644 --- a/src/Cellm/AddIn/Functions.cs +++ b/src/Cellm/AddIn/CellmFunctions.cs @@ -10,7 +10,7 @@ namespace Cellm.AddIn; -public static class Functions +public static class CellmFunctions { /// /// Sends a prompt to the default model configured in CellmConfiguration. @@ -73,7 +73,7 @@ public static object PromptWith( { try { - var arguments = ServiceLocator.Get() + var arguments = ServiceLocator.Get() .AddProvider(providerAndModel) .AddModel(providerAndModel) .AddInstructionsOrContext(instructionsOrContext) @@ -88,8 +88,8 @@ public static object PromptWith( var prompt = new PromptBuilder() .SetModel(arguments.Model) - .SetSystemMessage(SystemMessages.SystemMessage) .SetTemperature(arguments.Temperature) + .AddSystemMessage(SystemMessages.SystemMessage) .AddUserMessage(userMessage) .Build(); @@ -102,6 +102,7 @@ public static object PromptWith( catch (CellmException ex) { SentrySdk.CaptureException(ex); + Debug.WriteLine(ex); return ex.Message; } } @@ -117,22 +118,8 @@ public static object PromptWith( private static async Task CallModelAsync(Prompt prompt, string? provider = null, Uri? baseAddress = null) { - try - { - var client = ServiceLocator.Get(); - var response = await client.Send(prompt, provider, baseAddress); - var content = response.Messages.Last().Content; - return content; - } - catch (CellmException ex) - { - Debug.WriteLine(ex); - throw; - } - catch (Exception ex) - { - Debug.WriteLine(ex); - throw new CellmException("An unexpected error occurred", ex); - } + var client = ServiceLocator.Get(); + var response = await client.Send(prompt, provider, baseAddress); + return response.Messages.Last().Text ?? throw new NullReferenceException("No text response"); } } diff --git a/src/Cellm/AddIn/PromptWithArgumentParser.cs b/src/Cellm/AddIn/PromptArgumentParser.cs similarity index 82% rename from src/Cellm/AddIn/PromptWithArgumentParser.cs rename to src/Cellm/AddIn/PromptArgumentParser.cs index 3303fe6..80ca04d 100644 --- a/src/Cellm/AddIn/PromptWithArgumentParser.cs +++ b/src/Cellm/AddIn/PromptArgumentParser.cs @@ -1,5 +1,6 @@ using System.Text; using Cellm.AddIn.Exceptions; +using Cellm.Prompts; using ExcelDna.Integration; using Microsoft.Extensions.Configuration; using Microsoft.Office.Interop.Excel; @@ -8,7 +9,7 @@ namespace Cellm.AddIn; public record Arguments(string Provider, string Model, string Context, string Instructions, double Temperature); -public class PromptWithArgumentParser +public class PromptArgumentParser { private string? _provider; private string? _model; @@ -18,12 +19,12 @@ public class PromptWithArgumentParser private readonly IConfiguration _configuration; - public PromptWithArgumentParser(IConfiguration configuration) + public PromptArgumentParser(IConfiguration configuration) { _configuration = configuration; } - public PromptWithArgumentParser AddProvider(object providerAndModel) + public PromptArgumentParser AddProvider(object providerAndModel) { _provider = providerAndModel switch { @@ -35,7 +36,7 @@ public PromptWithArgumentParser AddProvider(object providerAndModel) return this; } - public PromptWithArgumentParser AddModel(object providerAndModel) + public PromptArgumentParser AddModel(object providerAndModel) { _model = providerAndModel switch { @@ -47,21 +48,21 @@ public PromptWithArgumentParser AddModel(object providerAndModel) return this; } - public PromptWithArgumentParser AddInstructionsOrContext(object instructionsOrContext) + public PromptArgumentParser AddInstructionsOrContext(object instructionsOrContext) { _instructionsOrContext = instructionsOrContext; return this; } - public PromptWithArgumentParser AddInstructionsOrTemperature(object instructionsOrTemperature) + public PromptArgumentParser AddInstructionsOrTemperature(object instructionsOrTemperature) { _instructionsOrTemperature = instructionsOrTemperature; return this; } - public PromptWithArgumentParser AddTemperature(object temperature) + public PromptArgumentParser AddTemperature(object temperature) { _temperature = temperature; @@ -92,17 +93,17 @@ public Arguments Parse() // "=PROMPT("Extract keywords", 0.7) (string instructions, double temperature, ExcelMissing) => new Arguments(provider, model, string.Empty, RenderInstructions(instructions), ParseTemperature(temperature)), // "=PROMPT(A1:B2) - (ExcelReference context, ExcelMissing, ExcelMissing) => new Arguments(provider, model, RenderContext(ParseCells(context)), RenderInstructions(SystemMessages.InlineInstructions), ParseTemperature(defaultTemperature)), + (ExcelReference context, ExcelMissing, ExcelMissing) => new Arguments(provider, model, RenderCells(ParseCells(context)), RenderInstructions(SystemMessages.InlineInstructions), ParseTemperature(defaultTemperature)), // "=PROMPT(A1:B2, 0.7) - (ExcelReference context, double temperature, ExcelMissing) => new Arguments(provider, model, RenderContext(ParseCells(context)), RenderInstructions(SystemMessages.InlineInstructions), ParseTemperature(defaultTemperature)), + (ExcelReference context, double temperature, ExcelMissing) => new Arguments(provider, model, RenderCells(ParseCells(context)), RenderInstructions(SystemMessages.InlineInstructions), ParseTemperature(defaultTemperature)), // "=PROMPT(A1:B2, "Extract keywords") - (ExcelReference context, string instructions, ExcelMissing) => new Arguments(provider, model, RenderContext(ParseCells(context)), RenderInstructions(instructions), ParseTemperature(defaultTemperature)), + (ExcelReference context, string instructions, ExcelMissing) => new Arguments(provider, model, RenderCells(ParseCells(context)), RenderInstructions(instructions), ParseTemperature(defaultTemperature)), // "=PROMPT(A1:B2, "Extract keywords", 0.7) - (ExcelReference context, string instructions, double temperature) => new Arguments(provider, model, RenderContext(ParseCells(context)), RenderInstructions(instructions), ParseTemperature(temperature)), + (ExcelReference context, string instructions, double temperature) => new Arguments(provider, model, RenderCells(ParseCells(context)), RenderInstructions(instructions), ParseTemperature(temperature)), // "=PROMPT(A1:B2, C1:D2) - (ExcelReference context, ExcelReference instructions, ExcelMissing) => new Arguments(provider, model, RenderContext(ParseCells(context)), RenderInstructions(ParseCells(instructions)), ParseTemperature(defaultTemperature)), + (ExcelReference context, ExcelReference instructions, ExcelMissing) => new Arguments(provider, model, RenderCells(ParseCells(context)), RenderInstructions(ParseCells(instructions)), ParseTemperature(defaultTemperature)), // "=PROMPT(A1:B2, C1:D2, 0.7) - (ExcelReference context, ExcelReference instructions, double temperature) => new Arguments(provider, model, RenderContext(ParseCells(context)), RenderInstructions(ParseCells(instructions)), ParseTemperature(temperature)), + (ExcelReference context, ExcelReference instructions, double temperature) => new Arguments(provider, model, RenderCells(ParseCells(context)), RenderInstructions(ParseCells(instructions)), ParseTemperature(temperature)), // Anything else _ => throw new ArgumentException($"Invalid arguments ({_instructionsOrContext?.GetType().Name}, {_instructionsOrTemperature?.GetType().Name}, {_temperature?.GetType().Name})") }; @@ -110,7 +111,7 @@ public Arguments Parse() private static string GetProvider(string providerAndModel) { - var index = providerAndModel.IndexOf("/"); + var index = providerAndModel.IndexOf('/'); if (index < 0) { @@ -122,7 +123,7 @@ private static string GetProvider(string providerAndModel) private static string GetModel(string providerAndModel) { - var index = providerAndModel.IndexOf("/"); + var index = providerAndModel.IndexOf('/'); if (index < 0) { @@ -203,7 +204,7 @@ private static string GetRowName(int rowNumber) return (rowNumber + 1).ToString(); } - private static string RenderContext(string context) + private static string RenderCells(string context) { return new StringBuilder() .AppendLine("") diff --git a/src/Cellm/AddIn/SystemMessages.cs b/src/Cellm/Prompts/SystemMessages.cs similarity index 94% rename from src/Cellm/AddIn/SystemMessages.cs rename to src/Cellm/Prompts/SystemMessages.cs index 971fbb3..bedcad4 100644 --- a/src/Cellm/AddIn/SystemMessages.cs +++ b/src/Cellm/Prompts/SystemMessages.cs @@ -1,4 +1,4 @@ -namespace Cellm.AddIn; +namespace Cellm.Prompts; internal static class SystemMessages { diff --git a/src/Cellm/Services/ServiceLocator.cs b/src/Cellm/Services/ServiceLocator.cs index c54a52e..bb47490 100644 --- a/src/Cellm/Services/ServiceLocator.cs +++ b/src/Cellm/Services/ServiceLocator.cs @@ -87,12 +87,13 @@ private static IServiceCollection ConfigureServices(IServiceCollection services) .AddSingleton(configuration) .AddMemoryCache() .AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())) - .AddTransient() + .AddTransient() .AddSingleton() .AddSingleton(); // Tools services + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton()