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.

StatefulChatService.cs 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 
  2. using LLama.WebAPI.Models;
  3. using Microsoft;
  4. namespace LLama.WebAPI.Services;
  5. public class StatefulChatService : IDisposable
  6. {
  7. private readonly ChatSession _session;
  8. private readonly LLamaModel _model;
  9. private bool _continue = false;
  10. private const string SystemPrompt = "Transcript of a dialog, where the User interacts with an Assistant. Assistant is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.\n\n"
  11. + "User: ";
  12. public StatefulChatService(IConfiguration configuration)
  13. {
  14. _model = new LLamaModel(new Common.ModelParams(configuration["ModelPath"], contextSize: 512));
  15. _session = new ChatSession(new InteractiveExecutor(_model))
  16. ;//.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8));
  17. }
  18. public void Dispose()
  19. {
  20. _model?.Dispose();
  21. }
  22. public string Send(SendMessageInput input)
  23. {
  24. var userInput = input.Text;
  25. if (!_continue)
  26. {
  27. userInput = SystemPrompt + userInput;
  28. Console.Write(SystemPrompt);
  29. _continue = true;
  30. }
  31. Console.ForegroundColor = ConsoleColor.Green;
  32. Console.Write(input.Text);
  33. Console.ForegroundColor = ConsoleColor.White;
  34. var outputs = _session.Chat(userInput, new Common.InferenceParams()
  35. {
  36. RepeatPenalty = 1.0f,
  37. AntiPrompts = new string[] { "User:" },
  38. });
  39. var result = "";
  40. foreach (var output in outputs)
  41. {
  42. Console.Write(output);
  43. result += output;
  44. }
  45. return result;
  46. }
  47. }