forked from TheoKanning/openai-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilize retrofit2.http.Streaming and retrofit2.Call<ResponseBody> in additional OpenAIApi methods to enable a streamable ResponseBody. Utilize retrofit2.Callback to get the streamable ResponseBody, parse Server Sent Events (SSE) and emit them using io.reactivex.FlowableEmitter. Enable: - Streaming of raw bytes - Streaming of Java objects - Shutdown of OkHttp ExecutorService Fixes: TheoKanning#51, TheoKanning#83, TheoKanning#182, TheoKanning#184
- Loading branch information
Showing
9 changed files
with
403 additions
and
8 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
api/src/main/java/com/theokanning/openai/completion/CompletionChunk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.theokanning.openai.completion; | ||
|
||
import lombok.Data; | ||
import java.util.List; | ||
|
||
/** | ||
* Object containing a response chunk from the completions streaming api. | ||
* | ||
* https://beta.openai.com/docs/api-reference/completions/create | ||
*/ | ||
@Data | ||
public class CompletionChunk { | ||
/** | ||
* A unique id assigned to this completion. | ||
*/ | ||
String id; | ||
|
||
/**https://beta.openai.com/docs/api-reference/create-completion | ||
* The type of object returned, should be "text_completion" | ||
*/ | ||
String object; | ||
|
||
/** | ||
* The creation time in epoch seconds. | ||
*/ | ||
long created; | ||
|
||
/** | ||
* The GPT-3 model used. | ||
*/ | ||
String model; | ||
|
||
/** | ||
* A list of generated completions. | ||
*/ | ||
List<CompletionChoice> choices; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionChunk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.theokanning.openai.completion.chat; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Object containing a response chunk from the chat completions streaming api. | ||
*/ | ||
@Data | ||
public class ChatCompletionChunk { | ||
/** | ||
* Unique id assigned to this chat completion. | ||
*/ | ||
String id; | ||
|
||
/** | ||
* The type of object returned, should be "chat.completion.chunk" | ||
*/ | ||
String object; | ||
|
||
/** | ||
* The creation time in epoch seconds. | ||
*/ | ||
long created; | ||
|
||
/** | ||
* The GPT-3.5 model used. | ||
*/ | ||
String model; | ||
|
||
/** | ||
* A list of all generated completions. | ||
*/ | ||
List<ChatCompletionChoice> choices; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package example; | ||
|
||
import com.theokanning.openai.service.OpenAiService; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import com.theokanning.openai.completion.CompletionRequest; | ||
import com.theokanning.openai.completion.chat.ChatCompletionRequest; | ||
import com.theokanning.openai.completion.chat.ChatMessage; | ||
import com.theokanning.openai.completion.chat.ChatMessageRole; | ||
|
||
public class OpenAiApiStreamExample { | ||
public static void main(String... args) { | ||
String token = System.getenv("OPENAI_TOKEN"); | ||
OpenAiService service = new OpenAiService(token); | ||
|
||
System.out.println("\nCreating completion..."); | ||
CompletionRequest completionRequest = CompletionRequest.builder() | ||
.model("ada") | ||
.prompt("Somebody once told me the world is gonna roll me") | ||
.echo(true) | ||
.user("testing") | ||
.n(3) | ||
.build(); | ||
|
||
/* | ||
Note: when using blockingForEach the calling Thread waits until the loop finishes. | ||
Use forEach instaed of blockignForEach if you don't want the calling Thread to wait. | ||
*/ | ||
|
||
// stream raw bytes | ||
service | ||
.streamCompletionBytes(completionRequest) | ||
.doOnError( e -> { | ||
e.printStackTrace(); | ||
}) | ||
.blockingForEach( bytes -> { | ||
System.out.print(new String(bytes)); | ||
}); | ||
|
||
// stream CompletionChunks | ||
service | ||
.streamCompletion(completionRequest) | ||
.doOnError( e -> { | ||
e.printStackTrace(); | ||
}) | ||
.blockingForEach(System.out::println); | ||
|
||
|
||
final List<ChatMessage> messages = new ArrayList<>(); | ||
final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a dog and will speak as such."); | ||
messages.add(systemMessage); | ||
|
||
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest | ||
.builder() | ||
.model("gpt-3.5-turbo") | ||
.messages(messages) | ||
.n(5) | ||
.maxTokens(50) | ||
.logitBias(new HashMap<>()) | ||
.build(); | ||
|
||
// stream ChatCompletionChunks | ||
service | ||
.streamChatCompletion(chatCompletionRequest) | ||
.doOnError( e -> { | ||
e.printStackTrace(); | ||
}) | ||
.blockingForEach(System.out::println); | ||
|
||
/* | ||
* shutdown the OkHttp ExecutorService to | ||
* exit immediately after the loops have finished | ||
*/ | ||
service.shutdownExecutor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.