Skip to content

Commit c6b138e

Browse files
committed
📝
1 parent 02a61b2 commit c6b138e

File tree

5 files changed

+180
-3
lines changed

5 files changed

+180
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.0.4'
9191

9292
### 最简使用
9393

94+
也可以使用这个类进行测试 [ConsoleChatGPT](src/main/java/com/plexpt/chatgpt/ConsoleChatGPT.java)
95+
9496
```java
9597
//国内需要代理
9698
Proxy proxy = Proxys.http("127.0.0.1", 1080);

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.plexpt</groupId>
77
<artifactId>chatgpt</artifactId>
8-
<version>4.0.4</version>
8+
<version>4.0.5</version>
99
<name>chatgpt</name>
1010
<description>ChatGPT4.0、 ChatGPT Java SDK.</description>
1111
<url>https://chat.plexpt.com</url>
@@ -235,7 +235,7 @@
235235
<manifest>
236236
<addClasspath>true</addClasspath>
237237
<classpathPrefix>lib/</classpathPrefix>
238-
<mainClass>com.github.plexpt.chatgpt.ChatGPT</mainClass>
238+
<mainClass>com.plexpt.chatgpt.ConsoleChatGPT</mainClass>
239239
</manifest>
240240
</archive>
241241
</configuration>

src/main/java/com/plexpt/chatgpt/ChatGPT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,18 @@ public BigDecimal balance() {
185185
return response.getTotalAvailable();
186186
}
187187

188+
/**
189+
* 余额查询
190+
*
191+
* @return
192+
*/
193+
public static BigDecimal balance(String key) {
194+
ChatGPT chatGPT = ChatGPT.builder()
195+
.apiKey(key)
196+
.build()
197+
.init();
198+
199+
return chatGPT.balance();
200+
}
201+
188202
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package com.plexpt.chatgpt;
2+
3+
import com.plexpt.chatgpt.entity.chat.Message;
4+
import com.plexpt.chatgpt.listener.ConsoleStreamListener;
5+
import com.plexpt.chatgpt.util.Proxys;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.math.BigDecimal;
11+
import java.net.Proxy;
12+
import java.util.ArrayList;
13+
import java.util.Arrays;
14+
import java.util.List;
15+
import java.util.concurrent.CountDownLatch;
16+
import java.util.stream.Collectors;
17+
18+
import cn.hutool.core.util.NumberUtil;
19+
import lombok.SneakyThrows;
20+
import lombok.extern.slf4j.Slf4j;
21+
22+
23+
/**
24+
* open ai 客户端
25+
*
26+
* @author plexpt
27+
*/
28+
29+
@Slf4j
30+
31+
public class ConsoleChatGPT {
32+
33+
public static Proxy proxy = Proxy.NO_PROXY;
34+
35+
public static void main(String[] args) {
36+
37+
System.out.println("ChatGPT - Java command-line interface");
38+
System.out.println("Press enter twice to submit your question.");
39+
System.out.println();
40+
System.out.println("按两次回车以提交您的问题!!!");
41+
System.out.println("按两次回车以提交您的问题!!!");
42+
System.out.println("按两次回车以提交您的问题!!!");
43+
44+
45+
System.out.println();
46+
System.out.println("Please enter APIKEY, press Enter twice to submit:");
47+
String key = getInput("请输入APIKEY,按两次回车以提交:\n");
48+
check(key);
49+
50+
// 询问用户是否使用代理 国内需要代理
51+
System.out.println("是否使用代理?(y/n): ");
52+
System.out.println("use proxy?(y/n): ");
53+
String useProxy = getInput("按两次回车以提交:\n");
54+
if (useProxy.equalsIgnoreCase("y")) {
55+
56+
// 输入代理地址
57+
System.out.println("请输入代理类型(http/socks): ");
58+
String type = getInput("按两次回车以提交:\n");
59+
60+
// 输入代理地址
61+
System.out.println("请输入代理IP: ");
62+
String proxyHost = getInput("按两次回车以提交:\n");
63+
64+
// 输入代理端口
65+
System.out.println("请输入代理端口: ");
66+
String portStr = getInput("按两次回车以提交:\n");
67+
Integer proxyPort = Integer.parseInt(portStr);
68+
69+
if (type.equals("http")) {
70+
proxy = Proxys.http(proxyHost, proxyPort);
71+
} else {
72+
proxy = Proxys.socks5(proxyHost, proxyPort);
73+
}
74+
75+
}
76+
77+
System.out.println("Inquiry balance...");
78+
System.out.println("查询余额中...");
79+
BigDecimal balance = getBalance(key);
80+
System.out.println("API KEY balance: " + balance.toPlainString());
81+
82+
if (!NumberUtil.isGreater(balance, BigDecimal.ZERO)) {
83+
System.out.println("API KEY 余额不足: ");
84+
return;
85+
}
86+
87+
88+
while (true) {
89+
String prompt = getInput("\nYou:\n");
90+
91+
ChatGPTStream chatGPT = ChatGPTStream.builder()
92+
.apiKey(key)
93+
.proxy(proxy)
94+
.build()
95+
.init();
96+
97+
System.out.println("AI: ");
98+
99+
100+
//卡住
101+
CountDownLatch countDownLatch = new CountDownLatch(1);
102+
103+
Message message = Message.of(prompt);
104+
ConsoleStreamListener listener = new ConsoleStreamListener() {
105+
@Override
106+
public void onError(Throwable throwable, String response) {
107+
throwable.printStackTrace();
108+
countDownLatch.countDown();
109+
}
110+
};
111+
112+
listener.setOnComplate(msg -> {
113+
countDownLatch.countDown();
114+
});
115+
chatGPT.streamChatCompletion(Arrays.asList(message), listener);
116+
117+
try {
118+
countDownLatch.await();
119+
} catch (InterruptedException e) {
120+
e.printStackTrace();
121+
}
122+
123+
}
124+
125+
126+
}
127+
128+
private static BigDecimal getBalance(String key) {
129+
130+
ChatGPT chatGPT = ChatGPT.builder()
131+
.apiKey(key)
132+
.proxy(proxy)
133+
.build()
134+
.init();
135+
136+
return chatGPT.balance();
137+
}
138+
139+
private static void check(String key) {
140+
if (key == null || key.isEmpty()) {
141+
throw new RuntimeException("请输入正确的KEY");
142+
}
143+
}
144+
145+
@SneakyThrows
146+
public static String getInput(String prompt) {
147+
System.out.print(prompt);
148+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
149+
List<String> lines = new ArrayList<>();
150+
String line;
151+
try {
152+
while ((line = reader.readLine()) != null && !line.isEmpty()) {
153+
lines.add(line);
154+
}
155+
} catch (IOException e) {
156+
e.printStackTrace();
157+
}
158+
return lines.stream().collect(Collectors.joining("\n"));
159+
}
160+
161+
}
162+

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public void onClosed(EventSource eventSource) {
7070
@Override
7171
public void onEvent(EventSource eventSource, String id, String type, String data) {
7272
if (data.equals("[DONE]")) {
73-
log.info("Chat session completed: {}", lastMessage);
7473
onComplate.accept(lastMessage);
7574
return;
7675
}

0 commit comments

Comments
 (0)