-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore (examples): add useObject to next-openai-pages (#2117)
- Loading branch information
1 parent
bd0226f
commit caa198b
Showing
3 changed files
with
65 additions
and
11 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,25 @@ | ||
import { openai } from '@ai-sdk/openai'; | ||
import { streamObject } from 'ai'; | ||
import { z } from 'zod'; | ||
|
||
export const maxDuration = 30; | ||
|
||
export async function POST(req: Request) { | ||
const context = await req.json(); | ||
|
||
const result = await streamObject({ | ||
model: openai('gpt-4-turbo'), | ||
schema: z.object({ | ||
notifications: z.array( | ||
z.object({ | ||
name: z.string().describe('Name of a fictional person.'), | ||
message: z.string().describe('Message. Do not use emojis or links.'), | ||
}), | ||
), | ||
}), | ||
prompt: | ||
`Generate 3 notifications for a messages app in this context:` + context, | ||
}); | ||
|
||
return result.toTextStreamResponse(); | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/next-openai-pages/pages/basics/stream-object/index.tsx
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,29 @@ | ||
import { experimental_useObject } from 'ai/react'; | ||
import { z } from 'zod'; | ||
|
||
export default function Page() { | ||
const { object, setInput } = experimental_useObject({ | ||
api: '/api/stream-object', | ||
schema: z.object({ content: z.string() }), | ||
}); | ||
|
||
return ( | ||
<div className="p-2 flex flex-col gap-2"> | ||
<div | ||
className="p-2 bg-zinc-100 cursor-pointer" | ||
onClick={async () => { | ||
setInput('Final exams'); | ||
}} | ||
> | ||
Generate | ||
</div> | ||
|
||
<pre | ||
className="text-sm w-full whitespace-pre-wrap" | ||
data-testid="generation" | ||
> | ||
{JSON.stringify(object, null, 2)} | ||
</pre> | ||
</div> | ||
); | ||
} |
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