Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
language:
|
4 |
+
- pt
|
5 |
+
base_model:
|
6 |
+
- Qwen/Qwen3-0.6B
|
7 |
+
---
|
8 |
+
|
9 |
+
O Tokenizer do Qwen/Qwen3-0.6B com "chat_template" modificado para forçar respostas em português, mesmo que "enable_thinking" seja True ou False.
|
10 |
+
|
11 |
+
```python
|
12 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
13 |
+
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
15 |
+
"Qwen/Qwen3-0.6B", # Modelo original
|
16 |
+
torch_dtype="auto",
|
17 |
+
device_map="auto"
|
18 |
+
)
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
20 |
+
"cnmoro/Qwen3-0.6B-Portuguese-Tokenizer" # Tokenizer custom
|
21 |
+
)
|
22 |
+
|
23 |
+
# Prepara os inputs
|
24 |
+
prompt = "Write a very brief introduction to Large Language Models (LLMs)."
|
25 |
+
messages = [
|
26 |
+
{"role": "user", "content": prompt}
|
27 |
+
]
|
28 |
+
|
29 |
+
# Reasoning >ATIVADO<
|
30 |
+
text = tokenizer.apply_chat_template(
|
31 |
+
messages,
|
32 |
+
tokenize=False,
|
33 |
+
add_generation_prompt=True,
|
34 |
+
enable_thinking=True
|
35 |
+
)
|
36 |
+
|
37 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
38 |
+
generated_ids = model.generate(
|
39 |
+
**model_inputs,
|
40 |
+
max_new_tokens=32768
|
41 |
+
)
|
42 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
43 |
+
# parsing thinking content
|
44 |
+
try:
|
45 |
+
# rindex finding 151668 (</think>)
|
46 |
+
index = len(output_ids) - output_ids[::-1].index(151668)
|
47 |
+
except ValueError:
|
48 |
+
index = 0
|
49 |
+
|
50 |
+
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
|
51 |
+
if thinking_content: thinking_content = "Ok, o usuário " + thinking_content.replace("</think>", "")
|
52 |
+
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
|
53 |
+
|
54 |
+
print("thinking content:", thinking_content)
|
55 |
+
# thinking content: Ok, o usuário quer uma introdução muito breve sobre os modelos de linguagem natural (LLMs).
|
56 |
+
# Preciso ser específico, mas conciso. Devo mencionar o que são, os tipos e as aplicações.
|
57 |
+
# Tenho que ser breve, como um intro para um artigo ou um texto.
|
58 |
+
# O que devo dizer? Devo começar com o que é LLMs, depois os tipos, e finalmente as aplicações.
|
59 |
+
# Devo usar frases simples e diretas, sem complexidade. Preciso garantir que o intro seja breve, como um parágrafo.
|
60 |
+
# Vou testar isso. Vou chamar de "uma tecnologia de linguagem que permite a criação e interação com textos complexos".
|
61 |
+
# Deixar de repetir o que já disse.
|
62 |
+
# O que devo dizer agora? Okay, vou escrever o intro.
|
63 |
+
|
64 |
+
print("content:", content)
|
65 |
+
# content: Large Language Models (LLMs) são modelos de linguagem que permitem a criação e interação
|
66 |
+
# com textos complexos, adaptando-se a diferentes contextos e usos, sendo aplicações em áreas como
|
67 |
+
# inteligência artificial, comunicação com humanos e automação.
|
68 |
+
|
69 |
+
###### ---------------------------------- ######
|
70 |
+
|
71 |
+
# Reasoning >DESATIVADO<
|
72 |
+
text = tokenizer.apply_chat_template(
|
73 |
+
messages,
|
74 |
+
tokenize=False,
|
75 |
+
add_generation_prompt=True,
|
76 |
+
enable_thinking=False
|
77 |
+
)
|
78 |
+
|
79 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
80 |
+
generated_ids = model.generate(
|
81 |
+
**model_inputs,
|
82 |
+
max_new_tokens=32768
|
83 |
+
)
|
84 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
85 |
+
# parsing thinking content
|
86 |
+
try:
|
87 |
+
# rindex finding 151668 (</think>)
|
88 |
+
index = len(output_ids) - output_ids[::-1].index(151668)
|
89 |
+
except ValueError:
|
90 |
+
index = 0
|
91 |
+
|
92 |
+
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
|
93 |
+
if thinking_content: thinking_content = "Ok, o usuário " + thinking_content.replace("</think>", "")
|
94 |
+
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
|
95 |
+
|
96 |
+
print("thinking content:", thinking_content)
|
97 |
+
#
|
98 |
+
|
99 |
+
print("content:", content)
|
100 |
+
# content: "Large Language Models (LLMs) são modelos de linguagem de grande escala que permitem a
|
101 |
+
# geração de textos com base em padrões e conhecimento humano."
|
102 |
+
```
|