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

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