Browse Source

add stream example

tags/v0.4.2-preview
xbotter 3 years ago
parent
commit
a074385568
No known key found for this signature in database GPG Key ID: D299220A7FE5CF1E
2 changed files with 42 additions and 0 deletions
  1. +15
    -0
      LLama.WebAPI/Controllers/ChatController.cs
  2. +27
    -0
      LLama.WebAPI/Services/StatefulChatService.cs

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

@@ -23,6 +23,21 @@ namespace LLama.WebAPI.Controllers
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)
{


+ 27
- 0
LLama.WebAPI/Services/StatefulChatService.cs View File

@@ -1,6 +1,7 @@

using LLama.WebAPI.Models;
using Microsoft;
using System.Runtime.CompilerServices;

namespace LLama.WebAPI.Services;

@@ -52,4 +53,30 @@ public class StatefulChatService : IDisposable

return result;
}

public async IAsyncEnumerable<string> SendStream(SendMessageInput input)
{
var userInput = input.Text;
if (!_continue)
{
userInput = SystemPrompt + userInput;
Console.Write(SystemPrompt);
_continue = true;
}

Console.ForegroundColor = ConsoleColor.Green;
Console.Write(input.Text);

Console.ForegroundColor = ConsoleColor.White;
var outputs = _session.ChatAsync(userInput, new Common.InferenceParams()
{
RepeatPenalty = 1.0f,
AntiPrompts = new string[] { "User:" },
});
await foreach (var output in outputs)
{
Console.Write(output);
yield return output;
}
}
}

Loading…
Cancel
Save