|
| 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 | + |
0 commit comments