added kg-rag notebook

This commit is contained in:
root
2024-03-18 13:31:19 -07:00
parent 37c1b8cc90
commit 2ab25a6d8f
3 changed files with 342 additions and 2606 deletions

316
notebooks/gpt_prompts.ipynb Normal file
View File

@@ -0,0 +1,316 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 26,
"id": "3d3dca32-b77f-471d-b834-20ac795f9f17",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.chdir('..')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "9da344d2-8e45-4574-aa19-4ad76c566101",
"metadata": {},
"outputs": [],
"source": [
"from kg_rag.utility import *\n"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "b44bf274-41d1-4153-a65e-bfb9b90ebcc6",
"metadata": {},
"outputs": [],
"source": [
"\n",
"def generate_response(question, llm, kg_rag_flag, evidence_flag=False, temperature=0):\n",
" CHAT_MODEL_ID = llm\n",
" CHAT_DEPLOYMENT_ID = llm\n",
" \n",
" if kg_rag_flag:\n",
" SYSTEM_PROMPT = system_prompts[\"KG_RAG_BASED_TEXT_GENERATION\"]\n",
" CONTEXT_VOLUME = int(config_data[\"CONTEXT_VOLUME\"])\n",
" QUESTION_VS_CONTEXT_SIMILARITY_PERCENTILE_THRESHOLD = float(config_data[\"QUESTION_VS_CONTEXT_SIMILARITY_PERCENTILE_THRESHOLD\"])\n",
" QUESTION_VS_CONTEXT_MINIMUM_SIMILARITY = float(config_data[\"QUESTION_VS_CONTEXT_MINIMUM_SIMILARITY\"])\n",
" VECTOR_DB_PATH = config_data[\"VECTOR_DB_PATH\"]\n",
" NODE_CONTEXT_PATH = config_data[\"NODE_CONTEXT_PATH\"]\n",
" SENTENCE_EMBEDDING_MODEL_FOR_NODE_RETRIEVAL = config_data[\"SENTENCE_EMBEDDING_MODEL_FOR_NODE_RETRIEVAL\"]\n",
" SENTENCE_EMBEDDING_MODEL_FOR_CONTEXT_RETRIEVAL = config_data[\"SENTENCE_EMBEDDING_MODEL_FOR_CONTEXT_RETRIEVAL\"]\n",
" vectorstore = load_chroma(VECTOR_DB_PATH, SENTENCE_EMBEDDING_MODEL_FOR_NODE_RETRIEVAL)\n",
" embedding_function_for_context_retrieval = load_sentence_transformer(SENTENCE_EMBEDDING_MODEL_FOR_CONTEXT_RETRIEVAL)\n",
" node_context_df = pd.read_csv(NODE_CONTEXT_PATH)\n",
" context = retrieve_context(question, vectorstore, embedding_function_for_context_retrieval, node_context_df, CONTEXT_VOLUME, QUESTION_VS_CONTEXT_SIMILARITY_PERCENTILE_THRESHOLD, QUESTION_VS_CONTEXT_MINIMUM_SIMILARITY, evidence_flag)\n",
" enriched_prompt = \"Context: \"+ context + \"\\n\" + \"Question: \" + question\n",
" question = enriched_prompt\n",
" else:\n",
" SYSTEM_PROMPT = system_prompts[\"PROMPT_BASED_TEXT_GENERATION\"]\n",
" \n",
" output = get_GPT_response(question, SYSTEM_PROMPT, CHAT_MODEL_ID, CHAT_DEPLOYMENT_ID, temperature=temperature)\n",
" stream_out(output)\n"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "33c0771d-e6be-406b-9b17-51f6377bcb6a",
"metadata": {},
"outputs": [],
"source": [
"\n",
"LLM_TO_USE = 'gpt-4'\n",
"TEMPERATURE = config_data[\"LLM_TEMPERATURE\"]\n"
]
},
{
"cell_type": "markdown",
"id": "aab5d68e-ed47-4e36-986f-8842287d8ab6",
"metadata": {},
"source": [
"## Question 1:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bbbdb428-6d01-43f2-9e58-b919e7a68736",
"metadata": {},
"outputs": [],
"source": [
"question = 'Are there any latest drugs used for weight management in patients with Bardet-Biedl Syndrome?'\n"
]
},
{
"cell_type": "markdown",
"id": "19ffb33b-bce6-4cb1-8f86-76080fcf0e40",
"metadata": {},
"source": [
"### With KG-RAG"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "02374d3c-5711-4704-8b37-7eda0965c9b0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Yes, the compound Setmelanotide is used to treat Bardet-Biedl syndrome. This information is sourced from ChEMBL and DrugCentral.\n",
"\n"
]
}
],
"source": [
"KG_RAG_FLAG = True\n",
"EDGE_EVIDENCE_FLAG = True #Used only when KG_RAG_FLAG=True\n",
"\n",
"generate_response(question, LLM_TO_USE, KG_RAG_FLAG, evidence_flag=EDGE_EVIDENCE_FLAG, temperature=TEMPERATURE)\n"
]
},
{
"cell_type": "markdown",
"id": "17be2ecd-40f1-44b5-8f79-9358264a3703",
"metadata": {},
"source": [
"### Without KG-RAG"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "06bf55f2-eccf-4c26-b65a-0ed2ed30e689",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"As of my knowledge up to date, there are no specific drugs designed for weight management in patients with Bardet-Biedl Syndrome. The treatment generally involves managing the symptoms and complications. However, any new developments would be best advised by a healthcare professional.\n",
"\n"
]
}
],
"source": [
"KG_RAG_FLAG = False\n",
"\n",
"generate_response(question, LLM_TO_USE, KG_RAG_FLAG, temperature=TEMPERATURE)\n"
]
},
{
"cell_type": "markdown",
"id": "afa24795-71e1-4f1a-ac99-b8d106513f76",
"metadata": {},
"source": [
"## Question 2:"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "eca290bb-fe30-4bb2-ab36-405c2151bccb",
"metadata": {},
"outputs": [],
"source": [
"question = 'Is it PNPLA3 or HLA-B that has a significant association with the disease liver benign neoplasm?'\n"
]
},
{
"cell_type": "markdown",
"id": "0cd8e67f-f769-4130-8e7d-52e0524c66df",
"metadata": {},
"source": [
"### With KG-RAG"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "3dc1d019-8676-49d9-82c3-18ebe5497dbd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"PNPLA3 has a more significant association with the disease liver benign neoplasm, as indicated by its lower GWAS p-value of 4e-14 compared to HLA-B's p-value of 2e-08. The provenance of this association is GWAS.\n",
"\n"
]
}
],
"source": [
"KG_RAG_FLAG = True\n",
"EDGE_EVIDENCE_FLAG = True #Used only when KG_RAG_FLAG=True\n",
"\n",
"generate_response(question, LLM_TO_USE, KG_RAG_FLAG, evidence_flag=EDGE_EVIDENCE_FLAG, temperature=TEMPERATURE)\n"
]
},
{
"cell_type": "markdown",
"id": "30121a5b-3956-41bd-ac4a-62e32744ba99",
"metadata": {},
"source": [
"### Without KG-RAG"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "8614706d-90d9-49e8-9481-30628505ba2e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"It is PNPLA3 that has a significant association with the disease liver benign neoplasm.\n",
"\n"
]
}
],
"source": [
"KG_RAG_FLAG = False\n",
"\n",
"generate_response(question, LLM_TO_USE, KG_RAG_FLAG, temperature=TEMPERATURE)\n"
]
},
{
"cell_type": "markdown",
"id": "1fd3811b-546f-4ec3-8207-f9323e372744",
"metadata": {},
"source": [
"## Other questions:"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "2a1c9337-fd39-45b0-b12a-6de9b5971b9e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"question = 'Which drugs are contraindicated in obsessive compulsive disorder?'\n"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "e6852cb3-8bf9-408b-ab65-492b75c690ed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The drugs that are contraindicated in obsessive-compulsive disorder include Orphenadrine, PHENYLPROPANOLAMINE POLISTIREX, CHLORPHENIRAMINE POLISTIREX, CODEINE POLISTIREX, Ibuprofen Lysine, Benzoic Acid, (R)-3-(1-Hydroxy-2-(methylamino)ethyl)phenol 2,3-dihydroxysuccinate, Dexibuprofen, Phenacetin, Pheniramine Maleate, Pyrilamine, Butalbital, Propoxyphene, Ibuprofen, Orphenadrine Citrate, Phenobarbital, Caffeine, Caffeine Citrate, 2-Amino-1-phenyl-1-propanol, Thiamine, Dihydrocodeine tartrate, Chlorpheniramine, (1S,2R)-1-benzyl-3-(dimethylamino)-2-methyl-1-phenylpropyl propanoate naphtalene-2-sulfonic acid, Phenylephrine, Ergotamine, Codeine, Riboflavin, and Pheniramine. The provenance of this information is DrugCentral.\n",
"\n"
]
}
],
"source": [
"KG_RAG_FLAG = True\n",
"EDGE_EVIDENCE_FLAG = True \n",
"\n",
"generate_response(question, LLM_TO_USE, KG_RAG_FLAG, evidence_flag=EDGE_EVIDENCE_FLAG, temperature=TEMPERATURE)\n"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "b8079bc6-d309-4c88-9440-376aa43d972e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There are no specific drugs that are universally contraindicated in obsessive-compulsive disorder (OCD). However, certain medications like benzodiazepines and atypical antipsychotics may potentially worsen OCD symptoms. The choice of medication always depends on the individual's overall health, the severity of their symptoms, and their response to treatment.\n",
"\n"
]
}
],
"source": [
"KG_RAG_FLAG = False\n",
"\n",
"generate_response(question, LLM_TO_USE, KG_RAG_FLAG, temperature=TEMPERATURE)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d44098cc-ca4c-4ffa-a5ca-e273a029cb67",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because it is too large Load Diff