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.

StatelessChatService.cs 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using LLama.Common;
  2. using Microsoft.AspNetCore.Http;
  3. using System.Text;
  4. using static LLama.LLamaTransforms;
  5. namespace LLama.WebAPI.Services
  6. {
  7. public class StatelessChatService
  8. {
  9. private readonly LLamaContext _context;
  10. private readonly ChatSession _session;
  11. public StatelessChatService(IConfiguration configuration)
  12. {
  13. var @params = new Common.ModelParams(configuration["ModelPath"]!)
  14. {
  15. ContextSize = 512,
  16. };
  17. // todo: share weights from a central service
  18. using var weights = LLamaWeights.LoadFromFile(@params);
  19. _context = new LLamaContext(weights, @params);
  20. // TODO: replace with a stateless executor
  21. _session = new ChatSession(new InteractiveExecutor(_context))
  22. .WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8))
  23. .WithHistoryTransform(new HistoryTransform());
  24. }
  25. public async Task<string> SendAsync(ChatHistory history)
  26. {
  27. var result = _session.ChatAsync(history, new InferenceParams()
  28. {
  29. AntiPrompts = new string[] { "User:" },
  30. });
  31. var sb = new StringBuilder();
  32. await foreach (var r in result)
  33. {
  34. Console.Write(r);
  35. sb.Append(r);
  36. }
  37. return sb.ToString();
  38. }
  39. }
  40. public class HistoryTransform : DefaultHistoryTransform
  41. {
  42. public override string HistoryToText(ChatHistory history)
  43. {
  44. return base.HistoryToText(history) + "\n Assistant:";
  45. }
  46. }
  47. }