Spaces:
Sleeping
Sleeping
File size: 4,723 Bytes
184406c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
// ==========================================
// 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,
}; |