File size: 10,654 Bytes
9754890
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/satoc/miniforge3/envs/gradio/lib/python3.12/site-packages/IPython/core/interactiveshell.py:3577: LangChainDeprecationWarning: As of langchain-core 0.3.0, LangChain uses pydantic v2 internally. The langchain_core.pydantic_v1 module was a compatibility shim for pydantic v1, and should no longer be used. Please update the code to import from Pydantic directly.\n",
      "\n",
      "For example, replace imports like: `from langchain_core.pydantic_v1 import BaseModel`\n",
      "with: `from pydantic import BaseModel`\n",
      "or the v1 compatibility namespace if you are working in a code base that has not been fully upgraded to pydantic 2 yet. \tfrom pydantic.v1 import BaseModel\n",
      "\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n",
      "/Users/satoc/miniforge3/envs/gradio/lib/python3.12/site-packages/sentence_transformers/cross_encoder/CrossEncoder.py:13: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
      "  from tqdm.autonotebook import tqdm, trange\n",
      "/Users/satoc/miniforge3/envs/gradio/lib/python3.12/site-packages/transformers/tokenization_utils_base.py:1617: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from langchain_community.agent_toolkits import create_sql_agent\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_groq import ChatGroq\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_core.pydantic_v1 import BaseModel, Field\n",
    "import pandas as pd\n",
    "from pydantic import BaseModel, Field\n",
    "\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "from langchain_community.vectorstores import Chroma\n",
    "from langchain.embeddings import HuggingFaceEmbeddings\n",
    "from langchain_core.runnables import RunnablePassthrough\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "\n",
    "\n",
    "gpt = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)\n",
    "#agent_gpt_executor = create_sql_agent(gpt, db=db, agent_type=\"tool-calling\", verbose=True)\n",
    "\n",
    "## make database\n",
    "from langchain_community.utilities import SQLDatabase\n",
    "from sqlalchemy import create_engine\n",
    "\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "from langchain.schema import SystemMessage\n",
    "from langchain_core.prompts import MessagesPlaceholder\n",
    "#agent_groq_executor = create_sql_agent(llm, db=db, agent_type=\"tool-calling\", verbose=True)\n",
    "\n",
    "from OpenAITools.FetchTools import fetch_clinical_trials, fetch_clinical_trials_jp\n",
    "from OpenAITools.CrinicalTrialTools import QuestionModifierEnglish, TumorNameExtractor, SimpleClinicalTrialAgent,GraderAgent,LLMTranslator,generate_ex_question_English\n",
    "\n",
    "from OpenAITools.JRCTTools import get_matched_df,GetJRCTCriteria\n",
    "from sentence_transformers import SentenceTransformer\n",
    "from sentence_transformers import util\n",
    "groq = ChatGroq(model_name=\"llama3-70b-8192\", temperature=0)\n",
    "#agent_groq_executor = create_sql_agent(groq, db=db, agent_type=\"tool-calling\", verbose=True)\n",
    "\n",
    "import ast\n",
    "import torch\n",
    "age = \"65\"\n",
    "sex =\"男性\"\n",
    "tumor_type =\"glioma\"\n",
    "#tumor_type = \"gastric cancer\"\n",
    "GeneMutation =\"HER2\"\n",
    "Meseable = \"有り\"\n",
    "Biopsiable = \"有り\"\n",
    "NCTID = 'NCT06441994'\n",
    "\n",
    "#Define extractor\n",
    "Translator = LLMTranslator(groq)\n",
    "TumorName = Translator.translate(tumor_type)\n",
    "selectionModel =  SentenceTransformer('pritamdeka/S-PubMedBert-MS-MARCO')\n",
    "ex_question = generate_ex_question_English(age, sex, TumorName, GeneMutation, Meseable, Biopsiable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "basedf = pd.read_csv(\"../ClinicalTrialCSV/JRCT20241215CancerPost.csv\", index_col=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/yw/qz00x75d7kb98f7vm8dkhkvw0000gn/T/ipykernel_52046/2492212482.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['AgentJudgment'] = None\n",
      "/var/folders/yw/qz00x75d7kb98f7vm8dkhkvw0000gn/T/ipykernel_52046/2492212482.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['AgentGrade'] = None\n"
     ]
    }
   ],
   "source": [
    "df = get_matched_df(basedf=basedf, query=TumorName, model=selectionModel, threshold=0.925)\n",
    "df['AgentJudgment'] = None\n",
    "df['AgentGrade'] = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "CriteriaCheckAgent = SimpleClinicalTrialAgent(groq)\n",
    "grader_agent = GraderAgent(groq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The patient is eligible for this clinical trial based on the provided information, as there are no exclusion criteria that would prevent him from participating.\n",
      "yes\n",
      "The patient is eligible for this clinical trial based on the provided criteria, as the patient's age (65) falls within the specified range (18-75), and there is no mention of any exclusion criteria being met.\n",
      "yes\n",
      "The patient is not eligible for this clinical trial because the patient's age (65) exceeds the upper limit of 75 years old specified in inclusion criterion (7).\n",
      "no\n",
      "Based on the provided information, the patient is eligible for this clinical trial. The patient meets the inclusion criteria for Cohort 1, as they have a glioma (WHO Grade 2-4) diagnosed by pathological diagnosis of resection or biopsy, have a history of chemotherapy or radiation therapy, and have measurable lesions based on the RANO criteria. The patient's age of 65 meets the age requirement, and there is no mention of any exclusion criteria that would prevent the patient from participating.\n",
      "yes\n",
      "The patient is not eligible for this clinical trial because the inclusion criteria specify that patients must have a BRAF fusion or rearrangement, but the patient has a HER2 gene mutation.\n",
      "no\n",
      "Based on the provided criteria, the 65-year-old male patient with glioma and a known HER2 gene mutation, measurable and biopsiable tumor, appears to be eligible for this clinical trial, as long as he meets the remaining inclusion criteria (e.g., life expectancy, recovery from prior therapy, organ function, etc.) and does not have any of the exclusion criteria.\n",
      "yes\n",
      "The patient is eligible for this clinical trial based on the provided information, as the patient meets the inclusion criteria (age, measurable tumor, biopsiable tumor) and does not have any obvious exclusions (e.g., no mention of corticosteroids, immunosuppressive agents, or other exclusion criteria). However, some information is missing, such as Karnofsky Performance Status, neutrophil count, hemoglobin, platelet count, and other lab values, which would need to be confirmed to ensure full eligibility.\n",
      "unclear\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(df)):\n",
    "    TargetCriteria = GetJRCTCriteria(df, i)\n",
    "    AgentJudgment = CriteriaCheckAgent.evaluate_eligibility(TargetCriteria, ex_question)\n",
    "    print(AgentJudgment)\n",
    "    AgentGrade = grader_agent.evaluate_eligibility(AgentJudgment)\n",
    "    print(AgentGrade)\n",
    "    \n",
    "    # df.locを使って値を代入(行・列名で指定)\n",
    "    df.loc[df.index[i], 'AgentJudgment'] = AgentJudgment\n",
    "    df.loc[df.index[i], 'AgentGrade'] = AgentGrade\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "for i in range(len(df)):\n",
    "    TargetCriteria = GetJRCTCriteria(df,i)\n",
    "    AgentJudgment = CriteriaCheckAgent.evaluate_eligibility(TargetCriteria, ex_question)\n",
    "    print(AgentJudgment)\n",
    "    AgentGrade = grader_agent.evaluate_eligibility(AgentJudgment)\n",
    "    print(AgentGrade)\n",
    "    df.iloc[i,:]['AgentJudgment'] = AgentJudgment\n",
    "    df.iloc[i,:]['AgentJudgment'] = AgentGrade\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    " # 列を指定した順に並び替え\n",
    "columns_order = ['JRCT ID', 'Title', '研究・治験の目的','AgentJudgment', 'AgentGrade','主たる選択基準', '主たる除外基準','Inclusion Criteria','Exclusion Criteria','NCT No', 'JapicCTI No']\n",
    "df = df[columns_order]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gradio",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}