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.

ExtensionMethods.cs 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Microsoft.SemanticKernel.ChatCompletion;
  2. namespace LLamaSharp.SemanticKernel;
  3. public static class ExtensionMethods
  4. {
  5. public static global::LLama.Common.ChatHistory ToLLamaSharpChatHistory(this ChatHistory chatHistory, bool ignoreCase = true)
  6. {
  7. if (chatHistory is null)
  8. {
  9. throw new ArgumentNullException(nameof(chatHistory));
  10. }
  11. var history = new global::LLama.Common.ChatHistory();
  12. foreach (var chat in chatHistory)
  13. {
  14. var role = Enum.TryParse<global::LLama.Common.AuthorRole>(chat.Role.Label, ignoreCase, out var _role) ? _role : global::LLama.Common.AuthorRole.Unknown;
  15. history.AddMessage(role, chat.Content);
  16. }
  17. return history;
  18. }
  19. /// <summary>
  20. /// Convert LLamaSharpPromptExecutionSettings to LLamaSharp InferenceParams
  21. /// </summary>
  22. /// <param name="requestSettings"></param>
  23. /// <returns></returns>
  24. internal static global::LLama.Common.InferenceParams ToLLamaSharpInferenceParams(this LLamaSharpPromptExecutionSettings requestSettings)
  25. {
  26. if (requestSettings is null)
  27. {
  28. throw new ArgumentNullException(nameof(requestSettings));
  29. }
  30. var antiPrompts = new List<string>(requestSettings.StopSequences)
  31. { LLama.Common.AuthorRole.User.ToString() + ":" ,
  32. LLama.Common.AuthorRole.Assistant.ToString() + ":",
  33. LLama.Common.AuthorRole.System.ToString() + ":"};
  34. return new global::LLama.Common.InferenceParams
  35. {
  36. Temperature = (float)requestSettings.Temperature,
  37. TopP = (float)requestSettings.TopP,
  38. PresencePenalty = (float)requestSettings.PresencePenalty,
  39. FrequencyPenalty = (float)requestSettings.FrequencyPenalty,
  40. AntiPrompts = antiPrompts,
  41. MaxTokens = requestSettings.MaxTokens ?? -1
  42. };
  43. }
  44. }