- 
                Notifications
    You must be signed in to change notification settings 
- Fork 25.6k
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
Changes from 7 commits
78ab1da
              f92f348
              510e3c5
              d6d19be
              a6eaec6
              9faf6f6
              b23bdfb
              136416d
              ff6ccf5
              80537a4
              b44bab6
              b1a76c3
              1bf81ed
              e70752f
              b219e72
              c950380
              bf882a0
              f9b086f
              8e08b9e
              08ab2f6
              4ed865c
              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,5 @@ | ||
| pr: 129146 | ||
| summary: "[ML] Add IBM watsonx Completion and Chat Completion support to the Inference Plugin" | ||
| area: Machine Learning | ||
| type: enhancement | ||
| issues: [] | 
| 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,18 +7,19 @@ | |
|  | ||
| 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.util.Map; | ||
| import java.util.Objects; | ||
|  | ||
| public abstract class IbmWatsonxModel extends Model { | ||
| public abstract class IbmWatsonxModel extends 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. Can you clarify why this needs to be a  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 type needs to be used in GenericRequestManager | ||
|  | ||
| private final IbmWatsonxRateLimitServiceSettings rateLimitServiceSettings; | ||
|  | ||
|  | @@ -49,4 +50,14 @@ public IbmWatsonxModel(IbmWatsonxModel model, TaskSettings taskSettings) { | |
| public IbmWatsonxRateLimitServiceSettings rateLimitServiceSettings() { | ||
| return rateLimitServiceSettings; | ||
| } | ||
|  | ||
| @Override | ||
| public int rateLimitGroupingHash() { | ||
| return Objects.hash(this.rateLimitServiceSettings); | ||
| } | ||
|  | ||
| @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.
Given that we're only merging to 9.2 (no longer backporting new features to 8.19) we shouldn't need this transport version anymore. Would you be able to remove it and any references to it?
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.
Thank you, Daniel.
It was supposed to be used for a backport and has no references in the current PR so I just removed the constant.
I will also close a spec backport PR as not required then