Spaces:
Sleeping
Sleeping
// ========================================== | |
// API HANDLER FILE | |
// ========================================== | |
require('dotenv').config(); | |
const OpenAI = require('openai'); | |
const fs = require('fs'); | |
// Initialize OpenAI client | |
const openai = new OpenAI({ | |
apiKey: process.env.OPENAI_UNLIMITED_API_KEY, | |
}); | |
// ------------------------------------------ | |
// Chat Completions (Text Generation, Reasoning, Function Calling) | |
// ------------------------------------------ | |
async function createChatCompletion(requestBody, res) { | |
if (requestBody.stream) { | |
// Handle streaming response | |
try { | |
const completion = await openai.chat.completions.create({ | |
...requestBody, | |
stream: true, | |
}); | |
res.setHeader('Content-Type', 'text/event-stream'); | |
res.setHeader('Cache-Control', 'no-cache'); | |
res.flushHeaders(); | |
for await (const part of completion) { | |
const data = JSON.stringify(part); | |
res.write(`data: ${data}\n\n`); | |
} | |
res.write('data: [DONE]\n\n'); | |
res.end(); | |
} catch (error) { | |
console.error('Error in createChatCompletion:', error); | |
if (!res.headersSent) { | |
res.status(500).json(error.response ? error.response.data : error); | |
} else { | |
res.write(`data: ${JSON.stringify({ error: error.message })}\n\n`); | |
res.end(); | |
} | |
} | |
} else { | |
// Non-streaming case | |
try { | |
const completion = await openai.chat.completions.create(requestBody); | |
return completion; | |
} catch (error) { | |
throw error.response ? error.response.data : error; | |
} | |
} | |
} | |
// ------------------------------------------ | |
// Image Generation | |
// ------------------------------------------ | |
async function createImage(requestBody) { | |
try { | |
const response = await openai.images.generate(requestBody); | |
return response; | |
} catch (error) { | |
throw error.response ? error.response.data : error; | |
} | |
} | |
// ------------------------------------------ | |
// Image Editing | |
// ------------------------------------------ | |
async function createImageEdit(requestBody) { | |
try { | |
const response = await openai.images.edit({ | |
image: fs.createReadStream(requestBody.image), | |
mask: fs.createReadStream(requestBody.mask), | |
prompt: requestBody.prompt, | |
n: requestBody.n, | |
size: requestBody.size, | |
}); | |
return response; | |
} catch (error) { | |
throw error.response ? error.response.data : error; | |
} | |
} | |
// ------------------------------------------ | |
// Image Variation | |
// ------------------------------------------ | |
async function createImageVariation(requestBody) { | |
try { | |
const response = await openai.images.createVariation({ | |
image: fs.createReadStream(requestBody.image), | |
n: requestBody.n, | |
size: requestBody.size, | |
}); | |
return response; | |
} catch (error) { | |
throw error.response ? error.response.data : error; | |
} | |
} | |
// ------------------------------------------ | |
// Audio Transcription | |
// ------------------------------------------ | |
async function createTranscription(requestBody) { | |
try { | |
const response = await openai.audio.transcriptions.create({ | |
file: fs.createReadStream(requestBody.file), | |
model: requestBody.model, | |
}); | |
return response; | |
} catch (error) { | |
throw error.response ? error.response.data : error; | |
} | |
} | |
// ------------------------------------------ | |
// Text to Speech | |
// ------------------------------------------ | |
async function createSpeech(requestBody, res) { | |
try { | |
const response = await openai.audio.speech.create({ | |
...requestBody, | |
}); | |
const arrayBuffer = await response.arrayBuffer(); | |
res.setHeader('Content-Type', 'audio/mpeg'); | |
res.send(Buffer.from(arrayBuffer)); | |
} catch (error) { | |
console.error('Error in createSpeech:', error); | |
if (!res.headersSent) { | |
res.status(500).json(error.response ? error.response.data : error); | |
} | |
} | |
} | |
// ------------------------------------------ | |
// Vision (Image Analysis) | |
// ------------------------------------------ | |
async function createVisionAnalysis(requestBody) { | |
try { | |
const response = await openai.chat.completions.create(requestBody); | |
return response; | |
} catch (error) { | |
throw error.response ? error.response.data : error; | |
} | |
} | |
// ------------------------------------------ | |
// Export Modules | |
// ------------------------------------------ | |
module.exports = { | |
createChatCompletion, | |
createImage, | |
createImageEdit, | |
createImageVariation, | |
createTranscription, | |
createSpeech, | |
createVisionAnalysis, | |
}; |