-
Notifications
You must be signed in to change notification settings - Fork 712
/
Copy pathStreamTest.java
85 lines (67 loc) · 2.48 KB
/
StreamTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.plexpt.chatgpt;
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
import com.plexpt.chatgpt.entity.chat.Message;
import com.plexpt.chatgpt.listener.ConsoleStreamListener;
import com.plexpt.chatgpt.listener.SseStreamListener;
import com.plexpt.chatgpt.util.Proxys;
import okhttp3.Dispatcher;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.net.Proxy;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
/**
* 测试类
*
* @author plexpt
*/
public class StreamTest {
private ChatGPTStream chatGPTStream;
@Before
public void before() {
Proxy proxy = Proxys.http("127.0.0.1", 1080);
chatGPTStream = ChatGPTStream.builder()
.apiKey("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")
.proxy(proxy)
.timeout(600)
.okHttpCustomizer(builder -> {
// 自定义一个dispatcher
return builder.dispatcher(new Dispatcher(Executors.newSingleThreadExecutor()));
})
.apiHost("https://api.openai.com/")
.build()
.init();
}
@Test
public void chatCompletions() {
ConsoleStreamListener listener = new ConsoleStreamListener();
Message message = Message.of("写一段七言绝句诗,题目是:火锅!");
ChatCompletion chatCompletion = ChatCompletion.builder()
.messages(Arrays.asList(message))
.build();
chatGPTStream.streamChatCompletion(chatCompletion, listener);
//卡住测试
CountDownLatch countDownLatch = new CountDownLatch(1);
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@GetMapping("/chat/sse")
@CrossOrigin
public SseEmitter sseEmitter(String prompt) {
SseEmitter sseEmitter = new SseEmitter(-1L);
SseStreamListener listener = new SseStreamListener(sseEmitter);
Message message = Message.of(prompt);
listener.setOnComplete(msg -> {
//回答完成,可以做一些事情
});
chatGPTStream.streamChatCompletion(Arrays.asList(message), listener);
return sseEmitter;
}
}