dummy content
This commit is contained in:
parent
568c6bdcb9
commit
df83c9d3f0
7 changed files with 225 additions and 5 deletions
27
app/api/chat/route.ts
Normal file
27
app/api/chat/route.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { OpenAIStream, StreamingTextResponse } from 'ai'
|
||||
import { Configuration, OpenAIApi } from 'openai-edge'
|
||||
|
||||
// Create an OpenAI API client (that's edge friendly!)
|
||||
const config = new Configuration({
|
||||
apiKey: process.env.OPENAI_API_KEY
|
||||
})
|
||||
const openai = new OpenAIApi(config)
|
||||
|
||||
// IMPORTANT! Set the runtime to edge
|
||||
export const runtime = 'edge'
|
||||
|
||||
export async function POST(req: Request) {
|
||||
// Extract the `messages` from the body of the request
|
||||
const { messages } = await req.json()
|
||||
|
||||
// Ask OpenAI for a streaming chat completion given the prompt
|
||||
const response = await openai.createChatCompletion({
|
||||
model: 'gpt-3.5-turbo',
|
||||
stream: true,
|
||||
messages
|
||||
})
|
||||
// Convert the response into a friendly text-stream
|
||||
const stream = OpenAIStream(response)
|
||||
// Respond with the stream
|
||||
return new StreamingTextResponse(stream)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue