File size: 4,295 Bytes
a9649b9
 
 
e4e0301
311dd65
a9649b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e0301
 
 
 
a9649b9
e4e0301
 
a9649b9
e4e0301
 
 
 
 
 
 
a9649b9
 
e4e0301
a9649b9
e4e0301
a9649b9
 
e4e0301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9649b9
 
 
 
e4e0301
 
 
a9649b9
 
e4e0301
 
 
 
 
 
 
 
 
a9649b9
 
e4e0301
 
a9649b9
 
e4e0301
a9649b9
 
 
e4e0301
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# VietCoMath Model Usage

## Overview
This example snipe code for running the VietCoMath-01 small model for mathematical Coding problem-solving and General Multi tasks.
This model is based from LLama 3.0 8B. 

#### Helper Functions
```python
import re

def check_patterns(response):
    """
    Check if the response contains all required XML patterns.
    
    Args:
        response (str): The model's generated response
    
    Returns:
        str: Parsed response or 'Missing' if patterns are incomplete
    """
    patterns = {
        'answer': r'<answer>(.*?)</answer>',
        'reflection': r'<reflection>(.*?)</reflection>',
        'steps': r'<step>(.*?)</step>',
        'count': r'<count>(.*?)</count>'
    }
    
    matches = {
        'answer': re.search(patterns['answer'], response, re.DOTALL),
        'reflection': re.search(patterns['reflection'], response, re.DOTALL),
        'steps': re.findall(patterns['steps'], response, re.DOTALL),
        'count': re.findall(patterns['count'], response, re.DOTALL)
    }
    
    return "Missing" if not all([matches['answer'], matches['reflection'], matches['steps'], matches['count']]) else response

def parse_response(response):
    """
    Parse the model's response and extract key components.
    
    Args:
        response (str): The model's generated response
    
    Returns:
        tuple: Parsed answer, reflection, steps, and clarification
    """
    response_check = check_patterns(response)
    
    if response_check == "Missing":
        clarification_match = re.search(r'<clarification>(.*?)</clarification>', response, re.DOTALL)
        clarification = clarification_match.group(1).strip() if clarification_match else response
        return "", "", [], clarification
    else:
        answer_match = re.search(r'<answer>(.*?)</answer>', response, re.DOTALL)
        reflection_match = re.search(r'<reflection>(.*?)</reflection>', response, re.DOTALL)
        
        answer = answer_match.group(1).strip() if answer_match else ""
        reflection = reflection_match.group(1).strip() if reflection_match else ""
        steps = re.findall(r'<step>(.*?)</step>', response, re.DOTALL)
        
        return answer, reflection, steps, ""
```


## Usage

### Basic Text Generation
```python
import transformers
import torch

# Load the model
model_id = "VietnamAIHub/VietCoMath-o1-8B"
pipeline = transformers.pipeline(
    "text-generation",
    model=model_id,
    model_kwargs={"torch_dtype": torch.bfloat16},
    device_map="auto",
)


# Example mathematical word problem

problem = "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?"

# Prepare messages
messages = [
    {"role": "system", "content": ""},
    {"role": "user", "content": f"{problem}"},
]

# Define terminators
terminators = [
    pipeline.tokenizer.eos_token_id,
    pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
]

# Generate text
outputs = pipeline(
    messages,
    max_new_tokens=256,
    eos_token_id=terminators,
    do_sample=True,
    temperature=0.6,
    top_p=0.9,
)

# Print generated text
generated_text=outputs[0]["generated_text"][-1]

answer, reflection, steps, clarification = parse_response(generated_text)

print(clarification)
print("------------Internal Thinking-------------")
print(steps)
print(reflection)
print("------------End of Internal Thinking-------------\n")

print("------------Final Answer-------------")
print(answer)
print("------------End of Answer-------------")

## Limitations
- The model is Small scale May Failed in Very difficult problems, Please check the result


## License
[Model is based LLama 3B]

## Citation

@misc {VietnamAIHub,
	author       = { {VietnamAIHub} },
	title        = { VietCoMath-o1-8B},
	year         = 2024,
	url          = { https://huggingface.co/VietnamAIHub/VietCoMath-o1-8B },
	doi          = { 10.57967/hf/3743 },
	publisher    = { Hugging Face }
}

## Collaboration & Contribution
Bạn có thể kết nối trực tiếp với Trần Nhiệm [email protected]
Hoặc có thể chat trực tiếp ở: LinkedIn Facebook. X. Zalo +886 934 311 751