root commited on
Commit ·
dc7af0d
1
Parent(s): 08310b0
Update space
Browse files- .dockerignore +3 -0
- Dockerfile +20 -0
- LICENSE +0 -0
- package.json +18 -0
- src/app.js +24 -0
- src/controllers/chatController.js +38 -0
- src/routes/chat.js +8 -0
.dockerignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
node_modules
|
| 2 |
+
npm-debug.log
|
| 3 |
+
.env
|
Dockerfile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Node.js image
|
| 2 |
+
FROM node:18-alpine
|
| 3 |
+
|
| 4 |
+
# Create app directory
|
| 5 |
+
WORKDIR /usr/src/app
|
| 6 |
+
|
| 7 |
+
# Copy package files
|
| 8 |
+
COPY package*.json ./
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN npm install
|
| 12 |
+
|
| 13 |
+
# Copy source files
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Expose port
|
| 17 |
+
EXPOSE ${PORT}
|
| 18 |
+
|
| 19 |
+
# Start the app
|
| 20 |
+
CMD ["npm", "start", "node src/app.js"]
|
LICENSE
ADDED
|
File without changes
|
package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "openai-express-api",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "Express API for OpenAI with Docker",
|
| 5 |
+
"main": "src/app.js",
|
| 6 |
+
"scripts": {
|
| 7 |
+
"start": "node src/app.js",
|
| 8 |
+
"dev": "nodemon src/app.js"
|
| 9 |
+
},
|
| 10 |
+
"dependencies": {
|
| 11 |
+
"dotenv": "^16.4.1",
|
| 12 |
+
"express": "^4.18.2",
|
| 13 |
+
"openai": "^4.28.0"
|
| 14 |
+
},
|
| 15 |
+
"devDependencies": {
|
| 16 |
+
"nodemon": "^3.0.3"
|
| 17 |
+
}
|
| 18 |
+
}
|
src/app.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express';
|
| 2 |
+
import dotenv from 'dotenv';
|
| 3 |
+
import chatRoutes from './routes/chat.js';
|
| 4 |
+
|
| 5 |
+
dotenv.config();
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
const port = process.env.PORT || 3000;
|
| 9 |
+
|
| 10 |
+
// Middleware
|
| 11 |
+
app.use(express.json());
|
| 12 |
+
|
| 13 |
+
// Routes
|
| 14 |
+
app.use('/api/chat', chatRoutes);
|
| 15 |
+
|
| 16 |
+
// Health check
|
| 17 |
+
app.get('/', (req, res) => {
|
| 18 |
+
res.status(200).json({ status: 'OK', message: 'API is running' });
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
// Start server
|
| 22 |
+
app.listen(port, () => {
|
| 23 |
+
console.log(`Server running on port ${port}`);
|
| 24 |
+
});
|
src/controllers/chatController.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { OpenAI } from "openai";
|
| 2 |
+
|
| 3 |
+
const client = new OpenAI({
|
| 4 |
+
baseURL: "https://router.huggingface.co/novita/v3/openai",
|
| 5 |
+
apiKey: process.env.NOVITA_API_KEY,
|
| 6 |
+
});
|
| 7 |
+
|
| 8 |
+
export const chatCompletion = async (req, res) => {
|
| 9 |
+
try {
|
| 10 |
+
const { messages } = req.body;
|
| 11 |
+
|
| 12 |
+
// Default system message if not provided
|
| 13 |
+
if (!messages.some(m => m.role === 'system')) {
|
| 14 |
+
messages.unshift({
|
| 15 |
+
role: 'system',
|
| 16 |
+
content: 'Anda adalah asisten AI yang ramah, baik hati, dan selalu membantu. Gunakan bahasa Indonesia yang santun dan mudah dimengerti.'
|
| 17 |
+
});
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
const stream = await client.chat.completions.create({
|
| 21 |
+
model: process.env.MODEL,
|
| 22 |
+
messages,
|
| 23 |
+
stream: true,
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
res.setHeader('Content-Type', 'text/plain');
|
| 27 |
+
|
| 28 |
+
for await (const chunk of stream) {
|
| 29 |
+
const content = chunk.choices[0]?.delta?.content || '';
|
| 30 |
+
res.write(content);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
res.end();
|
| 34 |
+
} catch (error) {
|
| 35 |
+
console.error('Error:', error);
|
| 36 |
+
res.status(500).json({ error: 'Internal server error' });
|
| 37 |
+
}
|
| 38 |
+
};
|
src/routes/chat.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express';
|
| 2 |
+
import { chatCompletion } from '../controllers/chatController.js';
|
| 3 |
+
|
| 4 |
+
const router = express.Router();
|
| 5 |
+
|
| 6 |
+
router.post('/', chatCompletion);
|
| 7 |
+
|
| 8 |
+
export default router;
|