|
|
|
@ -1,10 +1,13 @@
|
|
|
|
|
package xyz.wbsite.ai;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.thread.ThreadUtil;
|
|
|
|
|
import dev.langchain4j.data.message.AiMessage;
|
|
|
|
|
import dev.langchain4j.data.message.ChatMessage;
|
|
|
|
|
import dev.langchain4j.data.message.UserMessage;
|
|
|
|
|
import dev.langchain4j.model.StreamingResponseHandler;
|
|
|
|
|
import dev.langchain4j.model.chat.response.ChatResponse;
|
|
|
|
|
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
|
|
|
|
|
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
|
|
|
|
|
import dev.langchain4j.model.output.Response;
|
|
|
|
|
|
|
|
|
@ -26,20 +29,25 @@ public class TestStreamChat {
|
|
|
|
|
UserMessage.from("假如树上有10只鸟,10分钟前飞走了2只,5分钟前又飞回了1只,刚刚又来了3只,那现在树上有几只鸟?")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
model.generate(messages, new StreamingResponseHandler<AiMessage>() {
|
|
|
|
|
ThreadUtil.execAsync(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onNext(String s) {
|
|
|
|
|
System.out.print(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Throwable throwable) {
|
|
|
|
|
System.err.println(throwable.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onComplete(Response<AiMessage> response) {
|
|
|
|
|
System.out.println("onComplete");
|
|
|
|
|
public void run() {
|
|
|
|
|
model.chat(messages, new StreamingChatResponseHandler(){
|
|
|
|
|
@Override
|
|
|
|
|
public void onPartialResponse(String s) {
|
|
|
|
|
System.out.print(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCompleteResponse(ChatResponse chatResponse) {
|
|
|
|
|
System.out.println("onComplete");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Throwable throwable) {
|
|
|
|
|
System.err.println(throwable.getMessage());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|