-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
74 lines (63 loc) · 2.3 KB
/
index.mjs
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
import express from 'express';
import bodyParser from 'body-parser';
import axios from 'axios';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
// Load environment variables
dotenv.config();
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const azureMLApiKey = process.env.AZURE_ML_API_KEY;
const azureMLEndpoint = process.env.AZURE_ML_ENDPOINT;
// Correct path resolution for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Function to use Azure ML Inference API
const generateTextWithAzureML = async (inputText) => {
try {
const response = await axios.post(
azureMLEndpoint,
{ input_data: { input_string: [{ role: 'user', content: inputText }], parameters: { max_new_tokens: 256 } } },
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${azureMLApiKey}`
}
}
);
console.log('Received response from Azure ML:', response.data);
return response.data.output || 'No response generated';
} catch (error) {
console.error('Error using Azure ML Inference API:', error);
throw new Error('Failed to generate text with Azure ML Inference API');
}
};
// Serve static files from the public directory
app.use(express.static(path.join(__dirname, 'Public')));
app.post('/chat', async (req, res) => {
const incomingMessage = req.body.message.trim().toLowerCase();
let responseMessage = '';
const words = incomingMessage.split(' ');
if (words.includes('stock')) {
responseMessage = 'Currently, fetching stock data is not available. Please ask other questions.';
} else {
try {
responseMessage = await generateTextWithAzureML(incomingMessage);
} catch (error) {
responseMessage = 'Sorry, there was an error fetching the data. Please try again later.';
}
}
res.json({ message: cleanResponse(responseMessage) });
});
function cleanResponse(response) {
if (typeof response === 'string') {
return response.replace(/"conversation_id":".*?"/g, '').replace(/^\{result:/, '').replace(/}$/, '').trim();
}
return response;
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});