|
| 1 | + |
| 2 | +# ChatGPT-Java |
| 3 | +A **Java** wrapper for the unofficial reverse-engineered [ChatGPT](https://chat.openai.com/) API. |
| 4 | + |
| 5 | +## Add ChatGPT-Java to your own build |
| 6 | +### Maven |
| 7 | +```xml |
| 8 | +<repository> |
| 9 | + <id>chatgpt-java</id> |
| 10 | + <url>https://raw.github.com/AcaiSoftware/chatgpt-java/repository/</url> |
| 11 | +</repository> |
| 12 | + |
| 13 | +<dependency> |
| 14 | + <groupId>gg.acai</groupId> |
| 15 | + <artifactId>chatgpt-client</artifactId> |
| 16 | + <version>1.0</version> |
| 17 | +</dependency> |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage Examples |
| 21 | +### Registering ChatGPT |
| 22 | +All builder fields are optional except for ``sessionToken`` |
| 23 | +```java |
| 24 | +ChatGPT chatGpt = ChatGPT.newBuilder() |
| 25 | + .sessionToken("token_here") // required field |
| 26 | + .addExceptionAttribute(new ParsedExceptionEntry("exception keyword", Exception.class)) // adds an exception attribute |
| 27 | + .build(); // builds the ChatGPT client |
| 28 | +``` |
| 29 | + |
| 30 | +### Session Token |
| 31 | +Not required, the ChatGPT client checks & verifies the session token upon the build procedure. |
| 32 | +The client will throw a `TokenExpiredException` if the token has expired. |
| 33 | +```java |
| 34 | + chatGpt.getComplexAccessCache().refreshAccessToken() // refreshing cache and verifies session token |
| 35 | + .whenComplete((accessToken) -> { // called when the promise is completed, not required |
| 36 | + System.out.println("Access token: " + accessToken); |
| 37 | + }); |
| 38 | +``` |
| 39 | + |
| 40 | +### ChatGPT Conversation |
| 41 | +Supports both asynchronous & synchronous handling |
| 42 | + |
| 43 | +Create a conversation with a promise completing the response |
| 44 | +```java |
| 45 | +Conversation conversation = chatGpt.createConversation(); |
| 46 | +conversation.sendMessageAsync("Hello!") |
| 47 | + .whenComplete((response) -> { |
| 48 | + System.out.println("Response: " + response); |
| 49 | + }); |
| 50 | +``` |
| 51 | + |
| 52 | +Create a conversation with an event stream listener |
| 53 | +```java |
| 54 | +Conversation conversation = chatGPT.createConversation(); // creates a new conversation |
| 55 | +conversation.setStreamResponseListener(new StreamResponseListener() { |
| 56 | + @Override |
| 57 | + public void onResponse(StreamResponse response) { |
| 58 | + System.out.println(response.getMessage()); // response returned by the event stream |
| 59 | + } |
| 60 | +}).sendMessageAsync("Hello World"); // the message to send to ChatGPT |
| 61 | +``` |
| 62 | + |
| 63 | +## Documentation |
| 64 | +// Link to docs |
| 65 | + |
| 66 | +## Features |
| 67 | +* Functional Style |
| 68 | +* Sending messages with responses |
| 69 | +* Event Stream Response |
| 70 | +* Asynchronous & Synchronous methods |
| 71 | +* Token Access Cache |
| 72 | +* Conversations |
| 73 | +* Simple Builders |
| 74 | +* Easy to use, but powerful |
| 75 | +* Optimal performance |
| 76 | + |
| 77 | +## Important Notes |
| 78 | +Due to the fact that this is developed using the unofficial reverse-engineered API, be aware that it could break at any time. |
| 79 | +However, we will work to fix any issues as they may arise. |
| 80 | + |
| 81 | +## Contributing |
| 82 | +Contributions are highly appreciated! If you feel your pull request is useful, go ahead! |
| 83 | +Before creating a pull request, make sure your changes works as it should and give a description on what it provides. |
0 commit comments