-
Notifications
You must be signed in to change notification settings - Fork 25.2k
[ML] Custom Inference Service #125679
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
base: main
Are you sure you want to change the base?
[ML] Custom Inference Service #125679
Conversation
Hi @davidkyle, I've created a changelog YAML for you. |
...ce/src/main/java/org/elasticsearch/xpack/inference/services/custom/CustomSecretSettings.java
Outdated
Show resolved
Hide resolved
...ence/src/main/java/org/elasticsearch/xpack/inference/services/custom/CustomTaskSettings.java
Outdated
Show resolved
Hide resolved
TimeValue timeout, | ||
ActionListener<List<ChunkedInference>> listener | ||
) { | ||
listener.onFailure(new ElasticsearchStatusException("Chunking not supported by the {} service", RestStatus.BAD_REQUEST, NAME)); |
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.
we will see how to support this in the next PR, to support semantic_text
@weizijun the Elasticsearch security team have advised against adding JsonPATH as a dependency, the last commit to the GitHub project was over a year ago and the project does not appear to be activity maintained. If a critical vulnerability was found in JsonPATH Elasticsearch would be exposed to it and there are no guarantees that the CVE would be fixed. The team at Elastic considered using another Json path library but have decided to implement the features we need ourselves. The Elasticsearch code base already contains a lot of code for parsing JSON that we can use and writing our own implementation avoids adding another dependency. |
d785a6c
to
ad55337
Compare
…icsearch into custom-inference-service
…icsearch into custom-inference-service
@@ -36,7 +36,7 @@ public abstract class BaseResponseHandler implements ResponseHandler { | |||
public static final String METHOD_NOT_ALLOWED = "Received a method not allowed status code"; | |||
|
|||
protected final String requestType; | |||
private final ResponseParser parseFunction; | |||
protected final ResponseParser parseFunction; |
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.
Making this available so the custom response handler can immediately return on a parse failure instead of retrying.
private static final LazyInitializable<InferenceServiceConfiguration, RuntimeException> configuration = new LazyInitializable<>( | ||
() -> { | ||
var configurationMap = new HashMap<String, SettingsConfiguration>(); | ||
// TODO revisit 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.
We'll need to create some more complex configuration types to support the fields (like maps, lists of lists etc). Maybe for now we don't expose this in the services API?
|
||
Map<String, Object> headers = extractOptionalMap(map, HEADERS, ModelConfigurations.SERVICE_SETTINGS, validationException); | ||
removeNullValues(headers); | ||
var stringHeaders = validateMapStringValues(headers, HEADERS, validationException, false); |
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.
This should limit the values in the header map to only strings.
removeNullValues(parameters); | ||
validateMapValues( | ||
parameters, | ||
List.of(String.class, Integer.class, Double.class, Float.class, Boolean.class), |
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.
Restricting the task settings to these types (no nested fields aka maps or lists).
public static final String QUERY_PARAMETERS = "query_parameters"; | ||
|
||
public static QueryParameters fromMap(Map<String, Object> map, ValidationException validationException) { | ||
List<Tuple<String, String>> queryParams = extractOptionalListOfStringTuples( |
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.
Query parameters can have duplicate keys which is why I'm not using a map here.
uri = buildUri(); | ||
} | ||
|
||
private static void addStringParams(Map<String, String> stringParams, Map<String, ?> paramsToAdd) { |
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.
Fields like the url, query parameters, and headers should not have their values converted to json format. This only accepts strings and doesn't manipulate them.
} | ||
} | ||
|
||
private static void addJsonStringParams(Map<String, String> jsonStringParams, Map<String, ?> params) { |
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.
Fields like the request body need to be a valid json object so we'll convert the values into json
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class SerializableSecureString implements ToXContentFragment, Writeable { |
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.
If we need to serialize the api key or some secrets to the body of a request this class will make that process a little easier by implementing toXContent()
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** |
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.
This class is an attempt to push a lot of the duplicate logic in the inference service tests into a central place. If we create more services we should leverage this base class to remove the copy/paste.
Pinging @elastic/ml-core (Team:ML) |
Taking the ideas and commits from #124299
Notable changes from initial PR:
path
andmethod
nestingurl
fieldquery_string
as this can be placed directly in theurl
description
andversion
as they weren't usedsparse_result
andvalue
fieldsresponse.error_parser
to indicate the location to find the error message fieldpath
path
field to tell it where to find that nested mapformat
field that specifies how the response is structure (elser's structure is an array of maps, where the key is the token id and the value is the weight, this parser expects the map to have a token id field and a weight field)Add Custom Model support to Inference API.
You can use this Inference API to invoke models that support the HTTP format.
Inference Endpoint Creation:
Endpoint creation
Support task_type
Parameter Description
Parameter Description
secret_parameters
: secret parameters like api_key can be defined here.headers
(optional):https' header parametersrequest.content
: The body structure of the request requires passing in the string-escaped result of the JSON format HTTP request body.NOTE: Unfortunately, if we aren't using kibana the content string needs to be a single line
response.json_parser
: We need to parse the returned response into an object that Elasticsearch can recognize.(TextEmbeddingFloatResults, SparseEmbeddingResults, RankedDocsResults, ChatCompletionResults)Therefore, we use jsonPath syntax to parse the necessary content from the response.
(For the text_embedding type, we need a
List<List<Float>>
object. The same applies to other types.)Different task types have different json_parser parameters.
response.error_parser
: Since each 3rd party service can have its own error response format we'll need the user to give us the location to retrieve the base error message. For example, openai's error structure is here: https://platform.openai.com/docs/api-reference/realtime-server-events/error. We'd want to extract themessage
field. An example of that might look like:task_settings.parameters
: Due to the limitations of the inference framework, if the model requires more parameters to be configured, they can be set in task_settings.parameters. These parameters can be placed in the request.body as placeholders and replaced with the configured values when constructing the request.Testing
🚧 In progress
Jon Testing
OpenAI
Texting Embedding
Cohere
Rerank
Alibaba Testing
we use Alibaba Cloud AI Search Model for example,
Please replace the value of
secret_parameters.api_key
with your api_key.text_embedding
sparse_embedding
rerank
completion
In the completion module, we demonstrated how to use the
task_settings.parameters
parameter for more flexible parameter configuration.To understand completion interface definition for the Alibaba Cloud AI Search completion API, please refer to the official documentation alibaba cloud ai search completion api doc