-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add Hugging Face Rerank support #127966
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
Add Hugging Face Rerank support #127966
Changes from 1 commit
4d9ec59
b58aab4
c567137
a4ebb87
5d316c1
f74e9e5
2ea07f0
0054891
82fd86d
733818c
cb67ac0
f97f818
88d6929
a52a1d8
2eae767
c8c74d6
ae1a1d2
1764a4d
7f30c6a
4ee7f1f
887389f
68755a6
43398ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,22 @@ static String mockDenseServiceModelConfig() { | |
"""; | ||
} | ||
|
||
static String mockRerankServiceModelConfig() { | ||
return """ | ||
{ | ||
"task_type": "rerank", | ||
"service": "rerank_test_service", | ||
"service_settings": { | ||
"model": "rerank_model", | ||
"api_key": "abc64" | ||
}, | ||
"task_settings": { | ||
"return_documents": true | ||
} | ||
} | ||
"""; | ||
} | ||
|
||
static void deleteModel(String modelId) throws IOException { | ||
var request = new Request("DELETE", "_inference/" + modelId); | ||
var response = client().performRequest(request); | ||
|
@@ -484,6 +500,10 @@ private String jsonBody(List<String> input, @Nullable String query) { | |
@SuppressWarnings("unchecked") | ||
protected void assertNonEmptyInferenceResults(Map<String, Object> resultMap, int expectedNumberOfResults, TaskType taskType) { | ||
switch (taskType) { | ||
case RERANK -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this method is not called with TaskType.RERANK param anywhere. meaning assertion isn't triggered. |
||
var results = (List<Map<String, Object>>) resultMap.get(TaskType.RERANK.toString()); | ||
assertThat(results, hasSize(expectedNumberOfResults)); | ||
} | ||
case SPARSE_EMBEDDING -> { | ||
var results = (List<Map<String, Object>>) resultMap.get(TaskType.SPARSE_EMBEDDING.toString()); | ||
assertThat(results, hasSize(expectedNumberOfResults)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ public void parseRequestConfig( | |
) { | ||
try { | ||
Map<String, Object> serviceSettingsMap = removeFromMapOrThrowIfNull(config, ModelConfigurations.SERVICE_SETTINGS); | ||
Map<String, Object> taskSettingsMap = removeFromMapOrThrowIfNull(config, ModelConfigurations.TASK_SETTINGS); | ||
|
||
ChunkingSettings chunkingSettings = null; | ||
if (TaskType.TEXT_EMBEDDING.equals(taskType)) { | ||
|
@@ -65,7 +66,7 @@ public void parseRequestConfig( | |
); | ||
} | ||
|
||
var model = createModel( | ||
var modelBuilder = new HuggingFaceModelInput.Builder( | ||
inferenceEntityId, | ||
taskType, | ||
serviceSettingsMap, | ||
|
@@ -75,8 +76,13 @@ public void parseRequestConfig( | |
ConfigurationParseContext.REQUEST | ||
); | ||
|
||
var model = createModel( | ||
TaskType.RERANK.equals(taskType) ? modelBuilder.withTaskSettings(taskSettingsMap).build() : modelBuilder.build() | ||
); | ||
|
||
throwIfNotEmptyMap(config, name()); | ||
throwIfNotEmptyMap(serviceSettingsMap, name()); | ||
throwIfNotEmptyMap(taskSettingsMap, name()); | ||
|
||
parsedModelListener.onResponse(model); | ||
} catch (Exception e) { | ||
|
@@ -92,14 +98,15 @@ public HuggingFaceModel parsePersistedConfigWithSecrets( | |
Map<String, Object> secrets | ||
) { | ||
Map<String, Object> serviceSettingsMap = removeFromMapOrThrowIfNull(config, ModelConfigurations.SERVICE_SETTINGS); | ||
Map<String, Object> taskSettingsMap = removeFromMapOrThrowIfNull(config, ModelConfigurations.TASK_SETTINGS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct me if I'm wrong. but won't that throw an exception if there are no task settings in config? If so, doesn't that affect other integrations that don't require TASK_SETTINGS to be present? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added Rerank type check to ensure the method isn't used for other tasks |
||
Map<String, Object> secretSettingsMap = removeFromMapOrThrowIfNull(secrets, ModelSecrets.SECRET_SETTINGS); | ||
|
||
ChunkingSettings chunkingSettings = null; | ||
if (TaskType.TEXT_EMBEDDING.equals(taskType)) { | ||
chunkingSettings = ChunkingSettingsBuilder.fromMap(removeFromMap(config, ModelConfigurations.CHUNKING_SETTINGS)); | ||
} | ||
|
||
return createModel( | ||
var modelBuilder = new HuggingFaceModelInput.Builder( | ||
inferenceEntityId, | ||
taskType, | ||
serviceSettingsMap, | ||
|
@@ -108,18 +115,23 @@ public HuggingFaceModel parsePersistedConfigWithSecrets( | |
parsePersistedConfigErrorMsg(inferenceEntityId, name()), | ||
ConfigurationParseContext.PERSISTENT | ||
); | ||
|
||
return createModel( | ||
TaskType.RERANK.equals(taskType) ? modelBuilder.withTaskSettings(taskSettingsMap).build() : modelBuilder.build() | ||
); | ||
} | ||
|
||
@Override | ||
public HuggingFaceModel parsePersistedConfig(String inferenceEntityId, TaskType taskType, Map<String, Object> config) { | ||
Map<String, Object> serviceSettingsMap = removeFromMapOrThrowIfNull(config, ModelConfigurations.SERVICE_SETTINGS); | ||
Map<String, Object> taskSettingsMap = removeFromMapOrThrowIfNull(config, ModelConfigurations.TASK_SETTINGS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added type check before using the methos.Thanks |
||
|
||
ChunkingSettings chunkingSettings = null; | ||
if (TaskType.TEXT_EMBEDDING.equals(taskType)) { | ||
chunkingSettings = ChunkingSettingsBuilder.fromMap(removeFromMap(config, ModelConfigurations.CHUNKING_SETTINGS)); | ||
} | ||
|
||
return createModel( | ||
var modelBuilder = new HuggingFaceModelInput.Builder( | ||
inferenceEntityId, | ||
taskType, | ||
serviceSettingsMap, | ||
|
@@ -128,17 +140,13 @@ public HuggingFaceModel parsePersistedConfig(String inferenceEntityId, TaskType | |
parsePersistedConfigErrorMsg(inferenceEntityId, name()), | ||
ConfigurationParseContext.PERSISTENT | ||
); | ||
|
||
return createModel( | ||
TaskType.RERANK.equals(taskType) ? modelBuilder.withTaskSettings(taskSettingsMap).build() : modelBuilder.build() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the builder accepts null task settings so how about we just pass in the task settings map, regardless of it being null or not. That way we don't need to check for rerank here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The models accept the task settings map as is now. Thanks |
||
); | ||
} | ||
|
||
protected abstract HuggingFaceModel createModel( | ||
String inferenceEntityId, | ||
TaskType taskType, | ||
Map<String, Object> serviceSettings, | ||
ChunkingSettings chunkingSettings, | ||
Map<String, Object> secretSettings, | ||
String failureMessage, | ||
ConfigurationParseContext context | ||
); | ||
protected abstract HuggingFaceModel createModel(HuggingFaceModelInput input); | ||
|
||
@Override | ||
public void doInfer( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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.huggingface; | ||
|
||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.inference.ChunkingSettings; | ||
import org.elasticsearch.inference.TaskType; | ||
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext; | ||
|
||
import java.util.Map; | ||
|
||
public class HuggingFaceModelInput { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we make this a record and maybe rename it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. The record fits better. Thanks. Done |
||
private final String inferenceEntityId; | ||
private final TaskType taskType; | ||
private final Map<String, Object> serviceSettings; | ||
@Nullable | ||
private final Map<String, Object> taskSettings; | ||
private final ChunkingSettings chunkingSettings; | ||
@Nullable | ||
private final Map<String, Object> secretSettings; | ||
private final String failureMessage; | ||
private final ConfigurationParseContext context; | ||
|
||
public HuggingFaceModelInput(Builder builder) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this private? We probably want the instantiation done through the builder. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The builder was replaced with the record as suggested so not needed anymore. |
||
this.inferenceEntityId = builder.inferenceEntityId; | ||
this.taskType = builder.taskType; | ||
this.serviceSettings = builder.serviceSettings; | ||
this.taskSettings = builder.taskSettings; | ||
this.chunkingSettings = builder.chunkingSettings; | ||
this.secretSettings = builder.secretSettings; | ||
this.failureMessage = builder.failureMessage; | ||
this.context = builder.context; | ||
} | ||
|
||
public String getInferenceEntityId() { | ||
return inferenceEntityId; | ||
} | ||
|
||
public TaskType getTaskType() { | ||
return taskType; | ||
} | ||
|
||
public Map<String, Object> getServiceSettings() { | ||
return serviceSettings; | ||
} | ||
|
||
@Nullable | ||
public Map<String, Object> getTaskSettings() { | ||
return taskSettings; | ||
} | ||
|
||
public ChunkingSettings getChunkingSettings() { | ||
return chunkingSettings; | ||
} | ||
|
||
@Nullable | ||
public Map<String, Object> getSecretSettings() { | ||
return secretSettings; | ||
} | ||
|
||
public String getFailureMessage() { | ||
return failureMessage; | ||
} | ||
|
||
public ConfigurationParseContext getContext() { | ||
return context; | ||
} | ||
|
||
public static class Builder { | ||
private String inferenceEntityId; | ||
private TaskType taskType; | ||
private Map<String, Object> serviceSettings; | ||
@Nullable | ||
private Map<String, Object> taskSettings; | ||
private ChunkingSettings chunkingSettings; | ||
@Nullable | ||
Map<String, Object> secretSettings; | ||
private String failureMessage; | ||
private ConfigurationParseContext context; | ||
|
||
public Builder( | ||
String inferenceEntityId, | ||
TaskType taskType, | ||
Map<String, Object> serviceSettings, | ||
ChunkingSettings chunkingSettings, | ||
@Nullable Map<String, Object> secretSettings, | ||
String failureMessage, | ||
ConfigurationParseContext context | ||
) { | ||
this.inferenceEntityId = inferenceEntityId; | ||
this.taskType = taskType; | ||
this.serviceSettings = serviceSettings; | ||
this.chunkingSettings = chunkingSettings; | ||
this.secretSettings = secretSettings; | ||
this.failureMessage = failureMessage; | ||
this.context = context; | ||
} | ||
|
||
public Builder withTaskSettings(Map<String, Object> taskSettings) { | ||
this.taskSettings = taskSettings; | ||
return this; | ||
} | ||
|
||
public HuggingFaceModelInput build() { | ||
return new HuggingFaceModelInput(this); | ||
} | ||
} | ||
} |
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.
I'm wondering if methods you've added to this class are actually used somewhere. Methods you've taken for reference are being called. The ones you've added - are not.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for noticing. It's used now