-
Notifications
You must be signed in to change notification settings - Fork 25.3k
[ML] Refactor OpenAI request managers #124144
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
Changes from all commits
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
package org.elasticsearch.xpack.inference.external.http.sender; | ||
|
||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.xpack.inference.services.RateLimitGroupingModel; | ||
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings; | ||
|
||
import java.util.Objects; | ||
|
@@ -17,14 +18,31 @@ | |
abstract class BaseRequestManager implements RequestManager { | ||
private final ThreadPool threadPool; | ||
private final String inferenceEntityId; | ||
private final Object rateLimitGroup; | ||
// It's possible that two inference endpoints have the same information defining the group but have different | ||
// rate limits then they should be in different groups otherwise whoever initially created the group will set | ||
// the rate and the other inference endpoint's rate will be ignored | ||
private final EndpointGrouping endpointGrouping; | ||
private final RateLimitSettings rateLimitSettings; | ||
|
||
BaseRequestManager(ThreadPool threadPool, String inferenceEntityId, Object rateLimitGroup, RateLimitSettings rateLimitSettings) { | ||
this.threadPool = Objects.requireNonNull(threadPool); | ||
this.inferenceEntityId = Objects.requireNonNull(inferenceEntityId); | ||
this.rateLimitGroup = Objects.requireNonNull(rateLimitGroup); | ||
this.rateLimitSettings = Objects.requireNonNull(rateLimitSettings); | ||
|
||
Objects.requireNonNull(rateLimitSettings); | ||
this.endpointGrouping = new EndpointGrouping(Objects.requireNonNull(rateLimitGroup).hashCode(), rateLimitSettings); | ||
this.rateLimitSettings = rateLimitSettings; | ||
} | ||
|
||
BaseRequestManager(ThreadPool threadPool, RateLimitGroupingModel rateLimitGroupingModel) { | ||
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. This is a stopgap. Once all the request managers are refactored the old constructor can be removed. |
||
this.threadPool = Objects.requireNonNull(threadPool); | ||
Objects.requireNonNull(rateLimitGroupingModel); | ||
|
||
this.inferenceEntityId = rateLimitGroupingModel.inferenceEntityId(); | ||
this.endpointGrouping = new EndpointGrouping( | ||
rateLimitGroupingModel.rateLimitGroupingHash(), | ||
rateLimitGroupingModel.rateLimitSettings() | ||
); | ||
this.rateLimitSettings = rateLimitGroupingModel.rateLimitSettings(); | ||
} | ||
|
||
protected void execute(Runnable runnable) { | ||
|
@@ -38,16 +56,13 @@ public String inferenceEntityId() { | |
|
||
@Override | ||
public Object rateLimitGrouping() { | ||
// It's possible that two inference endpoints have the same information defining the group but have different | ||
// rate limits then they should be in different groups otherwise whoever initially created the group will set | ||
// the rate and the other inference endpoint's rate will be ignored | ||
return new EndpointGrouping(rateLimitGroup, rateLimitSettings); | ||
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. We don't need to be recreating the object on each call. |
||
return endpointGrouping; | ||
} | ||
|
||
@Override | ||
public RateLimitSettings rateLimitSettings() { | ||
return rateLimitSettings; | ||
} | ||
|
||
private record EndpointGrouping(Object group, RateLimitSettings settings) {} | ||
private record EndpointGrouping(int group, RateLimitSettings settings) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.http.sender; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.inference.InferenceServiceResults; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.xpack.inference.external.http.retry.RequestSender; | ||
import org.elasticsearch.xpack.inference.external.http.retry.ResponseHandler; | ||
import org.elasticsearch.xpack.inference.external.request.Request; | ||
import org.elasticsearch.xpack.inference.services.RateLimitGroupingModel; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* This is a temporary class to use while we refactor all the request managers. After all the request managers extend | ||
* this class we'll move this functionality directly into the {@link BaseRequestManager}. | ||
*/ | ||
public class GenericRequestManager<T extends InferenceInputs> extends BaseRequestManager { | ||
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. Once all the request managers are refactored, I envision that we'll be able to move this logic up into the base class. |
||
private static final Logger logger = LogManager.getLogger(GenericRequestManager.class); | ||
|
||
protected final ResponseHandler responseHandler; | ||
protected final Function<T, Request> requestCreator; | ||
protected final Class<T> inputType; | ||
|
||
public GenericRequestManager( | ||
ThreadPool threadPool, | ||
RateLimitGroupingModel rateLimitGroupingModel, | ||
ResponseHandler responseHandler, | ||
Function<T, Request> requestCreator, | ||
Class<T> inputType | ||
) { | ||
super(threadPool, rateLimitGroupingModel); | ||
this.responseHandler = Objects.requireNonNull(responseHandler); | ||
this.requestCreator = Objects.requireNonNull(requestCreator); | ||
this.inputType = Objects.requireNonNull(inputType); | ||
} | ||
|
||
@Override | ||
public void execute( | ||
InferenceInputs inferenceInputs, | ||
RequestSender requestSender, | ||
Supplier<Boolean> hasRequestCompletedFunction, | ||
ActionListener<InferenceServiceResults> listener | ||
) { | ||
var request = requestCreator.apply(inferenceInputs.castTo(inputType)); | ||
|
||
execute(new ExecutableInferenceRequest(requestSender, logger, request, responseHandler, hasRequestCompletedFunction, listener)); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
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 changes are basically to move the logic from the request manager files into here.