Browse Source

add history chat example

tags/v0.4.2-preview
xbotter 2 years ago
parent
commit
16f2cb9c4e
No known key found for this signature in database GPG Key ID: D299220A7FE5CF1E
4 changed files with 69 additions and 8 deletions
  1. +15
    -5
      LLama.WebAPI/Controllers/ChatController.cs
  2. +10
    -0
      LLama.WebAPI/Models/SendMessageInput.cs
  3. +1
    -2
      LLama.WebAPI/Services/StatefulChatService.cs
  4. +43
    -1
      LLama.WebAPI/Services/StatelessChatService.cs

+ 15
- 5
LLama.WebAPI/Controllers/ChatController.cs View File

@@ -1,3 +1,4 @@
using LLama.Common;
using LLama.WebAPI.Models; using LLama.WebAPI.Models;
using LLama.WebAPI.Services; using LLama.WebAPI.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -9,20 +10,29 @@ namespace LLama.WebAPI.Controllers
[Route("[controller]")] [Route("[controller]")]
public class ChatController : ControllerBase public class ChatController : ControllerBase
{ {
private readonly StatefulChatService _service;
private readonly ILogger<ChatController> _logger; private readonly ILogger<ChatController> _logger;


public ChatController(ILogger<ChatController> logger,
StatefulChatService service)
public ChatController(ILogger<ChatController> logger)
{ {
_logger = logger; _logger = logger;
_service = service;
} }


[HttpPost("Send")] [HttpPost("Send")]
public string SendMessage([FromBody] SendMessageInput input)
public string SendMessage([FromBody] SendMessageInput input, [FromServices] StatefulChatService _service)
{ {
return _service.Send(input); 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);
}
} }
} }

+ 10
- 0
LLama.WebAPI/Models/SendMessageInput.cs View File

@@ -4,3 +4,13 @@ public class SendMessageInput
{ {
public string Text { get; set; } 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; }
}
}

+ 1
- 2
LLama.WebAPI/Services/StatefulChatService.cs View File

@@ -16,8 +16,7 @@ public class StatefulChatService : IDisposable
public StatefulChatService(IConfiguration configuration) public StatefulChatService(IConfiguration configuration)
{ {
_model = new LLamaModel(new Common.ModelParams(configuration["ModelPath"], contextSize: 512)); _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() public void Dispose()


+ 43
- 1
LLama.WebAPI/Services/StatelessChatService.cs 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 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:";
}

} }
} }

Loading…
Cancel
Save