Skip to content

Commit 6b3d47d

Browse files
committed
refact
1 parent 4d829e5 commit 6b3d47d

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.4.0'
117117
.temperature(0.9)
118118
.build();
119119
ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);
120-
Message res = response.getChoices().get(0).getMessage();
121-
System.out.println(res);
120+
System.out.println(response.toPlainString());
122121

123122
```
124123
### 函数调用(Function Call)

src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletionResponse.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import com.plexpt.chatgpt.entity.billing.Usage;
66
import lombok.Data;
7+
import org.springframework.util.CollectionUtils;
78

89
import java.util.List;
10+
import java.util.Optional;
911

1012
/**
1113
* chat答案类
@@ -25,4 +27,31 @@ public class ChatCompletionResponse {
2527
private Usage usage;
2628
Object logprobs;
2729

30+
31+
public String toPlainString() {
32+
if (CollectionUtils.isEmpty(this.getChoices())) {
33+
return "";
34+
}
35+
36+
37+
return Optional.ofNullable(this.getChoices())
38+
.map(e -> e.get(0))
39+
.map(ChatChoice::getMessage)
40+
.map(Message::getContent)
41+
.orElse("");
42+
}
43+
44+
public String toPlainStringStream() {
45+
if (CollectionUtils.isEmpty(this.getChoices())) {
46+
return "";
47+
}
48+
49+
50+
return Optional.ofNullable(this.getChoices())
51+
.map(e -> e.get(0))
52+
.map(ChatChoice::getDelta)
53+
.map(Message::getContent)
54+
.orElse("");
55+
}
56+
2857
}

src/main/java/com/plexpt/chatgpt/listener/AbstractStreamListener.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.plexpt.chatgpt.listener;
22

3-
import com.plexpt.chatgpt.entity.chat.ChatChoice;
43
import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse;
5-
import com.plexpt.chatgpt.entity.chat.Message;
64
import com.plexpt.chatgpt.util.fastjson.JSON;
75
import lombok.Getter;
86
import lombok.Setter;
@@ -11,8 +9,8 @@
119
import okhttp3.Response;
1210
import okhttp3.sse.EventSource;
1311
import okhttp3.sse.EventSourceListener;
12+
import org.springframework.util.StringUtils;
1413

15-
import java.util.List;
1614
import java.util.Objects;
1715
import java.util.function.Consumer;
1816

@@ -73,15 +71,11 @@ public void onEvent(EventSource eventSource, String id, String type, String data
7371
}
7472

7573
ChatCompletionResponse response = JSON.parseObject(data, ChatCompletionResponse.class);
76-
// 读取Json
77-
List<ChatChoice> choices = response.getChoices();
78-
if (choices == null || choices.isEmpty()) {
79-
return;
80-
}
81-
Message delta = choices.get(0).getDelta();
82-
String text = delta.getContent();
8374

84-
if (text != null) {
75+
String text = response.toPlainStringStream();
76+
77+
if (!StringUtils.isEmpty(text)) {
78+
8579
lastMessage += text;
8680

8781
onMsg(text);

0 commit comments

Comments
 (0)