#AI
zhou zhou
3 天以前 e6369c9b64a82eeada6b6f0658d2ae786787e101
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
            }
        };
    }
 
}