{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "a3d6ff53-2176-44aa-8590-ec0aa301342d", "metadata": {}, "outputs": [], "source": [ "from vllm import LLM, SamplingParams\n", "import pandas as pd\n", "import numpy as np\n", "import torch.nn.functional as F\n", "import torch\n", "from transformers import AutoTokenizer\n", "from transformers import AutoModelForCausalLM\n", "import re\n", "import os\n" ] }, { "cell_type": "code", "execution_count": null, "id": "62669512-19e7-43cd-a518-4572eea700af", "metadata": { "scrolled": true }, "outputs": [], "source": [ "llama = LLM(model='hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4', tensor_parallel_size = 2, \n", " gpu_memory_utilization=0.90,\n", " download_dir = \"../../\", max_model_len=5000)" ] }, { "cell_type": "code", "execution_count": null, "id": "16bca2af-0cf4-41f2-ae28-d2c669a1af21", "metadata": {}, "outputs": [], "source": [ "def ask_about_trials_loosely(patient_summaries, trial_summaries, llama_model):\n", "\n", " tokenizer = llama_model.get_tokenizer()\n", "\n", " prompts = []\n", "\n", " for patient_summary, trial_summary in zip(patient_summaries, trial_summaries):\n", " messages = [{'role':'system', 'content': \"\"\"You are a brilliant oncologist with encyclopedic knowledge about cancer and its treatment. \n", " Your job is to evaluate whether a given clinical trial is a reasonable consideration for a patient, given a clinical trial summary and a patient summary.\\n\"\"\"}, \n", " {'role':'user', 'content': \"Here is a summary of the clinical trial:\\n\" + trial_summary + \"\\nHere is a summary of the patient:\\n\" + patient_summary + \"\"\"\n", "Base your judgment on whether the patient generally fits the cancer type(s), cancer burden, prior treatment(s), and biomarker criteria specified for the trial.\n", "You do not have to determine if the patient is actually eligible; instead please just evaluate whether it is reasonable for the trial to be considered further by the patient's oncologist.\n", "Some trials have biomarker requirements that are not assessed until formal eligibility screening begins; please ignore these requirements.\n", "Reason step by step, then answer the question \"Is this trial a reasonable consideration for this patient?\" with a one-word \"Yes!\" or \"No!\" answer.\n", "Make sure to include the exclamation point in your final one-word answer.\"\"\"}]\n", "\n", " \n", " prompt = tokenizer.apply_chat_template(conversation=messages, add_generation_prompt=True, tokenize=False)\n", " prompts.append(prompt)\n", " \n", " responses = llama_model.generate(\n", " prompts, \n", " SamplingParams(\n", " temperature=0.0,\n", " top_p=0.2,\n", " max_tokens=2048,\n", " repetition_penalty=1.2,\n", " stop_token_ids=[tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids(\"<|eot_id|>\")], # KEYPOINT HERE\n", " ))\n", "\n", " response_texts = [x.outputs[0].text for x in responses]\n", "\n", " eligibility_results = []\n", "\n", " for response_text in response_texts:\n", " if (\"Yes!\" in response_text) or (\"YES!\" in response_text):\n", " eligibility_results.append(1.0)\n", " else:\n", " eligibility_results.append(0.0)\n", " \n", " return responses, response_texts, eligibility_results\n", " \n", "\n", " \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2ce4cce6-3833-451a-98c6-d7f4c7b948c6", "metadata": {}, "outputs": [], "source": [ "patient_cohort_candidates = pd.read_csv('top_ten_cohorts_tocheck_synthetic_round2.csv')" ] }, { "cell_type": "code", "execution_count": null, "id": "dbb846d2-20e3-4361-b69b-82aa31c1f789", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1fb1aa2f-6c28-4a4e-a4e1-bfd69d0b39a1", "metadata": {}, "outputs": [], "source": [ "patient_cohort_candidates.info()" ] }, { "cell_type": "code", "execution_count": null, "id": "d30bf018-e135-40be-b636-0ba17acf8e61", "metadata": {}, "outputs": [], "source": [ "%%capture\n", "output_list = []\n", "batch_list = []\n", "\n", "num_in_batch = 0\n", "\n", "for i in range(0, patient_cohort_candidates.shape[0]):\n", " \n", " batch_list.append(patient_cohort_candidates.iloc[[i]])\n", " num_in_batch += 1\n", " \n", " if (num_in_batch == 500) or (i == (patient_cohort_candidates.shape[0] - 1)):\n", "\n", " output = pd.concat(batch_list, axis=0)\n", " _, output['llama_response'], output['eligibility_result'] = ask_about_trials_loosely(output['patient_summary'].tolist(), output['this_space'].astype(str).tolist(), llama)\n", "\n", " output_list.append(output)\n", " num_in_batch = 0\n", " batch_list = []\n", " \n", " if (len(output_list) > 0 and (i % 500 == 0)) or (i == (patient_cohort_candidates.shape[0] - 1)):\n", " output_file = pd.concat(output_list, axis=0)\n", " output_file.to_csv('top_ten_cohorts_checked_synthetic_round2.csv')\n" ] }, { "cell_type": "code", "execution_count": null, "id": "91534c0e-4873-4eda-9a69-53660a84b4df", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "eaebffcc-4b62-4ab6-a077-69a6e4340773", "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.9.18" } }, "nbformat": 4, "nbformat_minor": 5 }