-
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 (ai/core): add experimental output setting to streamText (#4117)
- Loading branch information
Showing
15 changed files
with
649 additions
and
81 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,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
feat (ai/core): add experimental output setting to streamText |
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
24 changes: 24 additions & 0 deletions
24
content/docs/07-reference/05-ai-sdk-errors/ai-no-output-specified-error.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,24 @@ | ||
--- | ||
title: AI_NoOutputSpecifiedError | ||
description: Learn how to fix AI_NoOutputSpecifiedError | ||
--- | ||
|
||
# AI_NoOutputSpecifiedError | ||
|
||
This error occurs when no output format was specified for the AI response, and output-related methods are called. | ||
|
||
## Properties | ||
|
||
- `message`: The error message (defaults to 'No output specified.') | ||
|
||
## Checking for this Error | ||
|
||
You can check if an error is an instance of `AI_NoOutputSpecifiedError` using: | ||
|
||
```typescript | ||
import { NoOutputSpecifiedError } from 'ai'; | ||
|
||
if (NoOutputSpecifiedError.isInstance(error)) { | ||
// Handle the 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
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 { openai } from '@ai-sdk/openai'; | ||
import { Output, streamText, tool } from 'ai'; | ||
import 'dotenv/config'; | ||
import { z } from 'zod'; | ||
|
||
async function main() { | ||
const { experimental_partialOutputStream: partialOutputStream } = streamText({ | ||
model: openai('gpt-4o-mini', { structuredOutputs: true }), | ||
tools: { | ||
weather: tool({ | ||
description: 'Get the weather in a location', | ||
parameters: z.object({ | ||
location: z.string().describe('The location to get the weather for'), | ||
}), | ||
// location below is inferred to be a string: | ||
execute: async ({ location }) => ({ | ||
location, | ||
temperature: 72 + Math.floor(Math.random() * 21) - 10, | ||
}), | ||
}), | ||
}, | ||
experimental_output: Output.object({ | ||
schema: z.object({ | ||
elements: z.array( | ||
z.object({ | ||
location: z.string(), | ||
temperature: z.number(), | ||
touristAttraction: z.string(), | ||
}), | ||
), | ||
}), | ||
}), | ||
maxSteps: 2, | ||
prompt: | ||
'What is the weather and the main tourist attraction in San Francisco, London Paris, and Berlin?', | ||
}); | ||
|
||
// [{ location: 'San Francisco', temperature: 81 }, ...] | ||
for await (const partialOutput of partialOutputStream) { | ||
console.clear(); | ||
console.log(partialOutput); | ||
} | ||
} | ||
|
||
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
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
Oops, something went wrong.