This repo contains Physician-Ko-8B, a medical language model with 8 billion parameters. This model builds upon the foundation of LLaMA-3-physician-8b-instruct model fine-tuned with a Korean dataset.
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model_name = "eded0902/Physician-Ko-8B"
5tokenizer_name = "YiDuo1999/Llama-3-Physician-8B-Instruct"
6device_map = 'auto'
7
8model = AutoModelForCausalLM.from_pretrained( model_name, trust_remote_code=True,use_cache=False,device_map=device_map)
9tokenizer = AutoTokenizer.from_pretrained(tokenizer_name, trust_remote_code=True)
10
11tokenizer.chat_template = "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"
12eos_token_id = [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>"), tokenizer.convert_tokens_to_ids("<|im_end|>")]
13tokenizer.pad_token = tokenizer.eos_token
14
15def askme(question):
16 sys_message = '''
17 You are an AI Medical Assistant trained on a vast dataset of health information. Please be thorough and
18 provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help.
19 '''
20 # Create messages structured for the chat template
21 messages = [{"role": "system", "content": sys_message}, {"role": "user", "content": question}]
22
23 # Applying chat template
24 prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
25 inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
26 outputs = model.generate(**inputs, max_new_tokens=1000, use_cache=True)
27
28 # Extract and return the generated text, removing the prompt
29 response_text = tokenizer.batch_decode(outputs)[0].strip()
30 answer = response_text.split('<|im_start|>assistant')[-1].split('<|im_end|>')[0].strip()
31 return answer
32
33# Example usage
34# - Context: First describe your problem.
35# - Question: Then make the question.
36question = '''HIV가 뭐야?'''
37print(askme(question))
1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2from langchain.llms.huggingface_pipeline import HuggingFacePipeline
3from langchain_core.prompts import PromptTemplate
4
5model_name = "eded0902/Physician-Ko-8B"
6tokenizer_name = "YiDuo1999/Llama-3-Physician-8B-Instruct"
7device_map = 'auto'
8
9model = AutoModelForCausalLM.from_pretrained( model_name, trust_remote_code=True,use_cache=False,device_map=device_map)
10tokenizer = AutoTokenizer.from_pretrained(tokenizer_name, trust_remote_code=True)
11
12tokenizer.chat_template = "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"
13eos_token_id = [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>"), tokenizer.convert_tokens_to_ids("<|im_end|>")]
14tokenizer.pad_token = tokenizer.eos_token
15
16pipe = pipeline("text-generation",
17 model=model,
18 tokenizer=tokenizer,
19 max_new_tokens=512
20 )
21hf = HuggingFacePipeline(pipeline=pipe)
22
23sys_message = """ You are an AI Medical Assistant trained on a vast dataset of health information. Please be thorough and
24 provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help.
25 """
26question = "HIV가 뭐야?"
27# Create messages structured for the chat template
28messages = [{"role": "system", "content": sys_message}, {"role": "user", "content": question}]
29
30# Applying chat template
31template = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
32prompt = PromptTemplate.from_template(template)
33
34chain = prompt | hf
35
36print(chain.invoke({"question": question})[len(template):].split('<|im_end|>')[0].strip())