Views
No views yet
unsloth/llama-3.2-1b-instruct-bnb-4bit, trained to classify whether an answer is derived from a given context based on a legal question.1{
2 "question": "What is the legal definition of contract?",
3 "answer": "A contract is a legally binding agreement between two parties.",
4 "context": "Contract law defines a contract as an agreement between two or more parties that creates legally enforceable obligations.",
5 "label": 1
6}unsloth/llama-3.2-1b-instruct-bnb-4bit["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]1<|begin_of_text|><|start_header_id|>system<|end_header_id|>
2You are a context relevance classifier. Given a question, answer, and context, determine if the answer was generated from the given context. Respond with either "YES" if the answer is derived from the context, or "NO" if it is not.
3
4<|eot_id|><|start_header_id|>user<|end_header_id|>
5Question: {question}
6
7Answer: {answer}
8
9Context: {context}
10
11Was this answer generated from the given context? Respond with YES or NO only.
12<|eot_id|><|start_header_id|>assistant<|end_header_id|>
13{YES/NO}1pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
2pip install --no-deps "xformers<0.0.27" "trl<0.9.0" peft accelerate bitsandbytes1from unsloth import FastLanguageModel
2import torch
3
4# Load the fine-tuned model
5model, tokenizer = FastLanguageModel.from_pretrained(
6 model_name="axondendriteplus/llama-3.2-1b-context-relevance-classifier",
7 max_seq_length=2048,
8 dtype=None,
9 load_in_4bit=True,
10)
11
12# Enable inference mode
13FastLanguageModel.for_inference(model)
14
15def classify_answer(question, answer, context):
16 """Classify if answer is generated from context"""
17 prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
18You are a context relevance classifier. Given a question, answer, and context, determine if the answer was generated from the given context. Respond with either "YES" if the answer is derived from the context, or "NO" if it is not.
19
20<|eot_id|><|start_header_id|>user<|end_header_id|>
21Question: {question}
22
23Answer: {answer}
24
25Context: {context}
26
27Was this answer generated from the given context? Respond with YES or NO only.
28<|eot_id|><|start_header_id|>assistant<|end_header_id|>
29"""
30
31 inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
32
33 with torch.no_grad():
34 outputs = model.generate(
35 **inputs,
36 max_new_tokens=5,
37 use_cache=True,
38 do_sample=False,
39 repetition_penalty=1.1,
40 eos_token_id=tokenizer.eos_token_id,
41 )
42
43 response = tokenizer.decode(outputs[0], skip_special_tokens=True)
44 prediction = response.split("assistant")[-1].strip()
45
46 return "YES" in prediction.upper()
47
48# Example usage
49question = "What is the legal definition of contract?"
50answer = "A contract is a legally binding agreement between two parties."
51context = "Contract law defines a contract as an agreement between two or more parties that creates legally enforceable obligations."
52
53result = classify_answer(question, answer, context)
54print(f"Classification result: {'Relevant' if result else 'Not Relevant'}")1question = "What are the elements of a valid contract?"
2answer = "A valid contract requires offer, acceptance, and consideration."
3context = "For a contract to be legally binding, it must contain three essential elements: an offer, acceptance of that offer, and consideration."
4# Expected output: YES (Relevant)1question = "What is negligence in tort law?"
2answer = "A contract is a legally binding agreement."
3context = "Negligence is the failure to exercise reasonable care that results in harm to another person."
4# Expected output: NO (Not Relevant)1question = "What is the statute of limitations?"
2answer = "It's a time limit for filing lawsuits, typically 2-3 years."
3context = "The statute of limitations is a law that sets the maximum time after an event within which legal proceedings may be initiated."
4# Expected output: YES (Relevant)