|
9 | 9 |
|
10 | 10 | [简体中文文档](https://github.com/PlexPt/chatgpt-java/blob/main/README_zh.md). |
11 | 11 |
|
12 | | -Reverse Engineered ChatGPT by OpenAI. Extensible for chatbots etc. |
| 12 | +SDK for OpenAI ChatGPT. If you find it helpful, please give it a star in the upper right corner. |
13 | 13 |
|
14 | | -Thanks to [revChatGPT](https://github.com/acheong08/ChatGPT). |
| 14 | +# Features |
15 | 15 |
|
| 16 | +| Feature | Support | |
| 17 | +| :--------------: | :---------: | |
| 18 | +| GPT 3.5 | Supported | |
| 19 | +| GPT 4.0 | Supported | |
| 20 | +| GPT 4.0-32k | Supported | |
| 21 | +| Streaming Dialog | Supported | |
| 22 | +| Blocking Dialog | Supported | |
| 23 | +| Front-end | None | |
| 24 | +| Context | Supported | |
| 25 | +| Token Compute | Coming Soon | |
| 26 | +| Multiple Keys | Supported | |
| 27 | +| Proxy | Supported | |
| 28 | +| Reverse Proxy | Supported | |
16 | 29 |
|
17 | | -# Features |
18 | 30 |  |
19 | 31 |
|
20 | 32 |  |
21 | 33 |
|
22 | 34 | ## USE |
23 | 35 |
|
24 | | -maven |
| 36 | +#### maven |
| 37 | + |
25 | 38 | ``` |
26 | 39 | <dependency> |
27 | 40 | <groupId>com.github.plexpt</groupId> |
28 | 41 | <artifactId>chatgpt</artifactId> |
29 | | - <version>1.2.0</version> |
| 42 | + <version>4.0.1</version> |
30 | 43 | </dependency> |
31 | 44 | ``` |
32 | 45 |
|
33 | | -gradle |
| 46 | +#### gradle |
| 47 | + |
34 | 48 | ``` |
35 | | -implementation group: 'com.github.plexpt', name: 'chatgpt', version: '1.2.0' |
| 49 | +implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.0.1' |
36 | 50 | ``` |
37 | 51 |
|
| 52 | +### Quick Start |
38 | 53 |
|
39 | | -then |
40 | 54 | ``` |
41 | | - Chatbot chatbot = new Chatbot("sessionToken"); |
42 | | - Map<String, Object> chatResponse = chatbot.getChatResponse("hello"); |
43 | | - System.out.println(chatResponse.get("message")); |
| 55 | +
|
| 56 | + ChatGPT chatGPT = ChatGPT.builder() |
| 57 | + .apiKey("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") |
| 58 | + .build() |
| 59 | + .init(); |
| 60 | + |
| 61 | + String res = chatGPT.chat("hello!"); |
| 62 | + System.out.println(res); |
| 63 | +``` |
| 64 | +### Advanced Usage |
| 65 | + |
| 66 | +```java |
| 67 | + //Proxy is required in some contry |
| 68 | + Proxy proxy = Proxys.http("127.0.0.1", 1080); |
| 69 | + |
| 70 | + ChatGPT chatGPT = ChatGPT.builder() |
| 71 | + .apiKey("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") |
| 72 | + .proxy(proxy) |
| 73 | + .timeout(900) |
| 74 | + .apiHost("https://api.openai.com/") //Reverse proxy address |
| 75 | + .build() |
| 76 | + .init(); |
| 77 | + |
| 78 | + Message system = Message.ofSystem("You are now a poet, specializing in writing seven character quatrains"); |
| 79 | + Message message = Message.of("Write a seven-character quatrain poem titled: Future!"); |
| 80 | + |
| 81 | + ChatCompletion chatCompletion = ChatCompletion.builder() |
| 82 | + .model(ChatCompletion.Model.GPT_3_5_TURBO.getName()) |
| 83 | + .messages(Arrays.asList(system, message)) |
| 84 | + .maxTokens(3000) |
| 85 | + .temperature(0.9) |
| 86 | + .build(); |
| 87 | + ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion); |
| 88 | + Message res = response.getChoices().get(0).getMessage(); |
| 89 | + System.out.println(res); |
| 90 | +``` |
| 91 | + |
| 92 | +### Streaming Usage |
| 93 | + |
| 94 | +```java |
| 95 | + |
| 96 | + |
| 97 | + ChatGPTStream chatGPTStream = ChatGPTStream.builder() |
| 98 | + .timeout(600) |
| 99 | + .apiKey("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") |
| 100 | + .build() |
| 101 | + .init(); |
| 102 | + |
| 103 | + |
| 104 | + ConsoleStreamListener listener = new ConsoleStreamListener(); |
| 105 | + Message message = Message.of("Write a seven-character quatrain poem titled: Future!"); |
| 106 | + ChatCompletion chatCompletion = ChatCompletion.builder() |
| 107 | + .messages(Arrays.asList(message)) |
| 108 | + .build(); |
| 109 | + chatGPTStream.streamChatCompletion(chatCompletion, listener); |
| 110 | + |
44 | 111 | ``` |
45 | | -### Get sessionToken |
46 | | -https://github.com/acheong08/ChatGPT/wiki/Setup#token-authentication |
47 | 112 |
|
| 113 | +### Using Spring SseEmitter with Streaming |
48 | 114 |
|
49 | | -### CLI use |
50 | | -1. download from release |
51 | | -2. edit config.json |
52 | | -3. run |
| 115 | +Refer to [SseStreamListener](https://chat.plexpt.com/src/main/java/com/plexpt/chatgpt/listener/SseStreamListener.java) |
53 | 116 |
|
| 117 | +```java |
54 | 118 |
|
55 | | -# Awesome ChatGPT |
56 | | -[My list](https://github.com/stars/acheong08/lists/awesome-chatgpt) |
| 119 | + @GetMapping("/chat/sse") |
| 120 | + @CrossOrigin |
| 121 | + public SseEmitter sseEmitter(String prompt) { |
| 122 | + |
57 | 123 |
|
58 | | -If you have a cool project you want added to the list, open an issue. |
| 124 | + ChatGPTStream chatGPTStream = ChatGPTStream.builder() |
| 125 | + .timeout(600) |
| 126 | + .apiKey("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") |
| 127 | + .apiHost("https://api.openai.com/") |
| 128 | + .build() |
| 129 | + .init(); |
| 130 | + |
| 131 | + SseEmitter sseEmitter = new SseEmitter(-1L); |
59 | 132 |
|
60 | | -# Disclaimers |
61 | | -This is not an official OpenAI product. This is a personal project and is not affiliated with OpenAI in any way. Don't sue me |
| 133 | + SseStreamListener listener = new SseStreamListener(sseEmitter); |
| 134 | + Message message = Message.of(prompt); |
62 | 135 |
|
63 | | -### This is a library and not intended for direct CLI use |
64 | | -The CLI functionality is for demo and testing only. Captcha is not supported (For unclean IP addresses) |
| 136 | + listener.setOnComplate(msg -> { |
| 137 | + //The answer is complete, do something |
| 138 | + }); |
| 139 | + chatGPTStream.streamChatCompletion(Arrays.asList(message), listener); |
| 140 | + |
| 141 | + |
| 142 | + return sseEmitter; |
| 143 | + } |
| 144 | + |
| 145 | +``` |
65 | 146 |
|
66 | | -### CLI use |
67 | | -[@rawandahmad698](https://github.com/rawandahmad698) has a much better CLI tool at |
68 | 147 |
|
69 | | -**[PyChatGPT](https://github.com/rawandahmad698/PyChatGPT)** supports captcha! |
70 | 148 |
|
71 | 149 | # Star History |
72 | 150 |
|
|
0 commit comments