56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
const express = require('express');
|
|
const axios = require('axios');
|
|
const cors = require('cors');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
|
|
// 中间件
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
// API路由
|
|
app.post('/api/chat', async (req, res) => {
|
|
try {
|
|
const userMessage = req.body.message;
|
|
|
|
const response = await axios.post('https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation', {
|
|
model: "farui-plus",
|
|
input: {
|
|
messages: [
|
|
{ role: "system", content: "You are a helpful assistant." },
|
|
{ role: "user", content: userMessage }
|
|
]
|
|
}
|
|
}, {
|
|
headers: {
|
|
'Authorization': `Bearer YOUR_API_KEY`,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
res.json({
|
|
status: 'success',
|
|
response: response.data
|
|
});
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({
|
|
status: 'error',
|
|
message: 'Internal server error'
|
|
});
|
|
}
|
|
});
|
|
|
|
// 前端路由
|
|
app.get('*', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Server is running on http://localhost:${PORT}`);
|
|
});
|
|
|