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.

LLamaSharpChatCompletion.cs 5.3 kB

📝 Update LLamaSharpChatCompletion and LLama.Unittest - Updated LLamaSharpChatCompletion class in LLama.SemanticKernel/ChatCompletion/LLamaSharpChatCompletion.cs - Changed the type of the "_model" field from "StatelessExecutor" to "ILLamaExecutor" - Updated the constructor to accept an "ILLamaExecutor" parameter instead of a "StatelessExecutor" parameter - Updated LLamaSharpChatCompletion class in LLama.SemanticKernel/LLamaSharp.SemanticKernel.csproj - Updated LLama.Unittest project in LLama.Unittest/LLama.Unittest.csproj - Added a "PackageReference" for "Moq" version 4.20.70 - Added ExtensionMethodsTests class in LLama.Unittest/SemanticKernel/ExtensionMethodsTests.cs - Added tests for the "ToLLamaSharpChatHistory" and "ToLLamaSharpInferenceParams" extension methods - Added LLamaSharpChatCompletionTests class in LLama.Unittest/SemanticKernel/LLamaSharpChatCompletionTests.cs - Added tests for the LLamaSharpChatCompletion class ℹ️ The LLamaSharpChatCompletion class in the LLama.SemanticKernel project has been updated to use the ILLamaExecutor interface instead of the StatelessExecutor class. This change allows for better abstraction and flexibility in the implementation of the LLamaSharpChatCompletion class. The LLamaSharpChatCompletion class is responsible for providing chat completion functionality in the LLamaSharp project. The LLama.Unittest project has also been updated to include tests for the LLamaSharpChatCompletion class and the extension methods used by the class.
1 year ago
📝 Update LLamaSharpChatCompletion and LLama.Unittest - Updated LLamaSharpChatCompletion class in LLama.SemanticKernel/ChatCompletion/LLamaSharpChatCompletion.cs - Changed the type of the "_model" field from "StatelessExecutor" to "ILLamaExecutor" - Updated the constructor to accept an "ILLamaExecutor" parameter instead of a "StatelessExecutor" parameter - Updated LLamaSharpChatCompletion class in LLama.SemanticKernel/LLamaSharp.SemanticKernel.csproj - Updated LLama.Unittest project in LLama.Unittest/LLama.Unittest.csproj - Added a "PackageReference" for "Moq" version 4.20.70 - Added ExtensionMethodsTests class in LLama.Unittest/SemanticKernel/ExtensionMethodsTests.cs - Added tests for the "ToLLamaSharpChatHistory" and "ToLLamaSharpInferenceParams" extension methods - Added LLamaSharpChatCompletionTests class in LLama.Unittest/SemanticKernel/LLamaSharpChatCompletionTests.cs - Added tests for the LLamaSharpChatCompletion class ℹ️ The LLamaSharpChatCompletion class in the LLama.SemanticKernel project has been updated to use the ILLamaExecutor interface instead of the StatelessExecutor class. This change allows for better abstraction and flexibility in the implementation of the LLamaSharpChatCompletion class. The LLamaSharpChatCompletion class is responsible for providing chat completion functionality in the LLamaSharp project. The LLama.Unittest project has also been updated to include tests for the LLamaSharpChatCompletion class and the extension methods used by the class.
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using LLama;
  2. using LLama.Abstractions;
  3. using Microsoft.SemanticKernel;
  4. using Microsoft.SemanticKernel.ChatCompletion;
  5. using Microsoft.SemanticKernel.Services;
  6. using System;
  7. using System.IO;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using static LLama.InteractiveExecutor;
  11. using static LLama.LLamaTransforms;
  12. namespace LLamaSharp.SemanticKernel.ChatCompletion;
  13. /// <summary>
  14. /// LLamaSharp ChatCompletion
  15. /// </summary>
  16. public sealed class LLamaSharpChatCompletion : IChatCompletionService
  17. {
  18. private readonly ILLamaExecutor _model;
  19. private LLamaSharpPromptExecutionSettings defaultRequestSettings;
  20. private readonly IHistoryTransform historyTransform;
  21. private readonly ITextStreamTransform outputTransform;
  22. private readonly Dictionary<string, object?> _attributes = new();
  23. private readonly bool _isStatefulExecutor;
  24. public IReadOnlyDictionary<string, object?> Attributes => this._attributes;
  25. static LLamaSharpPromptExecutionSettings GetDefaultSettings()
  26. {
  27. return new LLamaSharpPromptExecutionSettings
  28. {
  29. MaxTokens = 256,
  30. Temperature = 0,
  31. TopP = 0,
  32. StopSequences = new List<string>()
  33. };
  34. }
  35. public LLamaSharpChatCompletion(ILLamaExecutor model,
  36. LLamaSharpPromptExecutionSettings? defaultRequestSettings = default,
  37. IHistoryTransform? historyTransform = null,
  38. ITextStreamTransform? outputTransform = null)
  39. {
  40. this._model = model;
  41. this._isStatefulExecutor = this._model is StatefulExecutorBase;
  42. this.defaultRequestSettings = defaultRequestSettings ?? GetDefaultSettings();
  43. this.historyTransform = historyTransform ?? new HistoryTransform();
  44. this.outputTransform = outputTransform ?? new KeywordTextOutputStreamTransform(new[] { $"{LLama.Common.AuthorRole.User}:",
  45. $"{LLama.Common.AuthorRole.Assistant}:",
  46. $"{LLama.Common.AuthorRole.System}:"});
  47. }
  48. public ChatHistory CreateNewChat(string? instructions = "")
  49. {
  50. var history = new ChatHistory();
  51. if (instructions != null && !string.IsNullOrEmpty(instructions))
  52. {
  53. history.AddSystemMessage(instructions);
  54. }
  55. return history;
  56. }
  57. /// <inheritdoc/>
  58. public async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
  59. {
  60. var settings = executionSettings != null
  61. ? LLamaSharpPromptExecutionSettings.FromRequestSettings(executionSettings)
  62. : defaultRequestSettings;
  63. string prompt = this._getFormattedPrompt(chatHistory);
  64. var result = _model.InferAsync(prompt, settings.ToLLamaSharpInferenceParams(), cancellationToken);
  65. var output = outputTransform.TransformAsync(result);
  66. var sb = new StringBuilder();
  67. await foreach (var token in output)
  68. {
  69. sb.Append(token);
  70. }
  71. return new List<ChatMessageContent> { new(AuthorRole.Assistant, sb.ToString()) }.AsReadOnly();
  72. }
  73. /// <inheritdoc/>
  74. public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
  75. {
  76. var settings = executionSettings != null
  77. ? LLamaSharpPromptExecutionSettings.FromRequestSettings(executionSettings)
  78. : defaultRequestSettings;
  79. string prompt = this._getFormattedPrompt(chatHistory);
  80. var result = _model.InferAsync(prompt, settings.ToLLamaSharpInferenceParams(), cancellationToken);
  81. var output = outputTransform.TransformAsync(result);
  82. await foreach (var token in output)
  83. {
  84. yield return new StreamingChatMessageContent(AuthorRole.Assistant, token);
  85. }
  86. }
  87. /// <summary>
  88. /// Return either the entire formatted chatHistory or just the most recent message based on
  89. /// whether the model extends StatefulExecutorBase or not.
  90. /// </summary>
  91. /// <param name="chatHistory"></param>
  92. /// <returns>The formatted prompt</returns>
  93. private string _getFormattedPrompt(ChatHistory chatHistory){
  94. string prompt;
  95. if (this._isStatefulExecutor){
  96. InteractiveExecutorState state = (InteractiveExecutorState)((StatefulExecutorBase)this._model).GetStateData();
  97. if (state.IsPromptRun)
  98. {
  99. prompt = historyTransform.HistoryToText(chatHistory.ToLLamaSharpChatHistory());
  100. }
  101. else
  102. {
  103. ChatHistory temp_history = new();
  104. temp_history.AddUserMessage(chatHistory.Last().Content);
  105. prompt = historyTransform.HistoryToText(temp_history.ToLLamaSharpChatHistory());
  106. }
  107. }
  108. else
  109. {
  110. prompt = historyTransform.HistoryToText(chatHistory.ToLLamaSharpChatHistory());
  111. }
  112. return prompt;
  113. }
  114. }