-
Notifications
You must be signed in to change notification settings - Fork 276
Thinking blocks support #1219
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
Thinking blocks support #1219
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d535e1c
Thinking Blocks Support:
igordayen a2cdbaa
Unwanted file removal
igordayen a68b43e
Fix Thinking Detector Test and thinkingPatterns
igordayen 5cd6c83
Align StreamingJacksonOutputConverterTest with common Thinking Tags
igordayen 2da0c6e
Increase Code Coverage per Sonar Report
igordayen 12c87c5
Even More New Code Coverage per Sonar Report
igordayen 38b0839
Even More and More New Code Coverage per Sonar Report
igordayen 1f88b6b
Lines and Branch Coverage Increase
igordayen eba91e5
Address Sonar Violations
igordayen cd78f47
NOSONAR directive for safe cast
igordayen e813eae
Refactored ChatClientLlmOperations by moving exception handling into …
igordayen 2d4e150
Increase Code Coverage for newly added private methods
igordayen 9a85a9a
Thinking Functionality Refactoring per code review feedback:
igordayen bbf9d5e
Include Thinking section into User Guide
igordayen 248e9fa
Restored Streaming Section
igordayen 688a53a
Include Thinking into Core PromptRunner API:
igordayen 421e284
Removed Thinking Prompt Runner Builder and also:
igordayen 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
102 changes: 102 additions & 0 deletions
102
...l-agent-api/src/main/java/com/embabel/agent/api/thinking/ThinkingPromptRunnerBuilder.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,102 @@ | ||
| /* | ||
| * Copyright 2024-2025 Embabel Software, Inc. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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 com.embabel.agent.api.thinking; | ||
|
|
||
| import com.embabel.agent.api.common.PromptRunner; | ||
| import com.embabel.agent.api.common.thinking.ThinkingPromptRunnerOperations; | ||
| import com.embabel.common.core.thinking.ThinkingResponse; | ||
|
|
||
| /** | ||
| * Builder pattern to provide Java equivalent of Kotlin's withThinking() extension function. | ||
| * Solves the problem that Java cannot directly call Kotlin extension functions. | ||
| * | ||
| * <p>This builder enables Java developers to access thinking-aware prompt operations | ||
| * that extract LLM reasoning blocks alongside converted results.</p> | ||
| * | ||
| * <h2>Usage</h2> | ||
| * | ||
| * <pre>{@code | ||
| * // Java usage | ||
| * ThinkingPromptRunnerOperations thinkingOps = new ThinkingPromptRunnerBuilder(promptRunner) | ||
| * .withThinking(); | ||
| * | ||
| * ThinkingResponse<Person> result = thinkingOps.createObject("analyze this", Person.class); | ||
| * Person person = result.getResult(); // The converted object | ||
| * List<ThinkingBlock> thinking = result.getThinkingBlocks(); // LLM reasoning blocks | ||
| * }</pre> | ||
| * | ||
| * <h2>Thinking Extraction</h2> | ||
| * | ||
| * <p>When supported by the underlying LLM operations (e.g., OperationContextPromptRunner | ||
| * with ChatClientLlmOperations), automatically extracts thinking content in various formats:</p> | ||
| * | ||
| * <ul> | ||
| * <li>Tagged thinking: {@code <think>reasoning here</think>}, {@code <analysis>content</analysis>}</li> | ||
| * <li>Prefix thinking: {@code //THINKING: reasoning here}</li> | ||
| * <li>Untagged thinking: raw text content before JSON objects</li> | ||
| * </ul> | ||
| * | ||
| * <p>For implementations that don't support thinking extraction, returns graceful degradation | ||
| * with empty thinking blocks while preserving the original response content.</p> | ||
| * | ||
| * @see ThinkingPromptRunnerOperations for available thinking-aware methods | ||
| * @see ThinkingResponse for response structure | ||
| * @see com.embabel.common.core.thinking.ThinkingBlock for thinking content details | ||
| */ | ||
| public record ThinkingPromptRunnerBuilder(PromptRunner runner) { | ||
|
|
||
| /** | ||
| * Java equivalent of Kotlin's withThinking() extension function. | ||
igordayen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * <p>Creates thinking-aware prompt operations that return both converted results | ||
| * and the reasoning content that LLMs generate during processing.</p> | ||
| * | ||
| * <p><strong>Implementation Behavior:</strong></p> | ||
| * | ||
| * <ul> | ||
| * <li><strong>OperationContextPromptRunner (Production):</strong> Real thinking extraction | ||
| * using ChatClientLlmOperations with populated thinking blocks</li> | ||
| * <li><strong>Other Implementations (Graceful Degradation):</strong> Returns wrapper with | ||
| * empty thinking blocks while preserving original response content</li> | ||
| * </ul> | ||
| * | ||
| * @return Enhanced prompt runner operations with thinking block extraction capabilities | ||
| * @throws UnsupportedOperationException if the underlying LLM operations don't support | ||
| * thinking extraction and require ChatClientLlmOperations (only for specific implementations) | ||
| * | ||
| */ | ||
| public ThinkingPromptRunnerOperations withThinking() { | ||
igordayen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return runner.withThinking(); | ||
| } | ||
|
|
||
| /** | ||
| * Factory method for creating a builder instance. | ||
| * | ||
| * <p>Provides a fluent interface for Java developers who prefer static factory methods:</p> | ||
| * | ||
| * <pre>{@code | ||
| * ThinkingPromptRunnerOperations thinkingOps = ThinkingPromptRunnerBuilder | ||
| * .from(promptRunner) | ||
| * .withThinking(); | ||
| * }</pre> | ||
| * | ||
| * @param runner The prompt runner operations to enhance with thinking capabilities | ||
| * @return A new ThinkingPromptRunnerBuilder instance | ||
| */ | ||
| public static ThinkingPromptRunnerBuilder from(PromptRunner runner) { | ||
| return new ThinkingPromptRunnerBuilder(runner); | ||
| } | ||
| } | ||
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.