Skip to content

Commit

Permalink
close stream correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesDuboisSAP committed Aug 21, 2024
1 parent b3190a5 commit f0fa3e6
Showing 1 changed file with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionFunction;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionOutput;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionParameters;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionStream;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionTool;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatMessage.OpenAiChatUserMessage;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatMessage.OpenAiChatUserMessage.ImageDetailLevel;
Expand Down Expand Up @@ -68,23 +69,24 @@ public static ResponseEntity<ResponseBodyEmitter> stream() {
ThreadContextExecutors.getExecutor()
.submit(
() -> {
try (Stream<OpenAiChatCompletionOutput> stream =
OpenAiClient.forModel(GPT_35_TURBO).stream(request).getDeltaStream()) {
stream.forEach(
delta -> {
try {
// TODO: Change the types to nullable? Maybe create a class
// OpenAiChatCompletionDelta...
if (!delta.getChoices().isEmpty()
&& delta.getChoices().get(0).getMessage() != null
&& delta.getChoices().get(0).getMessage().getContent() != null) {
emitter.send(delta.getChoices().get(0).getMessage().getContent());
}
} catch (IOException e) {
log.error(Arrays.toString(e.getStackTrace()));
emitter.completeWithError(e);
}
});
try (var stream = OpenAiClient.forModel(GPT_35_TURBO).stream(request)) {
stream
.getDeltaStream()
.forEach(
delta -> {
try {
// TODO: Change the types to nullable? Maybe create a class
// OpenAiChatCompletionDelta...
if (!delta.getChoices().isEmpty()
&& delta.getChoices().get(0).getMessage() != null
&& delta.getChoices().get(0).getMessage().getContent() != null) {
emitter.send(delta.getChoices().get(0).getMessage().getContent());
}
} catch (IOException e) {
log.error(Arrays.toString(e.getStackTrace()));
emitter.completeWithError(e);
}
});
// Once all the data is sent, complete the emitter
emitter.complete();
} catch (Exception e) {
Expand Down

0 comments on commit f0fa3e6

Please sign in to comment.