mirror of
https://github.com/ccmjga/zhilu-admin
synced 2026-03-14 05:33:42 +08:00
fix error async
This commit is contained in:
@@ -2,14 +2,14 @@ package com.zl.mjga.controller;
|
||||
|
||||
import com.zl.mjga.service.DeepSeekAiService;
|
||||
import dev.langchain4j.service.TokenStream;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.time.Duration;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
|
||||
@@ -22,20 +22,20 @@ public class AiController {
|
||||
private final DeepSeekAiService deepSeekAiService;
|
||||
|
||||
@PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<String> chat(@RequestBody String userMessage) {
|
||||
public Flux<String> chat(Principal principal, @RequestBody String userMessage) {
|
||||
Sinks.Many<String> sink = Sinks.many().unicast().onBackpressureBuffer();
|
||||
TokenStream chat = deepSeekAiService.chat("123", userMessage);
|
||||
TokenStream chat = deepSeekAiService.chat(principal.getName(), userMessage);
|
||||
chat.onPartialResponse(sink::tryEmitNext)
|
||||
.onCompleteResponse(
|
||||
r -> {
|
||||
sink.tryEmitNext("[DONE]");
|
||||
sink.tryEmitComplete();
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
})
|
||||
.onError(sink::tryEmitError)
|
||||
.start();
|
||||
|
||||
return sink.asFlux()
|
||||
.timeout(Duration.ofSeconds(60))
|
||||
.onErrorResume(e -> Flux.just("Timeout occurred"));
|
||||
return sink.asFlux()
|
||||
.timeout(Duration.ofSeconds(120))
|
||||
.doOnCancel(SecurityContextHolder::clearContext)
|
||||
.doOnTerminate(SecurityContextHolder::clearContext);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user