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

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