-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] Integrate with DeepSeek API #122218
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
Merged
Merged
[ML] Integrate with DeepSeek API #122218
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ec94f53
[ML] Integrate with DeepSeek API
prwhelan 746ed7c
Update docs/changelog/122218.yaml
prwhelan 508839a
Merge branch 'main' into deepseek
prwhelan 5d5bd0a
Finish integration
prwhelan 5d4e47b
Merge branch 'main' into deepseek
prwhelan 65e8b26
Fix InferenceGetServicesIT
prwhelan 7009516
Merge branch 'main' of https://github.com/prwhelan/elasticsearch into…
prwhelan 9e9fbc1
Adding 8.x TransportVersion
prwhelan 0173947
Use new error message API
prwhelan aa79769
Move request packages to external
prwhelan 9c31d35
Move max tokens up a level so it can be customized like model id
prwhelan fe5d29c
Fix tests to match the new stream error handling
prwhelan ea24ac1
Adding per-node rate limit comment
prwhelan 3be7f2e
Merge branch 'main' of https://github.com/prwhelan/elasticsearch into…
prwhelan 68dbda3
Fix import statements from merge
prwhelan 9d1482c
Merge branch 'main' of https://github.com/prwhelan/elasticsearch into…
prwhelan 8329b2d
Merge branch 'main' of https://github.com/prwhelan/elasticsearch into…
prwhelan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 122218 | ||
| summary: Integrate with `DeepSeek` API | ||
| area: Machine Learning | ||
| type: enhancement | ||
| issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...n/java/org/elasticsearch/xpack/inference/external/http/sender/DeepSeekRequestManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * 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.openai.OpenAiChatCompletionResponseHandler; | ||
| import org.elasticsearch.xpack.inference.external.openai.OpenAiUnifiedChatCompletionResponseHandler; | ||
| import org.elasticsearch.xpack.inference.external.request.deepseek.DeepSeekChatCompletionRequest; | ||
| import org.elasticsearch.xpack.inference.external.response.openai.OpenAiChatCompletionResponseEntity; | ||
| import org.elasticsearch.xpack.inference.services.deepseek.DeepSeekChatCompletionModel; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static org.elasticsearch.xpack.inference.external.http.sender.InferenceInputs.createUnsupportedTypeException; | ||
|
|
||
| public class DeepSeekRequestManager extends BaseRequestManager { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(DeepSeekRequestManager.class); | ||
|
|
||
| private static final ResponseHandler CHAT_COMPLETION = createChatCompletionHandler(); | ||
| private static final ResponseHandler COMPLETION = createCompletionHandler(); | ||
|
|
||
| private final DeepSeekChatCompletionModel model; | ||
|
|
||
| public DeepSeekRequestManager(DeepSeekChatCompletionModel model, ThreadPool threadPool) { | ||
| super(threadPool, model.getInferenceEntityId(), model.rateLimitGroup(), model.rateLimitSettings()); | ||
| this.model = Objects.requireNonNull(model); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute( | ||
| InferenceInputs inferenceInputs, | ||
| RequestSender requestSender, | ||
| Supplier<Boolean> hasRequestCompletedFunction, | ||
| ActionListener<InferenceServiceResults> listener | ||
| ) { | ||
| switch (inferenceInputs) { | ||
| case UnifiedChatInput uci -> execute(uci, requestSender, hasRequestCompletedFunction, listener); | ||
| case ChatCompletionInput cci -> execute(cci, requestSender, hasRequestCompletedFunction, listener); | ||
| default -> throw createUnsupportedTypeException(inferenceInputs, UnifiedChatInput.class); | ||
| } | ||
| } | ||
|
|
||
| private void execute( | ||
| UnifiedChatInput inferenceInputs, | ||
| RequestSender requestSender, | ||
| Supplier<Boolean> hasRequestCompletedFunction, | ||
| ActionListener<InferenceServiceResults> listener | ||
| ) { | ||
| var request = new DeepSeekChatCompletionRequest(inferenceInputs, model); | ||
| execute(new ExecutableInferenceRequest(requestSender, logger, request, CHAT_COMPLETION, hasRequestCompletedFunction, listener)); | ||
| } | ||
|
|
||
| private void execute( | ||
| ChatCompletionInput inferenceInputs, | ||
| RequestSender requestSender, | ||
| Supplier<Boolean> hasRequestCompletedFunction, | ||
| ActionListener<InferenceServiceResults> listener | ||
| ) { | ||
| var unifiedInputs = new UnifiedChatInput(inferenceInputs.getInputs(), "user", inferenceInputs.stream()); | ||
| var request = new DeepSeekChatCompletionRequest(unifiedInputs, model); | ||
| execute(new ExecutableInferenceRequest(requestSender, logger, request, COMPLETION, hasRequestCompletedFunction, listener)); | ||
| } | ||
|
|
||
| private static ResponseHandler createChatCompletionHandler() { | ||
| return new OpenAiUnifiedChatCompletionResponseHandler("deepseek chat completion", OpenAiChatCompletionResponseEntity::fromResponse); | ||
| } | ||
|
|
||
| private static ResponseHandler createCompletionHandler() { | ||
| return new OpenAiChatCompletionResponseHandler("deepseek completion", OpenAiChatCompletionResponseEntity::fromResponse); | ||
| } | ||
| } |
87 changes: 87 additions & 0 deletions
87
...lasticsearch/xpack/inference/external/request/deepseek/DeepSeekChatCompletionRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * 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.request.deepseek; | ||
|
|
||
| import org.apache.http.HttpHeaders; | ||
| import org.apache.http.client.methods.HttpPost; | ||
| import org.apache.http.entity.ByteArrayEntity; | ||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.xcontent.ToXContent; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
| import org.elasticsearch.xcontent.json.JsonXContent; | ||
| import org.elasticsearch.xpack.inference.external.http.sender.UnifiedChatInput; | ||
| import org.elasticsearch.xpack.inference.external.request.HttpRequest; | ||
| import org.elasticsearch.xpack.inference.external.request.Request; | ||
| import org.elasticsearch.xpack.inference.services.deepseek.DeepSeekChatCompletionModel; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.xpack.inference.external.request.RequestUtils.createAuthBearerHeader; | ||
|
|
||
| public class DeepSeekChatCompletionRequest implements Request { | ||
|
|
||
| private final DeepSeekChatCompletionModel model; | ||
| private final UnifiedChatInput unifiedChatInput; | ||
|
|
||
| public DeepSeekChatCompletionRequest(UnifiedChatInput unifiedChatInput, DeepSeekChatCompletionModel model) { | ||
| this.unifiedChatInput = Objects.requireNonNull(unifiedChatInput); | ||
| this.model = Objects.requireNonNull(model); | ||
| } | ||
|
|
||
| @Override | ||
| public HttpRequest createHttpRequest() { | ||
| HttpPost httpPost = new HttpPost(model.uri()); | ||
|
|
||
| httpPost.setEntity(createEntity()); | ||
|
|
||
| httpPost.setHeader(HttpHeaders.CONTENT_TYPE, XContentType.JSON.mediaType()); | ||
| httpPost.setHeader(createAuthBearerHeader(model.apiKey())); | ||
|
|
||
| return new HttpRequest(httpPost, getInferenceEntityId()); | ||
| } | ||
|
|
||
| private ByteArrayEntity createEntity() { | ||
| try (var builder = JsonXContent.contentBuilder()) { | ||
| new DeepSeekChatCompletionRequestEntity(unifiedChatInput, model).toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
| return new ByteArrayEntity(Strings.toString(builder).getBytes(StandardCharsets.UTF_8)); | ||
| } catch (IOException e) { | ||
| throw new ElasticsearchException("Failed to serialize request payload.", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public URI getURI() { | ||
| return model.uri(); | ||
| } | ||
|
|
||
| @Override | ||
| public Request truncate() { | ||
| // No truncation for OpenAI chat completions | ||
prwhelan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean[] getTruncationInfo() { | ||
| // No truncation for OpenAI chat completions | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getInferenceEntityId() { | ||
| return model.getInferenceEntityId(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isStreaming() { | ||
| return unifiedChatInput.stream(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.