-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (44 loc) · 1.96 KB
/
Copy pathserver.js
File metadata and controls
55 lines (44 loc) · 1.96 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { OpenAI } = require('langchain/llms/openai');
const { extractKeywords_text } = require('./client_info_mgt/client_info_mgt');
const { summarizePubmedOutput, summarizeWikipediaOutput } = require('./content_optimizer/llm');
const { getContentByKeywords } = require('./content_fetcher/pubmed');
const { fetchWikipediaData } = require('./content_fetcher/wikipedia');
const { getTopContent } = require('./content_ranker/content_ranker');
const app = express();
const PORT = 3000;
app.use(cors());
app.use(bodyParser.json());
app.post('/demo', async (req, res) => {
try {
const { query, userPreference, record_num } = req.body;
if (!query || query.trim() === '') {
return res.status(400).json({ error: 'Input text cannot be empty.' });
}
const llm = new OpenAI({
openAIApiKey: process.env.OPENAI_API_KEY,
});
const keywords = await extractKeywords_text(query, userPreference.split(','), llm);
const rawPubmedContent = await getContentByKeywords(keywords.replace(/,/g, '+'), record_num);
const rawWikiContent = await fetchWikipediaData(keywords.replace(/,/g, ' '), record_num);
const pubmedContent = await summarizePubmedOutput(rawPubmedContent, record_num);
const wikiContent = await summarizeWikipediaOutput(rawWikiContent, record_num);
const concatenatedContent = pubmedContent.map((item) => item.content)
.concat(wikiContent.map((item) => item.content));
const topContent = await getTopContent(userPreference, concatenatedContent);
res.json({
keywords: keywords,
pubmedContent: pubmedContent,
wikiContent: wikiContent,
topContent: topContent,
});
} catch (error) {
console.error('Error handling /demo request:', error);
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});