-
Notifications
You must be signed in to change notification settings - Fork 414
/
Copy pathChatController.cs
53 lines (42 loc) · 1.61 KB
/
ChatController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using LLama.Common;
using LLama.WebAPI.Models;
using LLama.WebAPI.Services;
using Microsoft.AspNetCore.Mvc;
using System;
namespace LLama.WebAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class ChatController : ControllerBase
{
private readonly ILogger<ChatController> _logger;
public ChatController(ILogger<ChatController> logger)
{
_logger = logger;
}
[HttpPost("Send")]
public Task<string> SendMessage([FromBody] SendMessageInput input, [FromServices] StatefulChatService _service)
{
return _service.Send(input);
}
[HttpPost("Send/Stream")]
public async Task SendMessageStream([FromBody] SendMessageInput input, [FromServices] StatefulChatService _service, CancellationToken cancellationToken)
{
Response.ContentType = "text/event-stream";
await foreach (var r in _service.SendStream(input))
{
await Response.WriteAsync("data:" + r + "\n\n", cancellationToken);
await Response.Body.FlushAsync(cancellationToken);
}
await Response.CompleteAsync();
}
[HttpPost("History")]
public async Task<string> SendHistory([FromBody] HistoryInput input, [FromServices] StatelessChatService _service)
{
var history = new ChatHistory();
var messages = input.Messages.Select(m => new Message(Enum.Parse<AuthorRole>(m.Role), m.Content));
history.Messages.AddRange(messages);
return await _service.SendAsync(history);
}
}
}