SDK for OpenAI ChatGPT. If you find it helpful, please give it a star in the upper right corner.
Feature | Support |
---|---|
GPT 3.5 | Supported |
GPT 4.0 | Supported |
GPT 4.0-32k | Supported |
Streaming Dialog | Supported |
Blocking Dialog | Supported |
Front-end | None |
Context | Supported |
Token Compute | Coming Soon |
Multiple Keys | Supported |
Proxy | Supported |
Reverse Proxy | Supported |
<dependency>
<groupId>com.github.plexpt</groupId>
<artifactId>chatgpt</artifactId>
<version>4.0.1</version>
</dependency>
implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.0.1'
ChatGPT chatGPT = ChatGPT.builder()
.apiKey("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")
.build()
.init();
String res = chatGPT.chat("hello!");
System.out.println(res);
//Proxy is required in some contry
Proxy proxy = Proxys.http("127.0.0.1", 1080);
ChatGPT chatGPT = ChatGPT.builder()
.apiKey("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")
.proxy(proxy)
.timeout(900)
.apiHost("https://api.openai.com/") //Reverse proxy address
.build()
.init();
Message system = Message.ofSystem("You are now a poet, specializing in writing seven character quatrains");
Message message = Message.of("Write a seven-character quatrain poem titled: Future!");
ChatCompletion chatCompletion = ChatCompletion.builder()
.model(ChatCompletion.Model.GPT_3_5_TURBO.getName())
.messages(Arrays.asList(system, message))
.maxTokens(3000)
.temperature(0.9)
.build();
ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);
Message res = response.getChoices().get(0).getMessage();
System.out.println(res);
ChatGPTStream chatGPTStream = ChatGPTStream.builder()
.timeout(600)
.apiKey("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")
.build()
.init();
ConsoleStreamListener listener = new ConsoleStreamListener();
Message message = Message.of("Write a seven-character quatrain poem titled: Future!");
ChatCompletion chatCompletion = ChatCompletion.builder()
.messages(Arrays.asList(message))
.build();
chatGPTStream.streamChatCompletion(chatCompletion, listener);
Refer to SseStreamListener
@GetMapping("/chat/sse")
@CrossOrigin
public SseEmitter sseEmitter(String prompt) {
ChatGPTStream chatGPTStream = ChatGPTStream.builder()
.timeout(600)
.apiKey("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")
.apiHost("https://api.openai.com/")
.build()
.init();
SseEmitter sseEmitter = new SseEmitter(-1L);
SseStreamListener listener = new SseStreamListener(sseEmitter);
Message message = Message.of(prompt);
listener.setOnComplate(msg -> {
//The answer is complete, do something
});
chatGPTStream.streamChatCompletion(Arrays.asList(message), listener);
return sseEmitter;
}