forked from PlexPt/chatgpt-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamTest.java
More file actions
85 lines (70 loc) · 2.91 KB
/
StreamTest.java
File metadata and controls
85 lines (70 loc) · 2.91 KB
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 cf.vbnm.chatgpt;
import cf.vbnm.chatgpt.client.ChatGPTClient;
import cf.vbnm.chatgpt.entity.chat.ChatCompletion;
import cf.vbnm.chatgpt.entity.chat.ChatMessage;
import cf.vbnm.chatgpt.listener.ConsoleListener;
import cf.vbnm.chatgpt.spi.GeneralSupport;
import cf.vbnm.chatgpt.spi.LiteralArgs;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import java.util.Arrays;
/**
* 测试类
*
* @author plexpt
*/
public class StreamTest {
@EnableChatGPTClient(@LiteralArgs(completionPath = "https://.openai.azure.com/openai/deployments//chat/completions?api-version=2023-03-15-preview",
headerName = {"api-key", "Content-Type"}, headerValue = {"", MediaType.APPLICATION_JSON_VALUE}))
public static class InnerConfig {
@Bean
public ObjectMapper getObjectMapper() {
return new ObjectMapper();
}
}
public static class Support implements GeneralSupport {
@Override
public String completionPath() {
return "https://.openai.azure.com/openai/deployments//chat/completions?api-version=2023-03-15-preview";
}
@Override
public String generateImagePath() {
return "https://api.openai.com/v1/images/generations";
}
@Override
public HttpHeaders headers() {
HttpHeaders headers = new HttpHeaders();
headers.set("api-key", "");
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
}
private ChatGPTClient chatGPTStream;
@Before
public void before() {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(InnerConfig.class);
chatGPTStream = annotationConfigApplicationContext.getBean(ChatGPTClient.class);
}
@Test
public void chatCompletions() throws JsonProcessingException {
ChatMessage system = ChatMessage.ofSystem("你现在是一个诗人,专门写七言绝句");
ChatMessage chatMessage = ChatMessage.of("写首诗,题目是好热");
ChatCompletion chatCompletion = new ChatCompletion()
.chatMessages(Arrays.asList(system, chatMessage));
System.out.println(new ObjectMapper().writeValueAsString(chatCompletion));
chatGPTStream.streamChatCompletion(chatCompletion, new ConsoleListener(new ObjectMapper()));
//卡住测试
// CountDownLatch countDownLatch = new CountDownLatch(1);
// try {
// countDownLatch.await();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}