Skip to content

Commit

Permalink
feat: Add support for OpenAI tools (#18)
Browse files Browse the repository at this point in the history
* Rewrite clients to MediatR request handlers
* Add glob tool
  • Loading branch information
kaspermarstal authored Oct 22, 2024
1 parent a14547d commit 0cc6423
Show file tree
Hide file tree
Showing 55 changed files with 1,603 additions and 1,117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,4 @@ appsettings.Local.json
docker/ollama-cache
docker/vllm-cache
*.xlsx
TODO.md
1 change: 0 additions & 1 deletion src/Cellm/AddIn/ArgumentParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Text;
using Cellm.AddIn.Exceptions;
using Cellm.AddIn.Prompts;
using Cellm.Services.Configuration;
using ExcelDna.Integration;
using Microsoft.Extensions.Configuration;
Expand Down
4 changes: 3 additions & 1 deletion src/Cellm/AddIn/Cellm.cs → src/Cellm/AddIn/CellmAddIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Cellm.AddIn;

public class Cellm : IExcelAddIn
public class CellmAddIn : IExcelAddIn
{
public void AutoOpen()
{
Expand All @@ -13,6 +13,8 @@ public void AutoOpen()
SentrySdk.CaptureException(ex);
return ex.Message;
});

_ = ServiceLocator.ServiceProvider;
}

public void AutoClose()
Expand Down
2 changes: 1 addition & 1 deletion src/Cellm/AddIn/CellmConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CellmConfiguration

public double DefaultTemperature { get; init; }

public int MaxTokens { get; init; }
public int MaxOutputTokens { get; init; }

public int CacheTimeoutInSeconds { get; init; }
}
Expand Down
36 changes: 24 additions & 12 deletions src/Cellm/AddIn/CellmFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Text;
using System.Diagnostics;
using System.Text;
using Cellm.AddIn.Exceptions;
using Cellm.AddIn.Prompts;
using Cellm.Models;
using Cellm.Prompts;
using Cellm.Services;
using Cellm.Services.Configuration;
using ExcelDna.Integration;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;

namespace Cellm.AddIn;

Expand All @@ -31,17 +33,23 @@ public static object Prompt(
[ExcelArgument(Name = "InstructionsOrTemperature", Description = "A cell or range of cells with instructions or a temperature")] object instructionsOrTemperature,
[ExcelArgument(Name = "Temperature", Description = "Temperature")] object temperature)
{
var cellmConfiguration = ServiceLocator.Get<IOptions<CellmConfiguration>>().Value;
var configuration = ServiceLocator.Get<IConfiguration>();

var provider = configuration.GetSection(nameof(CellmConfiguration)).GetValue<string>(nameof(CellmConfiguration.DefaultProvider))
?? throw new ArgumentException(nameof(CellmConfiguration.DefaultProvider));

var model = configuration.GetSection($"{provider}Configuration").GetValue<string>(nameof(IProviderConfiguration.DefaultModel))
?? throw new ArgumentException(nameof(IProviderConfiguration.DefaultModel));

return PromptWith(
$"{cellmConfiguration.DefaultProvider}/{cellmConfiguration.DefaultModel}",
$"{provider}/{model}",
context,
instructionsOrTemperature,
temperature);
}

/// <summary>
/// Sends a prompt to a specific model.
/// Sends a prompt to the specified model.
/// </summary>
/// <param name="providerAndModel">The provider and model in the format "provider/model".</param>
/// <param name="context">A cell or range of cells containing the context for the prompt.</param>
Expand Down Expand Up @@ -79,15 +87,16 @@ public static object PromptWith(
.ToString();

var prompt = new PromptBuilder()
.SetModel(arguments.Model)
.SetSystemMessage(CellmPrompts.SystemMessage)
.SetTemperature(arguments.Temperature)
.AddUserMessage(userMessage)
.Build();

// ExcelAsyncUtil yields Excel's main thread, Task.Run enables async/await in inner code
return ExcelAsyncUtil.Run(nameof(Prompt), new object[] { context, instructionsOrTemperature, temperature }, () =>
return ExcelAsyncUtil.Run(nameof(PromptWith), new object[] { providerAndModel, context, instructionsOrTemperature, temperature }, () =>
{
return Task.Run(async () => await CallModelAsync(prompt, arguments.Provider, arguments.Model)).GetAwaiter().GetResult();
return Task.Run(async () => await CallModelAsync(prompt, arguments.Provider)).GetAwaiter().GetResult();
});
}
catch (CellmException ex)
Expand All @@ -106,20 +115,23 @@ public static object PromptWith(
/// <returns>A task that represents the asynchronous operation. The task result contains the model's response as a string.</returns>
/// <exception cref="CellmException">Thrown when an unexpected error occurs during the operation.</exception>

private static async Task<string> CallModelAsync(Prompt prompt, string? provider = null, string? model = null, Uri? baseAddress = null)
private static async Task<string> CallModelAsync(Prompt prompt, string? provider = null, Uri? baseAddress = null)
{
try
{
var client = ServiceLocator.Get<IClient>();
var response = await client.Send(prompt, provider, model, baseAddress);
return response.Messages.Last().Content;
var response = await client.Send(prompt, provider, baseAddress);
var content = response.Messages.Last().Content;
return content;
}
catch (CellmException)
catch (CellmException ex)
{
Debug.WriteLine(ex);
throw;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
throw new CellmException("An unexpected error occurred", ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Cellm.AddIn.Prompts;
namespace Cellm.AddIn;

internal static class CellmPrompts
{
Expand Down
12 changes: 0 additions & 12 deletions src/Cellm/AddIn/Prompts/Prompt.cs

This file was deleted.

4 changes: 4 additions & 0 deletions src/Cellm/Cellm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
<ItemGroup>
<PackageReference Include="ExcelDna.Addin" Version="1.8.0" />
<PackageReference Include="ExcelDna.Interop" Version="15.0.1" />
<PackageReference Include="JsonPatch.Net" Version="3.1.1" />
<PackageReference Include="JsonSchema.Net.Generation" Version="4.5.1" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.8.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
Expand Down
167 changes: 0 additions & 167 deletions src/Cellm/Models/Anthropic/AnthropicClient.cs

This file was deleted.

5 changes: 5 additions & 0 deletions src/Cellm/Models/Anthropic/AnthropicRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Cellm.Prompts;

namespace Cellm.Models.Anthropic;

internal record AnthropicRequest(Prompt Prompt, string? Provider, Uri? BaseAddress) : IModelRequest<AnthropicResponse>;
Loading

0 comments on commit 0cc6423

Please sign in to comment.