package com.vincent.rsf.ai.gateway.controller;
|
|
import com.vincent.rsf.ai.gateway.dto.GatewayChatRequest;
|
import com.vincent.rsf.ai.gateway.service.AiGatewayService;
|
import com.vincent.rsf.ai.gateway.service.GatewayStreamEvent;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import org.springframework.http.MediaType;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
|
import javax.annotation.Resource;
|
import java.io.IOException;
|
import java.nio.charset.StandardCharsets;
|
|
@RestController
|
@RequestMapping("/internal/chat")
|
public class AiGatewayController {
|
|
@Resource
|
private AiGatewayService aiGatewayService;
|
@Resource
|
private ObjectMapper objectMapper;
|
|
@PostMapping(value = "/stream", produces = "application/x-ndjson")
|
public StreamingResponseBody stream(@RequestBody GatewayChatRequest request) {
|
return outputStream -> {
|
try {
|
aiGatewayService.stream(request, event -> {
|
String json = objectMapper.writeValueAsString(event) + "\n";
|
outputStream.write(json.getBytes(StandardCharsets.UTF_8));
|
outputStream.flush();
|
});
|
} catch (Exception e) {
|
throw new IOException(e);
|
}
|
};
|
}
|
|
}
|