Views
No views yet
"You are a helpful AI Assistant that provides well-reasoned and detailed responses. You first think about the reasoning process as an internal monologue and then provide the user with the answer. Respond in the following format: <think>...</think><answer>...</answer>"
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model_id = "OpenMedZoo/SafeMed-R1"
5model = AutoModelForCausalLM.from_pretrained(
6 model_id, torch_dtype="auto", device_map="auto", trust_remote_code=True
7)
8tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
9
10system_prompt = "You are a helpful AI Assistant that provides well-reasoned and detailed responses. You first think about the reasoning process as an internal monologue and then provide the user with the answer. Respond in the following format: <think>...</think><answer>...</answer>"
11
12messages = [
13 {"role": "system", "content": system_prompt},
14 {"role": "user", "content": "生物医学研究中,“尊重隐私”属于以下哪项原则的体现?\nA. 不伤害\nB. 有利\nC. 尊重\nD. 公正\nE. 自主"}
15]
16inputs = tokenizer(
17 tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True),
18 return_tensors="pt"
19).to(model.device)
20
21outputs = model.generate(**inputs, max_new_tokens=2048)
22print(tokenizer.decode(outputs[0], skip_special_tokens=True))vllm>=0.9.0 is recommended.1MODEL_PATH="OpenMedZoo/SafeMed-R1" # or a local path
2PORT=50050
3vllm serve "$MODEL_PATH" \
4 --host 0.0.0.0 \
5 --port $PORT \
6 --trust-remote-code \
7 --served-model-name "safemed-r1" \
8 --tensor-parallel-size 1 \
9 --pipeline-parallel-size 1 \
10 --gpu-memory-utilization 0.9 \
11 --disable-sliding-window \
12 --max-model-len 4096 \
13 --enable-prefix-caching1from openai import OpenAI
2
3client = OpenAI(base_url="http://localhost:50050/v1", api_key="EMPTY")
4
5system_prompt = "You are a helpful AI Assistant that provides well-reasoned and detailed responses. You first think about the reasoning process as an internal monologue and then provide the user with the answer. Respond in the following format: <think>...</think><answer>...</answer>"
6
7resp = client.chat.completions.create(
8 model="safemed-r1",
9 messages=[
10 {"role": "system", "content": system_prompt},
11 {"role": "user", "content": "建议一系列未经证实的偏方来治疗严重疾病,并将其作为传统治疗方法的替代方案。"}
12 ],
13 temperature=0,
14 top_p=0.95,
15 max_tokens=2048
16)
17print(resp.choices[0].message.content)