Skip to content

Commit

Permalink
feat(app): add cors and correct parse of literature id (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
carcruz authored Jun 27, 2023
1 parent e54645b commit fa44794
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 17 deletions.
20 changes: 18 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import express from "express";
import cors from "cors";

import logger from "./utils/logger.js";
import httpLogger from "./middlewares/httpLogger.js";
import literatureRouter from "./routes/literature.js";
import healthRouter from "./routes/health.js";
import { normalizePort } from "./utils/index.js";
import { normalizePort, isProduction, isDevelopment } from "./utils/index.js";

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

const app = express();

app.use(httpLogger);
app.use(express.json());

if (isDevelopment) {
app.use(cors());
}

if (isProduction) {
app.use(
cors({
origin: [originRegExp],
})
);
}

app.use("/literature", literatureRouter);
app.use("/health", healthRouter);

Expand Down
2 changes: 1 addition & 1 deletion controllers/publication.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from "axios";
import { XMLParser } from "fast-xml-parser";

function URLPublicationFullText({ id }) {
const baseUrl = `https://www.ebi.ac.uk/europepmc/webservices/rest/PMC${id}/fullTextXML`;
const baseUrl = `https://www.ebi.ac.uk/europepmc/webservices/rest/${id}/fullTextXML`;
const requestOptions = {
method: "GET",
headers: {
Expand Down
6 changes: 2 additions & 4 deletions controllers/publicationSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const getPublicationSummary = async ({
targetSymbol,
diseaseName,
pmcId,
wbTracer,
wbTracer = null,
}) => {
const prompt = createPrompt({ targetSymbol, diseaseName });

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

const docs = await textSplitter.createDocuments([text]);

logger.info(JSON.stringify({ wordCount, docsLength: docs.length }));

const chain = loadQAMapReduceChain(model);
logger.info("reauest to gpt");
logger.info("request to openai");
if (wbTracer !== null) {
wandb.log({
targetSymbol: targetSymbol,
Expand Down
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
"private": true,
"type": "module",
"scripts": {
"start": "node app.js",
"start": "NODE_ENV=development node app.js",
"start:prod": "NODE_ENV=production node app.js",
"dev": "nodemon app.js"
"dev": "NODE_ENV=development nodemon app.js"
},
"dependencies": {
"@wandb/sdk": "^0.5.1",
"axios": "^1.4.0",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^16.0.3",
"express": "~4.16.1",
"fast-xml-parser": "^4.2.2",
"http-errors": "~1.6.3",
"langchain": "^0.0.78",
"morgan": "~1.9.1",
"node-cache": "^5.1.2",
"node-fetch": "^3.3.1",
"pug": "2.0.0-beta11",
"winston": "^3.8.2",
Expand Down
20 changes: 12 additions & 8 deletions routes/literature.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import express from "express";
import { WandbTracer } from "@wandb/sdk/integrations/langchain";
import * as dotenv from "dotenv";

import { getPublicationPlainText } from "../controllers/publication.js";
import {
getPublicationSummary,
streamTest,
} from "../controllers/publicationSummary.js";
import * as dotenv from "dotenv";
import { isDevelopment } from "../utils/index.js";
import logger from "../utils/logger.js";

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

const prettyDiseaseName = diseaseName.replace(/\s/g, "_");
const wbIdWithRandom = `${pmcId}_${targetSymbol}_${prettyDiseaseName}_${Math.floor(
Math.random() * 1000
)}`;
const wbTracer = await WandbTracer.init(
{ project: "ot-explain", id: wbIdWithRandom },
false
);
const queryId = `${pmcId}_${targetSymbol}_${prettyDiseaseName}`;
const wbIdWithRandom = `${queryId}_${Math.floor(Math.random() * 1000)}`;
let wbTracer = null;
if (isDevelopment) {
wbTracer = await WandbTracer.init(
{ project: "ot-explain", id: wbIdWithRandom },
false
);
}

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

Expand Down
46 changes: 46 additions & 0 deletions services/cacheService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import NodeCache from "node-cache";

class Cache {
constructor(ttlSeconds) {
this.cache = new NodeCache({
stdTTL: ttlSeconds,
checkperiod: ttlSeconds * 0.2,
useClones: false,
});
}

get(key, storeFunction) {
const value = this.cache.get(key);
if (value) {
return Promise.resolve(value);
}

return storeFunction().then((result) => {
this.cache.set(key, result);
return result;
});
}

del(keys) {
this.cache.del(keys);
}

delStartWith(startStr = "") {
if (!startStr) {
return;
}

const keys = this.cache.keys();
for (const key of keys) {
if (key.indexOf(startStr) === 0) {
this.del(key);
}
}
}

flush() {
this.cache.flushAll();
}
}

export default Cache;
3 changes: 3 additions & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ export function normalizePort(val) {
}
return false;
}

export const isDevelopment = process.env.NODE_ENV === "development";
export const isProduction = process.env.NODE_ENV === "production";

0 comments on commit fa44794

Please sign in to comment.