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.5 kB

123456789101112131415161718192021222324252627282930313233
  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. public LLamaSharpTextCompletion(ILLamaExecutor executor)
  11. {
  12. this.executor = executor;
  13. }
  14. public async Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(string text, AIRequestSettings? requestSettings, CancellationToken cancellationToken = default)
  15. {
  16. var settings = (ChatRequestSettings?)requestSettings;
  17. var result = executor.InferAsync(text, settings?.ToLLamaSharpInferenceParams(), cancellationToken);
  18. return await Task.FromResult(new List<ITextResult> { new LLamaTextResult(result) }.AsReadOnly()).ConfigureAwait(false);
  19. }
  20. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously.
  21. public async IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(string text, AIRequestSettings? requestSettings,[EnumeratorCancellation] CancellationToken cancellationToken = default)
  22. #pragma warning restore CS1998
  23. {
  24. var settings = (ChatRequestSettings?)requestSettings;
  25. var result = executor.InferAsync(text, settings?.ToLLamaSharpInferenceParams(), cancellationToken);
  26. yield return new LLamaTextResult(result);
  27. }
  28. }