-
Notifications
You must be signed in to change notification settings - Fork 401
feat(thinking): add tag selection API with include/exclude filtering (#1790) #1795
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
Abtiotm
wants to merge
1
commit into
embabel:main
Choose a base branch
from
Abtiotm:codex/issue-1790-thinking-prompt-tag
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.
Open
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,12 +31,15 @@ import com.embabel.common.ai.model.LlmOptions | |
| import com.embabel.common.ai.prompt.PromptContributor | ||
| import com.embabel.common.ai.prompt.PromptContributorConsumer | ||
| import com.embabel.common.core.MobyNameGenerator | ||
| import com.embabel.common.core.thinking.ThinkingBlock | ||
| import com.embabel.common.core.thinking.ThinkingTagType | ||
| import com.embabel.common.core.types.HasInfoString | ||
| import com.embabel.common.util.indent | ||
| import com.fasterxml.jackson.annotation.JsonIgnore | ||
| import jakarta.validation.ConstraintViolation | ||
| import java.lang.reflect.Field | ||
| import java.util.function.Predicate | ||
| import org.slf4j.Logger | ||
|
|
||
| /** | ||
| * Spec for calling an LLM. Optional LlmOptions, | ||
|
|
@@ -99,6 +102,49 @@ private data class LlmCallImpl( | |
| override val validation: Boolean = true, | ||
| ) : LlmCall | ||
|
|
||
| data class ThinkingTagSelection( | ||
| val include: Set<String> = emptySet(), | ||
| val exclude: Set<String> = emptySet(), | ||
| ) { | ||
| init { | ||
| require((include + exclude).all(TAG_NAME_REGEX::matches)) { | ||
| "Thinking tags must be valid XML-style tag names matching [a-zA-Z][a-zA-Z0-9_-]*" | ||
| } | ||
| require(include.intersect(exclude).isEmpty()) { | ||
| "Thinking tags cannot be both included and excluded" | ||
| } | ||
| } | ||
|
|
||
| // Non-TAG blocks (prefix/untagged) are always retained — only XML TAG blocks are filtered. | ||
| fun filter(blocks: List<ThinkingBlock>): List<ThinkingBlock> = | ||
| blocks.filter { block -> | ||
| block.tagType != ThinkingTagType.TAG || | ||
| ((include.isEmpty() || block.tagValue in include) && block.tagValue !in exclude) | ||
| } | ||
|
|
||
| fun missingFrom(systemPrompt: String): Set<String> = | ||
| include.filter { tag -> !systemPrompt.contains(TAG_OPEN_REGEX(tag)) }.toSet() | ||
|
|
||
| // Warns when a declared tag is absent from the system prompt — without it, hasThinking() silently returns false. | ||
| fun warnIfMissing(systemPrompt: String, logger: Logger) { | ||
| val missing = missingFrom(systemPrompt) | ||
| if (missing.isNotEmpty()) { | ||
| logger.warn( | ||
| "Thinking tag(s) {} not found in the system prompt — the LLM may not generate matching blocks", | ||
| missing, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| val TAG_NAME_REGEX = Regex("[a-zA-Z][a-zA-Z0-9_-]*") | ||
|
|
||
| // Boundary after the tag name: '>' (open), whitespace (attributes), or '/' (self-closing). | ||
| // Without it, "think" would match "<thinking>". | ||
| private fun TAG_OPEN_REGEX(tag: String): Regex = Regex("<$tag(?:>|\\s|/)") | ||
|
Contributor
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.
Not a hot path (runs once per LLM call setup), so non-blocking, but worth a |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Encapsulates an interaction with an LLM. | ||
| * An LlmInteraction is a specific instance of an LlmCall. | ||
|
|
@@ -133,6 +179,7 @@ data class LlmInteraction( | |
| val toolCallInspectors: List<ToolCallInspector> = emptyList(), | ||
| val toolCallContext: ToolCallContext = ToolCallContext.EMPTY, | ||
| val toolNotFoundPolicy: ToolNotFoundPolicy? = null, | ||
| val thinkingTags: ThinkingTagSelection = ThinkingTagSelection(), | ||
| ) : LlmCall { | ||
|
|
||
| override val name: String = id.value | ||
|
|
||
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.
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.
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.
Inconsistent default fallback behavior between the two new overloads.
thinking(include, exclude)(line 493) throwsUnsupportedOperationExceptionwhen non-empty sets are passed to an implementation that hasn't overridden it, butthinking(tag)(line 500) just callsthinking()and silently drops the tag.A PromptRunner implementer who overrides one but forgets the other gets loud failure in one case and silent wrong behavior in the other. Make line 500 delegate to
thinking(include = setOf(tag))so both paths fail the same way.