Skip to content

Commit

Permalink
small updates (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
robch authored Dec 15, 2023
1 parent 8a3efa9 commit 606bc37
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"_Name": "OpenAI Chat Completions in Java",
"_Name": "OpenAI Chat Completions (Streaming) in Java",
"_Language": "Java",
"OPENAI_ENDPOINT": "<insert your OpenAI endpoint here>",
"OPENAI_API_KEY": "<insert your OpenAI API key here>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public Flux<ChatCompletions> getChatCompletionsStreamingAsync(String userPrompt,
Flux<ChatCompletions> response = client.getChatCompletionsStream(deploymentName, options);

response.subscribe(chatResponse -> {
// Process each response as it comes in.
if (chatResponse.getChoices() != null) {
for (ChatChoice update : chatResponse.getChoices()) {
if (update.getDelta() == null || update.getDelta().getContent() == null)
Expand All @@ -94,13 +93,9 @@ public Flux<ChatCompletions> getChatCompletionsStreamingAsync(String userPrompt,

options.getMessages().add(new ChatRequestAssistantMessage(responseContent.toString()));
}
}, error -> {
// Error encountered
}, () -> {
// Stream completed
});

return response; // response.toIterable();
return response;
}

public static void main(String[] args) {
Expand All @@ -113,13 +108,12 @@ public static void main(String[] args) {
if (userPrompt.isEmpty() || "exit".equals(userPrompt))
break;

System.out.print("Assistant: ");
System.out.print("\nAssistant: ");
Flux<ChatCompletions> responseFlux = chat.getChatCompletionsStreamingAsync(userPrompt, update -> {
System.out.print(update.getContent());
});
// block until the last element is available
responseFlux.blockLast(Duration.ofSeconds(20));
System.out.println("");
System.out.println("\n");
}
scanner.close();
}
Expand Down

0 comments on commit 606bc37

Please sign in to comment.