84 lines
3.2 KiB
Java
84 lines
3.2 KiB
Java
package me.neurodock.glados.rest.controller;
|
|
|
|
import io.swagger.v3.oas.annotations.media.Content;
|
|
import io.swagger.v3.oas.annotations.media.Schema;
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|
import me.neurodock.backend.open.ai.OpenAIAssistentMessage;
|
|
import me.neurodock.glados.rest.APIApplication;
|
|
import me.neurodock.glados.rest.payloads.MessagePayload;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
@RestController
|
|
@RequestMapping("/v1/message")
|
|
public class Message {
|
|
private final APIApplication apiApplication;
|
|
|
|
public Message(APIApplication apiApplication) {
|
|
this.apiApplication = apiApplication;
|
|
}
|
|
|
|
@PostMapping("/send")
|
|
@ApiResponses(value = {
|
|
@ApiResponse(responseCode = "200", description = "Success",
|
|
content = @Content(
|
|
mediaType = "application/json",
|
|
schema = @Schema(oneOf = { String.class, MessagePayload.class })
|
|
)
|
|
)
|
|
})
|
|
public ResponseEntity<?> send(@RequestBody MessagePayload payload)
|
|
{
|
|
if(payload.stream()) {
|
|
APIApplication.getInstence().sendAndHandleMessageNonBlocking(payload.message());
|
|
return ResponseEntity.ok("success");
|
|
}
|
|
else {
|
|
me.neurodock.llm.Message message = APIApplication.getInstence().sendAndHandleMessage(payload.message(), payload.noVoice());
|
|
MessagePayload response;
|
|
if(message instanceof OpenAIAssistentMessage msg)
|
|
{
|
|
response = new MessagePayload(
|
|
(String) message.getContent(),
|
|
me.neurodock.llm.Message.Role.ASSISTANT,
|
|
true,
|
|
payload.noVoice(),
|
|
false,
|
|
msg.getEndReason());
|
|
}
|
|
else
|
|
{
|
|
response = new MessagePayload(
|
|
(String) message.getContent(),
|
|
me.neurodock.llm.Message.Role.ASSISTANT,
|
|
true,
|
|
payload.noVoice(),
|
|
false,
|
|
OpenAIAssistentMessage.Reason.NULL);
|
|
}
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
}
|
|
|
|
@GetMapping("/init")
|
|
@ApiResponses(value = {
|
|
@ApiResponse(responseCode = "200", description = "Return of all current messages")
|
|
})
|
|
public ResponseEntity<ArrayList<MessagePayload>> init()
|
|
{
|
|
ArrayList<MessagePayload> body = new ArrayList<>();
|
|
|
|
APIApplication.getInstence().getCore().getChatObject().getConversation().forEach(mess -> {
|
|
if (mess instanceof OpenAIAssistentMessage msg)
|
|
body.add(new MessagePayload((String) mess.getContent(), mess.getRole(), false, false, false, msg.getEndReason()));
|
|
else
|
|
body.add(new MessagePayload((String) mess.getContent(), mess.getRole(), false, false, false, OpenAIAssistentMessage.Reason.STOP));
|
|
});
|
|
|
|
return ResponseEntity.ok(body);
|
|
}
|
|
}
|