diff --git a/LLama.WebAPI/Controllers/ChatController.cs b/LLama.WebAPI/Controllers/ChatController.cs index 1953d2af..dab8595d 100644 --- a/LLama.WebAPI/Controllers/ChatController.cs +++ b/LLama.WebAPI/Controllers/ChatController.cs @@ -1,3 +1,4 @@ +using LLama.Common; using LLama.WebAPI.Models; using LLama.WebAPI.Services; using Microsoft.AspNetCore.Mvc; @@ -9,20 +10,29 @@ namespace LLama.WebAPI.Controllers [Route("[controller]")] public class ChatController : ControllerBase { - private readonly StatefulChatService _service; private readonly ILogger _logger; - public ChatController(ILogger logger, - StatefulChatService service) + public ChatController(ILogger logger) { _logger = logger; - _service = service; } [HttpPost("Send")] - public string SendMessage([FromBody] SendMessageInput input) + public string SendMessage([FromBody] SendMessageInput input, [FromServices] StatefulChatService _service) { return _service.Send(input); } + + [HttpPost("History")] + public async Task SendHistory([FromBody] HistoryInput input, [FromServices] StatelessChatService _service) + { + var history = new ChatHistory(); + + var messages = input.Messages.Select(m => new ChatHistory.Message(Enum.Parse(m.Role), m.Content)); + + history.Messages.AddRange(messages); + + return await _service.SendAsync(history); + } } } \ No newline at end of file diff --git a/LLama.WebAPI/Models/SendMessageInput.cs b/LLama.WebAPI/Models/SendMessageInput.cs index 741b631f..11152ff8 100644 --- a/LLama.WebAPI/Models/SendMessageInput.cs +++ b/LLama.WebAPI/Models/SendMessageInput.cs @@ -4,3 +4,13 @@ public class SendMessageInput { public string Text { get; set; } } + +public class HistoryInput +{ + public List Messages { get; set; } + public class HistoryItem + { + public string Role { get; set; } + public string Content { get; set; } + } +} \ No newline at end of file diff --git a/LLama.WebAPI/Services/StatefulChatService.cs b/LLama.WebAPI/Services/StatefulChatService.cs index 9c06e8b5..f7afe1a0 100644 --- a/LLama.WebAPI/Services/StatefulChatService.cs +++ b/LLama.WebAPI/Services/StatefulChatService.cs @@ -16,8 +16,7 @@ public class StatefulChatService : IDisposable public StatefulChatService(IConfiguration configuration) { _model = new LLamaModel(new Common.ModelParams(configuration["ModelPath"], contextSize: 512)); - _session = new ChatSession(new InteractiveExecutor(_model)) - ;//.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8)); + _session = new ChatSession(new InteractiveExecutor(_model)); } public void Dispose() diff --git a/LLama.WebAPI/Services/StatelessChatService.cs b/LLama.WebAPI/Services/StatelessChatService.cs index c87ae61f..c1356646 100644 --- a/LLama.WebAPI/Services/StatelessChatService.cs +++ b/LLama.WebAPI/Services/StatelessChatService.cs @@ -1,6 +1,48 @@ -namespace LLama.WebAPI.Services +using LLama.Common; +using Microsoft.AspNetCore.Http; +using System.Text; +using static LLama.LLamaTransforms; + +namespace LLama.WebAPI.Services { public class StatelessChatService { + private readonly LLamaModel _model; + private readonly ChatSession _session; + + public StatelessChatService(IConfiguration configuration) + { + _model = new LLamaModel(new ModelParams(configuration["ModelPath"], contextSize: 512)); + // TODO: replace with a stateless executor + _session = new ChatSession(new InteractiveExecutor(_model)) + .WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8)) + .WithHistoryTransform(new HistoryTransform()); + } + + public async Task SendAsync(ChatHistory history) + { + var result = _session.ChatAsync(history, new InferenceParams() + { + AntiPrompts = new string[] { "User:" }, + }); + + var sb = new StringBuilder(); + await foreach (var r in result) + { + Console.Write(r); + sb.Append(r); + } + + return sb.ToString(); + + } + } + public class HistoryTransform : DefaultHistoryTransform + { + public override string HistoryToText(ChatHistory history) + { + return base.HistoryToText(history) + "\n Assistant:"; + } + } }