Medical fine tuned version of LLAMA-3-8B quantized in 4 bits using common open source datasets and showing improvements over multilingual tasks. It has been used the standard bitquantized technique for post-fine-tuning quantization reducing the computational time complexity and space complexity required to run the model. The overall architecture it's all LLAMA-3 based.
This repository provides a fine-tuned version of the powerful Llama3 8B model, specifically designed to answer medical questions in an informative way. It leverages the rich knowledge contained in the AI Medical Chatbot dataset (
ruslanmv/ai-medical-chatbot).
This model is accessible through the Hugging Face Transformers library. Install it using pip:
1pip install git+https://github.com/huggingface/accelerate.git
2pip install git+https://github.com/huggingface/transformers.git
3pip install bitsandbytes
4
1
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3import torch
4
5# Load tokenizer and model
6model_id = "ruslanmv/llama3-8B-medical"
7
8quantization_config = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_compute_dtype=torch.bfloat16
11)
12
13tokenizer = AutoTokenizer.from_pretrained(model_id)
14device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
15
16model = AutoModelForCausalLM.from_pretrained(model_id, config=quantization_config)
17
18def create_prompt(user_query):
19 B_INST, E_INST = "<s>[INST]", "[/INST]"
20 B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
21 DEFAULT_SYSTEM_PROMPT = """\
22 You are an AI Medical Chatbot Assistant, provide comprehensive and informative responses to your inquiries.
23 If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
24 SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
25 instruction = f"User asks: {user_query}\n"
26 prompt = B_INST + SYSTEM_PROMPT + instruction + E_INST
27 return prompt.strip()
28
29def generate_text(model, tokenizer, prompt,
30 max_length=200,
31 temperature=0.8,
32 num_return_sequences=1):
33 prompt = create_prompt(user_query)
34 # Tokenize the prompt
35 input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device) # Move input_ids to the same device as the model
36 # Generate text
37 output = model.generate(
38 input_ids=input_ids,
39 max_length=max_length,
40 temperature=temperature,
41 num_return_sequences=num_return_sequences,
42 pad_token_id=tokenizer.eos_token_id, # Set pad token to end of sequence token
43 do_sample=True
44 )
45 # Decode the generated output
46 generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
47
48 # Split the generated text based on the prompt and take the portion after it
49 generated_text = generated_text.split(prompt)[-1].strip()
50
51 return generated_text
52# Example usage
53# - Context: First describe your problem.
54# - Question: Then make the question.
55user_query = "I'm a 35-year-old male experiencing symptoms like fatigue, increased sensitivity to cold, and dry, itchy skin. Could these be indicative of hypothyroidism?"
56generated_text = generate_text(model, tokenizer, user_query)
57print(generated_text)
This model is intended for informational purposes only and should not be used as a substitute for professional medical advice. Always consult with a qualified healthcare provider for any medical concerns.
This model is distributed under the Apache License 2.0 (see LICENSE file for details).
We welcome contributions to this repository! If you have improvements or suggestions, feel free to create a pull request.
While we strive to provide informative responses, the accuracy of the model's outputs cannot be guaranteed. It is crucial to consult a doctor or other healthcare professional for definitive medical advice.