-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add reasoning token support (#4462)
Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
- Loading branch information
Showing
30 changed files
with
887 additions
and
13 deletions.
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,9 @@ | ||
--- | ||
'@ai-sdk/openai-compatible': patch | ||
'@ai-sdk/deepseek': patch | ||
'@ai-sdk/provider': patch | ||
'@ai-sdk/ui-utils': patch | ||
'ai': patch | ||
--- | ||
|
||
feat: add reasoning token support |
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
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
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
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
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
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
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
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
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,38 @@ | ||
import { deepseek } from '@ai-sdk/deepseek'; | ||
import { streamText } from 'ai'; | ||
import 'dotenv/config'; | ||
|
||
async function main() { | ||
const result = streamText({ | ||
model: deepseek('deepseek-reasoner'), | ||
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); |
23 changes: 23 additions & 0 deletions
23
examples/ai-core/src/stream-text/deepseek-reasoning-on-chunk.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,23 @@ | ||
import { deepseek } from '@ai-sdk/deepseek'; | ||
import { streamText } from 'ai'; | ||
import 'dotenv/config'; | ||
|
||
async function main() { | ||
const result = streamText({ | ||
model: deepseek('deepseek-reasoner'), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
onChunk({ chunk }) { | ||
if (chunk.type === 'reasoning') { | ||
console.log('reasoning', chunk.textDelta); | ||
} | ||
}, | ||
}); | ||
|
||
// consume stream: | ||
for await (const part of result.fullStream) { | ||
} | ||
|
||
console.log('reasoning', await result.reasoning); | ||
} | ||
|
||
main().catch(console.error); |
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
Oops, something went wrong.