diff --git a/README.md b/README.md index 97a9aa8..0e10ac1 100644 --- a/README.md +++ b/README.md @@ -117,8 +117,7 @@ implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.4.0' .temperature(0.9) .build(); ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion); - Message res = response.getChoices().get(0).getMessage(); - System.out.println(res); + System.out.println(response.toPlainString()); ``` ### 函数调用(Function Call) diff --git a/src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletionResponse.java b/src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletionResponse.java index 078f818..0538c1f 100644 --- a/src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletionResponse.java +++ b/src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletionResponse.java @@ -4,8 +4,10 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.plexpt.chatgpt.entity.billing.Usage; import lombok.Data; +import org.springframework.util.CollectionUtils; import java.util.List; +import java.util.Optional; /** * chat答案类 @@ -25,4 +27,31 @@ public class ChatCompletionResponse { private Usage usage; Object logprobs; + + public String toPlainString() { + if (CollectionUtils.isEmpty(this.getChoices())) { + return ""; + } + + + return Optional.ofNullable(this.getChoices()) + .map(e -> e.get(0)) + .map(ChatChoice::getMessage) + .map(Message::getContent) + .orElse(""); + } + + public String toPlainStringStream() { + if (CollectionUtils.isEmpty(this.getChoices())) { + return ""; + } + + + return Optional.ofNullable(this.getChoices()) + .map(e -> e.get(0)) + .map(ChatChoice::getDelta) + .map(Message::getContent) + .orElse(""); + } + } diff --git a/src/main/java/com/plexpt/chatgpt/listener/AbstractStreamListener.java b/src/main/java/com/plexpt/chatgpt/listener/AbstractStreamListener.java index 2f5cefc..e24ac07 100644 --- a/src/main/java/com/plexpt/chatgpt/listener/AbstractStreamListener.java +++ b/src/main/java/com/plexpt/chatgpt/listener/AbstractStreamListener.java @@ -1,8 +1,6 @@ package com.plexpt.chatgpt.listener; -import com.plexpt.chatgpt.entity.chat.ChatChoice; import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse; -import com.plexpt.chatgpt.entity.chat.Message; import com.plexpt.chatgpt.util.fastjson.JSON; import lombok.Getter; import lombok.Setter; @@ -11,8 +9,8 @@ import okhttp3.Response; import okhttp3.sse.EventSource; import okhttp3.sse.EventSourceListener; +import org.springframework.util.StringUtils; -import java.util.List; import java.util.Objects; import java.util.function.Consumer; @@ -73,15 +71,11 @@ public void onEvent(EventSource eventSource, String id, String type, String data } ChatCompletionResponse response = JSON.parseObject(data, ChatCompletionResponse.class); - // 读取Json - List choices = response.getChoices(); - if (choices == null || choices.isEmpty()) { - return; - } - Message delta = choices.get(0).getDelta(); - String text = delta.getContent(); - if (text != null) { + String text = response.toPlainStringStream(); + + if (!StringUtils.isEmpty(text)) { + lastMessage += text; onMsg(text);