-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
feat (core): add extractReasoningMiddleware #4541
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4aa310a
1
lgrammel b9287fc
2
lgrammel d6debc5
3
lgrammel b0cc9c4
4
lgrammel f5959d0
5
lgrammel 47b0ceb
6
lgrammel 0b7d6e9
7
lgrammel 0a0b0a5
clean
lgrammel 5098b01
Merge branch 'main' into lg/mXJMpoMa
lgrammel 74700f4
example
lgrammel 8a0234c
changset
lgrammel 6a30a9f
docs
lgrammel e5cb813
ref
lgrammel a19df8c
note
lgrammel 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 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 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
feat (core): add extractReasoningMiddleware |
This file contains 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
61 changes: 61 additions & 0 deletions
61
content/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx
This file contains 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,61 @@ | ||
--- | ||
title: extractReasoningMiddleware | ||
description: Middleware that extracts XML-tagged reasoning sections from generated text | ||
--- | ||
|
||
# `extractReasoningMiddleware()` | ||
|
||
`extractReasoningMiddleware` is a middleware function that extracts XML-tagged reasoning sections from generated text and exposes them separately from the main text content. This is particularly useful when you want to separate an AI model's reasoning process from its final output. | ||
|
||
```ts | ||
import { extractReasoningMiddleware } from 'ai'; | ||
|
||
const middleware = extractReasoningMiddleware({ | ||
tagName: 'reasoning', | ||
separator: '\n', | ||
}); | ||
``` | ||
|
||
## Import | ||
|
||
<Snippet | ||
text={`import { extractReasoningMiddleware } from "ai"`} | ||
prompt={false} | ||
/> | ||
|
||
## API Signature | ||
|
||
### Parameters | ||
|
||
<PropertiesTable | ||
content={[ | ||
{ | ||
name: 'tagName', | ||
type: 'string', | ||
isOptional: false, | ||
description: | ||
'The name of the XML tag to extract reasoning from (without angle brackets)', | ||
}, | ||
{ | ||
name: 'separator', | ||
type: 'string', | ||
isOptional: true, | ||
description: | ||
'The separator to use between reasoning and text sections. Defaults to "\\n"', | ||
}, | ||
]} | ||
/> | ||
|
||
### Returns | ||
|
||
Returns a middleware object that: | ||
|
||
- Processes both streaming and non-streaming responses | ||
- Extracts content between specified XML tags as reasoning | ||
- Removes the XML tags and reasoning from the main text | ||
- Adds a `reasoning` property to the result containing the extracted content | ||
- Maintains proper separation between text sections using the specified separator | ||
|
||
### Type Parameters | ||
|
||
The middleware works with the `LanguageModelV1StreamPart` type for streaming responses. |
This file contains 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
File renamed without changes.
45 changes: 45 additions & 0 deletions
45
examples/ai-core/src/stream-text/groq-reasoning-fullstream.ts
This file contains 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,45 @@ | ||
import { groq } from '@ai-sdk/groq'; | ||
import { | ||
experimental_wrapLanguageModel, | ||
extractReasoningMiddleware, | ||
streamText, | ||
} from 'ai'; | ||
import 'dotenv/config'; | ||
|
||
async function main() { | ||
const result = streamText({ | ||
model: experimental_wrapLanguageModel({ | ||
model: groq('deepseek-r1-distill-llama-70b'), | ||
middleware: extractReasoningMiddleware({ tagName: 'think' }), | ||
}), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
}); | ||
|
||
let enteredReasoning = false; | ||
let enteredText = false; | ||
for await (const part of result.fullStream) { | ||
if (part.type === 'reasoning') { | ||
if (!enteredReasoning) { | ||
enteredReasoning = true; | ||
console.log('\nSTREAMING REASONING:\n'); | ||
} | ||
process.stdout.write(part.textDelta); | ||
} else if (part.type === 'text-delta') { | ||
if (!enteredText) { | ||
enteredText = true; | ||
console.log('\nSTREAMING TEXT:\n'); | ||
} | ||
process.stdout.write(part.textDelta); | ||
} | ||
} | ||
|
||
console.log(); | ||
console.log('\nFINAL REASONING:\n', await result.reasoning); | ||
console.log('\nFINAL TEXT:\n', await result.text); | ||
|
||
console.log(); | ||
console.log('Token usage:', await result.usage); | ||
console.log('Finish reason:', await result.finishReason); | ||
} | ||
|
||
main().catch(console.error); |
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.
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.
can we show how it would be used and accessed too?
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.
added a note, I don't want to duplicate everywhere
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.
heard but I mean more for how you access reasoning - eg. is it
await result.reasoning
?