Views
No views yet
1git clone https://github.com/xmed-lab/Med-RwR.git
2conda create -n medrwr python==3.10
3conda activate medrwr
4pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu124
5pip install -r requirements.txt
6pip install -e .python retrieve/retrieve.py1import os
2import torch
3
4from swift.llm.infer.infer_engine.pt_engine_retrieve_infer import PtEngine
5from swift.llm.infer.protocol import RequestConfig
6from swift.llm.template.template_inputs import InferRequest
7
8SYSTEM_PROMPT = """You are an experienced expert in medicine. You are given a question, an image and a list of choices. You are required to select the correct answer from the choices.
9First observe the image, think about the question and each choice within <think> </think> tags. During thinking, if needed, retrieve medical knowledge using <query> </query> tags. Only one query is allowed. An external agent will retrieve information and return it within <retrieve> </retrieve> tags.
10You can use the retrieved information to continue thinking and further query if more information is needed. When you can reach a conclusion, output your answer within <answer> </answer> tags.
11The output should be in the following format:
121. If you need more information, output <think> ... </think>\n<query> ... </query>\n<retrieve> ... </retrieve>\n (Multiple think-query-retrieve cycles may occur)
132. If you can directly reach a conclusion without query, output <think> ... </think>\n<answer> ... </answer>"""
14
15
16def run_demo(model, messages, max_new_tokens=512, temperature=0.6, top_p=None, top_k=None, repetition_penalty=None, attn_impl="flash_attn", device_map="cuda"):
17 engine = PtEngine(
18 model_id_or_path=model,
19 attn_impl=attn_impl,
20 device_map=device_map,
21 max_batch_size=1,
22 )
23 infer_request = InferRequest(messages=messages)
24 request_cfg = RequestConfig(
25 max_tokens=max_new_tokens,
26 temperature=temperature,
27 top_p=top_p,
28 top_k=top_k,
29 repetition_penalty=repetition_penalty,
30 )
31 outputs = engine.infer([infer_request], request_cfg, template=engine.default_template, use_tqdm=False)
32 first = outputs[0]
33 text = first.choices[0].message.content
34 print(text)
35
36if __name__ == "__main__":
37 model_path = "Luxuriant16/MedRwR"
38 image_path = "Image Path Here"
39 question = "User Input Here"
40 messages = [
41 {
42 "role": "system",
43 "content": SYSTEM_PROMPT
44 },
45 {
46 "role": "user",
47 "content": [
48 {"type": "image", "image": image_path},
49 {"type": "text", "text": question},
50 ],
51 }
52 ]
53 run_demo(model=model_path, messages=messages)