File size: 6,433 Bytes
c6aed6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9754890
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6aed6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "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"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fetching data from: https://clinicaltrials.gov/api/v2/studies?query.titles=Stomach cancer SEARCH[Location](AREA[LocationCountry]Japan AND AREA[LocationStatus]Recruiting)&pageSize=100\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'ex_question' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 63\u001b[0m\n\u001b[1;32m     61\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m nct_id \u001b[38;5;129;01min\u001b[39;00m NCTIDs:\n\u001b[1;32m     62\u001b[0m     TargetCriteria \u001b[38;5;241m=\u001b[39m df\u001b[38;5;241m.\u001b[39mloc[df[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mNCTID\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m==\u001b[39m nct_id, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mEligibility Criteria\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mvalues[\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m---> 63\u001b[0m     AgentJudgment \u001b[38;5;241m=\u001b[39m CriteriaCheckAgent\u001b[38;5;241m.\u001b[39mevaluate_eligibility(TargetCriteria, \u001b[43mex_question\u001b[49m)\n\u001b[1;32m     64\u001b[0m     \u001b[38;5;28mprint\u001b[39m(AgentJudgment)\n\u001b[1;32m     66\u001b[0m     AgentGrade \u001b[38;5;241m=\u001b[39m grader_agent\u001b[38;5;241m.\u001b[39mevaluate_eligibility(AgentJudgment)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'ex_question' is not defined"
     ]
    }
   ],
   "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\n",
    "\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",
    "age = \"65\"\n",
    "sex =\"η”·ζ€§\"\n",
    "tumor_type =\"θƒƒη™Œ\"\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",
    "\n",
    "#Make db\n",
    "df = fetch_clinical_trials(TumorName)\n",
    "# ζ–°γ—γ„εˆ—γ‚’θΏ½εŠ \n",
    "df['AgentJudgment'] = None\n",
    "df['AgentGrade'] = None\n",
    "#df = df.iloc[:5, :]\n",
    "NCTIDs = list(df['NCTID' ])\n",
    "\n",
    "#Define agents\n",
    "#modifier = QuestionModifierEnglish(groq)\n",
    "CriteriaCheckAgent = SimpleClinicalTrialAgent(groq)\n",
    "grader_agent = GraderAgent(groq)\n",
    "\n",
    "\n",
    "for nct_id in NCTIDs:\n",
    "    TargetCriteria = df.loc[df['NCTID'] == nct_id, 'Eligibility Criteria'].values[0]\n",
    "    AgentJudgment = CriteriaCheckAgent.evaluate_eligibility(TargetCriteria, ex_question)\n",
    "    print(AgentJudgment)\n",
    "\n",
    "    AgentGrade = grader_agent.evaluate_eligibility(AgentJudgment)\n",
    "    print(AgentGrade)\n",
    "    # NCTIDγ«δΈ€θ‡΄γ™γ‚‹θ‘Œγ‚’θ¦‹γ€γ‘γ¦ζ›΄ζ–°\n",
    "    df.loc[df['NCTID'] == nct_id, 'AgentJudgment'] = AgentJudgment\n",
    "    df.loc[df['NCTID'] == nct_id, 'AgentGrade'] = AgentGrade"
   ]
  },
  {
   "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
}