BotSharp/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

81 lines
2.6 KiB
C#
Raw Normal View History

2023-08-19 00:15:02 +00:00
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Models;
2023-10-27 15:18:48 +00:00
using BotSharp.Abstraction.Models;
2023-08-19 00:15:02 +00:00
using BotSharp.OpenAPI.ViewModels.Conversations;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class ConversationController : ControllerBase, IApiAdapter
{
private readonly IServiceProvider _services;
private readonly IUserIdentity _user;
public ConversationController(IServiceProvider services,
2023-08-19 00:15:02 +00:00
IUserIdentity user)
{
_services = services;
_user = user;
}
[HttpPost("/conversation/{agentId}")]
2023-10-27 15:18:48 +00:00
public async Task<ConversationViewModel> NewConversation([FromRoute] string agentId, [FromBody] MessageConfig config)
2023-08-19 00:15:02 +00:00
{
var service = _services.GetRequiredService<IConversationService>();
2023-10-27 15:18:48 +00:00
var conv = new Conversation
2023-08-19 00:15:02 +00:00
{
AgentId = agentId
};
2023-10-27 15:18:48 +00:00
conv = await service.NewConversation(conv);
config.States.ForEach(x => conv.States[x.Split('=')[0]] = x.Split('=')[1]);
return ConversationViewModel.FromSession(conv);
2023-08-19 00:15:02 +00:00
}
[HttpDelete("/conversation/{agentId}/{conversationId}")]
public async Task DeleteConversation([FromRoute] string agentId, [FromRoute] string conversationId)
{
var service = _services.GetRequiredService<IConversationService>();
}
[HttpPost("/conversation/{agentId}/{conversationId}")]
public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
[FromRoute] string conversationId,
2023-09-09 15:37:38 +00:00
[FromBody] NewMessageModel input)
2023-08-19 00:15:02 +00:00
{
var conv = _services.GetRequiredService<IConversationService>();
2023-09-14 01:41:51 +00:00
conv.SetConversationId(conversationId, input.States);
2023-09-14 16:42:48 +00:00
conv.States.SetState("channel", input.Channel)
2023-09-18 08:35:02 +00:00
.SetState("provider", input.Provider)
.SetState("model", input.Model)
.SetState("temperature", input.Temperature)
.SetState("sampling_factor", input.SamplingFactor);
2023-09-14 01:41:51 +00:00
2023-08-19 00:15:02 +00:00
var response = new MessageResponseModel();
2023-10-27 15:18:48 +00:00
var inputMsg = new RoleDialogModel("user", input.Text);
await conv.SendMessage(agentId, inputMsg,
2023-08-19 00:15:02 +00:00
async msg =>
{
},
async fnExecuting =>
{
},
async fnExecuted =>
{
});
2023-08-19 00:15:02 +00:00
2023-10-27 15:18:48 +00:00
response.MessageId = inputMsg.MessageId;
response.Text = inputMsg.Content;
response.Data = inputMsg.Data;
response.Function = inputMsg.FunctionName;
response.Instruction = inputMsg.Instruction;
response.RichContent = inputMsg.RichContent;
2023-09-22 20:38:58 +00:00
2023-08-19 00:15:02 +00:00
return response;
}
}