Spaces:
Build error
Build error
Upload pages/api/chat.js with huggingface_hub
Browse files- pages/api/chat.js +29 -0
pages/api/chat.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { OpenAI } from 'openai';
|
| 2 |
+
|
| 3 |
+
const openai = new OpenAI({
|
| 4 |
+
apiKey: process.env.OPENAI_API_KEY || 'sk-proj-12345',
|
| 5 |
+
});
|
| 6 |
+
|
| 7 |
+
export default async function handler(req, res) {
|
| 8 |
+
if (req.method !== 'POST') {
|
| 9 |
+
return res.status(405).json({ error: 'Method not allowed' });
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
try {
|
| 13 |
+
const { messages, image } = req.body;
|
| 14 |
+
|
| 15 |
+
const completion = await openai.chat.completions.create({
|
| 16 |
+
model: "gpt-4-vision-preview",
|
| 17 |
+
messages: messages,
|
| 18 |
+
max_tokens: 500,
|
| 19 |
+
temperature: 0.7,
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
const response = completion.choices[0]?.message?.content || "I'm sorry, I couldn't process that request.";
|
| 23 |
+
|
| 24 |
+
res.status(200).json({ response });
|
| 25 |
+
} catch (error) {
|
| 26 |
+
console.error('Error:', error);
|
| 27 |
+
res.status(500).json({ error: 'Failed to process request' });
|
| 28 |
+
}
|
| 29 |
+
}
|