REFLECTOOL: Towards Reflection-Aware Tool-Augmented Clinical Agents

October 25, 2024 ยท View on GitHub

This is the official repository for "REFLECTOOL: Towards Reflection-Aware Tool-Augmented Clinical Agents"

[Webpage] [Paper] [Huggingface Dataset] [Leaderboard]

Outlines

๐Ÿ”ฆ About

ClinicalAgent Bench

Despite clinical agents succeeding in diverse signal interaction, they are oriented to a single clinical scenario and hence fail for broader applications. To evaluate clinical agents holistically, we propose ClinicalAgent Bench, a comprehensive medical agent benchmark consisting of 18 tasks across five key realistic clinical dimensions.

ReflecTool

Building on this, we introduce ReflectTool, a novel framework that excels at utilizing domain-specific tools within two stages. ReflectTool can search for supportive successful demonstrations from already built long-term memory to guide the tool selection strategy, and a verifier improves the tool usage according to the tool-wise experience with two verification methods--Iterative Refinement and Candidate Selection.

๐Ÿ† LeaderBoard

MethodsTypeTotalKnowledge& ReasoningMultimodalNumerical AnalysisData UnderstandingTrustworthiness
MedQAMMLUBioASQPubMedQAAvg.VQARADSLAKEOmniMedQAAvg.MedCalcEHRSQLMIMIC-IIIeICUAvg.MedMentionsemrQALongHealthQAAvg.MedHalt-RhtMedVQA-HaltEHR-HaltLongHaltAvg.
MedLlama3-8bLLM25.1458.1372.8266.1842.4059.88----22.457.928.657.4011.6122.9323.10-23.029.90-2.23-6.07
Qwen2-7BLLM38.0154.0469.5171.6848.2060.86----14.4218.0919.7021.7318.4918.3839.8675.2544.5014.11-3.9766.5028.19
Llama3-8bLLM35.6256.3270.3472.8253.8063.32----28.0817.0212.5321.8319.8728.5441.92-35.2329.22-18.90-24.06
Llama3.1-8bLLM42.4665.2076.5874.9253.0067.43----37.4411.3517.4222.0732.0842.4174.2549.5827.78-3.9760.5030.75
Qwen2-72B*LLM48.7671.2584.4882.8553.0072.90----32.1923.9833.9534.1531.0729.2042.8979.7550.6131.56-31.3058.5040.45
Llama3.1-70b*LLM47.5979.5888.1582.5257.4076.91----48.5216.4925.4426.4729.2325.7131.6980.0045.8028.22-22.4864.5038.40
GPT-3.5-turboLLM31.3158.6869.8875.4050.6063.64----20.5317.5724.3114.3019.1826.8821.64-24.269.78-26.55-18.17
MiniCPM-V-2.6MLLM29.2846.5861.1670.2347.2056.2948.7847.1273.7056.5313.281.611.631.884.6018.9217.425.2513.8612.4436.898.912.2515.12
InternVL-Chat-V1.5MLLM37.0250.8265.5664.8930.4052.9249.6741.4768.5053.2118.9117.9918.0520.8318.9526.4742.53*34.5025.7850.780.00*25.52
HuatuoGPT-Vision-7BMLLM41.1150.4366.1273.3054.0060.9653.6552.9792.1066.2413.564.399.279.769.2516.7438.4473.0042.7314.3323.442.4265.2526.36
HuatuoGPT-Vision-34BMLLM41.3154.8372.3673.7948.0062.2556.7653.7291.5067.3325.798.149.159.7913.2216.3441.68*29.0128.4443.7732.07*34.76
GPT-4o-miniMLLM52.6576.9085.6782.8449.2073.6550.4746.4759.2052.0550.4321.7328.0718.1929.6131.6640.0078.5050.0562.1153.7845.7470.0057.91
COTQwen2-7b Agent39.9452.4769.9772.2141.0058.91----19.1016.0623.8120.9519.9822.8319.9265.7536.1745.56-31.4957.0044.68
ReActQwen2-7b Agent42.7351.6167.6880.2448.6062.0335.9239.5972.9049.4718.6218.5225.0634.0024.0522.1924.0441.5029.2449.8935.3361.4948.7548.87
CRITICQwen2-7b Agent43.9752.8758.6871.6843.2056.6148.1242.7070.8053.8713.0923.5528.2033.1224.4925.4736.3350.2537.3530.4428.3357.6473.7547.54
ReflexionQwen2-7b Agent45.2551.7866.4874.6050.8060.9245.6847.9777.2056.9513.3717.5622.1630.2320.8330.3028.9253.0037.4150.5536.3362.9150.7550.14
ReflecTool (Iterative Refinement, k=2)Qwen2-7b Agent49.3750.1265.4776.3763.2063.7953.8845.7182.9060.8324.6824.2016.9222.0821.9743.6950.2761.0051.6554.4437.9956.7345.2548.60
ReflecTool (Candidates Selection, k=2)Qwen2-7b Agent49.0850.8164.8472.9862.6062.8156.7649.7679.2061.9124.6429.8725.1327.4826.7859.2143.4054.0052.2053.4434.2155.9723.2541.72
COTQwen2-72b Agent50.5872.5185.4581.0737.4069.11----29.8918.7323.5525.7224.4724.4352.0981.0052.5157.11-40.7970.7556.22
ReActQwen2-72b Agent53.3172.4385.6785.3862.4076.4750.0946.0173.0056.3726.4635.9731.4531.8731.4442.1155.0262.7553.2956.8924.7555.7858.5048.98
CRITICQwen2-72b Agent52.3571.8585.3187.8651.0074.0140.5850.8073.5054.9622.4435.4832.4733.2830.9233.6056.3675.5055.1551.7724.2252.0358.7546.69
ReflexionQwen2-72b Agent56.3770.7884.3087.0665.0076.7954.9950.0577.8060.9522.4540.1431.9433.4231.9952.3758.7364.0058.3759.0033.0059.0064.0053.75
ReflecTool (Iterative Refinement, k=2)Qwen2-72b Agent59.4373.3084.1184.6365.2076.8157.2148.8285.2063.7436.0147.4333.9636.3938.4554.5767.9668.0063.5159.5538.2157.8263.0054.65
ReflecTool (Candidates Selection, k=2)Qwen2-72b Agent59.6671.3784.0083.5066.2076.2757.6648.5481.9062.7036.7749.8931.2034.3838.0660.5166.6166.5064.5460.7739.6359.7866.7556.73

๐Ÿ’ซ News

  • ๐Ÿ”ฅ [2024/10/24] We release the ClinicalAgent Bench, comprised 18 tasks across five capacity dimensions!
  • ๐Ÿ”ฅ [2024/10/24] We release the code implementation of the ReflecTool!

๐Ÿ’ก Installations

Environment

conda create -n reflectool python=3.10.8
conda activate reflectool
# install torch
pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 --index-url https://download.pytorch.org/whl/cu118
# install java
conda install -c conda-forge openjdk=21.0.4

Dependency

git clone https://github.com/BlueZeros/ReflecTool.git
cd ReflecTool
pip install -e .

ToolBox

You can find details of the tools installation in here.

๐Ÿ“” Dataset

Download the dataset ClinicalAgentBench from here and put it into the temp folder. The ClinicalAgentBench has the structure below:

ReflecTool/
โ”œโ”€โ”€ ClinicalAgentBench/
โ”‚   โ”œโ”€โ”€ ablations/ # subset used for analysis
โ”‚   โ”œโ”€โ”€ train/ # dataset used for optimization stage
โ”‚   โ”œโ”€โ”€ test/ # dataset used for evaluation
โ”‚   โ”œโ”€โ”€ memory/ # few-shot samples, long-term memory and tool experience

๐Ÿ”ฌ Support Models

The support model list can be found in model_config. You should add corresponding folder path before using the model. The type of the supported models are shown below:

  • Local Models: Huggingface models load with AutoModelForCausalLM. We also support the vllm accelerate for these models (automatically used if vllm is installed).
    • Llama3
    • Llama3.1
    • Qwen1.5
    • Qwen2
    • Qwen2.5
  • OpenAI Models: OpenAI Model list.
    • GPT-3.5-turbo
    • GPT-4o-mini
    • GPT-4o
    • Note: need to set environment variables OPENAI_API_KEY={your_openai_api_key}.
  • MultiModal LLM: HuatuoGPT-Vision and MiniCPM.
    • HuatuoGPT-Vision
    • MiniCPM-V-2.6
    • InternVL-Chat-V1.5

๐Ÿ“ Evaluation

Baselines

Models
DATA_PATH=./ClinicalAgentBench
TASK_PATH=test

OUTPUT_PATH=./results/${TASK_PATH}
MEMORY_PATH=${DATA_PATH}/memory

DATASET=medqa
MODEL=qewn2-7b

EXP_NAME=${MODEL}
mkdir -p ${OUTPUT_PATH}/${DATASET}/${EXP_NAME}

python run.py \
    --data-path ${DATA_PATH} \
    --output-path ${OUTPUT_PATH} \
    --task-name ${TASK_PATH} \
    --test-split ${DATASET} \
    --exp-name ${EXP_NAME} \
    --model ${MODEL} \
    --log-print \
    --prompt-debug \
    --resume
Reflexion Agent
DATA_PATH=./ClinicalAgentBench
TASK_PATH=test

OUTPUT_PATH=./results/${TASK_PATH}
MEMORY_PATH=${DATA_PATH}/memory

DATASET="medqa"
MODEL=qwen2-7b
ACTIONS="all_wo_mm"
FEWSHOT=1

EXP_NAME=reflexion_${MODEL}-few_shot_${FEWSHOT}-${ACTIONS}
mkdir -p ${OUTPUT_PATH}/${DATASET}/${EXP_NAME}

python run.py \
    --data-path ${DATA_PATH} \
    --output-path ${OUTPUT_PATH} \
    --task-name ${TASK_PATH} \
    --test-split ${DATASET} \
    --exp-name ${EXP_NAME} \
    --agent "reflexion" \
    --model ${MODEL} \
    --actions ${ACTIONS} \
    --memory-path ${MEMORY_PATH} \
    --memory-type "reflexion_standard" \
    --few-shot ${FEWSHOT} \
    --resume
CRITIC Agent
DATA_PATH=./ClinicalAgentBench
TASK_PATH=test

OUTPUT_PATH=./results/${TASK_PATH}
MEMORY_PATH=${DATA_PATH}/memory

DATASET="medqa"
MODEL=qwen2-72b-int4

ACTIONS="all_wo_mm"
FEWSHOT=1

EXP_NAME=critic_${MODEL}-few_shot_${FEWSHOT}-${ACTIONS}
mkdir -p ${OUTPUT_PATH}/${DATASET}/${EXP_NAME}

python run.py \
    --data-path ${DATA_PATH} \
    --output-path ${OUTPUT_PATH} \
    --task-name ${TASK_PATH} \
    --test-split ${DATASET} \
    --exp-name ${EXP_NAME} \
    --agent "critic" \
    --model ${MODEL} \
    --actions ${ACTIONS} \
    --memory-path ${MEMORY_PATH} \
    --memory-type "critic_standard" \
    --few-shot ${FEWSHOT} \
    --resume \
    --log-print \
    --prompt-debug

ReflecTool

Optimization

optimization on medqa dataset (Knowledge task example)
DATA_PATH=./ClinicalAgentBench
TASK_PATH=train

OUTPUT_PATH=./results/${TASK_PATH}
MEMORY_PATH=${DATA_PATH}/my_memory

domain=medqa
MODEL=qwen2-7b
ACTIONS="all_wo_mm" # tool type. note that `all` and `mm` actions should use 2 gpu.
FEWSHOT=0 # num of few-shot demonstration

EXP_NAME=train-${MODEL}-few_shot_${FEWSHOT}-${ACTIONS}
mkdir -p ${OUTPUT_PATH}/${domain}/${EXP_NAME}

python train.py \
    --data-path ${DATA_PATH} \
    --output-path ${OUTPUT_PATH} \
    --task-name ${TASK_PATH} \
    --test-split ${domain} \
    --exp-name ${EXP_NAME} \
    --model ${MODEL} \
    --actions ${ACTIONS} \
    --memory-path ${MEMORY_PATH} \
    --max-exec-steps 15 \
    --few-shot ${FEWSHOT} \
    --write-memory \
    --memory-type task \
    --log-print \
    --resume
optimization on slake dataset (MultiModal task example)
DATA_PATH=./ClinicalAgentBench
TASK_PATH=train

OUTPUT_PATH=./results/${TASK_PATH}
MEMORY_PATH=${DATA_PATH}/my_memory

domain=slake
MODEL=qwen2-7b
ACTIONS="all" # tool type. note that `all` and `mm` actions should use 2 gpu.
FEWSHOT=0 # num of few-shot demonstration

EXP_NAME=train-${MODEL}-few_shot_${FEWSHOT}-${ACTIONS}
mkdir -p ${OUTPUT_PATH}/${domain}/${EXP_NAME}

python train.py \
    --data-path ${DATA_PATH} \
    --output-path ${OUTPUT_PATH} \
    --task-name ${TASK_PATH} \
    --test-split ${domain} \
    --exp-name ${EXP_NAME} \
    --model ${MODEL} \
    --actions ${ACTIONS} \
    --memory-path ${MEMORY_PATH} \
    --max-exec-steps 15 \
    --few-shot ${FEWSHOT} \
    --write-memory \
    --memory-type task \
    --log-print \
    --resume

Inference

Evaluation on medqa dataset with ReflecTool (Iterative Refinement)
DATA_PATH=./ClinicalAgentBench
TASK=test

OUTPUT_PATH=./results/${TASK}

DATASET=medqa
MODEL=qwen2-7b
ACTIONS="all_wo_mm" # tool type. note that `all` and `mm` actions should use 2 gpu.
FEWSHOT=1 # num of few-shot demonstration
MEMORY_PATH=${DATA_PATH}/memory/task/long_term_memory/${DATASET}

EXP_NAME=reflectool_refine-${MODEL}-few_shot_${FEWSHOT}-${ACTIONS}-task_memory
mkdir -p ${OUTPUT_PATH}/${TASK}/${DATASET}/${EXP_NAME}

python run.py \
    --data-path ${DATA_PATH} \
    --output-path ${OUTPUT_PATH} \
    --task-name ${TASK} \
    --test-split ${DATASET} \
    --exp-name ${EXP_NAME} \
    --agent "reflectool" \
    --model ${MODEL} \
    --actions ${ACTIONS} \
    --action-guide-path ${DATA_PATH}/memory/task/tool_experience/${DATASET}.json \
    --memory-path ${MEMORY_PATH} \
    --force-action \
    --action-search "refine" \
    --few-shot ${FEWSHOT} \
    --log-print \
    --prompt-debug \
    --resume
Evaluation on medqa dataset with ReflecTool (Candidate Selection)
DATA_PATH=./ClinicalAgentBench
TASK=test

OUTPUT_PATH=./results/${TASK}

DATASET=medqa
MODEL=qwen2-7b
ACTIONS="all_wo_mm" # tool type. note that `all` and `mm` actions should use 2 gpu.
FEWSHOT=1 # num of few-shot demonstration
MEMORY_PATH=${DATA_PATH}/memory/task/long_term_memory/${DATASET}

EXP_NAME=reflectool_select-${MODEL}-few_shot_${FEWSHOT}-${ACTIONS}-task_memory
mkdir -p ${OUTPUT_PATH}/${TASK}/${DATASET}/${EXP_NAME}

python run.py \
    --data-path ${DATA_PATH} \
    --output-path ${OUTPUT_PATH} \
    --task-name ${TASK} \
    --test-split ${DATASET} \
    --exp-name ${EXP_NAME} \
    --agent "reflectool" \
    --model ${MODEL} \
    --actions ${ACTIONS} \
    --action-guide-path ${DATA_PATH}/memory/task/tool_experience/${DATASET}.json \
    --memory-path ${MEMORY_PATH} \
    --force-action \
    --action-search "select" \
    --few-shot ${FEWSHOT} \
    --log-print \
    --prompt-debug \
    --resume

๐Ÿ“š Quickly Use

ReAct Agent

  • Question Answering
from reflectool.agents.TaskAgent import TaskAgent
from reflectool.commons.TaskPackage import TaskPackage
from reflectool.utilities import parse_args


args = parse_args()
args.model = "gpt-4o-mini"
Agent = TaskAgent(args)

task = TaskPackage(
    inputs="Can you explain me about the fever?",
    instruction="",
)
print(Agent(task))
  • Visual Question Answering
from reflectool.agents.TaskAgent import TaskAgent
from reflectool.commons.TaskPackage import TaskPackage
from reflectool.utilities import parse_args


args = parse_args()
args.model = "gpt-4o-mini"
Agent = TaskAgent(args)

task = TaskPackage(
    inputs="Can you descript the image?",
    instruction="",
    multimodal_inputs={
      "image": "{path to the image}",
    }
)
print(Agent(task))

ReflecTool Agent

  • Question Answering
from reflectool.agents.ReflecToolAgent import ReflecToolAgent
from reflectool.commons.TaskPackage import TaskPackage
from reflectool.utilities import parse_args


args = parse_args()
args.model = "gpt-4o-mini"
args.action_search = "refine" # verification method: "refine" or "select"
Agent = ReflecToolAgent(args)

task = TaskPackage(
    inputs="Can you explain me about the fever?",
    instruction="",
)
print(Agent(task))

๐Ÿชถ Acknowledgements

Thanks to the codebase we built upon:

Citation

@misc{liao2024reflectoolreflectionawaretoolaugmentedclinical,
      title={ReflecTool: Towards Reflection-Aware Tool-Augmented Clinical Agents}, 
      author={Yusheng Liao and Shuyang Jiang and Yanfeng Wang and Yu Wang},
      year={2024},
      eprint={2410.17657},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2410.17657}, 
}