-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OPIK-610 removed unnecessary interface
- Loading branch information
Showing
3 changed files
with
49 additions
and
62 deletions.
There are no files selected for viewing
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
52 changes: 0 additions & 52 deletions
52
...end/src/main/java/com/comet/opik/domain/llmproviders/DefaultLlmProviderStreamHandler.java
This file was deleted.
Oops, something went wrong.
48 changes: 44 additions & 4 deletions
48
...ik-backend/src/main/java/com/comet/opik/domain/llmproviders/LlmProviderStreamHandler.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 |
---|---|---|
@@ -1,9 +1,49 @@ | ||
package com.comet.opik.domain.llmproviders; | ||
|
||
import com.comet.opik.utils.JsonUtils; | ||
import dev.ai4j.openai4j.OpenAiHttpException; | ||
import io.dropwizard.jersey.errors.ErrorMessage; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.glassfish.jersey.server.ChunkedOutput; | ||
|
||
public interface LlmProviderStreamHandler { | ||
void handleMessage(Object item, ChunkedOutput<String> chunkedOutput); | ||
void handleClose(ChunkedOutput<String> chunkedOutput); | ||
void handleError(Throwable throwable, ChunkedOutput<String> chunkedOutput); | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
@Slf4j | ||
public class LlmProviderStreamHandler { | ||
private static final String UNEXPECTED_ERROR_CALLING_LLM_PROVIDER = "Unexpected error calling LLM provider"; | ||
|
||
public void handleMessage(Object item, ChunkedOutput<String> chunkedOutput) { | ||
if (chunkedOutput.isClosed()) { | ||
log.warn("Output stream is already closed"); | ||
return; | ||
} | ||
try { | ||
chunkedOutput.write(JsonUtils.writeValueAsString(item)); | ||
} catch (IOException ioException) { | ||
throw new UncheckedIOException(ioException); | ||
} | ||
} | ||
|
||
public void handleClose(ChunkedOutput<String> chunkedOutput) { | ||
try { | ||
chunkedOutput.close(); | ||
} catch (IOException ioException) { | ||
log.error("Failed to close output stream", ioException); | ||
} | ||
} | ||
|
||
public void handleError(Throwable throwable, ChunkedOutput<String> chunkedOutput) { | ||
log.error(UNEXPECTED_ERROR_CALLING_LLM_PROVIDER, throwable); | ||
var errorMessage = new ErrorMessage(UNEXPECTED_ERROR_CALLING_LLM_PROVIDER); | ||
if (throwable instanceof OpenAiHttpException openAiHttpException) { | ||
errorMessage = new ErrorMessage(openAiHttpException.code(), openAiHttpException.getMessage()); | ||
} | ||
try { | ||
handleMessage(errorMessage, chunkedOutput); | ||
} catch (UncheckedIOException uncheckedIOException) { | ||
log.error("Failed to stream error message to client", uncheckedIOException); | ||
} | ||
handleClose(chunkedOutput); | ||
} | ||
} |