Views
No views yet
pip install transformers torch1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4# Load model and tokenizer
5model_name = "thangvip/vietnamese-legal-grpo-4b-phase1"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype=torch.bfloat16,
10 device_map="auto"
11)
12
13# Format your legal question
14system_prompt = """Bạn là một chuyên gia pháp lý. Hãy trả lời câu hỏi bằng cách sử dụng phương pháp lập luận tam đoạn luận (syllogism).
15
16Trước tiên, hãy suy nghĩ về vấn đề trong thẻ <think></think>.
17
18Sau đó, trả lời theo định dạng sau:
19<answer>
20<major_premise>[Quy định pháp luật chung]</major_premise>
21<minor_premise>[Sự kiện cụ thể trong câu hỏi]</minor_premise>
22<conclusion>[Áp dụng quy định vào sự kiện để đưa ra kết luận]</conclusion>
23</answer>
24
25Hãy đảm bảo trích dẫn chính xác các điều luật liên quan."""
26
27question = "Một công ty có nghĩa vụ gì khi sa thải nhân viên do tái cơ cấu?"
28
29# Create conversation
30messages = [
31 {"role": "system", "content": system_prompt},
32 {"role": "user", "content": question}
33]
34
35# Generate response
36input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
37inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
38
39with torch.no_grad():
40 outputs = model.generate(
41 **inputs,
42 max_new_tokens=1024,
43 temperature=0.7,
44 do_sample=True,
45 pad_token_id=tokenizer.eos_token_id
46 )
47
48response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
49print(response)1from transformers import pipeline
2
3# Create text generation pipeline
4generator = pipeline(
5 "text-generation",
6 model="thangvip/vietnamese-legal-grpo-4b-phase1",
7 tokenizer="thangvip/vietnamese-legal-grpo-4b-phase1",
8 torch_dtype=torch.bfloat16,
9 device_map="auto"
10)
11
12# Generate legal reasoning
13prompt = "Câu hỏi: Quyền và nghĩa vụ của người thuê nhà khi hợp đồng thuê hết hạn?"
14result = generator(prompt, max_new_tokens=512, temperature=0.7)
15print(result[0]['generated_text'])1<think>
2[Internal reasoning about the legal question]
3</think>
4
5<answer>
6<major_premise>
7[General legal rule or principle applicable to the situation]
8</major_premise>
9
10<minor_premise>
11[Specific facts from the question that relate to the legal rule]
12</minor_premise>
13
14<conclusion>
15[Legal conclusion that follows logically from applying the rule to the facts]
16</conclusion>
17</answer>1@misc{vietnamese-legal-grpo-2024,
2 title={Vietnamese Legal Reasoning Model with GRPO},
3 author={Your Name},
4 year={2024},
5 publisher={Hugging Face},
6 url={https://huggingface.co/thangvip/vietnamese-legal-grpo-4b-phase1}
7}