-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update nuxt and nuxt examples (#2186)
Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
- Loading branch information
Showing
7 changed files
with
2,283 additions
and
3,108 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
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 |
---|---|---|
@@ -1,31 +1,25 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import OpenAI from 'openai'; | ||
import { ChatCompletionMessageParam } from 'openai/resources/chat'; | ||
import { streamText } from 'ai'; | ||
import { createOpenAI } from '@ai-sdk/openai'; | ||
|
||
export default defineLazyEventHandler(async () => { | ||
const apiKey = useRuntimeConfig().openaiApiKey; | ||
if (!apiKey) throw new Error('Missing OpenAI API key'); | ||
const openai = new OpenAI({ | ||
|
||
const openai = createOpenAI({ | ||
apiKey: apiKey, | ||
}); | ||
|
||
return defineEventHandler(async (event: any) => { | ||
// Extract the `prompt` from the body of the request | ||
const { messages } = (await readBody(event)) as { | ||
messages: ChatCompletionMessageParam[]; | ||
}; | ||
const { messages } = await readBody(event); | ||
|
||
// Ask OpenAI for a streaming chat completion given the prompt | ||
const response = await openai.chat.completions.create({ | ||
model: 'gpt-3.5-turbo', | ||
stream: true, | ||
const result = await streamText({ | ||
model: openai('gpt-3.5-turbo'), | ||
messages, | ||
}); | ||
|
||
// Convert the response into a friendly text-stream | ||
const stream = OpenAIStream(response); | ||
|
||
// Respond with the stream | ||
return new StreamingTextResponse(stream); | ||
return result.toAIStreamResponse(); | ||
}); | ||
}); |
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.