diff --git a/LLama.Examples/NewVersion/ChatSessionStripRoleName.cs b/LLama.Examples/NewVersion/ChatSessionStripRoleName.cs index 274a1bac..65ac8d91 100644 --- a/LLama.Examples/NewVersion/ChatSessionStripRoleName.cs +++ b/LLama.Examples/NewVersion/ChatSessionStripRoleName.cs @@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion var modelPath = Console.ReadLine(); var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); - var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5); + var parameters = new ModelParams(modelPath) + { + ContextSize = 1024, + Seed = 1337, + GpuLayerCount = 5 + }; using var model = LLamaWeights.LoadFromFile(parameters); using var context = model.CreateContext(parameters); var executor = new InteractiveExecutor(context); diff --git a/LLama.Examples/NewVersion/ChatSessionWithRoleName.cs b/LLama.Examples/NewVersion/ChatSessionWithRoleName.cs index b04fbcf3..dcbcc07b 100644 --- a/LLama.Examples/NewVersion/ChatSessionWithRoleName.cs +++ b/LLama.Examples/NewVersion/ChatSessionWithRoleName.cs @@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion var modelPath = Console.ReadLine(); var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); - var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5); + var parameters = new ModelParams(modelPath) + { + ContextSize = 1024, + Seed = 1337, + GpuLayerCount = 5 + }; using var model = LLamaWeights.LoadFromFile(parameters); using var context = model.CreateContext(parameters); var executor = new InteractiveExecutor(context); diff --git a/LLama.Examples/NewVersion/InstructModeExecute.cs b/LLama.Examples/NewVersion/InstructModeExecute.cs index 9eabe04a..b0e325f1 100644 --- a/LLama.Examples/NewVersion/InstructModeExecute.cs +++ b/LLama.Examples/NewVersion/InstructModeExecute.cs @@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion var modelPath = Console.ReadLine(); var prompt = File.ReadAllText("Assets/dan.txt").Trim(); - var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5); + var parameters = new ModelParams(modelPath) + { + ContextSize = 1024, + Seed = 1337, + GpuLayerCount = 5 + }; using var model = LLamaWeights.LoadFromFile(parameters); using var context = model.CreateContext(parameters); var executor = new InstructExecutor(context); diff --git a/LLama.Examples/NewVersion/InteractiveModeExecute.cs b/LLama.Examples/NewVersion/InteractiveModeExecute.cs index cbaf8844..8bc002eb 100644 --- a/LLama.Examples/NewVersion/InteractiveModeExecute.cs +++ b/LLama.Examples/NewVersion/InteractiveModeExecute.cs @@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion var modelPath = Console.ReadLine(); var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim(); - var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5); + var parameters = new ModelParams(modelPath) + { + ContextSize = 1024, + Seed = 1337, + GpuLayerCount = 5 + }; using var model = LLamaWeights.LoadFromFile(parameters); using var context = model.CreateContext(parameters); var ex = new InteractiveExecutor(context); diff --git a/LLama.Examples/NewVersion/LoadAndSaveSession.cs b/LLama.Examples/NewVersion/LoadAndSaveSession.cs index ff9c1237..948ac6cd 100644 --- a/LLama.Examples/NewVersion/LoadAndSaveSession.cs +++ b/LLama.Examples/NewVersion/LoadAndSaveSession.cs @@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion var modelPath = Console.ReadLine(); var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); - var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5); + var parameters = new ModelParams(modelPath) + { + ContextSize = 1024, + Seed = 1337, + GpuLayerCount = 5 + }; using var model = LLamaWeights.LoadFromFile(parameters); using var context = model.CreateContext(parameters); var ex = new InteractiveExecutor(context); @@ -45,7 +50,7 @@ namespace LLama.Examples.NewVersion Console.ForegroundColor = ConsoleColor.White; ex.Context.Dispose(); - ex = new(new LLamaContext(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5))); + ex = new(new LLamaContext(parameters)); session = new ChatSession(ex); session.LoadSession(statePath); diff --git a/LLama.Examples/NewVersion/LoadAndSaveState.cs b/LLama.Examples/NewVersion/LoadAndSaveState.cs index af7e6151..9f232982 100644 --- a/LLama.Examples/NewVersion/LoadAndSaveState.cs +++ b/LLama.Examples/NewVersion/LoadAndSaveState.cs @@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion var modelPath = Console.ReadLine(); var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim(); - var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5); + var parameters = new ModelParams(modelPath) + { + ContextSize = 1024, + Seed = 1337, + GpuLayerCount = 5 + }; using var model = LLamaWeights.LoadFromFile(parameters); using var context = model.CreateContext(parameters); var ex = new InteractiveExecutor(context); diff --git a/LLama.Examples/NewVersion/StatelessModeExecute.cs b/LLama.Examples/NewVersion/StatelessModeExecute.cs index 7f59e73e..ddd6227f 100644 --- a/LLama.Examples/NewVersion/StatelessModeExecute.cs +++ b/LLama.Examples/NewVersion/StatelessModeExecute.cs @@ -9,7 +9,12 @@ namespace LLama.Examples.NewVersion Console.Write("Please input your model path: "); var modelPath = Console.ReadLine(); - var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5); + var parameters = new ModelParams(modelPath) + { + ContextSize = 1024, + Seed = 1337, + GpuLayerCount = 5 + }; using var model = LLamaWeights.LoadFromFile(parameters); var ex = new StatelessExecutor(model, parameters); diff --git a/LLama.Unittest/BasicTest.cs b/LLama.Unittest/BasicTest.cs index 6fc206ed..fc3ab8bf 100644 --- a/LLama.Unittest/BasicTest.cs +++ b/LLama.Unittest/BasicTest.cs @@ -10,7 +10,10 @@ namespace LLama.Unittest public BasicTest() { - _params = new ModelParams("Models/llama-2-7b-chat.ggmlv3.q3_K_S.bin", contextSize: 2048); + _params = new ModelParams("Models/llama-2-7b-chat.ggmlv3.q3_K_S.bin") + { + ContextSize = 2048 + }; _model = LLamaWeights.LoadFromFile(_params); } diff --git a/LLama.Unittest/GrammarTest.cs b/LLama.Unittest/GrammarTest.cs index eebbf1d1..482268ea 100644 --- a/LLama.Unittest/GrammarTest.cs +++ b/LLama.Unittest/GrammarTest.cs @@ -11,7 +11,10 @@ namespace LLama.Unittest public GrammarTest() { - _params = new ModelParams("Models/llama-2-7b-chat.ggmlv3.q3_K_S.bin", contextSize: 2048); + _params = new ModelParams("Models/llama-2-7b-chat.ggmlv3.q3_K_S.bin") + { + ContextSize = 2048, + }; _model = LLamaWeights.LoadFromFile(_params); } diff --git a/LLama.Unittest/ModelsParamsTests.cs b/LLama.Unittest/ModelsParamsTests.cs new file mode 100644 index 00000000..3296ca09 --- /dev/null +++ b/LLama.Unittest/ModelsParamsTests.cs @@ -0,0 +1,45 @@ +using LLama.Common; + +namespace LLama.Unittest +{ + public class ModelsParamsTests + { + [Fact] + public void SerializeRoundTripSystemTextJson() + { + var expected = new ModelParams("abc/123") + { + BatchSize = 17, + ContextSize = 42, + LoraAdapter = "adapter", + GroupedQueryAttention = 7, + Seed = 42, + GpuLayerCount = 111 + }; + + var json = System.Text.Json.JsonSerializer.Serialize(expected); + var actual = System.Text.Json.JsonSerializer.Deserialize(json); + + Assert.Equal(expected, actual); + } + + [Fact] + public void SerializeRoundTripNewtonsoft() + { + var expected = new ModelParams("abc/123") + { + BatchSize = 17, + ContextSize = 42, + LoraAdapter = "adapter", + GroupedQueryAttention = 7, + Seed = 42, + GpuLayerCount = 111 + }; + + var json = Newtonsoft.Json.JsonConvert.SerializeObject(expected); + var actual = Newtonsoft.Json.JsonConvert.DeserializeObject(json); + + Assert.Equal(expected, actual); + } + } +} diff --git a/LLama.WebAPI/Services/StatefulChatService.cs b/LLama.WebAPI/Services/StatefulChatService.cs index d6924e6c..a9ac3a44 100644 --- a/LLama.WebAPI/Services/StatefulChatService.cs +++ b/LLama.WebAPI/Services/StatefulChatService.cs @@ -16,7 +16,10 @@ public class StatefulChatService : IDisposable public StatefulChatService(IConfiguration configuration) { - _context = new LLamaContext(new Common.ModelParams(configuration["ModelPath"], contextSize: 512)); + _context = new LLamaContext(new Common.ModelParams(configuration["ModelPath"]) + { + ContextSize = 512 + }); _session = new ChatSession(new InteractiveExecutor(_context)); } diff --git a/LLama.WebAPI/Services/StatelessChatService.cs b/LLama.WebAPI/Services/StatelessChatService.cs index 27c508dd..b924f4d8 100644 --- a/LLama.WebAPI/Services/StatelessChatService.cs +++ b/LLama.WebAPI/Services/StatelessChatService.cs @@ -12,7 +12,10 @@ namespace LLama.WebAPI.Services public StatelessChatService(IConfiguration configuration) { - _context = new LLamaContext(new ModelParams(configuration["ModelPath"], contextSize: 512)); + _context = new LLamaContext(new ModelParams(configuration["ModelPath"]) + { + ContextSize = 512, + }); // TODO: replace with a stateless executor _session = new ChatSession(new InteractiveExecutor(_context)) .WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8)) diff --git a/LLama/Common/ModelParams.cs b/LLama/Common/ModelParams.cs index bc26965c..2b23a37e 100644 --- a/LLama/Common/ModelParams.cs +++ b/LLama/Common/ModelParams.cs @@ -6,7 +6,7 @@ namespace LLama.Common /// /// The parameters for initializing a LLama model. /// - public class ModelParams + public record ModelParams : IModelParams { /// @@ -116,6 +116,22 @@ namespace LLama.Common /// public string Encoding { get; set; } = "UTF-8"; + /// + /// + /// + /// The model path. + [System.Text.Json.Serialization.JsonConstructor] + public ModelParams(string modelPath) + { + ModelPath = modelPath; + } + + private ModelParams() + { + // This constructor (default parameterless constructor) is used by Newtonsoft to deserialize! + ModelPath = ""; + } + /// /// /// @@ -139,6 +155,7 @@ namespace LLama.Common /// RoPE frequency scaling factor /// Use experimental mul_mat_q kernels /// The encoding to use to convert text for the model + [Obsolete("Use object initializer to set all optional parameters")] public ModelParams(string modelPath, int contextSize = 512, int gpuLayerCount = 20, int seed = 1337, bool useFp16Memory = true, bool useMemorymap = true, bool useMemoryLock = false, bool perplexity = false, @@ -161,10 +178,10 @@ namespace LLama.Common BatchSize = batchSize; ConvertEosToNewLine = convertEosToNewLine; EmbeddingMode = embeddingMode; - GroupedQueryAttention = groupedQueryAttention; + GroupedQueryAttention = groupedQueryAttention; RmsNormEpsilon = rmsNormEpsilon; - RopeFrequencyBase = ropeFrequencyBase; - RopeFrequencyScale = ropeFrequencyScale; + RopeFrequencyBase = ropeFrequencyBase; + RopeFrequencyScale = ropeFrequencyScale; MulMatQ = mulMatQ; Encoding = encoding; }