Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +7 -0
- index.js +34 -0
- package.json +13 -0
Dockerfile
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:20
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY package.json ./
|
| 4 |
+
RUN npm install
|
| 5 |
+
COPY . .
|
| 6 |
+
EXPOSE 7860
|
| 7 |
+
CMD ["npm", "start"]
|
index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import OpenAI from "openai";
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
app.use(express.json());
|
| 6 |
+
|
| 7 |
+
const openai = new OpenAI({
|
| 8 |
+
apiKey: process.env.OPENAI_API_KEY,
|
| 9 |
+
});
|
| 10 |
+
|
| 11 |
+
app.get("/", (req, res) => {
|
| 12 |
+
res.send("✅ Node.js OpenAI Proxy is running!");
|
| 13 |
+
});
|
| 14 |
+
|
| 15 |
+
app.post("/api/chat", async (req, res) => {
|
| 16 |
+
try {
|
| 17 |
+
const { messages } = req.body;
|
| 18 |
+
if (!messages) {
|
| 19 |
+
return res.status(400).json({ error: "Поле 'messages' обязательно." });
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const completion = await openai.chat.completions.create({
|
| 23 |
+
model: "gpt-4o-mini",
|
| 24 |
+
messages,
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
res.json(completion.choices[0].message);
|
| 28 |
+
} catch (err) {
|
| 29 |
+
console.error(err);
|
| 30 |
+
res.status(500).json({ error: err.message });
|
| 31 |
+
}
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
app.listen(7860, () => console.log("Server running on port 7860"));
|
package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "openai-proxy",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"main": "index.js",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"scripts": {
|
| 7 |
+
"start": "node index.js"
|
| 8 |
+
},
|
| 9 |
+
"dependencies": {
|
| 10 |
+
"express": "^4.19.2",
|
| 11 |
+
"openai": "^4.0.0"
|
| 12 |
+
}
|
| 13 |
+
}
|