-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
108 lines (94 loc) · 3.35 KB
/
server.js
File metadata and controls
108 lines (94 loc) · 3.35 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const express = require('express');
const cors = require('cors');
const fetch = require('node-fetch');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('.'));
// Serve static files
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/search', (req, res) => {
res.sendFile(path.join(__dirname, 'search.html'));
});
// Azure OpenAI with search endpoint
app.post('/api/openai-search', async (req, res) => {
try {
const {
endpoint,
apiKey,
deployment,
query,
searchEndpoint,
searchKey,
indexName,
maxTokens,
temperature
} = req.body;
if (!endpoint || !apiKey || !deployment || !query) {
return res.status(400).json({ error: 'Missing required parameters' });
}
const openAIUrl = `${endpoint}/openai/deployments/${deployment}/chat/completions?api-version=2024-02-15-preview`;
// Prepare the request body with search configuration
const requestBody = {
messages: [
{
role: "system",
content: `You are a helpful assistant that answers questions based on the provided search results.
Always cite the specific documents you reference in your answer.
If the search results don't contain enough information to answer the question, say so clearly.
Format your response with clear citations to the source documents.`
},
{
role: "user",
content: query
}
],
max_tokens: maxTokens || 1000,
temperature: temperature || 0.7
};
// Add search configuration if provided
if (searchEndpoint && searchKey && indexName) {
requestBody.data_sources = [
{
type: "AzureCognitiveSearch",
parameters: {
endpoint: searchEndpoint,
key: searchKey,
indexName: indexName
}
}
];
}
const response = await fetch(openAIUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'api-key': apiKey
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`OpenAI API failed: ${response.status} ${response.statusText} - ${errorText}`);
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('OpenAI search error:', error);
res.status(500).json({ error: error.message });
}
});
// Health check endpoint
app.get('/api/health', (req, res) => {
res.json({ status: 'OK', timestamp: new Date().toISOString() });
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log(`Video Indexer: http://localhost:${PORT}`);
console.log(`AI Search: http://localhost:${PORT}/search`);
});