Views
No views yet
1import re
2
3def check_patterns(response):
4 """
5 Check if the response contains all required XML patterns.
6
7 Args:
8 response (str): The model's generated response
9
10 Returns:
11 str: Parsed response or 'Missing' if patterns are incomplete
12 """
13 patterns = {
14 'answer': r'<answer>(.*?)</answer>',
15 'reflection': r'<reflection>(.*?)</reflection>',
16 'steps': r'<step>(.*?)</step>',
17 'count': r'<count>(.*?)</count>'
18 }
19
20 matches = {
21 'answer': re.search(patterns['answer'], response, re.DOTALL),
22 'reflection': re.search(patterns['reflection'], response, re.DOTALL),
23 'steps': re.findall(patterns['steps'], response, re.DOTALL),
24 'count': re.findall(patterns['count'], response, re.DOTALL)
25 }
26
27 return "Missing" if not all([matches['answer'], matches['reflection'], matches['steps'], matches['count']]) else response
28
29def parse_response(response):
30 """
31 Parse the model's response and extract key components.
32
33 Args:
34 response (str): The model's generated response
35
36 Returns:
37 tuple: Parsed answer, reflection, steps, and clarification
38 """
39 response_check = check_patterns(response)
40
41 if response_check == "Missing":
42 clarification_match = re.search(r'<clarification>(.*?)</clarification>', response, re.DOTALL)
43 clarification = clarification_match.group(1).strip() if clarification_match else response
44 return "", "", [], clarification
45 else:
46 answer_match = re.search(r'<answer>(.*?)</answer>', response, re.DOTALL)
47 reflection_match = re.search(r'<reflection>(.*?)</reflection>', response, re.DOTALL)
48
49 answer = answer_match.group(1).strip() if answer_match else ""
50 reflection = reflection_match.group(1).strip() if reflection_match else ""
51 steps = re.findall(r'<step>(.*?)</step>', response, re.DOTALL)
52
53 return answer, reflection, steps, ""1import transformers
2import torch
3
4# Load the model
5model_id = "VietnamAIHub/VietCoMath-o1-14B"
6pipeline = transformers.pipeline(
7 "text-generation",
8 model=model_id,
9 model_kwargs={"torch_dtype": torch.bfloat16},
10 device_map="auto",
11)
12
13
14# Example mathematical word problem
15
16problem = "Có 100 sinh viên đỗ đại học. Trong số đó, có 55 sinh viên chọn âm nhạc, 44 sinh viên chọn thể thao, và 20 sinh viên chọn cả 2. Hỏi có bao nhiêu sinh viên không chọn âm nhạc, cũng không chọn thể thao?"
17
18# Prepare messages
19messages = [
20 {"role": "system", "content": ""},
21 {"role": "user", "content": f"{problem}"},
22]
23
24# Define terminators
25terminators = [
26 pipeline.tokenizer.eos_token_id,
27 pipeline.tokenizer.convert_tokens_to_ids("<|im_end|>")
28]
29
30# Generate text
31outputs = pipeline(
32 messages,
33 max_new_tokens=256,
34 eos_token_id=terminators,
35 do_sample=True,
36 temperature=0.6,
37 top_p=0.9,
38)
39
40# Print generated text
41generated_text=outputs[0]["generated_text"][-1]
42
43answer, reflection, steps, clarification = parse_response(generated_text)
44
45print(clarification)
46print("------------Internal Thinking-------------")
47print(steps)
48print(reflection)
49print("------------End of Internal Thinking-------------\n")
50
51print("------------Final Answer-------------")
52print(answer)
53print("------------End of Answer-------------")
54
55## Limitations
56- The model is Small scale May Failed in Very difficult problems, Please check the result
57
58
59## License
60[Model is based LLama 3B]
61
62## Citation
63
64@misc {VietnamAIHub,
65 author = { {VietnamAIHub} },
66 title = { VietCoMath-o1-8B},
67 year = 2024,
68 url = { https://huggingface.co/VietnamAIHub/VietCoMath-o1-8B },
69 doi = { 10.57967/hf/3743 },
70 publisher = { Hugging Face }
71}
72
73## Collaboration & Contribution
74Bạn có thể kết nối trực tiếp với Trần Nhiệm tvnhiemhcmus@gmail.com
75Hoặc có thể chat trực tiếp ở: LinkedIn Facebook. X. Zalo +886 934 311 751