-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] Parse mid-stream errors from OpenAI and EIS #121806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2d82bce
[ML] Parse mid-stream errors from OpenAI and EIS
prwhelan 1073eda
Update docs/changelog/121806.yaml
prwhelan 9134c19
use stream_error for both
prwhelan 43de566
address comments
prwhelan b8d6ba9
Merge branch 'main' into inference/stream-error
prwhelan 667b018
Delete docs/changelog/121806.yaml
prwhelan e7364a6
Merge branch 'main' into inference/stream-error
prwhelan 3caea1e
Merge branch 'main' into inference/stream-error
prwhelan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
pr: 121806 | ||
summary: Parse mid-stream errors from OpenAI and EIS | ||
area: Machine Learning | ||
type: bug | ||
issues: [] |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import org.elasticsearch.xpack.core.inference.results.StreamingUnifiedChatCompletionResults; | ||
import org.elasticsearch.xpack.inference.common.DelegatingProcessor; | ||
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEvent; | ||
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEventField; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayDeque; | ||
|
@@ -28,6 +29,7 @@ | |
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
import java.util.function.BiFunction; | ||
|
||
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
import static org.elasticsearch.xpack.inference.external.response.XContentUtils.moveToFirstToken; | ||
|
@@ -57,7 +59,13 @@ public class OpenAiUnifiedStreamingProcessor extends DelegatingProcessor<Deque<S | |
public static final String PROMPT_TOKENS_FIELD = "prompt_tokens"; | ||
public static final String TOTAL_TOKENS_FIELD = "total_tokens"; | ||
|
||
private final BiFunction<String, Exception, Exception> errorParser; | ||
private final Deque<StreamingUnifiedChatCompletionResults.ChatCompletionChunk> buffer = new LinkedBlockingDeque<>(); | ||
private volatile boolean previousEventWasNotError = true; | ||
|
||
public OpenAiUnifiedStreamingProcessor(BiFunction<String, Exception, Exception> errorParser) { | ||
this.errorParser = errorParser; | ||
} | ||
|
||
@Override | ||
protected void upstreamRequest(long n) { | ||
|
@@ -71,7 +79,25 @@ protected void upstreamRequest(long n) { | |
@Override | ||
protected void next(Deque<ServerSentEvent> item) throws Exception { | ||
var parserConfig = XContentParserConfiguration.EMPTY.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE); | ||
var results = parseEvent(item, OpenAiUnifiedStreamingProcessor::parse, parserConfig, logger); | ||
|
||
var results = new ArrayDeque<StreamingUnifiedChatCompletionResults.ChatCompletionChunk>(item.size()); | ||
for (var event : item) { | ||
if (ServerSentEventField.EVENT == event.name() && "error".equals(event.value())) { | ||
previousEventWasNotError = false; | ||
|
||
} else if (ServerSentEventField.DATA == event.name() && event.hasValue()) { | ||
if (previousEventWasNotError) { | ||
try { | ||
var delta = parse(parserConfig, event); | ||
delta.forEachRemaining(results::offer); | ||
} catch (Exception e) { | ||
logger.warn("Failed to parse event from inference provider: {}", event); | ||
throw errorParser.apply(event.value(), e); | ||
} | ||
} else { | ||
throw errorParser.apply(event.value(), null); | ||
} | ||
} | ||
} | ||
|
||
if (results.isEmpty()) { | ||
upstream().request(1); | ||
|
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be the same as the EIS one?