20250404 流式响应

This commit is contained in:
liangjinglin 2025-04-04 22:53:16 +08:00
parent 01538da5e5
commit 88a9f4684e
4 changed files with 52 additions and 1 deletions

View File

@ -18,6 +18,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- LangChain Java -->
<dependency>
<groupId>dev.langchain4j</groupId>

View File

@ -1,8 +1,10 @@
package com.ai.config;
import dev.langchain4j.community.model.dashscope.QwenLanguageModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.model.language.LanguageModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -22,4 +24,12 @@ public class LanguageModelConfig {
.modelName(modelName)
.build();
}
@Bean
public QwenStreamingChatModel qwenStreamingChatModel(){
return QwenStreamingChatModel.builder()
.apiKey(apiKey)
.modelName(modelName)
.build();
}
}

View File

@ -2,11 +2,14 @@ package com.ai.controller;
import com.ai.service.*;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.community.model.zhipu.ZhipuAiImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
@ -14,6 +17,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.net.URI;
@ -42,6 +46,9 @@ public class LangChainController {
@Autowired
private EmbeddingService embeddingService;
@Autowired
private QwenStreamingChatModel qwenStreamingChatModel;
@GetMapping("/normal/chat")
public String normalChat(@RequestParam("input") String input) {
System.out.println("start normal chat...");
@ -91,4 +98,31 @@ public class LangChainController {
embeddingService.embedding(input);
return segmentAssist.chat(input);
}
@GetMapping(value = "/stream/chat", produces = "text/stream;charset=UTF-8")
public Flux<String> streamChat(@RequestParam("input") String input) {
System.out.println("start normal chat...");
Flux<String> flux = Flux.create(fluxSink -> {
qwenStreamingChatModel.chat(input, new StreamingChatResponseHandler() {
@Override
public void onPartialResponse(String s) {
fluxSink.next(s);
System.out.println(s);
}
@Override
public void onCompleteResponse(ChatResponse chatResponse) {
fluxSink.complete();
System.out.println("结束");
}
@Override
public void onError(Throwable throwable) {
fluxSink.error(throwable);
throwable.getMessage();
}
});
});
return flux;
}
}

View File

@ -22,3 +22,6 @@ GET http://localhost:8080/langchain/embedd/chat?input=我想要个便宜的办
### 测试 LangChainController 的 chat 接口
GET http://localhost:8080/langchain/normal/chat?input=今天天气如何
### 测试 LangChainController 的 chat 接口
GET http://localhost:8080/langchain/stream/chat?input=今天天气如何