You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatRequestSettings.cs 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Microsoft.SemanticKernel;
  2. using System.Text.Json;
  3. using System.Text.Json.Serialization;
  4. namespace LLamaSharp.SemanticKernel.ChatCompletion;
  5. [Obsolete("Use LLamaSharpPromptExecutionSettings instead")]
  6. public class ChatRequestSettings : PromptExecutionSettings
  7. {
  8. /// <summary>
  9. /// Temperature controls the randomness of the completion.
  10. /// The higher the temperature, the more random the completion.
  11. /// </summary>
  12. [JsonPropertyName("temperature")]
  13. public double Temperature { get; set; } = 0;
  14. /// <summary>
  15. /// TopP controls the diversity of the completion.
  16. /// The higher the TopP, the more diverse the completion.
  17. /// </summary>
  18. [JsonPropertyName("top_p")]
  19. public double TopP { get; set; } = 0;
  20. /// <summary>
  21. /// Number between -2.0 and 2.0. Positive values penalize new tokens
  22. /// based on whether they appear in the text so far, increasing the
  23. /// model's likelihood to talk about new topics.
  24. /// </summary>
  25. [JsonPropertyName("presence_penalty")]
  26. public double PresencePenalty { get; set; } = 0;
  27. /// <summary>
  28. /// Number between -2.0 and 2.0. Positive values penalize new tokens
  29. /// based on their existing frequency in the text so far, decreasing
  30. /// the model's likelihood to repeat the same line verbatim.
  31. /// </summary>
  32. [JsonPropertyName("frequency_penalty")]
  33. public double FrequencyPenalty { get; set; } = 0;
  34. /// <summary>
  35. /// Sequences where the completion will stop generating further tokens.
  36. /// </summary>
  37. [JsonPropertyName("stop_sequences")]
  38. public IList<string> StopSequences { get; set; } = Array.Empty<string>();
  39. /// <summary>
  40. /// How many completions to generate for each prompt. Default is 1.
  41. /// Note: Because this parameter generates many completions, it can quickly consume your token quota.
  42. /// Use carefully and ensure that you have reasonable settings for max_tokens and stop.
  43. /// </summary>
  44. [JsonPropertyName("results_per_prompt")]
  45. public int ResultsPerPrompt { get; set; } = 1;
  46. /// <summary>
  47. /// The maximum number of tokens to generate in the completion.
  48. /// </summary>
  49. [JsonPropertyName("max_tokens")]
  50. public int? MaxTokens { get; set; }
  51. /// <summary>
  52. /// Modify the likelihood of specified tokens appearing in the completion.
  53. /// </summary>
  54. [JsonPropertyName("token_selection_biases")]
  55. public IDictionary<int, int> TokenSelectionBiases { get; set; } = new Dictionary<int, int>();
  56. /// <summary>
  57. /// Create a new settings object with the values from another settings object.
  58. /// </summary>
  59. /// <param name="requestSettings">Template configuration</param>
  60. /// <param name="defaultMaxTokens">Default max tokens</param>
  61. /// <returns>An instance of OpenAIRequestSettings</returns>
  62. public static ChatRequestSettings FromRequestSettings(PromptExecutionSettings? requestSettings, int? defaultMaxTokens = null)
  63. {
  64. if (requestSettings is null)
  65. {
  66. return new ChatRequestSettings()
  67. {
  68. MaxTokens = defaultMaxTokens
  69. };
  70. }
  71. if (requestSettings is ChatRequestSettings requestSettingsChatRequestSettings)
  72. {
  73. return requestSettingsChatRequestSettings;
  74. }
  75. var json = JsonSerializer.Serialize(requestSettings);
  76. var chatRequestSettings = JsonSerializer.Deserialize<ChatRequestSettings>(json, s_options);
  77. if (chatRequestSettings is not null)
  78. {
  79. return chatRequestSettings;
  80. }
  81. throw new ArgumentException($"Invalid request settings, cannot convert to {nameof(ChatRequestSettings)}", nameof(requestSettings));
  82. }
  83. private static readonly JsonSerializerOptions s_options = CreateOptions();
  84. private static JsonSerializerOptions CreateOptions()
  85. {
  86. JsonSerializerOptions options = new()
  87. {
  88. WriteIndented = true,
  89. MaxDepth = 20,
  90. AllowTrailingCommas = true,
  91. PropertyNameCaseInsensitive = true,
  92. ReadCommentHandling = JsonCommentHandling.Skip,
  93. Converters = { new ChatRequestSettingsConverter() }
  94. };
  95. return options;
  96. }
  97. }