58 lines
1.9 KiB
Java
58 lines
1.9 KiB
Java
package com.iweb.langchain4j.controller;
|
|
|
|
import com.iweb.langchain4j.service.ChatAssistant;
|
|
import dev.langchain4j.model.chat.StreamingChatModel;
|
|
import dev.langchain4j.model.chat.response.ChatResponse;
|
|
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
|
|
import jakarta.annotation.Resource;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import reactor.core.publisher.Flux;
|
|
|
|
@RestController
|
|
@Slf4j
|
|
public class StreamController {
|
|
|
|
@Resource //直接使用 low-level LLM API
|
|
private StreamingChatModel chatModelQwen;
|
|
@Resource //自己封装接口使用 high-level LLM API
|
|
private ChatAssistant chatAssistant;
|
|
|
|
// http://localhost:9006/lc4j/chatstream/chat2?prompt=我是谁?
|
|
@GetMapping(value = "/lc4j/chatstream/chat2")
|
|
public Flux<String> chat3(@RequestParam(value = "prompt", defaultValue = "你是谁?") String prompt) {
|
|
return chatAssistant.chatFlux(prompt);
|
|
}
|
|
|
|
|
|
// http://localhost:9006/lc4j/chatstream/chat?prompt=我是谁?
|
|
@GetMapping(value = "/lc4j/chatstream/chat")
|
|
public Flux<String> chat(@RequestParam("prompt") String prompt) {
|
|
|
|
return Flux.create(emitter -> {
|
|
chatModelQwen.chat(prompt, new StreamingChatResponseHandler()
|
|
{
|
|
@Override
|
|
public void onPartialResponse(String partialResponse)
|
|
{
|
|
emitter.next(partialResponse);
|
|
}
|
|
|
|
@Override
|
|
public void onCompleteResponse(ChatResponse completeResponse)
|
|
{
|
|
emitter.complete();
|
|
}
|
|
|
|
@Override
|
|
public void onError(Throwable throwable)
|
|
{
|
|
emitter.error(throwable);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|