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.

LLamaSharpTextCompletion.cs 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637
  1. using LLama.Abstractions;
  2. using LLamaSharp.SemanticKernel.ChatCompletion;
  3. using Microsoft.SemanticKernel.AI;
  4. using Microsoft.SemanticKernel.AI.TextCompletion;
  5. using System.Runtime.CompilerServices;
  6. namespace LLamaSharp.SemanticKernel.TextCompletion;
  7. public sealed class LLamaSharpTextCompletion : ITextCompletion
  8. {
  9. public ILLamaExecutor executor;
  10. private readonly Dictionary<string, string> _attributes = new();
  11. public IReadOnlyDictionary<string, string> Attributes => this._attributes;
  12. public LLamaSharpTextCompletion(ILLamaExecutor executor)
  13. {
  14. this.executor = executor;
  15. }
  16. public async Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(string text, AIRequestSettings? requestSettings, CancellationToken cancellationToken = default)
  17. {
  18. var settings = ChatRequestSettings.FromRequestSettings(requestSettings);
  19. var result = executor.InferAsync(text, settings?.ToLLamaSharpInferenceParams(), cancellationToken);
  20. return await Task.FromResult(new List<ITextResult> { new LLamaTextResult(result) }.AsReadOnly()).ConfigureAwait(false);
  21. }
  22. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously.
  23. public async IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(string text, AIRequestSettings? requestSettings,[EnumeratorCancellation] CancellationToken cancellationToken = default)
  24. #pragma warning restore CS1998
  25. {
  26. var settings = ChatRequestSettings.FromRequestSettings(requestSettings);
  27. var result = executor.InferAsync(text, settings?.ToLLamaSharpInferenceParams(), cancellationToken);
  28. yield return new LLamaTextResult(result);
  29. }
  30. }