-
Notifications
You must be signed in to change notification settings - Fork 25.5k
[8.19] Add Mistral AI Chat Completion support to Inference Plugin (#128538) #128947
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
jonathan-buttner
merged 8 commits into
elastic:8.19
from
Jan-Kazlouski-elastic:backport/8.19/pr-128538
Jun 9, 2025
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f6e3ce
Change versions for Mistral Chat Completion version
Jan-Kazlouski-elastic 7fdc77c
Refactor model handling in MistralService to use instanceof for clean…
Jan-Kazlouski-elastic 852fdd6
Merge branch '8.19' into backport/8.19/pr-128538
Jan-Kazlouski-elastic e7556b0
Merge remote-tracking branch 'upstream/8.19' into backport/8.19/pr-12…
Jan-Kazlouski-elastic 1068ecf
Merge remote-tracking branch 'upstream/8.19' into backport/8.19/pr-12…
Jan-Kazlouski-elastic ec31b92
Merge remote-tracking branch 'upstream/8.19' into backport/8.19/pr-12…
Jan-Kazlouski-elastic 706ea05
Update Mistral Chat Completion 8.19 Version
Jan-Kazlouski-elastic 7af427c
Merge remote-tracking branch 'upstream/8.19' into backport/8.19/pr-12…
Jan-Kazlouski-elastic 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: 128538 | ||
summary: "Added Mistral Chat Completion support to the Inference Plugin" | ||
area: Machine Learning | ||
type: enhancement | ||
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
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
128 changes: 128 additions & 0 deletions
128
...org/elasticsearch/xpack/inference/external/response/streaming/StreamingErrorResponse.java
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,128 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.inference.external.response.streaming; | ||
|
||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.xcontent.ParseField; | ||
import org.elasticsearch.xcontent.XContentFactory; | ||
import org.elasticsearch.xcontent.XContentParser; | ||
import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
import org.elasticsearch.xcontent.XContentType; | ||
import org.elasticsearch.xpack.inference.external.http.HttpResult; | ||
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse; | ||
import org.elasticsearch.xpack.inference.external.response.ErrorMessageResponseEntity; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Represents an error response from a streaming inference service. | ||
* This class extends {@link ErrorResponse} and provides additional fields | ||
* specific to streaming errors, such as code, param, and type. | ||
* An example error response for a streaming service might look like: | ||
* <pre><code> | ||
* { | ||
* "error": { | ||
* "message": "Invalid input", | ||
* "code": "400", | ||
* "param": "input", | ||
* "type": "invalid_request_error" | ||
* } | ||
* } | ||
* </code></pre> | ||
* TODO: {@link ErrorMessageResponseEntity} is nearly identical to this, but doesn't parse as many fields. We must remove the duplication. | ||
*/ | ||
public class StreamingErrorResponse extends ErrorResponse { | ||
private static final ConstructingObjectParser<Optional<ErrorResponse>, Void> ERROR_PARSER = new ConstructingObjectParser<>( | ||
"streaming_error", | ||
true, | ||
args -> Optional.ofNullable((StreamingErrorResponse) args[0]) | ||
); | ||
private static final ConstructingObjectParser<StreamingErrorResponse, Void> ERROR_BODY_PARSER = new ConstructingObjectParser<>( | ||
"streaming_error", | ||
true, | ||
args -> new StreamingErrorResponse((String) args[0], (String) args[1], (String) args[2], (String) args[3]) | ||
); | ||
|
||
static { | ||
ERROR_BODY_PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("message")); | ||
ERROR_BODY_PARSER.declareStringOrNull(ConstructingObjectParser.optionalConstructorArg(), new ParseField("code")); | ||
ERROR_BODY_PARSER.declareStringOrNull(ConstructingObjectParser.optionalConstructorArg(), new ParseField("param")); | ||
ERROR_BODY_PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("type")); | ||
|
||
ERROR_PARSER.declareObjectOrNull( | ||
ConstructingObjectParser.optionalConstructorArg(), | ||
ERROR_BODY_PARSER, | ||
null, | ||
new ParseField("error") | ||
); | ||
} | ||
|
||
/** | ||
* Standard error response parser. This can be overridden for those subclasses that | ||
* have a different error response structure. | ||
* @param response The error response as an HttpResult | ||
*/ | ||
public static ErrorResponse fromResponse(HttpResult response) { | ||
try ( | ||
XContentParser parser = XContentFactory.xContent(XContentType.JSON) | ||
.createParser(XContentParserConfiguration.EMPTY, response.body()) | ||
) { | ||
return ERROR_PARSER.apply(parser, null).orElse(ErrorResponse.UNDEFINED_ERROR); | ||
} catch (Exception e) { | ||
// swallow the error | ||
} | ||
|
||
return ErrorResponse.UNDEFINED_ERROR; | ||
} | ||
|
||
/** | ||
* Standard error response parser. This can be overridden for those subclasses that | ||
* have a different error response structure. | ||
* @param response The error response as a string | ||
*/ | ||
public static ErrorResponse fromString(String response) { | ||
try ( | ||
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(XContentParserConfiguration.EMPTY, response) | ||
) { | ||
return ERROR_PARSER.apply(parser, null).orElse(ErrorResponse.UNDEFINED_ERROR); | ||
} catch (Exception e) { | ||
// swallow the error | ||
} | ||
|
||
return ErrorResponse.UNDEFINED_ERROR; | ||
} | ||
|
||
@Nullable | ||
private final String code; | ||
@Nullable | ||
private final String param; | ||
private final String type; | ||
|
||
StreamingErrorResponse(String errorMessage, @Nullable String code, @Nullable String param, String type) { | ||
super(errorMessage); | ||
this.code = code; | ||
this.param = param; | ||
this.type = Objects.requireNonNull(type); | ||
} | ||
|
||
@Nullable | ||
public String code() { | ||
return code; | ||
} | ||
|
||
@Nullable | ||
public String param() { | ||
return param; | ||
} | ||
|
||
public String type() { | ||
return type; | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
.../org/elasticsearch/xpack/inference/services/mistral/MistralCompletionResponseHandler.java
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,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.inference.services.mistral; | ||
|
||
import org.elasticsearch.xpack.inference.external.http.retry.ResponseParser; | ||
import org.elasticsearch.xpack.inference.services.mistral.response.MistralErrorResponse; | ||
import org.elasticsearch.xpack.inference.services.openai.OpenAiChatCompletionResponseHandler; | ||
|
||
/** | ||
* Handles non-streaming completion responses for Mistral models, extending the OpenAI completion response handler. | ||
* This class is specifically designed to handle Mistral's error response format. | ||
*/ | ||
public class MistralCompletionResponseHandler extends OpenAiChatCompletionResponseHandler { | ||
|
||
/** | ||
* Constructs a MistralCompletionResponseHandler with the specified request type and response parser. | ||
* | ||
* @param requestType The type of request being handled (e.g., "mistral completions"). | ||
* @param parseFunction The function to parse the response. | ||
*/ | ||
public MistralCompletionResponseHandler(String requestType, ResponseParser parseFunction) { | ||
super(requestType, parseFunction, MistralErrorResponse::fromResponse); | ||
} | ||
} |
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.
According to the comment from @bpintea left here: #128979 (comment)
TransportVersion for Mistral Completion changes was deleted.
So I'm adding version to TransportVersion as number that doesn't match initial version from main branch.
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.
@Jan-Kazlouski-elastic can you open a PR that only adds the transport version
ML_INFERENCE_MISTRAL_CHAT_COMPLETION_ADDED_8_19
to main? I think we need to reserve it there first and then once we merge that PR we can set the correct value in this PR.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.
Oh sorry just saw this: https://github.com/elastic/elasticsearch/pull/129033/files
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.
Merged latest changes from 8.19 so it could be merged quicker if approved.