-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Extract common ServiceSettings methods #105553
Merged
carlosdelest
merged 5 commits into
elastic:main
from
carlosdelest:carlosdelest/refactor-similarity-to-server
Feb 20, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bd6abbc
Move SimilarityMeasure to server code
carlosdelest 587ced2
Add dimensions and similarity to ServiceSettings, create ModelSetting…
carlosdelest 8b978cd
Revert javadoc changes
carlosdelest dfc2e54
Merge branch 'main' into carlosdelest/refactor-similarity-to-server
carlosdelest 9d5279e
Add javadoc, small refactorings
carlosdelest 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
77 changes: 77 additions & 0 deletions
77
server/src/main/java/org/elasticsearch/inference/ModelSettings.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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.inference; | ||
|
||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.xcontent.ParseField; | ||
import org.elasticsearch.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public record ModelSettings(TaskType taskType, String inferenceId, @Nullable Integer dimensions, @Nullable SimilarityMeasure similarity) { | ||
|
||
public static final String NAME = "model_settings"; | ||
private static final ParseField TASK_TYPE_FIELD = new ParseField("task_type"); | ||
private static final ParseField INFERENCE_ID_FIELD = new ParseField("inference_id"); | ||
private static final ParseField DIMENSIONS_FIELD = new ParseField("dimensions"); | ||
private static final ParseField SIMILARITY_FIELD = new ParseField("similarity"); | ||
|
||
public ModelSettings(TaskType taskType, String inferenceId, Integer dimensions, SimilarityMeasure similarity) { | ||
Objects.requireNonNull(taskType, "task type must not be null"); | ||
Objects.requireNonNull(inferenceId, "inferenceId must not be null"); | ||
this.taskType = taskType; | ||
this.inferenceId = inferenceId; | ||
this.dimensions = dimensions; | ||
this.similarity = similarity; | ||
} | ||
|
||
public ModelSettings(Model model) { | ||
this( | ||
model.getTaskType(), | ||
model.getInferenceEntityId(), | ||
model.getServiceSettings().dimensions(), | ||
model.getServiceSettings().similarity() | ||
); | ||
} | ||
|
||
public static ModelSettings parse(XContentParser parser) throws IOException { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
private static final ConstructingObjectParser<ModelSettings, Void> PARSER = new ConstructingObjectParser<>(NAME, args -> { | ||
TaskType taskType = TaskType.fromString((String) args[0]); | ||
String inferenceId = (String) args[1]; | ||
Integer dimensions = (Integer) args[2]; | ||
SimilarityMeasure similarity = args[3] == null ? null : SimilarityMeasure.fromString((String) args[2]); | ||
return new ModelSettings(taskType, inferenceId, dimensions, similarity); | ||
}); | ||
static { | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), TASK_TYPE_FIELD); | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), INFERENCE_ID_FIELD); | ||
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), DIMENSIONS_FIELD); | ||
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), SIMILARITY_FIELD); | ||
} | ||
|
||
public Map<String, Object> asMap() { | ||
Map<String, Object> attrsMap = new HashMap<>(); | ||
attrsMap.put(TASK_TYPE_FIELD.getPreferredName(), taskType.toString()); | ||
attrsMap.put(INFERENCE_ID_FIELD.getPreferredName(), inferenceId); | ||
if (dimensions != null) { | ||
attrsMap.put(DIMENSIONS_FIELD.getPreferredName(), dimensions); | ||
} | ||
if (similarity != null) { | ||
attrsMap.put(SIMILARITY_FIELD.getPreferredName(), similarity); | ||
} | ||
return Map.of(NAME, attrsMap); | ||
} | ||
} |
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
7 changes: 4 additions & 3 deletions
7
...k/inference/common/SimilarityMeasure.java → ...icsearch/inference/SimilarityMeasure.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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
Fixed this bug for inference