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

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