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

164 lines
6.8 KiB
C#
Raw Normal View History

2023-12-12 18:37:49 +00:00
using BotSharp.Abstraction.Agents.Models;
2023-09-01 22:26:25 +00:00
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Instructs.Models;
2023-10-13 20:08:59 +00:00
using BotSharp.Core.Infrastructures;
2023-09-07 22:06:41 +00:00
using BotSharp.OpenAPI.ViewModels.Instructs;
2024-06-26 03:38:01 +00:00
using NetTopologySuite.IO;
2023-09-01 22:26:25 +00:00
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
2023-12-27 15:53:54 +00:00
public class InstructModeController : ControllerBase
2023-09-01 22:26:25 +00:00
{
private readonly IServiceProvider _services;
2024-05-14 16:51:39 +00:00
private readonly ILogger<InstructModeController> _logger;
2023-09-01 22:26:25 +00:00
2024-05-14 16:51:39 +00:00
public InstructModeController(IServiceProvider services, ILogger<InstructModeController> logger)
2023-09-01 22:26:25 +00:00
{
_services = services;
2024-05-14 16:51:39 +00:00
_logger = logger;
2023-09-01 22:26:25 +00:00
}
[HttpPost("/instruct/{agentId}")]
2023-09-19 18:05:51 +00:00
public async Task<InstructResult> InstructCompletion([FromRoute] string agentId,
2023-09-07 22:06:41 +00:00
[FromBody] InstructMessageModel input)
2023-09-01 22:26:25 +00:00
{
2023-10-23 23:20:18 +00:00
var state = _services.GetRequiredService<IConversationStateService>();
2024-04-05 18:53:37 +00:00
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
state.SetState("provider", input.Provider, source: StateSource.External)
.SetState("model", input.Model, source: StateSource.External)
.SetState("model_id", input.ModelId, source: StateSource.External)
.SetState("instruction", input.Instruction, source: StateSource.External)
.SetState("input_text", input.Text,source: StateSource.External);
2023-10-23 23:20:18 +00:00
var instructor = _services.GetRequiredService<IInstructService>();
2023-10-28 20:59:26 +00:00
var result = await instructor.Execute(agentId,
new RoleDialogModel(AgentRole.User, input.Text),
2023-12-15 17:54:44 +00:00
templateName: input.Template,
instruction: input.Instruction);
2023-10-25 00:36:02 +00:00
result.States = state.GetStates();
return result;
2023-09-01 22:26:25 +00:00
}
2023-10-13 20:08:59 +00:00
[HttpPost("/instruct/text-completion")]
2023-10-23 00:37:33 +00:00
public async Task<string> TextCompletion([FromBody] IncomingMessageModel input)
2023-10-13 20:08:59 +00:00
{
2023-10-23 23:20:18 +00:00
var state = _services.GetRequiredService<IConversationStateService>();
2024-04-05 18:53:37 +00:00
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
state.SetState("provider", input.Provider, source: StateSource.External)
.SetState("model", input.Model, source: StateSource.External)
.SetState("model_id", input.ModelId, source: StateSource.External);
2023-10-23 00:37:33 +00:00
2023-10-13 20:08:59 +00:00
var textCompletion = CompletionProvider.GetTextCompletion(_services);
2023-12-05 00:12:57 +00:00
return await textCompletion.GetCompletion(input.Text, Guid.Empty.ToString(), Guid.NewGuid().ToString());
2023-10-13 20:08:59 +00:00
}
2023-12-12 18:32:04 +00:00
[HttpPost("/instruct/chat-completion")]
public async Task<string> ChatCompletion([FromBody] IncomingMessageModel input)
{
var state = _services.GetRequiredService<IConversationStateService>();
2024-04-05 18:53:37 +00:00
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
state.SetState("provider", input.Provider, source: StateSource.External)
.SetState("model", input.Model, source: StateSource.External)
.SetState("model_id", input.ModelId, source: StateSource.External);
2023-12-12 18:32:04 +00:00
var textCompletion = CompletionProvider.GetChatCompletion(_services);
2024-01-14 04:48:26 +00:00
var message = await textCompletion.GetChatCompletions(new Agent()
2023-12-12 18:32:04 +00:00
{
Id = Guid.Empty.ToString(),
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, input.Text)
2024-01-14 04:48:26 +00:00
});
return message.Content;
2023-12-12 18:32:04 +00:00
}
2024-05-14 16:51:39 +00:00
[HttpPost("/instruct/multi-modal")]
public async Task<string> MultiModalCompletion([FromBody] IncomingMessageModel input)
{
var state = _services.GetRequiredService<IConversationStateService>();
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
try
{
2024-05-17 16:35:20 +00:00
var completion = CompletionProvider.GetChatCompletion(_services, provider: input.Provider ?? "openai",
modelId: input.ModelId ?? "gpt-4", multiModal: true);
2024-05-14 16:51:39 +00:00
var message = await completion.GetChatCompletions(new Agent()
{
Id = Guid.Empty.ToString(),
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, input.Text)
{
Files = input.Files
}
});
return message.Content;
}
catch (Exception ex)
{
2024-06-24 19:32:52 +00:00
var error = $"Error in analyzing files. {ex.Message}";
_logger.LogError(error);
return error;
2024-05-14 16:51:39 +00:00
}
}
2024-06-24 18:03:05 +00:00
[HttpPost("/instruct/image-generation")]
public async Task<ImageGenerationViewModel> ImageGeneration([FromBody] IncomingMessageModel input)
{
var state = _services.GetRequiredService<IConversationStateService>();
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
var imageViewModel = new ImageGenerationViewModel();
try
{
2024-06-24 19:32:52 +00:00
var completion = CompletionProvider.GetImageGeneration(_services, provider: input.Provider ?? "openai",
modelId: input.ModelId ?? "dall-e", imageGenerate: true);
2024-06-24 18:03:05 +00:00
var message = await completion.GetImageGeneration(new Agent()
{
Id = Guid.Empty.ToString(),
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, input.Text)
});
2024-07-01 16:03:42 +00:00
imageViewModel.Content = message.Content;
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
2024-06-24 18:03:05 +00:00
return imageViewModel;
}
catch (Exception ex)
{
2024-06-24 19:32:52 +00:00
var error = $"Error in image generation. {ex.Message}";
_logger.LogError(error);
2024-06-24 18:03:05 +00:00
imageViewModel.Message = error;
return imageViewModel;
}
}
2024-07-01 19:31:48 +00:00
[HttpPost("/instruct/pdf-completion")]
public async Task<PdfCompletionViewModel> PdfCompletion([FromBody] IncomingMessageModel input)
{
var state = _services.GetRequiredService<IConversationStateService>();
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
var viewModel = new PdfCompletionViewModel();
try
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
2024-07-01 19:38:07 +00:00
var content = await fileService.InstructPdf(input.Provider, input.Model, input.ModelId, input.Text, input.Files);
2024-07-01 19:31:48 +00:00
viewModel.Content = content;
return viewModel;
}
catch (Exception ex)
{
var error = $"Error in pdf completion. {ex.Message}";
_logger.LogError(error);
viewModel.Message = error;
return viewModel;
}
}
2023-09-01 22:26:25 +00:00
}