Views
No views yet

<s>[SYSTEM_PROMPT]Sei un assistente AI per la lingua italiana di nome ANITA-NEXT (Advanced Natural-based interaction for the ITAlian language Next Generation) creato dal ricercatore Marco Polignano, Università degli Studi di Bari Aldo Moro, Italia. Sei un esperto della lingua, cultura, tradizioni, modo di pensare e storia italiana.
L'utente ti chiederà di risolvere un compito o rispondere ad una domanda. Rispondi e ragiona usando la lingua della domanda, preferendo l'Italiano.
Scrivi il tuo flusso di pensiero (monologo interiore) tra i tag <think></think>. Ragiona in modo disinvolto, scrivendo riflessioni e/o bozze, come se stessi lavorando a un esercizio su un foglio di carta.
Successivamente, scrivi la soluzione in modo chiaro, corretto, semplice ed esaustivo basandoti sul riassunto del tuo flusso di pensiero.
Se necessario, usa la notazione markdown per formattare la risposta.[/SYSTEM_PROMPT][INST]{ USER Prompt }[/INST]<think>{ ASSIST Thinking }</think>{ ASSIST Prompt }</s>transformers, you can easily get started with the following steps.pip.1pip install -U --no-deps bitsandbytes accelerate xformers transformers peft trl cut_cross_entropy unsloth_zoo
2pip install sentencepiece protobuf "datasets>=3.4.1,<4.0.0" "huggingface_hub>=0.34.0" hf_transfer1 from transformers import AutoModelForCausalLM, AutoTokenizer
2 import torch
3 from transformers import BitsAndBytesConfig
4
5
6 nf4_config = BitsAndBytesConfig(
7 load_in_4bit=True,
8 bnb_4bit_quant_type="nf4",
9 bnb_4bit_use_double_quant=True,
10 bnb_4bit_compute_dtype=torch.bfloat16
11 )
12
13 model_dir = "m-polignano/ANITA-NEXT-24B-Magistral-2506-ITA"
14 tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=True)
15 model = AutoModelForCausalLM.from_pretrained(
16 model_dir,
17 quantization_config=nf4_config,
18 device_map="auto",
19 torch_dtype=torch.bfloat16,
20 )
21
22 #Method 1
23 sys = '''Sei un assistente AI per la lingua italiana di nome ANITA-NEXT (Advanced Natural-based interaction for the ITAlian language Next Generation) creato dal ricercatore Marco Polignano, Università degli Studi di Bari Aldo Moro, Italia. Sei un esperto della lingua, cultura, tradizioni, modo di pensare e storia italiana.
24
25 L'utente ti chiederà di risolvere un compito o rispondere ad una domanda. Rispondi e ragiona usando la lingua della domanda, preferendo l'Italiano.
26 Scrivi il tuo flusso di pensiero (monologo interiore) tra i tag <think></think>. Ragiona in modo disinvolto, scrivendo riflessioni e/o bozze, come se stessi lavorando a un esercizio su un foglio di carta.
27 Successivamente, scrivi la soluzione in modo chiaro, corretto, semplice ed esaustivo basandoti sul riassunto del tuo flusso di pensiero.
28 Se necessario, usa la notazione markdown per formattare la risposta.'''
29 messages = [
30 {"role" : "system", "content" : sys},
31 {"role" : "user", "content" : "Chi è Carlo Magno?"}
32 ]
33 prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
34 inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
35 for k,v in inputs.items():
36 inputs[k] = v.cuda()
37 outputs = model.generate(**inputs, max_new_tokens=32786, do_sample=True, top_p=0.9, temperature=0.7)
38 results = tokenizer.batch_decode(outputs)[0]
39 print(results)
40
41 #Method 2
42 from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
43 from threading import Thread
44 import torch # Import torch to use .cuda() if needed
45
46 messages = [
47 {"role" : "user", "content" : "Chi è Marco Polo?"}
48 ]
49 prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
50 inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
51
52 # Move inputs to CUDA if your model is on CUDA
53 for k,v in inputs.items():
54 inputs[k] = v.cuda()
55
56 # --- 4. Create a TextIteratorStreamer ---
57 # skip_prompt=True: This ensures that the streamer only yields the newly generated tokens,
58 # not the initial prompt you fed to the model.
59 # skip_special_tokens=True: This removes special tokens (like <s>, </s>, <pad>) from the output.
60 streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
61
62 # --- 5. Define generation arguments, including the streamer ---
63 generation_kwargs = dict(
64 inputs,
65 streamer=streamer, # This is the key part for streaming!
66 max_new_tokens=32786,
67 do_sample=True,
68 top_p=0.9,
69 temperature=0.7,
70 # Add any other generation arguments you need
71 )
72
73 # --- 6. Run model.generate in a separate thread ---
74 # This is crucial because model.generate is a blocking call.
75 # By running it in a thread, your main script can simultaneously
76 # iterate over the streamer to get tokens as they are generated.
77 thread = Thread(target=model.generate, kwargs=generation_kwargs)
78 thread.start()
79
80 # --- 7. Iterate over the streamer to print tokens as they arrive ---
81 print("Generated text (streaming token by token):")
82 for new_text in streamer:
83 if "\\boxed" in new_text:
84 break
85 print(new_text, end="") # `end=""` prevents newlines between tokens
86 # You can also send 'new_text' to a web socket, a GUI, or any other output medium
87
88 # Optional: Wait for the thread to complete if you need to do something after generation
89 thread.join()
901@misc{polignano2024advanced,
2 title={Advanced Natural-based interaction for the ITAlian language: LLaMAntino-3-ANITA},
3 author={Marco Polignano and Pierpaolo Basile and Giovanni Semeraro},
4 year={2024},
5 eprint={2405.07101},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL}
8}1@article{rastogi2025magistral,
2 title={Magistral},
3 author={Rastogi, Abhinav and Jiang, Albert Q and Lo, Andy and Berrada, Gabrielle and Lample, Guillaume and Rute, Jason and Barmentlo, Joep and Yadav, Karmesh and Khandelwal, Kartik and Chandu, Khyathi Raghavi and others},
4 journal={arXiv preprint arXiv:2506.10910},
5 year={2025}
6}