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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using LLama.Abstractions;
  2. using LLamaSharp.SemanticKernel.ChatCompletion;
  3. using Microsoft.SemanticKernel;
  4. using Microsoft.SemanticKernel.Services;
  5. using Microsoft.SemanticKernel.TextGeneration;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. namespace LLamaSharp.SemanticKernel.TextCompletion;
  9. public sealed class LLamaSharpTextCompletion : ITextGenerationService
  10. {
  11. public ILLamaExecutor executor;
  12. private readonly Dictionary<string, object?> _attributes = new();
  13. public IReadOnlyDictionary<string, object?> Attributes => this._attributes;
  14. public LLamaSharpTextCompletion(ILLamaExecutor executor)
  15. {
  16. this.executor = executor;
  17. }
  18. /// <inheritdoc/>
  19. public async Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
  20. {
  21. var settings = ChatRequestSettings.FromRequestSettings(executionSettings);
  22. var result = executor.InferAsync(prompt, settings?.ToLLamaSharpInferenceParams(), cancellationToken);
  23. var sb = new StringBuilder();
  24. await foreach (var token in result)
  25. {
  26. sb.Append(token);
  27. }
  28. return new List<TextContent> { new(sb.ToString()) };
  29. }
  30. /// <inheritdoc/>
  31. public async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
  32. {
  33. var settings = ChatRequestSettings.FromRequestSettings(executionSettings);
  34. var result = executor.InferAsync(prompt, settings?.ToLLamaSharpInferenceParams(), cancellationToken);
  35. await foreach (var token in result)
  36. {
  37. yield return new StreamingTextContent(token);
  38. }
  39. }
  40. }