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

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