add history chat example

This commit is contained in:
xbotter 2023-06-24 19:43:32 +08:00
parent 2fe4ccfb58
commit 16f2cb9c4e
No known key found for this signature in database
GPG Key ID: D299220A7FE5CF1E
4 changed files with 69 additions and 8 deletions

View File

@ -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<ChatController> _logger;
public ChatController(ILogger<ChatController> logger,
StatefulChatService service)
public ChatController(ILogger<ChatController> 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<string> SendHistory([FromBody] HistoryInput input, [FromServices] StatelessChatService _service)
{
var history = new ChatHistory();
var messages = input.Messages.Select(m => new ChatHistory.Message(Enum.Parse<AuthorRole>(m.Role), m.Content));
history.Messages.AddRange(messages);
return await _service.SendAsync(history);
}
}
}

View File

@ -4,3 +4,13 @@ public class SendMessageInput
{
public string Text { get; set; }
}
public class HistoryInput
{
public List<HistoryItem> Messages { get; set; }
public class HistoryItem
{
public string Role { get; set; }
public string Content { get; set; }
}
}

View File

@ -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()

View File

@ -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<string> 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:";
}
}
}