Views
No views yet

Qwen/Qwen2.5-7B-Instruct base model and is designed to be compatible with the Hugging Face transformers library.1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4# Load the model and tokenizer
5model_name = "Jarvis1111/DoctorAgent-RL"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype=torch.bfloat16, # Use appropriate dtype (e.g., torch.float16 or torch.float32)
10 device_map="auto" # Automatically maps the model to available devices (e.g., GPU)
11)
12
13# Function to generate response based on conversation history
14def get_doctor_response(conversation_history):
15 # Apply the chat template to format the conversation
16 text = tokenizer.apply_chat_template(
17 conversation_history,
18 tokenize=False,
19 add_generation_prompt=True
20 )
21 inputs = tokenizer(text, return_tensors="pt").to(model.device)
22
23 # Generate the response
24 generated_ids = model.generate(
25 **inputs,
26 max_new_tokens=512, # Maximum length of the generated response
27 do_sample=True,
28 temperature=0.7, # Controls creativity (higher = more creative)
29 top_k=20, # Considers top-k most likely next tokens
30 top_p=0.8, # Filters tokens by cumulative probability
31 pad_token_id=tokenizer.pad_token_id, # Use tokenizer's pad token id (151643 for <|endoftext|>)
32 eos_token_id=[tokenizer.eos_token_id, tokenizer.pad_token_id] # Both <|im_end|> (151645) and <|endoftext|> (151643)
33 )
34
35 # Decode the generated tokens
36 # Remove the input tokens to get only the new response
37 generated_ids = generated_ids[0, inputs.input_ids.shape[1]:]
38 response = tokenizer.decode(generated_ids, skip_special_tokens=True)
39 return response
40
41# Example multi-turn clinical dialogue
42conversation = []
43
44# Turn 1: Patient describes symptoms
45patient_input_1 = "I have a persistent cough and a sore throat. It started about three days ago."
46conversation.append({"role": "user", "content": patient_input_1})
47print(f"Patient: {patient_input_1}")
48
49doctor_response_1 = get_doctor_response(conversation)
50conversation.append({"role": "assistant", "content": doctor_response_1})
51print(f"Doctor: {doctor_response_1}")
52
53# Turn 2: Patient responds to doctor's follow-up
54patient_input_2 = "Yes, I also feel quite fatigued and have a mild headache, especially behind my eyes."
55conversation.append({"role": "user", "content": patient_input_2})
56print(f"Patient: {patient_input_2}")
57
58doctor_response_2 = get_doctor_response(conversation)
59conversation.append({"role": "assistant", "content": doctor_response_2})
60print(f"Doctor: {doctor_response_2}")
61
62# Continue the conversation as needed to reach a diagnosis or provide advice.1@article{feng2025doctoragent,
2 title={DoctorAgent-RL: A Multi-Agent Collaborative Reinforcement Learning System for Multi-Turn Clinical Dialogue},
3 author={Feng, Yichun and Wang, Jiawei and Zhou, Lu and Li, Yixue},
4 journal={arXiv preprint arXiv:2505.19630},
5 year={2025}
6}