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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. }
  17. public void Dispose()
  18. {
  19. _model?.Dispose();
  20. }
  21. public string Send(SendMessageInput input)
  22. {
  23. var userInput = input.Text;
  24. if (!_continue)
  25. {
  26. userInput = SystemPrompt + userInput;
  27. Console.Write(SystemPrompt);
  28. _continue = true;
  29. }
  30. Console.ForegroundColor = ConsoleColor.Green;
  31. Console.Write(input.Text);
  32. Console.ForegroundColor = ConsoleColor.White;
  33. var outputs = _session.Chat(userInput, new Common.InferenceParams()
  34. {
  35. RepeatPenalty = 1.0f,
  36. AntiPrompts = new string[] { "User:" },
  37. });
  38. var result = "";
  39. foreach (var output in outputs)
  40. {
  41. Console.Write(output);
  42. result += output;
  43. }
  44. return result;
  45. }
  46. }