-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (66 loc) · 2.32 KB
/
index.js
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
75
76
77
78
79
const express = require('express');
const path = require('path');
const app = express();
const port = 3000;
app.use(express.static('public'));
app.use(express.json());
const apiKey = 'kTOqhkWcn956cMf7s1HYG780u4XgYNuZ';
// Function to create a chat session
async function createChatSession(apiKey, externalUserId) {
const response = await fetch('https://api.on-demand.io/chat/v1/sessions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'apikey': apiKey
},
body: JSON.stringify({
pluginIds: [],
externalUserId: externalUserId
})
});
const data = await response.json();
return data.data.id; // Extract session ID
}
// Function to submit a query using the session ID
async function submitQuery(apiKey, sessionId, query) {
const response = await fetch(`https://api.on-demand.io/chat/v1/sessions/${sessionId}/query`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'apikey': apiKey
},
body: JSON.stringify({
endpointId: 'predefined-openai-gpt4o',
query: query,
pluginIds: [
'plugin-1712327325',
'plugin-1713962163',
'plugin-1716334779',
'plugin-1716119225'
],
responseMode: 'sync'
})
});
const data = await response.json();
return data;
}
app.post('/chat', async (req, res) => {
const { message, userId } = req.body;
console.log('Received request:', { message, userId }); // Debug log
try {
const sessionId = await createChatSession(apiKey, userId || 'default-user');
console.log('Created session:', sessionId); // Debug log
const response = await submitQuery(apiKey, sessionId, message);
console.log('API response:', response); // Debug log
res.json(response);
} catch (error) {
console.error('Error details:', error); // More detailed error logging
res.status(500).json({ error: 'Internal server error', details: error.message });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});