From 54c01d4c2c295ba88e8beb70827cc8af323baaf2 Mon Sep 17 00:00:00 2001 From: Zoli Somogyi Date: Tue, 30 Apr 2024 19:28:31 +0200 Subject: [PATCH] Making old code obsolete - SemanticKernel: Correcting working with PromptExecutionSettings --- .../ChatCompletion/ChatRequestSettings.cs | 114 ++++++++++++++++++ .../ChatRequestSettingsConverter.cs | 105 ++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 LLama.SemanticKernel/ChatCompletion/ChatRequestSettings.cs create mode 100644 LLama.SemanticKernel/ChatCompletion/ChatRequestSettingsConverter.cs diff --git a/LLama.SemanticKernel/ChatCompletion/ChatRequestSettings.cs b/LLama.SemanticKernel/ChatCompletion/ChatRequestSettings.cs new file mode 100644 index 00000000..683f8c45 --- /dev/null +++ b/LLama.SemanticKernel/ChatCompletion/ChatRequestSettings.cs @@ -0,0 +1,114 @@ +using Microsoft.SemanticKernel; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace LLamaSharp.SemanticKernel.ChatCompletion; + +[Obsolete("Use LLamaSharpPromptExecutionSettings instead")] +public class ChatRequestSettings : PromptExecutionSettings +{ + /// + /// Temperature controls the randomness of the completion. + /// The higher the temperature, the more random the completion. + /// + [JsonPropertyName("temperature")] + public double Temperature { get; set; } = 0; + + /// + /// TopP controls the diversity of the completion. + /// The higher the TopP, the more diverse the completion. + /// + [JsonPropertyName("top_p")] + public double TopP { get; set; } = 0; + + /// + /// Number between -2.0 and 2.0. Positive values penalize new tokens + /// based on whether they appear in the text so far, increasing the + /// model's likelihood to talk about new topics. + /// + [JsonPropertyName("presence_penalty")] + public double PresencePenalty { get; set; } = 0; + + /// + /// Number between -2.0 and 2.0. Positive values penalize new tokens + /// based on their existing frequency in the text so far, decreasing + /// the model's likelihood to repeat the same line verbatim. + /// + [JsonPropertyName("frequency_penalty")] + public double FrequencyPenalty { get; set; } = 0; + + /// + /// Sequences where the completion will stop generating further tokens. + /// + [JsonPropertyName("stop_sequences")] + public IList StopSequences { get; set; } = Array.Empty(); + + /// + /// How many completions to generate for each prompt. Default is 1. + /// Note: Because this parameter generates many completions, it can quickly consume your token quota. + /// Use carefully and ensure that you have reasonable settings for max_tokens and stop. + /// + [JsonPropertyName("results_per_prompt")] + public int ResultsPerPrompt { get; set; } = 1; + + /// + /// The maximum number of tokens to generate in the completion. + /// + [JsonPropertyName("max_tokens")] + public int? MaxTokens { get; set; } + + /// + /// Modify the likelihood of specified tokens appearing in the completion. + /// + [JsonPropertyName("token_selection_biases")] + public IDictionary TokenSelectionBiases { get; set; } = new Dictionary(); + + /// + /// Create a new settings object with the values from another settings object. + /// + /// Template configuration + /// Default max tokens + /// An instance of OpenAIRequestSettings + public static ChatRequestSettings FromRequestSettings(PromptExecutionSettings? requestSettings, int? defaultMaxTokens = null) + { + if (requestSettings is null) + { + return new ChatRequestSettings() + { + MaxTokens = defaultMaxTokens + }; + } + + if (requestSettings is ChatRequestSettings requestSettingsChatRequestSettings) + { + return requestSettingsChatRequestSettings; + } + + var json = JsonSerializer.Serialize(requestSettings); + var chatRequestSettings = JsonSerializer.Deserialize(json, s_options); + + if (chatRequestSettings is not null) + { + return chatRequestSettings; + } + + throw new ArgumentException($"Invalid request settings, cannot convert to {nameof(ChatRequestSettings)}", nameof(requestSettings)); + } + + private static readonly JsonSerializerOptions s_options = CreateOptions(); + + private static JsonSerializerOptions CreateOptions() + { + JsonSerializerOptions options = new() + { + WriteIndented = true, + MaxDepth = 20, + AllowTrailingCommas = true, + PropertyNameCaseInsensitive = true, + ReadCommentHandling = JsonCommentHandling.Skip, + Converters = { new ChatRequestSettingsConverter() } + }; + + return options; + } +} diff --git a/LLama.SemanticKernel/ChatCompletion/ChatRequestSettingsConverter.cs b/LLama.SemanticKernel/ChatCompletion/ChatRequestSettingsConverter.cs new file mode 100644 index 00000000..15bc45cd --- /dev/null +++ b/LLama.SemanticKernel/ChatCompletion/ChatRequestSettingsConverter.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace LLamaSharp.SemanticKernel.ChatCompletion; + +/// +/// JSON converter for +/// +[Obsolete("Use LLamaSharpPromptExecutionSettingsConverter instead")] +public class ChatRequestSettingsConverter : JsonConverter +{ + /// + public override ChatRequestSettings? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var requestSettings = new ChatRequestSettings(); + + while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) + { + if (reader.TokenType == JsonTokenType.PropertyName) + { + string? propertyName = reader.GetString(); + + if (propertyName is not null) + { + // normalise property name to uppercase + propertyName = propertyName.ToUpperInvariant(); + } + + reader.Read(); + + switch (propertyName) + { + case "MODELID": + case "MODEL_ID": + requestSettings.ModelId = reader.GetString(); + break; + case "TEMPERATURE": + requestSettings.Temperature = reader.GetDouble(); + break; + case "TOPP": + case "TOP_P": + requestSettings.TopP = reader.GetDouble(); + break; + case "FREQUENCYPENALTY": + case "FREQUENCY_PENALTY": + requestSettings.FrequencyPenalty = reader.GetDouble(); + break; + case "PRESENCEPENALTY": + case "PRESENCE_PENALTY": + requestSettings.PresencePenalty = reader.GetDouble(); + break; + case "MAXTOKENS": + case "MAX_TOKENS": + requestSettings.MaxTokens = reader.GetInt32(); + break; + case "STOPSEQUENCES": + case "STOP_SEQUENCES": + requestSettings.StopSequences = JsonSerializer.Deserialize>(ref reader, options) ?? Array.Empty(); + break; + case "RESULTSPERPROMPT": + case "RESULTS_PER_PROMPT": + requestSettings.ResultsPerPrompt = reader.GetInt32(); + break; + case "TOKENSELECTIONBIASES": + case "TOKEN_SELECTION_BIASES": + requestSettings.TokenSelectionBiases = JsonSerializer.Deserialize>(ref reader, options) ?? new Dictionary(); + break; + default: + reader.Skip(); + break; + } + } + } + + return requestSettings; + } + + /// + public override void Write(Utf8JsonWriter writer, ChatRequestSettings value, JsonSerializerOptions options) + { + writer.WriteStartObject(); + + writer.WriteNumber("temperature", value.Temperature); + writer.WriteNumber("top_p", value.TopP); + writer.WriteNumber("frequency_penalty", value.FrequencyPenalty); + writer.WriteNumber("presence_penalty", value.PresencePenalty); + if (value.MaxTokens is null) + { + writer.WriteNull("max_tokens"); + } + else + { + writer.WriteNumber("max_tokens", (decimal)value.MaxTokens); + } + writer.WritePropertyName("stop_sequences"); + JsonSerializer.Serialize(writer, value.StopSequences, options); + writer.WriteNumber("results_per_prompt", value.ResultsPerPrompt); + writer.WritePropertyName("token_selection_biases"); + JsonSerializer.Serialize(writer, value.TokenSelectionBiases, options); + + writer.WriteEndObject(); + } +} \ No newline at end of file