Skip to content

Commit fa44794

Browse files
authored
feat(app): add cors and correct parse of literature id (#8)
1 parent e54645b commit fa44794

File tree

8 files changed

+141
-17
lines changed

8 files changed

+141
-17
lines changed

app.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
import express from "express";
2+
import cors from "cors";
3+
24
import logger from "./utils/logger.js";
35
import httpLogger from "./middlewares/httpLogger.js";
46
import literatureRouter from "./routes/literature.js";
57
import healthRouter from "./routes/health.js";
6-
import { normalizePort } from "./utils/index.js";
8+
import { normalizePort, isProduction, isDevelopment } from "./utils/index.js";
79

8-
var port = normalizePort(process.env.PORT || "8080");
10+
const port = normalizePort(process.env.PORT || "8080");
11+
const originRegExp = /^(.*\.)?opentargets\.(org|xwz)$/;
912

1013
const app = express();
14+
1115
app.use(httpLogger);
1216
app.use(express.json());
1317

18+
if (isDevelopment) {
19+
app.use(cors());
20+
}
21+
22+
if (isProduction) {
23+
app.use(
24+
cors({
25+
origin: [originRegExp],
26+
})
27+
);
28+
}
29+
1430
app.use("/literature", literatureRouter);
1531
app.use("/health", healthRouter);
1632

controllers/publication.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import axios from "axios";
22
import { XMLParser } from "fast-xml-parser";
33

44
function URLPublicationFullText({ id }) {
5-
const baseUrl = `https://www.ebi.ac.uk/europepmc/webservices/rest/PMC${id}/fullTextXML`;
5+
const baseUrl = `https://www.ebi.ac.uk/europepmc/webservices/rest/${id}/fullTextXML`;
66
const requestOptions = {
77
method: "GET",
88
headers: {

controllers/publicationSummary.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const getPublicationSummary = async ({
4646
targetSymbol,
4747
diseaseName,
4848
pmcId,
49-
wbTracer,
49+
wbTracer = null,
5050
}) => {
5151
const prompt = createPrompt({ targetSymbol, diseaseName });
5252

@@ -58,11 +58,9 @@ export const getPublicationSummary = async ({
5858
});
5959

6060
const docs = await textSplitter.createDocuments([text]);
61-
6261
logger.info(JSON.stringify({ wordCount, docsLength: docs.length }));
63-
6462
const chain = loadQAMapReduceChain(model);
65-
logger.info("reauest to gpt");
63+
logger.info("request to openai");
6664
if (wbTracer !== null) {
6765
wandb.log({
6866
targetSymbol: targetSymbol,

package-lock.json

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"start": "node app.js",
7+
"start": "NODE_ENV=development node app.js",
88
"start:prod": "NODE_ENV=production node app.js",
9-
"dev": "nodemon app.js"
9+
"dev": "NODE_ENV=development nodemon app.js"
1010
},
1111
"dependencies": {
1212
"@wandb/sdk": "^0.5.1",
1313
"axios": "^1.4.0",
1414
"cookie-parser": "~1.4.4",
15+
"cors": "^2.8.5",
1516
"debug": "~2.6.9",
1617
"dotenv": "^16.0.3",
1718
"express": "~4.16.1",
1819
"fast-xml-parser": "^4.2.2",
1920
"http-errors": "~1.6.3",
2021
"langchain": "^0.0.78",
2122
"morgan": "~1.9.1",
23+
"node-cache": "^5.1.2",
2224
"node-fetch": "^3.3.1",
2325
"pug": "2.0.0-beta11",
2426
"winston": "^3.8.2",

routes/literature.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import express from "express";
22
import { WandbTracer } from "@wandb/sdk/integrations/langchain";
3+
import * as dotenv from "dotenv";
4+
35
import { getPublicationPlainText } from "../controllers/publication.js";
46
import {
57
getPublicationSummary,
68
streamTest,
79
} from "../controllers/publicationSummary.js";
8-
import * as dotenv from "dotenv";
10+
import { isDevelopment } from "../utils/index.js";
911
import logger from "../utils/logger.js";
1012

1113
dotenv.config();
@@ -46,13 +48,15 @@ router.post("/publication/summary/", async (req, res) => {
4648
const { pmcId, targetSymbol, diseaseName } = req.body.payload;
4749

4850
const prettyDiseaseName = diseaseName.replace(/\s/g, "_");
49-
const wbIdWithRandom = `${pmcId}_${targetSymbol}_${prettyDiseaseName}_${Math.floor(
50-
Math.random() * 1000
51-
)}`;
52-
const wbTracer = await WandbTracer.init(
53-
{ project: "ot-explain", id: wbIdWithRandom },
54-
false
55-
);
51+
const queryId = `${pmcId}_${targetSymbol}_${prettyDiseaseName}`;
52+
const wbIdWithRandom = `${queryId}_${Math.floor(Math.random() * 1000)}`;
53+
let wbTracer = null;
54+
if (isDevelopment) {
55+
wbTracer = await WandbTracer.init(
56+
{ project: "ot-explain", id: wbIdWithRandom },
57+
false
58+
);
59+
}
5660

5761
logger.info(`Request on pub summary`);
5862

services/cacheService.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import NodeCache from "node-cache";
2+
3+
class Cache {
4+
constructor(ttlSeconds) {
5+
this.cache = new NodeCache({
6+
stdTTL: ttlSeconds,
7+
checkperiod: ttlSeconds * 0.2,
8+
useClones: false,
9+
});
10+
}
11+
12+
get(key, storeFunction) {
13+
const value = this.cache.get(key);
14+
if (value) {
15+
return Promise.resolve(value);
16+
}
17+
18+
return storeFunction().then((result) => {
19+
this.cache.set(key, result);
20+
return result;
21+
});
22+
}
23+
24+
del(keys) {
25+
this.cache.del(keys);
26+
}
27+
28+
delStartWith(startStr = "") {
29+
if (!startStr) {
30+
return;
31+
}
32+
33+
const keys = this.cache.keys();
34+
for (const key of keys) {
35+
if (key.indexOf(startStr) === 0) {
36+
this.del(key);
37+
}
38+
}
39+
}
40+
41+
flush() {
42+
this.cache.flushAll();
43+
}
44+
}
45+
46+
export default Cache;

utils/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ export function normalizePort(val) {
1010
}
1111
return false;
1212
}
13+
14+
export const isDevelopment = process.env.NODE_ENV === "development";
15+
export const isProduction = process.env.NODE_ENV === "production";

0 commit comments

Comments
 (0)