Skip to content

Commit 55d9014

Browse files
Enhance Llama streaming processing by adding support for nullable object arrays
1 parent ceef95a commit 55d9014

File tree

4 files changed

+13
-309
lines changed

4 files changed

+13
-309
lines changed

libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,18 @@ public <T> void declareField(BiConsumer<Value, T> consumer, ContextParser<Contex
220220
}
221221
}
222222

223+
@Override
224+
public <T> void declareObjectArrayOrNull(BiConsumer<Value, List<T>> consumer,
225+
ContextParser<Context, T> objectParser,
226+
ParseField field) {
227+
declareField(
228+
consumer,
229+
(p, c) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : parseArray(p, c, objectParser),
230+
field,
231+
ValueType.OBJECT_ARRAY_OR_NULL
232+
);
233+
}
234+
223235
@Override
224236
public <T> void declareNamedObject(
225237
BiConsumer<Value, T> consumer,

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/llama/completion/LlamaChatCompletionResponseHandler.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@
77

88
package org.elasticsearch.xpack.inference.services.llama.completion;
99

10-
import org.elasticsearch.inference.InferenceServiceResults;
11-
import org.elasticsearch.xpack.core.inference.results.StreamingUnifiedChatCompletionResults;
1210
import org.elasticsearch.xpack.core.inference.results.UnifiedChatCompletionException;
1311
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1412
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
1513
import org.elasticsearch.xpack.inference.external.http.retry.ResponseParser;
1614
import org.elasticsearch.xpack.inference.external.request.Request;
17-
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEventParser;
18-
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEventProcessor;
1915
import org.elasticsearch.xpack.inference.services.llama.response.LlamaErrorResponse;
2016
import org.elasticsearch.xpack.inference.services.openai.OpenAiUnifiedChatCompletionResponseHandler;
2117

2218
import java.util.Locale;
23-
import java.util.concurrent.Flow;
2419

2520
public class LlamaChatCompletionResponseHandler extends OpenAiUnifiedChatCompletionResponseHandler {
2621

@@ -49,13 +44,4 @@ protected Exception buildError(String message, Request request, HttpResult resul
4944
return super.buildError(message, request, result, errorResponse);
5045
}
5146
}
52-
53-
@Override
54-
public InferenceServiceResults parseResult(Request request, Flow.Publisher<HttpResult> flow) {
55-
var serverSentEventProcessor = new ServerSentEventProcessor(new ServerSentEventParser());
56-
var openAiProcessor = new LlamaStreamingProcessor((m, e) -> buildMidStreamError(request, m, e));
57-
flow.subscribe(serverSentEventProcessor);
58-
serverSentEventProcessor.subscribe(openAiProcessor);
59-
return new StreamingUnifiedChatCompletionResults(openAiProcessor);
60-
}
6147
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/llama/completion/LlamaStreamingProcessor.java

Lines changed: 0 additions & 294 deletions
This file was deleted.

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/openai/OpenAiUnifiedStreamingProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private static class DeltaParser {
198198
PARSER.declareStringOrNull(ConstructingObjectParser.optionalConstructorArg(), new ParseField(CONTENT_FIELD));
199199
PARSER.declareStringOrNull(ConstructingObjectParser.optionalConstructorArg(), new ParseField(REFUSAL_FIELD));
200200
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField(ROLE_FIELD));
201-
PARSER.declareObjectArray(
201+
PARSER.declareObjectArrayOrNull(
202202
ConstructingObjectParser.optionalConstructorArg(),
203203
(p, c) -> ChatCompletionChunkParser.ToolCallParser.parse(p),
204204
new ParseField(TOOL_CALLS_FIELD)

0 commit comments

Comments
 (0)