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.

LLamaSharpChatCompletion.cs 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using LLama;
  2. using Microsoft.SemanticKernel.AI.ChatCompletion;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace LLamaSharp.SemanticKernel.ChatCompletion;
  12. /// <summary>
  13. /// LLamaSharp ChatCompletion
  14. /// </summary>
  15. public sealed class LLamaSharpChatCompletion : IChatCompletion
  16. {
  17. private const string UserRole = "user:";
  18. private const string AssistantRole = "assistant:";
  19. private ChatSession session;
  20. public LLamaSharpChatCompletion(InteractiveExecutor model)
  21. {
  22. this.session = new ChatSession(model)
  23. .WithHistoryTransform(new HistoryTransform())
  24. .WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { UserRole, AssistantRole }));
  25. }
  26. /// <inheritdoc/>
  27. public ChatHistory CreateNewChat(string? instructions = "")
  28. {
  29. var history = new ChatHistory();
  30. if (instructions != null && !string.IsNullOrEmpty(instructions))
  31. {
  32. history.AddSystemMessage(instructions);
  33. }
  34. return history;
  35. }
  36. /// <inheritdoc/>
  37. public async Task<IReadOnlyList<IChatResult>> GetChatCompletionsAsync(ChatHistory chat, ChatRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
  38. {
  39. requestSettings ??= new ChatRequestSettings()
  40. {
  41. MaxTokens = 256,
  42. Temperature = 0,
  43. TopP = 0,
  44. StopSequences = new List<string> { }
  45. };
  46. var result = this.session.ChatAsync(chat.ToLLamaSharpChatHistory(), requestSettings.ToLLamaSharpInferenceParams(), cancellationToken);
  47. return new List<IChatResult> { new LLamaSharpChatResult(result) }.AsReadOnly();
  48. }
  49. /// <inheritdoc/>
  50. public async IAsyncEnumerable<IChatStreamingResult> GetStreamingChatCompletionsAsync(ChatHistory chat, ChatRequestSettings? requestSettings = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
  51. {
  52. requestSettings ??= new ChatRequestSettings()
  53. {
  54. MaxTokens = 256,
  55. Temperature = 0,
  56. TopP = 0,
  57. StopSequences = new List<string> { }
  58. };
  59. var result = this.session.ChatAsync(chat.ToLLamaSharpChatHistory(), requestSettings.ToLLamaSharpInferenceParams(), cancellationToken);
  60. yield return new LLamaSharpChatResult(result);
  61. }
  62. }