From 9eb81cb5492213f2897020b0b73d5256b665e1e3 Mon Sep 17 00:00:00 2001 From: mvenditto Date: Mon, 29 May 2023 01:57:00 +0200 Subject: [PATCH] C# Bindings - Prompt formatting (#712) * Added support for custom prompt formatting * more docs added * bump version --- gpt4all-bindings/csharp/Directory.Build.props | 2 +- gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs | 21 +++++++++++++++++-- .../Gpt4All/Model/DefaultPromptFormatter.cs | 16 ++++++++++++++ .../csharp/Gpt4All/Model/IGpt4AllModel.cs | 5 +++++ .../csharp/Gpt4All/Model/IPromptFormatter.cs | 14 +++++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 gpt4all-bindings/csharp/Gpt4All/Model/DefaultPromptFormatter.cs create mode 100644 gpt4all-bindings/csharp/Gpt4All/Model/IPromptFormatter.cs diff --git a/gpt4all-bindings/csharp/Directory.Build.props b/gpt4all-bindings/csharp/Directory.Build.props index 8a634077..8ce38889 100644 --- a/gpt4all-bindings/csharp/Directory.Build.props +++ b/gpt4all-bindings/csharp/Directory.Build.props @@ -5,7 +5,7 @@ en-US - 0.5.0 + 0.6.0 $(VersionSuffix) $(Version)$(VersionSuffix) true diff --git a/gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs b/gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs index 5db08f65..d129ace4 100644 --- a/gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs +++ b/gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs @@ -7,19 +7,33 @@ public class Gpt4All : IGpt4AllModel { private readonly ILLModel _model; + /// + public IPromptFormatter? PromptFormatter { get; set; } + internal Gpt4All(ILLModel model) { _model = model; + PromptFormatter = new DefaultPromptFormatter(); + } + + private string FormatPrompt(string prompt) + { + if (PromptFormatter == null) return prompt; + + return PromptFormatter.FormatPrompt(prompt); } public Task GetPredictionAsync(string text, PredictRequestOptions opts, CancellationToken cancellationToken = default) { + ArgumentNullException.ThrowIfNull(text); + return Task.Run(() => { var result = new TextPredictionResult(); var context = opts.ToPromptContext(); + var prompt = FormatPrompt(text); - _model.Prompt(text, context, responseCallback: e => + _model.Prompt(prompt, context, responseCallback: e => { if (e.IsError) { @@ -37,6 +51,8 @@ public class Gpt4All : IGpt4AllModel public Task GetStreamingPredictionAsync(string text, PredictRequestOptions opts, CancellationToken cancellationToken = default) { + ArgumentNullException.ThrowIfNull(text); + var result = new TextPredictionStreamingResult(); _ = Task.Run(() => @@ -44,8 +60,9 @@ public class Gpt4All : IGpt4AllModel try { var context = opts.ToPromptContext(); + var prompt = FormatPrompt(text); - _model.Prompt(text, context, responseCallback: e => + _model.Prompt(prompt, context, responseCallback: e => { if (e.IsError) { diff --git a/gpt4all-bindings/csharp/Gpt4All/Model/DefaultPromptFormatter.cs b/gpt4all-bindings/csharp/Gpt4All/Model/DefaultPromptFormatter.cs new file mode 100644 index 00000000..5be9cd2a --- /dev/null +++ b/gpt4all-bindings/csharp/Gpt4All/Model/DefaultPromptFormatter.cs @@ -0,0 +1,16 @@ +namespace Gpt4All; + +public class DefaultPromptFormatter : IPromptFormatter +{ + public string FormatPrompt(string prompt) + { + return $""" + ### Instruction: + The prompt below is a question to answer, a task to complete, or a conversation + to respond to; decide which and write an appropriate response. + ### Prompt: + {prompt} + ### Response: + """; + } +} diff --git a/gpt4all-bindings/csharp/Gpt4All/Model/IGpt4AllModel.cs b/gpt4all-bindings/csharp/Gpt4All/Model/IGpt4AllModel.cs index 168a477c..11bdd1b2 100644 --- a/gpt4all-bindings/csharp/Gpt4All/Model/IGpt4AllModel.cs +++ b/gpt4all-bindings/csharp/Gpt4All/Model/IGpt4AllModel.cs @@ -2,4 +2,9 @@ public interface IGpt4AllModel : ITextPrediction, IDisposable { + /// + /// The prompt formatter used to format the prompt before + /// feeding it to the model, if null no transformation is applied + /// + IPromptFormatter? PromptFormatter { get; set; } } diff --git a/gpt4all-bindings/csharp/Gpt4All/Model/IPromptFormatter.cs b/gpt4all-bindings/csharp/Gpt4All/Model/IPromptFormatter.cs new file mode 100644 index 00000000..f6bc19aa --- /dev/null +++ b/gpt4all-bindings/csharp/Gpt4All/Model/IPromptFormatter.cs @@ -0,0 +1,14 @@ +namespace Gpt4All; + +/// +/// Formats a prompt +/// +public interface IPromptFormatter +{ + /// + /// Format the provided prompt + /// + /// the input prompt + /// The formatted prompt + string FormatPrompt(string prompt); +}