-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
139 lines (106 loc) · 3.22 KB
/
Copy pathserver.js
File metadata and controls
139 lines (106 loc) · 3.22 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const cheerio = require('cheerio')
const axios = require('axios');
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // This is the default and can be omitted
});
const JSON_SCHEMA = `{
"matching_publications": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": "string",
"matching_keywords": {
"type": "array",
"items": "string"
}
}
}
}
}`
const express = require('express')
const cors = require('cors');
const app = express();
const port = 8011
const cors_options = {
origin : ['http://localhost:8010'],
}
app.use(cors(cors_options))
app.use(express.json())
app.post('/find-conference', async (req, res) => {
const conference_name = req.body.name
const conferences_response = await axios.get(`https://dblp.org/search/venue/api?q=${conference_name}&format=json`)
const conferences_response_json = await conferences_response.data
if (!('hit' in conferences_response_json.result.hits)) {
res.send([])
return
}
const conferences = conferences_response_json.result.hits.hit.map(conference => conference.info)
res.send(conferences)
})
app.post('/find-publications', async (req, res) => {
const conference_acronym = req.body.acronym
const publications_response = await axios.get(`https://dblp.org/search/publ/api?q=stream%3Astreams%2Fconf%2F${conference_acronym}%3A&h=1000&format=json`)
const publications_response_json = await publications_response.data
if (!('hit' in publications_response_json.result.hits)) {
res.send([])
return
}
const publications = publications_response_json.result.hits.hit.map(publication => publication.info)
res.send(publications)
})
app.post('/retrieve-abstract', async (req, res) => {
const doi = req.body.doi
/*
const doi_response = await axios.get(
`https://doi.org/${doi}`,
{
withCredentials: true,
headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' }
}
)
const doi_response_text = await doi_response.data
const $ = cheerio.load(doi_response_text)
let abstract = null
console.log($('#gs_res_ccl_mid').text())
if (doi_response.request._redirectable._currentUrl.includes('acm')) {
const abstract_children = $('#abstract').text()
console.log(abstract_children)
}*/
res.send(doi)
})
app.post('/ask-chat-gpt', async (req, res) => {
const keywords = req.body.keywords
const publications = req.body.publications
const prompt = `Your role is to find scientific publications linked to given keywords. You will only search in the given publication list with the given keyword list.
Keywords:
${keywords}
Scientific publications:
${publications}`
const chat_completion = await client.chat.completions.create({
messages: [
{
role: 'system',
content: `You are a helpful assistant designed to output JSON that follows the following json schema:\n${JSON_SCHEMA}`
},
{
role: 'user',
content: prompt
}
],
model: 'gpt-4o-mini',
temperature: 0.1,
top_p: 0.2
});
let data = chat_completion.choices[0].message.content
if (Array.isArray(data)) {
res.send(data)
}
else {
res.send(data.replace("```json", '').replace('```', ''))
}
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})