-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
79 lines (73 loc) · 2.79 KB
/
server.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
import express from "express";
const token = process.env.LLMFOUNDRY_TOKEN;
const port = process.env.PORT || 3000;
const app = express();
// Serve static files from public/ folder
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
// Extract endpoint
app.post("/extract", async (req, res) => {
const { subject, body } = req.body;
const apiUrl =
"https://llmfoundry.straive.com/azure/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-05-01-preview";
const response = await fetch(apiUrl, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
body: JSON.stringify({
messages: [
{
role: "system",
content:
"Extract the manuscript number or DOI as a JSON {id: ...}. Manuscript number looks like CMBL-D-24-00766 (Remove anything after 'R<nn...>'). DOI looks like 10.1038/s41586-024-02513-3. If none found, return {}",
},
{ role: "user", content: `${subject}\n\n${body}` },
],
response_format: { type: "json_object" },
}),
}).then((r) => r.json());
const json = response?.choices?.[0]?.message?.content ?? "";
if (json) {
try {
const { id } = JSON.parse(json);
res.send(/* html */ `
<h1>${subject}</h1>
<form id="chat" action="https://qts.springernature.com/chatbot.php" method="POST" enctype="multipart/form-data" target="_blank">
<input name="msg" value="${id}">
<button type="submit">Chat</button>
</form>
<script>
document.getElementById("chat").submit();
</script>
`);
} catch (e) {
console.error("Error parsing JSON", e);
res.status(500).send("Error parsing JSON");
}
} else {
res.status(500).send("No JSON found");
}
});
app.post("/compose", async (req, res) => {
const apiUrl =
"https://llmfoundry.straive.com/azure/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-05-01-preview";
const response = await fetch(apiUrl, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
body: JSON.stringify({
messages: [
{
role: "system",
content: `"subject" and "body" contain a user's email. Write an reply email explaining the status of the manuscript based on the rest of the information.`,
},
{ role: "user", content: JSON.stringify(req.body) },
],
}),
}).then((r) => r.json());
const content = response?.choices?.[0]?.message?.content ?? "";
res.send(/* html */ `<pre id="email">${content}</pre>
<script>navigator.clipboard.writeText(document.querySelector("#email").textContent);</script>
`);
});
app.listen(port, () => {
console.log(`Running on http://localhost:${port}/`);
});