fix error async

This commit is contained in:
Chuck1sn
2025-05-21 15:33:52 +08:00
parent 3bc65d2df4
commit fa4d790e81
9 changed files with 78 additions and 41 deletions

View File

@@ -7,6 +7,5 @@ import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.memory.ChatMemoryAccess;
public interface DeepSeekChatAssistant extends ChatMemoryAccess {
@SystemMessage("You are a good friend of mine. Answer using slang.")
TokenStream chat(@MemoryId String memoryId, @UserMessage String userMessage);
}

View File

@@ -39,7 +39,6 @@ public class WebSecurityConfig {
return new OrRequestMatcher(
new AntPathRequestMatcher("/auth/sign-in", HttpMethod.POST.name()),
new AntPathRequestMatcher("/auth/sign-up", HttpMethod.POST.name()),
new AntPathRequestMatcher("/ai/**", HttpMethod.POST.name()),
new AntPathRequestMatcher("/v3/api-docs/**", HttpMethod.GET.name()),
new AntPathRequestMatcher("/swagger-ui/**", HttpMethod.GET.name()),
new AntPathRequestMatcher("/swagger-ui.html", HttpMethod.GET.name()),
@@ -63,6 +62,9 @@ public class WebSecurityConfig {
.permitAll()
.anyRequest()
.authenticated())
.securityContext(securityContext -> securityContext
.requireExplicitSave(false)
)
.exceptionHandling(
(exceptionHandling) ->
exceptionHandling

View File

@@ -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);
}
}