Spaces:
Sleeping
Sleeping
cleaned clinical trial
Browse files- app.py +58 -55
- test_tool_clinical_trial.ipynb +71 -1
- tool_clinical_trial.py +2 -3
app.py
CHANGED
|
@@ -26,7 +26,7 @@ def is_clinical_question(query: str) -> bool:
|
|
| 26 |
import logging
|
| 27 |
from datetime import datetime
|
| 28 |
logging.info("Processing request")
|
| 29 |
-
from langfuse import get_client
|
| 30 |
langfuse = get_client()
|
| 31 |
|
| 32 |
|
|
@@ -59,7 +59,7 @@ except ImportError:
|
|
| 59 |
pass # OpenInference not installed
|
| 60 |
# --- END PATCH ---
|
| 61 |
|
| 62 |
-
def
|
| 63 |
"""Use a smolagent CodeAgent with tools to answer a question.
|
| 64 |
The agent streams its thought process (planning steps) and the final answer.
|
| 65 |
Args:
|
|
@@ -84,70 +84,73 @@ def answer_question(question, history):
|
|
| 84 |
try:
|
| 85 |
logging.info(f"Received question: {question}")
|
| 86 |
now = datetime.utcnow().isoformat()
|
| 87 |
-
span = langfuse.start_span(name=f"{now}_use_rag-request")
|
| 88 |
# append history to the next question
|
| 89 |
question_with_history = "Conversation history:\n" + str(history) + "\n\nNew user question:\n " + question
|
| 90 |
-
span.update(input=question_with_history)
|
| 91 |
-
for st in safe_agent.run(question_with_history,stream=True,return_full_result=True):
|
| 92 |
-
if isinstance(st, smolagents.memory.PlanningStep):
|
| 93 |
-
plan = 20*"# " + "\n# Planning of manager agent" + st.plan.split("## 2. Plan")[-1]
|
| 94 |
-
for m in plan.split("\n"):
|
| 95 |
-
thoughts += "\n" + m
|
| 96 |
-
yield thoughts, final_answer, history
|
| 97 |
-
|
| 98 |
-
elif isinstance(st, smolagents.memory.ToolCall):
|
| 99 |
-
code = 20*"-" + f"\n{st.name}\n\n" + st.dict()['function']['arguments']+ "\n"+ 20*"-"
|
| 100 |
-
for m in code.split("\n"):
|
| 101 |
-
thoughts += "\n" + m
|
| 102 |
-
yield thoughts, final_answer, history
|
| 103 |
-
|
| 104 |
-
elif isinstance(st, smolagents.agents.ActionOutput):
|
| 105 |
-
if not st.output:
|
| 106 |
-
thoughts += "\n\n\n****************\nNo output from action.\n****************\n\n"
|
| 107 |
-
yield thoughts, final_answer, history
|
| 108 |
-
else:
|
| 109 |
-
thoughts += "\n***********\nNow processing the output of the tool\n***********\n\n"
|
| 110 |
-
yield thoughts, final_answer, history
|
| 111 |
-
|
| 112 |
-
elif isinstance(st, smolagents.memory.ActionStep):
|
| 113 |
-
|
| 114 |
-
for chatmessage in st.model_input_messages:
|
| 115 |
-
if chatmessage.role == "assistant":
|
| 116 |
-
managed_agent_plan = chatmessage.content[0]['text'].split("2. Plan")[-1]
|
| 117 |
-
thoughts += "Managed agent plan:\n"
|
| 118 |
-
for l in managed_agent_plan.split("\n"):
|
| 119 |
-
thoughts += l
|
| 120 |
-
thoughts += "\n\n--> Code action from managed agent \n" + st.code_action +"\n\n"
|
| 121 |
-
yield thoughts, final_answer, history
|
| 122 |
-
thoughts += "\n********** End fo Step " + str(st.step_number) + " : *********\n" + str(st.token_usage) + "\nStep duration" + str(st.timing) + "\n\n"
|
| 123 |
-
yield thoughts, final_answer, history
|
| 124 |
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
final_answer = st.output
|
| 132 |
-
history.append({"question": question, "answer": final_answer})
|
| 133 |
-
yield thoughts, final_answer, history
|
| 134 |
|
| 135 |
-
|
| 136 |
|
| 137 |
except GeneratorExit:
|
| 138 |
print("Stream closed cleanly.")
|
| 139 |
-
span.end()
|
| 140 |
-
langfuse.flush()
|
| 141 |
return "","", ""
|
| 142 |
|
| 143 |
except gr.CancelledError:
|
| 144 |
print("Request cancelled")
|
| 145 |
-
span.end()
|
| 146 |
-
langfuse.flush()
|
| 147 |
return "Request cancelled","Submit new request", ""
|
| 148 |
-
|
| 149 |
-
span.end()
|
| 150 |
-
langfuse.flush()
|
| 151 |
|
| 152 |
|
| 153 |
def tool_clinical_trial(query_cond:str=None, query_term:str=None,query_lead:str=None,max_results: str="5") -> str:
|
|
@@ -156,7 +159,7 @@ def tool_clinical_trial(query_cond:str=None, query_term:str=None,query_lead:str=
|
|
| 156 |
|
| 157 |
Args:
|
| 158 |
query_cond (str): Disease or condition (e.g., 'lung cancer', 'diabetes')
|
| 159 |
-
query_term (str): Other terms (e.g., 'AREA[LastUpdatePostDate]RANGE[2023-01-15,MAX]').
|
| 160 |
query_lead (str): Searches the LeadSponsorName
|
| 161 |
max_results (int): Number of trials to return (max: 1000)
|
| 162 |
|
|
@@ -282,7 +285,7 @@ with gr.Blocks() as interface2:
|
|
| 282 |
chat_history = gr.State([])
|
| 283 |
|
| 284 |
submit_evt = submit_btn.click(
|
| 285 |
-
fn=
|
| 286 |
inputs=[question_input, chat_history],
|
| 287 |
outputs=[thoughts_output, response_output, chat_history],
|
| 288 |
queue=True
|
|
@@ -376,7 +379,7 @@ demo = gr.TabbedInterface(
|
|
| 376 |
gr.Textbox(label="Searches the LeadSponsorName",placeholder="Lilly OR Sanofi"),
|
| 377 |
gr.Textbox(label="Max results to retreive",placeholder=50)],
|
| 378 |
outputs=gr.Textbox(label="TOON formated response",lines=10, placeholder="Your answer will be provided here"),
|
| 379 |
-
api_name="
|
| 380 |
gr.Interface(
|
| 381 |
describe_figure,
|
| 382 |
gr.Image(type="pil"),
|
|
|
|
| 26 |
import logging
|
| 27 |
from datetime import datetime
|
| 28 |
logging.info("Processing request")
|
| 29 |
+
from langfuse import get_client, propagate_attributes
|
| 30 |
langfuse = get_client()
|
| 31 |
|
| 32 |
|
|
|
|
| 59 |
pass # OpenInference not installed
|
| 60 |
# --- END PATCH ---
|
| 61 |
|
| 62 |
+
def Agent(question, history):
|
| 63 |
"""Use a smolagent CodeAgent with tools to answer a question.
|
| 64 |
The agent streams its thought process (planning steps) and the final answer.
|
| 65 |
Args:
|
|
|
|
| 84 |
try:
|
| 85 |
logging.info(f"Received question: {question}")
|
| 86 |
now = datetime.utcnow().isoformat()
|
|
|
|
| 87 |
# append history to the next question
|
| 88 |
question_with_history = "Conversation history:\n" + str(history) + "\n\nNew user question:\n " + question
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
+
with langfuse.start_as_current_observation(
|
| 92 |
+
as_type="span",
|
| 93 |
+
name="process-chat-message"
|
| 94 |
+
) as root_span:
|
| 95 |
+
# Propagate session_id to all child observations
|
| 96 |
+
with propagate_attributes(session_id="chat-session-123"):
|
| 97 |
+
# All observations created here automatically have session_id
|
| 98 |
+
with root_span.start_as_current_observation(
|
| 99 |
+
as_type="generation",
|
| 100 |
+
name="generate-response",
|
| 101 |
+
model="gpt-4o"
|
| 102 |
+
) as gen:
|
| 103 |
+
# This generation automatically has session_id
|
| 104 |
+
pass
|
| 105 |
+
|
| 106 |
+
for st in safe_agent.run(question_with_history,stream=True,return_full_result=True):
|
| 107 |
+
if isinstance(st, smolagents.memory.PlanningStep):
|
| 108 |
+
plan = 20*"# " + "\n# Planning of manager agent" + st.plan.split("## 2. Plan")[-1]
|
| 109 |
+
for m in plan.split("\n"):
|
| 110 |
+
thoughts += "\n" + m
|
| 111 |
+
yield thoughts, final_answer, history
|
| 112 |
+
|
| 113 |
+
elif isinstance(st, smolagents.memory.ToolCall):
|
| 114 |
+
code = 20*"-" + f"\n{st.name}\n\n" + st.dict()['function']['arguments']+ "\n"+ 20*"-"
|
| 115 |
+
for m in code.split("\n"):
|
| 116 |
+
thoughts += "\n" + m
|
| 117 |
+
yield thoughts, final_answer, history
|
| 118 |
+
|
| 119 |
+
elif isinstance(st, smolagents.agents.ActionOutput):
|
| 120 |
+
if not st.output:
|
| 121 |
+
thoughts += "\n\n\n****************\nNo output from action.\n****************\n\n"
|
| 122 |
+
yield thoughts, final_answer, history
|
| 123 |
+
else:
|
| 124 |
+
thoughts += "\n***********\nNow processing the output of the tool\n***********\n\n"
|
| 125 |
+
yield thoughts, final_answer, history
|
| 126 |
+
|
| 127 |
+
elif isinstance(st, smolagents.memory.ActionStep):
|
| 128 |
+
for chatmessage in st.model_input_messages:
|
| 129 |
+
if chatmessage.role == "assistant":
|
| 130 |
+
managed_agent_plan = chatmessage.content[0]['text'].split("2. Plan")[-1]
|
| 131 |
+
thoughts += "Managed agent plan:\n"
|
| 132 |
+
for l in managed_agent_plan.split("\n"):
|
| 133 |
+
thoughts += l
|
| 134 |
+
thoughts += "\n\n--> Code action from managed agent \n" + st.code_action +"\n\n"
|
| 135 |
+
yield thoughts, final_answer, history
|
| 136 |
+
thoughts += "\n********** End fo Step " + str(st.step_number) + " : *********\n" + str(st.token_usage) + "\nStep duration" + str(st.timing) + "\n\n"
|
| 137 |
+
yield thoughts, final_answer, history
|
| 138 |
|
| 139 |
+
elif isinstance(st, smolagents.memory.FinalAnswerStep):
|
| 140 |
+
final_answer = st.output
|
| 141 |
+
history.append({"question": question, "answer": final_answer})
|
| 142 |
+
yield thoughts, final_answer, history
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
|
| 145 |
|
| 146 |
except GeneratorExit:
|
| 147 |
print("Stream closed cleanly.")
|
|
|
|
|
|
|
| 148 |
return "","", ""
|
| 149 |
|
| 150 |
except gr.CancelledError:
|
| 151 |
print("Request cancelled")
|
|
|
|
|
|
|
| 152 |
return "Request cancelled","Submit new request", ""
|
| 153 |
+
|
|
|
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
def tool_clinical_trial(query_cond:str=None, query_term:str=None,query_lead:str=None,max_results: str="5") -> str:
|
|
|
|
| 159 |
|
| 160 |
Args:
|
| 161 |
query_cond (str): Disease or condition (e.g., 'lung cancer', 'diabetes')
|
| 162 |
+
query_term (str): Other terms such as exact ID "NCTxxxxxxxx" or (e.g., 'AREA[LastUpdatePostDate]RANGE[2023-01-15,MAX]').
|
| 163 |
query_lead (str): Searches the LeadSponsorName
|
| 164 |
max_results (int): Number of trials to return (max: 1000)
|
| 165 |
|
|
|
|
| 285 |
chat_history = gr.State([])
|
| 286 |
|
| 287 |
submit_evt = submit_btn.click(
|
| 288 |
+
fn=Agent,
|
| 289 |
inputs=[question_input, chat_history],
|
| 290 |
outputs=[thoughts_output, response_output, chat_history],
|
| 291 |
queue=True
|
|
|
|
| 379 |
gr.Textbox(label="Searches the LeadSponsorName",placeholder="Lilly OR Sanofi"),
|
| 380 |
gr.Textbox(label="Max results to retreive",placeholder=50)],
|
| 381 |
outputs=gr.Textbox(label="TOON formated response",lines=10, placeholder="Your answer will be provided here"),
|
| 382 |
+
api_name="use_clinical_trial_to_create_context"),
|
| 383 |
gr.Interface(
|
| 384 |
describe_figure,
|
| 385 |
gr.Image(type="pil"),
|
test_tool_clinical_trial.ipynb
CHANGED
|
@@ -1,5 +1,67 @@
|
|
| 1 |
{
|
| 2 |
-
"cells": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
"metadata": {
|
| 4 |
"kernelspec": {
|
| 5 |
"display_name": "mcp-birthday",
|
|
@@ -7,7 +69,15 @@
|
|
| 7 |
"name": "python3"
|
| 8 |
},
|
| 9 |
"language_info": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"name": "python",
|
|
|
|
|
|
|
| 11 |
"version": "3.10.19"
|
| 12 |
}
|
| 13 |
},
|
|
|
|
| 1 |
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "8c893317",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stderr",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"/Applications/anaconda3/envs/mcp-birthday/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 14 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 15 |
+
]
|
| 16 |
+
}
|
| 17 |
+
],
|
| 18 |
+
"source": [
|
| 19 |
+
"from tool_clinical_trial import ClinicalTrialsSearchTool\n",
|
| 20 |
+
"\n",
|
| 21 |
+
"\n",
|
| 22 |
+
"c = ClinicalTrialsSearchTool()"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "code",
|
| 27 |
+
"execution_count": 4,
|
| 28 |
+
"id": "fa5fcb4a",
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"outputs": [],
|
| 31 |
+
"source": [
|
| 32 |
+
"import requests\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"params={'query.cond': 'abbvie', 'pageSize': 218, 'format': 'json'} \n",
|
| 35 |
+
"\n",
|
| 36 |
+
"response = requests.get(\n",
|
| 37 |
+
" \"https://clinicaltrials.gov/api/v2/studies\",\n",
|
| 38 |
+
" params=params,\n",
|
| 39 |
+
" timeout=30\n",
|
| 40 |
+
" )"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"cell_type": "code",
|
| 45 |
+
"execution_count": 9,
|
| 46 |
+
"id": "1dfc35d6",
|
| 47 |
+
"metadata": {},
|
| 48 |
+
"outputs": [
|
| 49 |
+
{
|
| 50 |
+
"data": {
|
| 51 |
+
"text/plain": [
|
| 52 |
+
"10"
|
| 53 |
+
]
|
| 54 |
+
},
|
| 55 |
+
"execution_count": 9,
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"output_type": "execute_result"
|
| 58 |
+
}
|
| 59 |
+
],
|
| 60 |
+
"source": [
|
| 61 |
+
"len(response.json()['studies'])"
|
| 62 |
+
]
|
| 63 |
+
}
|
| 64 |
+
],
|
| 65 |
"metadata": {
|
| 66 |
"kernelspec": {
|
| 67 |
"display_name": "mcp-birthday",
|
|
|
|
| 69 |
"name": "python3"
|
| 70 |
},
|
| 71 |
"language_info": {
|
| 72 |
+
"codemirror_mode": {
|
| 73 |
+
"name": "ipython",
|
| 74 |
+
"version": 3
|
| 75 |
+
},
|
| 76 |
+
"file_extension": ".py",
|
| 77 |
+
"mimetype": "text/x-python",
|
| 78 |
"name": "python",
|
| 79 |
+
"nbconvert_exporter": "python",
|
| 80 |
+
"pygments_lexer": "ipython3",
|
| 81 |
"version": "3.10.19"
|
| 82 |
}
|
| 83 |
},
|
tool_clinical_trial.py
CHANGED
|
@@ -395,7 +395,7 @@ class ClinicalTrialsSearchTool(Tool):
|
|
| 395 |
return result
|
| 396 |
|
| 397 |
|
| 398 |
-
def forward(self, query_cond:str=None, query_term:str=None,query_lead:str=None,max_results: int =
|
| 399 |
"""
|
| 400 |
Search ClinicalTrials.gov for trials.
|
| 401 |
|
|
@@ -419,13 +419,12 @@ class ClinicalTrialsSearchTool(Tool):
|
|
| 419 |
params = {k: v for k, v in params.items() if v is not None}
|
| 420 |
try:
|
| 421 |
response = requests.get(
|
| 422 |
-
"https://clinicaltrials.gov/api/v2/studies",
|
| 423 |
params=params,
|
| 424 |
timeout=30
|
| 425 |
)
|
| 426 |
response.raise_for_status()
|
| 427 |
studies = response.json().get("studies", [])
|
| 428 |
-
|
| 429 |
structured_trials = []
|
| 430 |
for i, study in enumerate(studies):
|
| 431 |
structured_data = self._extract_partner_info(study)
|
|
|
|
| 395 |
return result
|
| 396 |
|
| 397 |
|
| 398 |
+
def forward(self, query_cond:str=None, query_term:str=None,query_lead:str=None,max_results: int = None) -> list:
|
| 399 |
"""
|
| 400 |
Search ClinicalTrials.gov for trials.
|
| 401 |
|
|
|
|
| 419 |
params = {k: v for k, v in params.items() if v is not None}
|
| 420 |
try:
|
| 421 |
response = requests.get(
|
| 422 |
+
"https://clinicaltrials.gov/api/v2/studies", # LIMIT BY THE API ?
|
| 423 |
params=params,
|
| 424 |
timeout=30
|
| 425 |
)
|
| 426 |
response.raise_for_status()
|
| 427 |
studies = response.json().get("studies", [])
|
|
|
|
| 428 |
structured_trials = []
|
| 429 |
for i, study in enumerate(studies):
|
| 430 |
structured_data = self._extract_partner_info(study)
|