Spaces:
Running
Running
import { OpenAIStream, StreamingTextResponse } from 'ai'; | |
import OpenAI from 'openai'; | |
import { auth } from '@/auth'; | |
import { | |
ChatCompletionMessageParam, | |
ChatCompletionContentPart, | |
} from 'openai/resources'; | |
import { MessageBase } from '../../../lib/types'; | |
export const runtime = 'edge'; | |
export const POST = async (req: Request) => { | |
const json = await req.json(); | |
const { messages } = json as { | |
messages: MessageBase[]; | |
id: string; | |
url: string; | |
}; | |
const session = await auth(); | |
if (!session?.user?.email) { | |
return new Response('Unauthorized', { | |
status: 401, | |
}); | |
} | |
const formattedMessage: ChatCompletionMessageParam[] = messages.map( | |
message => { | |
const contentWithImage: ChatCompletionContentPart[] = [ | |
{ | |
type: 'text', | |
text: message.content as string, | |
}, | |
]; | |
return { | |
role: 'user', | |
content: contentWithImage, | |
}; | |
}, | |
); | |
const openai = new OpenAI({ | |
apiKey: process.env.OPENAI_API_KEY, | |
}); | |
const res = await openai.chat.completions.create({ | |
model: 'gpt-4-vision-preview', | |
messages: formattedMessage, | |
temperature: 0.3, | |
stream: true, | |
max_tokens: 300, | |
}); | |
const stream = OpenAIStream(res); | |
return new StreamingTextResponse(stream); | |
}; | |