Skip to content

Commit

Permalink
Bibliography summarization and Azure openai API change (#2)
Browse files Browse the repository at this point in the history
* changed code to work with azure openai instead of openai. 
* added a function that will summarize a list of numbered abstract from the bibliography
  • Loading branch information
EIjo authored Oct 31, 2024
1 parent af2eebd commit 21ac897
Show file tree
Hide file tree
Showing 6 changed files with 612 additions and 27 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

This repository holds the Open Targets AI API router.

For this API to function an .env file has to be added in the root folder of this project. For the values of these variables please contact the Open Targets team of The Hyve or create your own Azure OpenAI deployment.

### .env variables
```
AZURE_OPENAI_ENDPOINT = ...
AZURE_OPENAI_API_KEY = ...
AZURE_OPENAI_API_DEPLOYMENT_NAME = ...
AZURE_OPENAI_API_VERSION = ...
AZURE_OPENAI_API_INSTANCE_NAME = ...
MAX_LLM_REQUESTS = ...
HYVE_AI_API = ...
```

### Required stack

- [NodeJS >= v18](https://nodejs.org/en/)
Expand Down
10 changes: 8 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ const port = normalizePort(process.env.PORT || "8080");
const app = express();

app.use(httpLogger);
app.use(express.json());
app.use(express.json({limit: '50mb'}));

if (isDevelopment) {
app.use(cors());
app.use(cors({
origin: [
process.env.SERVER_NAME,
]
}
));
}

if (isProduction) {
Expand All @@ -26,6 +31,7 @@ if (isProduction) {
"https://partner-platform.dev.opentargets.xyz",
"https://platform.opentargets.org",
"https://platform.dev.opentargets.xyz",
process.env.SERVER_NAME
],
})
);
Expand Down
43 changes: 38 additions & 5 deletions controllers/publicationSummary.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { loadQAMapReduceChain } from "langchain/chains";
import { OpenAI } from "langchain/llms/openai";
import { AzureChatOpenAI } from "@langchain/openai";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import wandb from "@wandb/sdk";

Expand All @@ -10,10 +10,13 @@ dotenv.config();

// query setup
// summarization docs https://js.langchain.com/docs/api/chains/functions/loadQAMapReduceChain
const model = new OpenAI({
modelName: "gpt-4o-mini",
openAIApiKey: process.env.OPENAI_TOKEN,
temperature: 0.5,
const model = new AzureChatOpenAI({
temperature: .9,
azureOpenAIEndpoint: process.env.AZURE_OPENAI_ENDPOINT,
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION,
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
});

const createPrompt = ({ targetSymbol, diseaseName }) => {
Expand Down Expand Up @@ -41,6 +44,36 @@ export const streamTest = ({ res }) => {
sendAndSleep(res, 1);
};

export const getMulitpleAbstractSummary = async ({
name,
entity,
abstracts,
}) => {
var prompt =
`You are given abstracts related to ${name} ${entity}. Use information found in the abstracts to inform me about this ${entity}.
Combine the information found in the following abstracts into a single story.\n
Format the output as plaintext.\n
If abstract 1 is used as information source, add a citation [1] to the text if an abstact is used.\n
Only use abstracts that are listed as context as an abstract, do not make up numbers.
Never add a list of citations at the end of the story.
`

for(let i = 0; i < abstracts.length; i++) {
// if the abstracts are sent from the bibliography section
if(abstracts[i].hasOwnProperty("publication").hasOwnProperty("abstract")){
prompt = prompt.concat("\nAbstract [", abstracts[i].publication.number, "]\n Title:\n", abstracts[i].publication.title, "\nAbstract:\n", abstracts[i].publication.abstract.replace(/<[^>]*>?/gm, ''))
// if the abstracts are sent from the EuropePMC section
} else if(abstracts[i].hasOwnProperty("abstract")) {
prompt = prompt.concat("\nAbstract [", abstracts[i].number, "]\n Title:\n", abstracts[i].title, "\nAbstract:\n", abstracts[i].abstract.replace(/<[^>]*>?/gm, ''))
}
}
const apiResponse = await model.invoke(prompt);

return apiResponse
};



export const getPublicationSummary = async ({
text,
targetSymbol,
Expand Down
Loading

0 comments on commit 21ac897

Please sign in to comment.