-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Add none
chunking strategy to disable automatic chunking for inference endpoints
#129150
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0b4a1d3
Add `none` chunking strategy to disable automatic chunking for infere…
jimczi d766fa7
Update docs/changelog/129150.yaml
jimczi d6026e5
Merge branch 'main' into none_chunking_settings
jimczi fc0634f
Update docs/reference/elasticsearch/mapping-reference/semantic-text.md
jimczi f9d4d47
apply review comment
jimczi 54457ee
add 8.19 transport version
jimczi 11276d3
[CI] Auto commit changes from spotless
elasticsearchmachine 0dfa8aa
apply review comment
jimczi 2db85a5
Merge remote-tracking branch 'origin/none_chunking_settings' into non…
jimczi f88cd5e
Address review comments
jimczi 6889f1b
Merge remote-tracking branch 'upstream/main' into none_chunking_settings
jimczi 780c57b
fix wire tests assuming the instance should be different
jimczi 1f7940a
Merge remote-tracking branch 'upstream/main' into none_chunking_settings
jimczi 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,6 @@ | ||
pr: 129150 | ||
summary: Add `none` chunking strategy to disable automatic chunking for inference | ||
endpoints | ||
area: Machine Learning | ||
type: feature | ||
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
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
110 changes: 110 additions & 0 deletions
110
...erence/src/main/java/org/elasticsearch/xpack/inference/chunking/NoneChunkingSettings.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,110 @@ | ||
/* | ||
* 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.chunking; | ||
|
||
import org.elasticsearch.TransportVersion; | ||
import org.elasticsearch.TransportVersions; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.ValidationException; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.inference.ChunkingSettings; | ||
import org.elasticsearch.inference.ChunkingStrategy; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class NoneChunkingSettings implements ChunkingSettings { | ||
public static final String NAME = "NoneChunkingSettings"; | ||
public static NoneChunkingSettings INSTANCE = new NoneChunkingSettings(); | ||
|
||
private static final ChunkingStrategy STRATEGY = ChunkingStrategy.NONE; | ||
private static final Set<String> VALID_KEYS = Set.of(ChunkingSettingsOptions.STRATEGY.toString()); | ||
|
||
private NoneChunkingSettings() {} | ||
|
||
@Override | ||
public ChunkingStrategy getChunkingStrategy() { | ||
return STRATEGY; | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public TransportVersion getMinimalSupportedVersion() { | ||
throw new IllegalStateException("not used"); | ||
} | ||
|
||
@Override | ||
public boolean supportsVersion(TransportVersion version) { | ||
return version.isPatchFrom(TransportVersions.NONE_CHUNKING_STRATEGY_8_19) | ||
|| version.onOrAfter(TransportVersions.NONE_CHUNKING_STRATEGY); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException {} | ||
|
||
@Override | ||
public Map<String, Object> asMap() { | ||
return Map.of(ChunkingSettingsOptions.STRATEGY.toString(), STRATEGY.toString().toLowerCase(Locale.ROOT)); | ||
} | ||
|
||
public static NoneChunkingSettings fromMap(Map<String, Object> map) { | ||
ValidationException validationException = new ValidationException(); | ||
|
||
var invalidSettings = map.keySet().stream().filter(key -> VALID_KEYS.contains(key) == false).toArray(); | ||
if (invalidSettings.length > 0) { | ||
validationException.addValidationError( | ||
Strings.format( | ||
"When chunking is disabled (none), settings can not have the following: %s", | ||
Arrays.toString(invalidSettings) | ||
) | ||
); | ||
} | ||
|
||
if (validationException.validationErrors().isEmpty() == false) { | ||
throw validationException; | ||
} | ||
|
||
return NoneChunkingSettings.INSTANCE; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
{ | ||
builder.field(ChunkingSettingsOptions.STRATEGY.toString(), STRATEGY); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getClass()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...lugin/inference/src/main/java/org/elasticsearch/xpack/inference/chunking/NoopChunker.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,38 @@ | ||
/* | ||
* 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.chunking; | ||
|
||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.inference.ChunkingSettings; | ||
import org.elasticsearch.xpack.inference.services.openai.embeddings.OpenAiEmbeddingsModel; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A {@link Chunker} implementation that returns the input unchanged (no chunking is performed). | ||
* | ||
* <p><b>WARNING</b>If the input exceeds the maximum token limit, some services (such as {@link OpenAiEmbeddingsModel}) | ||
* may return an error. | ||
* </p> | ||
*/ | ||
public class NoopChunker implements Chunker { | ||
public static final NoopChunker INSTANCE = new NoopChunker(); | ||
|
||
private NoopChunker() {} | ||
|
||
@Override | ||
public List<ChunkOffset> chunk(String input, ChunkingSettings chunkingSettings) { | ||
if (chunkingSettings instanceof NoneChunkingSettings) { | ||
return List.of(new ChunkOffset(0, input.length())); | ||
} else { | ||
throw new IllegalArgumentException( | ||
Strings.format("NoopChunker can't use ChunkingSettings with strategy [%s]", chunkingSettings.getChunkingStrategy()) | ||
); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.