Views
No views yet
1%%capture
2!pip install unsloth
3
4!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir --no-deps git+https://github.com/unslothai/unsloth.git1metallurgy_prompt = """You are a highly knowledgeable assistant specializing in metallurgy, materials science,
2and engineering. Below is a technical instruction.Your task is to provide an accurate, domain-specific response that appropriately addresses the request.
3Ensure Your response is detailed,Provide scientifically rigorous and quantitative responses,Reference fundamental principles and mechanisms,
4Include potential equations, calculations, or microstructural insights where relevant,Support statements with scientific reasoning,
5Discuss potential variations or alternative interpretations
6
7
8### Instruction:
9{}
10
11### Input:
12{}
13
14### Response:
15{}"""
16
17from unsloth import FastLanguageModel
18import torch
19max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
20dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
21load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
22
23if True:
24 from unsloth import FastLanguageModel
25 model, tokenizer = FastLanguageModel.from_pretrained(
26 model_name = "Abdulrhman37/lora_model", # YOUR MODEL YOU USED FOR TRAINING
27 max_seq_length = max_seq_length,
28 dtype = dtype,
29 load_in_4bit = load_in_4bit,
30
31 )
32 FastLanguageModel.for_inference(model) # Enable native 2x faster inference
331# function tp process question
2def answer(q: str):
3 """
4 Generates a detailed response to a metallurgy-related question using a pre-trained language model.
5
6 Args:
7 q (str): The question or instruction to be answered.
8
9 Returns:
10 str: The generated response from the model, specifically the content after "### Response:".
11 """
12
13 # Initialize the language model for fast inference
14 FastLanguageModel.for_inference(model) # Enables 2x faster native inference
15
16 # Format the input question using the metallurgy prompt template
17 inputs = tokenizer(
18 [
19 metallurgy_prompt.format(
20 q, # Instruction: The main question
21 "", # Input: Empty for now as no specific input is provided
22 "" # Output: Placeholder for the generated response
23 )
24 ],
25 return_tensors="pt" # Return input tensors
26 ).to("cuda") # Transfer tensors to GPU for faster computation
27
28 # Generate the model's output based on the formatted input
29 outputs = model.generate(**inputs, use_cache=True) # Use cached values to speed up decoding
30
31 # Decode the model's output into readable text
32 result = tokenizer.batch_decode(outputs)
33
34 # Split the result into sections before and after "### Response:"
35 split_content = result[0].split("### Response:")
36 before_response = split_content[0].strip() # Extract content before "Response"
37 after_response = split_content[1].strip().replace('<|end_of_text|>', '') # Clean up response content
38
39 # Prepare a detailed response dictionary for debugging or additional processing
40 detailed = {
41 'after_response': after_response, # The main content of the generated response
42 'before_response': before_response, # Metadata or introductory content before the response
43 'full_result': result # The full raw output from the model
44 }
45
46 # Return only the generated response content
47 return detailed['after_response']
48
49
50# asking model a technical question
51q="To improve strength, toughness, and shock-resistance in Mg-Al-Mn system cast magnesium alloys (e.g. AM100A),what should I do ?"
52
53from pprint import pprint
54pprint(answer(q))