|
import express from 'express'; |
|
import axios from 'axios'; |
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
|
const app = express(); |
|
|
|
|
|
app.use(express.json()); |
|
app.use(express.text()); |
|
|
|
const modelMapping = { |
|
"gpt-4o-mini": "GPT-4o mini", |
|
"claude-haiku": "Claude Haiku", |
|
"llama-3": "Llama 3", |
|
"gemini-1.5": "Gemini 1.5", |
|
"gemini-flash": "Gemini Flash", |
|
"command-r": "Command R", |
|
}; |
|
|
|
|
|
const authMiddleware = (req, res, next) => { |
|
const authToken = process.env.AUTH_TOKEN; |
|
if (authToken) { |
|
const requestToken = req.headers.authorization || ''; |
|
const token = requestToken.replace('Bearer ', ''); |
|
if (token !== authToken) { |
|
return res.status(401).send('Access Denied'); |
|
} |
|
} |
|
next(); |
|
}; |
|
|
|
|
|
const getTempUserID = async () => { |
|
const response = await axios.get('https://playground.julius.ai/api/temp_user_id'); |
|
return response.data.temp_user_id; |
|
}; |
|
|
|
|
|
const splitIntoChunks = (text, chunkSize) => { |
|
const chars = Array.from(text); |
|
const chunks = []; |
|
for (let i = 0; i < chars.length; i += chunkSize) { |
|
chunks.push(chars.slice(i, i + chunkSize).join('')); |
|
} |
|
return chunks; |
|
}; |
|
|
|
|
|
const sendToJulius = async (tempUserID, message, model) => { |
|
const conversationID = uuidv4(); |
|
const data = { |
|
message: { |
|
content: message, |
|
role: "user" |
|
}, |
|
provider: "default", |
|
chat_mode: "auto", |
|
client_version: "20240130", |
|
theme: "dark", |
|
new_images: null, |
|
new_attachments: null, |
|
dataframe_format: "json", |
|
selectedModels: [model] |
|
}; |
|
|
|
const response = await axios({ |
|
method: 'post', |
|
url: 'https://playground.julius.ai/api/chat/message', |
|
data, |
|
headers: { |
|
'is-demo': tempUserID, |
|
'conversation-id': conversationID, |
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36', |
|
'interactive-charts': 'true', |
|
'use-dict': 'true', |
|
'Gcs': 'true', |
|
'Is-Native': 'false', |
|
'sec-ch-ua-platform': 'Windows' |
|
}, |
|
responseType: 'text' |
|
}); |
|
|
|
return response.data.split('\n') |
|
.filter(line => line.trim()) |
|
.map(line => { |
|
try { return JSON.parse(line); } |
|
catch { return null; } |
|
}) |
|
.filter(Boolean) |
|
.reduce((acc, item) => acc + (item.content || ''), ''); |
|
}; |
|
|
|
|
|
app.post('/v1/chat/completions', authMiddleware, async (req, res) => { |
|
try { |
|
const { messages, model: originalModel, stream } = req.body; |
|
const model = modelMapping[originalModel] || originalModel; |
|
const tempUserID = await getTempUserID(); |
|
const content = messages[messages.length - 1].content; |
|
|
|
|
|
const juliusResp = await sendToJulius(tempUserID, content, model); |
|
|
|
|
|
if (stream) { |
|
res.setHeader('Content-Type', 'text/event-stream'); |
|
res.setHeader('Cache-Control', 'no-cache'); |
|
res.setHeader('Connection', 'keep-alive'); |
|
|
|
const respId = `chatcmpl-${tempUserID}`; |
|
const created = Math.floor(Date.now() / 1000); |
|
const chunks = splitIntoChunks(juliusResp, 50); |
|
|
|
|
|
res.write(`data: ${JSON.stringify({ |
|
id: respId, |
|
object: "chat.completion.chunk", |
|
created, |
|
model, |
|
choices: [{ delta: { role: "assistant" }, index: 0 }] |
|
})}\n\n`); |
|
|
|
|
|
chunks.forEach((chunk, index) => { |
|
res.write(`data: ${JSON.stringify({ |
|
id: respId, |
|
object: "chat.completion.chunk", |
|
created, |
|
model, |
|
choices: [{ |
|
delta: { content: chunk }, |
|
index: 0, |
|
finish_reason: index === chunks.length - 1 ? "stop" : null |
|
}] |
|
})}\n\n`); |
|
}); |
|
|
|
|
|
res.write('data: [DONE]\n\n'); |
|
return res.end(); |
|
} |
|
|
|
|
|
res.json({ |
|
id: `chatcmpl-${tempUserID}`, |
|
object: "chat.completion", |
|
created: Math.floor(Date.now() / 1000), |
|
model, |
|
choices: [{ |
|
message: { |
|
role: "assistant", |
|
content: juliusResp |
|
}, |
|
finish_reason: "stop", |
|
index: 0 |
|
}] |
|
}); |
|
|
|
} catch (error) { |
|
console.error('Error:', error); |
|
res.status(500).json({ error: error.message }); |
|
} |
|
}); |
|
|
|
|
|
app.all('*', (req, res) => { |
|
res.json({ |
|
status: "Julius2Api Service Running...", |
|
message: "MoLoveSze..." |
|
}); |
|
}); |
|
|
|
|
|
const PORT = process.env.PORT || 3000; |
|
app.listen(PORT, () => { |
|
console.log(`Server running on port ${PORT}`); |
|
}); |