From 829f32b27dcf99bb535ab7c47ab132e1d68ddff3 Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Thu, 24 Aug 2023 00:59:32 +0100 Subject: [PATCH] - Added `Obsolete` attributes to the entire `OldVersion` namespace, so they can be removed in the future - Minor changes to cleanup some of the compiler warnings --- LLama.Examples/NewVersion/LoadAndSaveState.cs | 4 ++-- LLama.Examples/OldVersion/ChatSession.cs | 1 + .../OldVersion/ChatWithLLamaModel.cs | 1 + LLama.Examples/OldVersion/GetEmbeddings.cs | 1 + LLama.Examples/OldVersion/InstructMode.cs | 1 + LLama.Examples/OldVersion/SaveAndLoadState.cs | 1 + LLama/LLamaExecutorBase.cs | 2 +- LLama/LLamaInstructExecutor.cs | 2 +- LLama/LLamaInteractExecutor.cs | 6 ++++-- LLama/Native/NativeApi.cs | 2 +- LLama/OldVersion/ChatSession.cs | 4 +++- LLama/OldVersion/IChatModel.cs | 4 +++- LLama/OldVersion/LLamaEmbedder.cs | 8 +++++--- LLama/OldVersion/LLamaModel.cs | 7 ++++++- LLama/OldVersion/LLamaParams.cs | 4 ++++ LLama/OldVersion/LLamaTypes.cs | 19 ++++++++++++++++++- LLama/OldVersion/Utils.cs | 4 +++- LLama/Utils.cs | 19 ++++++++++++++++--- 18 files changed, 72 insertions(+), 18 deletions(-) diff --git a/LLama.Examples/NewVersion/LoadAndSaveState.cs b/LLama.Examples/NewVersion/LoadAndSaveState.cs index af7e6151..9cf1bd0d 100644 --- a/LLama.Examples/NewVersion/LoadAndSaveState.cs +++ b/LLama.Examples/NewVersion/LoadAndSaveState.cs @@ -34,11 +34,11 @@ namespace LLama.Examples.NewVersion if (prompt == "save") { Console.Write("Your path to save model state: "); - string modelStatePath = Console.ReadLine(); + var modelStatePath = Console.ReadLine(); ex.Context.SaveState(modelStatePath); Console.Write("Your path to save executor state: "); - string executorStatePath = Console.ReadLine(); + var executorStatePath = Console.ReadLine(); ex.SaveState(executorStatePath); Console.ForegroundColor = ConsoleColor.Yellow; diff --git a/LLama.Examples/OldVersion/ChatSession.cs b/LLama.Examples/OldVersion/ChatSession.cs index 52216803..3f851532 100644 --- a/LLama.Examples/OldVersion/ChatSession.cs +++ b/LLama.Examples/OldVersion/ChatSession.cs @@ -7,6 +7,7 @@ using LLama.OldVersion; namespace LLama.Examples.Old { + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public class ChatSession { LLama.OldVersion.ChatSession _session; diff --git a/LLama.Examples/OldVersion/ChatWithLLamaModel.cs b/LLama.Examples/OldVersion/ChatWithLLamaModel.cs index 452b5b2d..88adebc7 100644 --- a/LLama.Examples/OldVersion/ChatWithLLamaModel.cs +++ b/LLama.Examples/OldVersion/ChatWithLLamaModel.cs @@ -7,6 +7,7 @@ using LLama.OldVersion; namespace LLama.Examples.Old { + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public class ChatWithLLamaModel { LLama.OldVersion.LLamaModel _model; diff --git a/LLama.Examples/OldVersion/GetEmbeddings.cs b/LLama.Examples/OldVersion/GetEmbeddings.cs index df620ea1..8dd28109 100644 --- a/LLama.Examples/OldVersion/GetEmbeddings.cs +++ b/LLama.Examples/OldVersion/GetEmbeddings.cs @@ -7,6 +7,7 @@ using LLama.OldVersion; namespace LLama.Examples.Old { + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public class GetEmbeddings { LLama.OldVersion.LLamaEmbedder _embedder; diff --git a/LLama.Examples/OldVersion/InstructMode.cs b/LLama.Examples/OldVersion/InstructMode.cs index 2b954e3f..ce123366 100644 --- a/LLama.Examples/OldVersion/InstructMode.cs +++ b/LLama.Examples/OldVersion/InstructMode.cs @@ -7,6 +7,7 @@ using LLama.OldVersion; namespace LLama.Examples.Old { + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public class InstructMode { LLama.OldVersion.LLamaModel _model; diff --git a/LLama.Examples/OldVersion/SaveAndLoadState.cs b/LLama.Examples/OldVersion/SaveAndLoadState.cs index bcb77409..abe6bbfb 100644 --- a/LLama.Examples/OldVersion/SaveAndLoadState.cs +++ b/LLama.Examples/OldVersion/SaveAndLoadState.cs @@ -7,6 +7,7 @@ using LLama.OldVersion; namespace LLama.Examples.Old { + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public class SaveAndLoadState: IDisposable { LLama.OldVersion.LLamaModel _model; diff --git a/LLama/LLamaExecutorBase.cs b/LLama/LLamaExecutorBase.cs index 173cc10d..73dd439c 100644 --- a/LLama/LLamaExecutorBase.cs +++ b/LLama/LLamaExecutorBase.cs @@ -367,7 +367,7 @@ namespace LLama public int MatchingSessionTokensCount { get; set; } [JsonPropertyName("path_session")] - public string SessionFilePath { get; set; } + public string? SessionFilePath { get; set; } [JsonPropertyName("embd")] public List Embeds { get; set; } diff --git a/LLama/LLamaInstructExecutor.cs b/LLama/LLamaInstructExecutor.cs index 982cea64..7d9a4ed4 100644 --- a/LLama/LLamaInstructExecutor.cs +++ b/LLama/LLamaInstructExecutor.cs @@ -84,7 +84,7 @@ namespace LLama /// public override void SaveState(string filename) { - InstructExecutorState state = GetStateData() as InstructExecutorState; + InstructExecutorState state = (InstructExecutorState)GetStateData(); using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)) { JsonSerializer.Serialize(fs, state); diff --git a/LLama/LLamaInteractExecutor.cs b/LLama/LLamaInteractExecutor.cs index 94aa7c2a..595ddb3b 100644 --- a/LLama/LLamaInteractExecutor.cs +++ b/LLama/LLamaInteractExecutor.cs @@ -72,10 +72,10 @@ namespace LLama /// public override void SaveState(string filename) { - InteractiveExecutorState state = GetStateData() as InteractiveExecutorState; + InteractiveExecutorState state = (InteractiveExecutorState)GetStateData(); using(FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)) { - JsonSerializer.Serialize(fs, state); + JsonSerializer.Serialize(fs, state); } } /// @@ -121,7 +121,9 @@ namespace LLama /// /// Return whether to break the generation. /// + /// /// + /// /// protected override bool PostProcess(IInferenceParams inferenceParams, InferStateArgs args, out IEnumerable? extraOutputs) { diff --git a/LLama/Native/NativeApi.cs b/LLama/Native/NativeApi.cs index 466e5263..0a1c7c1c 100644 --- a/LLama/Native/NativeApi.cs +++ b/LLama/Native/NativeApi.cs @@ -64,7 +64,7 @@ namespace LLama.Native /// Return NULL on failure /// /// - /// + /// /// [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr llama_load_model_from_file(string path_model, LLamaContextParams @params); diff --git a/LLama/OldVersion/ChatSession.cs b/LLama/OldVersion/ChatSession.cs index 1bf954fa..42f589b3 100644 --- a/LLama/OldVersion/ChatSession.cs +++ b/LLama/OldVersion/ChatSession.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; using System.IO; -using System.Text; + +#pragma warning disable namespace LLama.OldVersion { + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public class ChatSession where T : IChatModel { IChatModel _model; diff --git a/LLama/OldVersion/IChatModel.cs b/LLama/OldVersion/IChatModel.cs index 7fbd898b..697e05cd 100644 --- a/LLama/OldVersion/IChatModel.cs +++ b/LLama/OldVersion/IChatModel.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; -using System.Text; + +#pragma warning disable namespace LLama.OldVersion { + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public interface IChatModel { string Name { get; } diff --git a/LLama/OldVersion/LLamaEmbedder.cs b/LLama/OldVersion/LLamaEmbedder.cs index 823c4437..04a8abc9 100644 --- a/LLama/OldVersion/LLamaEmbedder.cs +++ b/LLama/OldVersion/LLamaEmbedder.cs @@ -1,12 +1,14 @@ using LLama.Native; using System; -using System.Collections.Generic; -using System.Text; using LLama.Exceptions; +#pragma warning disable + namespace LLama.OldVersion { - public class LLamaEmbedder : IDisposable + [Obsolete("The entire LLama.OldVersion namespace will be removed")] + public class LLamaEmbedder + : IDisposable { SafeLLamaContextHandle _ctx; diff --git a/LLama/OldVersion/LLamaModel.cs b/LLama/OldVersion/LLamaModel.cs index bf400ba4..4a1a0b2c 100644 --- a/LLama/OldVersion/LLamaModel.cs +++ b/LLama/OldVersion/LLamaModel.cs @@ -9,10 +9,15 @@ using System.Linq; using System.Text; using LLama.Common; +#pragma warning disable + namespace LLama.OldVersion { using llama_token = Int32; - public class LLamaModel : IChatModel, IDisposable + + [Obsolete("The entire LLama.OldVersion namespace will be removed")] + public class LLamaModel + : IChatModel, IDisposable { LLamaParams _params; SafeLLamaContextHandle _ctx; diff --git a/LLama/OldVersion/LLamaParams.cs b/LLama/OldVersion/LLamaParams.cs index a2d677d8..f58daa0c 100644 --- a/LLama/OldVersion/LLamaParams.cs +++ b/LLama/OldVersion/LLamaParams.cs @@ -1,9 +1,13 @@ using System; using System.Collections.Generic; +#pragma warning disable + namespace LLama.OldVersion { using llama_token = Int32; + + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public struct LLamaParams { public int seed; // RNG seed diff --git a/LLama/OldVersion/LLamaTypes.cs b/LLama/OldVersion/LLamaTypes.cs index d0bd4ad7..c6823f56 100644 --- a/LLama/OldVersion/LLamaTypes.cs +++ b/LLama/OldVersion/LLamaTypes.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; -using System.Text; + +#pragma warning disable namespace LLama.OldVersion { @@ -9,33 +10,49 @@ namespace LLama.OldVersion Human, Assistant } + + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record EmbeddingUsage(int PromptTokens, int TotalTokens); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record EmbeddingData(int Index, string Object, float[] Embedding); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record Embedding(string Object, string Model, EmbeddingData[] Data, EmbeddingUsage Usage); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record CompletionLogprobs(int[] TextOffset, float[] TokenLogProbs, string[] Tokens, Dictionary[] TopLogprobs); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record CompletionChoice(string Text, int Index, CompletionLogprobs? Logprobs, string? FinishReason); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record CompletionUsage(int PromptTokens, int CompletionTokens, int TotalTokens); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record CompletionChunk(string Id, string Object, int Created, string Model, CompletionChoice[] Choices); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record Completion(string Id, string Object, int Created, string Model, CompletionChoice[] Choices, CompletionUsage Usage); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record ChatCompletionMessage(ChatRole Role, string Content, string? Name = null); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record ChatCompletionChoice(int Index, ChatCompletionMessage Message, string? FinishReason); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record ChatCompletion(string Id, string Object, int Created, string Model, ChatCompletionChoice[] Choices, CompletionUsage Usage); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record ChatCompletionChunkDelta(string? Role, string? Content); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record ChatCompletionChunkChoice(int Index, ChatCompletionChunkDelta Delta, string? FinishReason); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record ChatCompletionChunk(string Id, string Model, string Object, int Created, ChatCompletionChunkChoice[] Choices); + [Obsolete("The entire LLama.OldVersion namespace will be removed")] public record ChatMessageRecord(ChatCompletionMessage Message, DateTime Time); } diff --git a/LLama/OldVersion/Utils.cs b/LLama/OldVersion/Utils.cs index eb0986dd..ec85ca30 100644 --- a/LLama/OldVersion/Utils.cs +++ b/LLama/OldVersion/Utils.cs @@ -3,14 +3,16 @@ using System; using System.Collections.Generic; using System.Text; using LLama.Exceptions; -using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.IO; +#pragma warning disable + namespace LLama.OldVersion { using llama_token = Int32; + internal static class Utils { public static SafeLLamaContextHandle llama_init_from_gpt_params(ref LLamaParams @params) diff --git a/LLama/Utils.cs b/LLama/Utils.cs index f6bc7202..bfce9f3b 100644 --- a/LLama/Utils.cs +++ b/LLama/Utils.cs @@ -9,6 +9,7 @@ using LLama.Extensions; namespace LLama { using llama_token = Int32; + public static class Utils { public static SafeLLamaContextHandle InitLLamaContextFromModelParams(IModelParams @params) @@ -20,13 +21,17 @@ namespace LLama } [Obsolete("Use SafeLLamaContextHandle Tokenize method instead")] + #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public static IEnumerable Tokenize(SafeLLamaContextHandle ctx, string text, bool add_bos, Encoding encoding) + #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member { return ctx.Tokenize(text, add_bos, encoding); } [Obsolete("Use SafeLLamaContextHandle GetLogits method instead")] + #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public static Span GetLogits(SafeLLamaContextHandle ctx, int length) + #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member { if (length != ctx.VocabCount) throw new ArgumentException("length must be the VocabSize"); @@ -35,33 +40,41 @@ namespace LLama } [Obsolete("Use SafeLLamaContextHandle Eval method instead")] + #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public static int Eval(SafeLLamaContextHandle ctx, llama_token[] tokens, int startIndex, int n_tokens, int n_past, int n_threads) + #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member { var slice = tokens.AsSpan().Slice(startIndex, n_tokens); return ctx.Eval(slice, n_past, n_threads) ? 0 : 1; } [Obsolete("Use SafeLLamaContextHandle TokenToString method instead")] + #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public static string TokenToString(llama_token token, SafeLLamaContextHandle ctx, Encoding encoding) + #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member { return ctx.TokenToString(token, encoding); } [Obsolete("No longer used internally by LlamaSharp")] + #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public static string PtrToString(IntPtr ptr, Encoding encoding) + #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member { #if NET6_0_OR_GREATER + // ReSharper disable once PossibleUnintendedReferenceComparison if(encoding == Encoding.UTF8) { - return Marshal.PtrToStringUTF8(ptr); + return Marshal.PtrToStringUTF8(ptr)!; } + // ReSharper disable once PossibleUnintendedReferenceComparison else if(encoding == Encoding.Unicode) { - return Marshal.PtrToStringUni(ptr); + return Marshal.PtrToStringUni(ptr)!; } else { - return Marshal.PtrToStringAuto(ptr); + return Marshal.PtrToStringAuto(ptr)!; } #else unsafe