-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add support for removing thinking tags from input text in BeanOutputConverter #4667
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
Open
liugddx
wants to merge
4
commits into
spring-projects:main
Choose a base branch
from
liugddx:bugfix-4650
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+853
−25
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
58ed2e7
feat: add support for removing thinking tags from input text in BeanO…
liugddx 548cf82
style: apply spring-javaformat to fix formatting violations
liugddx d997c80
feat: enhance BeanOutputConverter with customizable text cleaning cap…
liugddx 8927879
feat: enhance BeanOutputConverter with customizable text cleaning cap…
liugddx 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
108 changes: 108 additions & 0 deletions
108
...ai-model/src/main/java/org/springframework/ai/converter/CompositeResponseTextCleaner.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,108 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.converter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A composite {@link ResponseTextCleaner} that applies multiple cleaners in sequence. | ||
* This allows for a flexible pipeline of text cleaning operations. | ||
* | ||
* @author liugddx | ||
* @since 1.0.0 | ||
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. It will be probably 1.1.0. To be confirmed. |
||
*/ | ||
public class CompositeResponseTextCleaner implements ResponseTextCleaner { | ||
|
||
private final List<ResponseTextCleaner> cleaners; | ||
|
||
/** | ||
* Creates a composite cleaner with the given cleaners. | ||
* @param cleaners the list of cleaners to apply in order | ||
*/ | ||
public CompositeResponseTextCleaner(List<ResponseTextCleaner> cleaners) { | ||
Assert.notNull(cleaners, "cleaners cannot be null"); | ||
this.cleaners = new ArrayList<>(cleaners); | ||
} | ||
|
||
/** | ||
* Creates a composite cleaner with no cleaners. Text will be returned unchanged. | ||
*/ | ||
public CompositeResponseTextCleaner() { | ||
this(new ArrayList<>()); | ||
} | ||
|
||
/** | ||
* Creates a composite cleaner with the given cleaners. | ||
* @param cleaners the cleaners to apply in order | ||
*/ | ||
public CompositeResponseTextCleaner(ResponseTextCleaner... cleaners) { | ||
this(Arrays.asList(cleaners)); | ||
} | ||
|
||
@Override | ||
public String clean(String text) { | ||
String result = text; | ||
for (ResponseTextCleaner cleaner : this.cleaners) { | ||
result = cleaner.clean(result); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Creates a builder for constructing a composite cleaner. | ||
* @return a new builder instance | ||
*/ | ||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
/** | ||
* Builder for {@link CompositeResponseTextCleaner}. | ||
*/ | ||
public static final class Builder { | ||
|
||
private final List<ResponseTextCleaner> cleaners = new ArrayList<>(); | ||
|
||
private Builder() { | ||
} | ||
|
||
/** | ||
* Add a cleaner to the pipeline. | ||
* @param cleaner the cleaner to add | ||
* @return this builder | ||
*/ | ||
public Builder addCleaner(ResponseTextCleaner cleaner) { | ||
Assert.notNull(cleaner, "cleaner cannot be null"); | ||
this.cleaners.add(cleaner); | ||
return this; | ||
} | ||
|
||
/** | ||
* Build the composite cleaner. | ||
* @return a new composite cleaner instance | ||
*/ | ||
public CompositeResponseTextCleaner build() { | ||
return new CompositeResponseTextCleaner(this.cleaners); | ||
} | ||
|
||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
spring-ai-model/src/main/java/org/springframework/ai/converter/MarkdownCodeBlockCleaner.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,73 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.converter; | ||
|
||
/** | ||
* A {@link ResponseTextCleaner} that removes markdown code block formatting from LLM | ||
* responses. This cleaner handles: | ||
* <ul> | ||
* <li>{@code ```json ... ```}</li> | ||
* <li>{@code ``` ... ```}</li> | ||
* </ul> | ||
* | ||
* @author liugddx | ||
* @since 1.0.0 | ||
*/ | ||
public class MarkdownCodeBlockCleaner implements ResponseTextCleaner { | ||
|
||
@Override | ||
public String clean(String text) { | ||
if (text == null || text.isEmpty()) { | ||
return text; | ||
} | ||
|
||
// Trim leading and trailing whitespace first | ||
text = text.trim(); | ||
|
||
// Check for and remove triple backticks | ||
if (text.startsWith("```") && text.endsWith("```")) { | ||
// Remove the first line if it contains "```json" or similar | ||
String[] lines = text.split("\n", 2); | ||
if (lines[0].trim().toLowerCase().startsWith("```")) { | ||
// Extract language identifier if present | ||
String firstLine = lines[0].trim(); | ||
if (firstLine.length() > 3) { | ||
// Has language identifier like ```json | ||
text = lines.length > 1 ? lines[1] : ""; | ||
} | ||
else { | ||
// Just ``` without language | ||
text = text.substring(3); | ||
} | ||
} | ||
else { | ||
text = text.substring(3); | ||
} | ||
|
||
// Remove trailing ``` | ||
if (text.endsWith("```")) { | ||
text = text.substring(0, text.length() - 3); | ||
} | ||
|
||
// Trim again to remove any potential whitespace | ||
text = text.trim(); | ||
} | ||
|
||
return text; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
spring-ai-model/src/main/java/org/springframework/ai/converter/ResponseTextCleaner.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,37 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.converter; | ||
|
||
/** | ||
* Strategy interface for cleaning LLM response text before parsing. Different | ||
* implementations can handle various response formats and patterns from different AI | ||
* models. | ||
* | ||
* @author liugddx | ||
* @since 1.0.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface ResponseTextCleaner { | ||
|
||
/** | ||
* Clean the given text by removing unwanted patterns, tags, or formatting. | ||
* @param text the raw text from LLM response | ||
* @return the cleaned text ready for parsing | ||
*/ | ||
String clean(String text); | ||
|
||
} |
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.
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.
I am not sure that
ThinkingTagCleaner
should be added by default, especially for the non-thinking models.