2023-05-27 01:58:31 +00:00
|
|
|
using BotSharp.Abstraction.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Net.Http.Headers;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System;
|
|
|
|
|
using Azure.AI.OpenAI;
|
2023-06-07 00:59:32 +00:00
|
|
|
using BotSharp.Abstraction.ApiAdapters;
|
|
|
|
|
using BotSharp.Plugin.ChatbotUI.ViewModels;
|
2023-06-10 02:56:04 +00:00
|
|
|
using BotSharp.Abstraction.TextGeneratives;
|
2023-05-27 01:58:31 +00:00
|
|
|
|
2023-06-07 00:59:32 +00:00
|
|
|
namespace BotSharp.Plugin.ChatbotUI.Controllers;
|
2023-05-27 01:58:31 +00:00
|
|
|
|
|
|
|
|
[ApiController]
|
2023-06-07 00:59:32 +00:00
|
|
|
public class ChatbotUiController : ControllerBase, IApiAdapter
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
|
|
|
|
private readonly ILogger<ChatbotUiController> _logger;
|
2023-06-10 02:56:04 +00:00
|
|
|
private readonly IChatCompletionProvider _chatCompletionProvider;
|
2023-05-27 01:58:31 +00:00
|
|
|
|
|
|
|
|
public ChatbotUiController(ILogger<ChatbotUiController> logger,
|
2023-06-10 02:56:04 +00:00
|
|
|
IChatCompletionProvider chatCompletionProvider)
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2023-06-10 02:56:04 +00:00
|
|
|
_chatCompletionProvider = chatCompletionProvider;
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("/v1/models")]
|
|
|
|
|
public OpenAiModels GetOpenAiModels()
|
|
|
|
|
{
|
|
|
|
|
return new OpenAiModels
|
|
|
|
|
{
|
|
|
|
|
Data = new List<AiModel>
|
|
|
|
|
{
|
|
|
|
|
new AiModel
|
|
|
|
|
{
|
|
|
|
|
Id = "gpt-3.5-turbo",
|
|
|
|
|
Model = "gpt-3.5-turbo",
|
|
|
|
|
Name = "Default (GPT-3.5)",
|
|
|
|
|
MaxLength = 4000,
|
|
|
|
|
TokenLimit = 4000
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/v1/chat/completions")]
|
2023-05-29 01:06:05 +00:00
|
|
|
public async Task SendMessage([FromBody] OpenAiMessageInput input)
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
|
|
|
|
Response.StatusCode = 200;
|
|
|
|
|
Response.Headers.Add(HeaderNames.ContentType, "text/event-stream");
|
|
|
|
|
Response.Headers.Add(HeaderNames.CacheControl, "no-cache");
|
|
|
|
|
Response.Headers.Add(HeaderNames.Connection, "keep-alive");
|
|
|
|
|
var outputStream = Response.Body;
|
|
|
|
|
|
2023-05-29 01:06:05 +00:00
|
|
|
var conversations = input.Messages.Skip(1).Select(x => new RoleDialogModel
|
|
|
|
|
{
|
|
|
|
|
Role = x.Role,
|
|
|
|
|
Content = x.Content
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
2023-06-10 02:56:04 +00:00
|
|
|
await _chatCompletionProvider.GetChatCompletionsAsync(conversations,
|
2023-05-28 16:30:27 +00:00
|
|
|
async content =>
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
|
|
|
|
await OnChunkReceived(outputStream, content);
|
2023-05-28 16:30:27 +00:00
|
|
|
});
|
2023-05-29 01:06:05 +00:00
|
|
|
|
|
|
|
|
await OnEventCompleted(outputStream);
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task OnChunkReceived(Stream outputStream, string content)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var response = new OpenAiChatOutput
|
|
|
|
|
{
|
|
|
|
|
Choices = new List<OpenAiChoice>
|
|
|
|
|
{
|
|
|
|
|
new OpenAiChoice
|
|
|
|
|
{
|
|
|
|
|
Delta = new ChatMessage(ChatRole.Assistant, content)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var json = JsonConvert.SerializeObject(response, new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
Formatting = Formatting.None,
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var buffer = Encoding.UTF8.GetBytes($"data:{json}\n");
|
|
|
|
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
|
|
|
|
|
buffer = Encoding.UTF8.GetBytes("\n");
|
|
|
|
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task OnEventCompleted(Stream outputStream)
|
|
|
|
|
{
|
|
|
|
|
var buffer = Encoding.UTF8.GetBytes("data:[DONE]\n");
|
|
|
|
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
buffer = Encoding.UTF8.GetBytes("\n");
|
|
|
|
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
await outputStream.FlushAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|