-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Add Ibm Granite Completion and Chat Completion support #129146
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?
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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.ibmwatsonx; | ||
|
||
import org.elasticsearch.xpack.core.inference.results.UnifiedChatCompletionException; | ||
import org.elasticsearch.xpack.inference.external.http.HttpResult; | ||
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse; | ||
import org.elasticsearch.xpack.inference.external.http.retry.ResponseParser; | ||
import org.elasticsearch.xpack.inference.external.request.Request; | ||
import org.elasticsearch.xpack.inference.services.ibmwatsonx.response.IbmWatsonxErrorResponseEntity; | ||
import org.elasticsearch.xpack.inference.services.openai.OpenAiUnifiedChatCompletionResponseHandler; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* Handles streaming chat completion responses and error parsing for Watsonx inference endpoints. | ||
* Adapts the OpenAI handler to support Watsonx's error schema. | ||
*/ | ||
public class IbmWatsonUnifiedChatCompletionResponseHandler extends OpenAiUnifiedChatCompletionResponseHandler { | ||
|
||
private static final String WATSONX_ERROR = "watsonx_error"; | ||
|
||
public IbmWatsonUnifiedChatCompletionResponseHandler(String requestType, ResponseParser parseFunction) { | ||
super(requestType, parseFunction, IbmWatsonxErrorResponseEntity::fromResponse); | ||
} | ||
|
||
@Override | ||
protected Exception buildError(String message, Request request, HttpResult result, ErrorResponse errorResponse) { | ||
assert request.isStreaming() : "Only streaming requests support this format"; | ||
var responseStatusCode = result.response().getStatusLine().getStatusCode(); | ||
if (request.isStreaming()) { | ||
var errorMessage = errorMessage(message, request, result, errorResponse, responseStatusCode); | ||
var restStatus = toRestStatus(responseStatusCode); | ||
return errorResponse instanceof IbmWatsonxErrorResponseEntity | ||
? new UnifiedChatCompletionException(restStatus, errorMessage, WATSONX_ERROR, restStatus.name().toLowerCase(Locale.ROOT)) | ||
: new UnifiedChatCompletionException( | ||
restStatus, | ||
errorMessage, | ||
createErrorType(errorResponse), | ||
restStatus.name().toLowerCase(Locale.ROOT) | ||
); | ||
} else { | ||
return super.buildError(message, request, result, errorResponse); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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.ibmwatsonx; | ||
|
||
import org.elasticsearch.xpack.inference.external.http.retry.ResponseParser; | ||
import org.elasticsearch.xpack.inference.services.ibmwatsonx.response.IbmWatsonxErrorResponseEntity; | ||
import org.elasticsearch.xpack.inference.services.openai.OpenAiChatCompletionResponseHandler; | ||
|
||
public class IbmWatsonxCompletionResponseHandler extends OpenAiChatCompletionResponseHandler { | ||
|
||
/** | ||
* Constructs a IbmWatsonxCompletionResponseHandler with the specified request type and response parser. | ||
* | ||
* @param requestType The type of request being handled (e.g., "IBM Watsonx completions"). | ||
* @param parseFunction The function to parse the response. | ||
*/ | ||
public IbmWatsonxCompletionResponseHandler(String requestType, ResponseParser parseFunction) { | ||
super(requestType, parseFunction, IbmWatsonxErrorResponseEntity::fromResponse); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,21 +7,25 @@ | |
|
||
package org.elasticsearch.xpack.inference.services.ibmwatsonx; | ||
|
||
import org.elasticsearch.inference.Model; | ||
import org.elasticsearch.inference.ModelConfigurations; | ||
import org.elasticsearch.inference.ModelSecrets; | ||
import org.elasticsearch.inference.ServiceSettings; | ||
import org.elasticsearch.inference.TaskSettings; | ||
import org.elasticsearch.xpack.inference.external.action.ExecutableAction; | ||
import org.elasticsearch.xpack.inference.services.RateLimitGroupingModel; | ||
import org.elasticsearch.xpack.inference.services.ibmwatsonx.action.IbmWatsonxActionVisitor; | ||
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings; | ||
|
||
import java.net.URI; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public abstract class IbmWatsonxModel extends Model { | ||
public abstract class IbmWatsonxModel extends RateLimitGroupingModel { | ||
|
||
private final IbmWatsonxRateLimitServiceSettings rateLimitServiceSettings; | ||
|
||
protected URI uri; | ||
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. Is URI only going to be used for completion/chat completion use cases? If yes, can it be in the completion model implementation instead? |
||
|
||
public IbmWatsonxModel( | ||
ModelConfigurations configurations, | ||
ModelSecrets secrets, | ||
|
@@ -49,4 +53,14 @@ public IbmWatsonxModel(IbmWatsonxModel model, TaskSettings taskSettings) { | |
public IbmWatsonxRateLimitServiceSettings rateLimitServiceSettings() { | ||
return rateLimitServiceSettings; | ||
} | ||
|
||
@Override | ||
public int rateLimitGroupingHash() { | ||
return Objects.hash(uri); | ||
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. Why does this not need to include the |
||
} | ||
|
||
@Override | ||
public RateLimitSettings rateLimitSettings() { | ||
return this.rateLimitServiceSettings().rateLimitSettings(); | ||
} | ||
} |
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.
Can you clarify why this needs to be a
RateLimitGroupingModel
?