Update README.md
Browse files
README.md
CHANGED
|
@@ -1 +1,115 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ahma-3B-RAG
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
Ahma-3B-RAG is a 3B-parameter language model fine-tuned on **Retrieval-Augmented Generation (RAG) problems** using approximately **20,000 synthetically generated samples**. The synthetic data was created using **Nemotron-70B** and **DeepSeekV3** to improve the model's ability to handle RAG-based tasks effectively.
|
| 5 |
+
|
| 6 |
+
## Model Information
|
| 7 |
+
- **Model Name:** Ahma-3B-RAG
|
| 8 |
+
- **Training Data:** ~20k synthetic RAG samples (Nemotron-70B, DeepSeekV3)
|
| 9 |
+
- **Use Case:** RAG-based response generation
|
| 10 |
+
- **Primary Language:** Finnish
|
| 11 |
+
|
| 12 |
+
## Installation & Dependencies
|
| 13 |
+
Before using the model, make sure you have the necessary dependencies installed:
|
| 14 |
+
|
| 15 |
+
```bash
|
| 16 |
+
pip install torch transformers
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
```python
|
| 20 |
+
# Tests were run with the following package versions
|
| 21 |
+
# You can try with different versions as well but these should at least work
|
| 22 |
+
import transformers
|
| 23 |
+
import flash_attn
|
| 24 |
+
import torch
|
| 25 |
+
|
| 26 |
+
assert transformers.__version__ == 4.48.1
|
| 27 |
+
assert torch.__version__ == 2.1.2+cu121
|
| 28 |
+
assert flash_attn.__version__ == 2.7.3
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## Model Loading
|
| 32 |
+
To load the model efficiently, use the following function:
|
| 33 |
+
|
| 34 |
+
```python
|
| 35 |
+
import torch
|
| 36 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 37 |
+
|
| 38 |
+
def load_llama_model(model_path, max_seq_length=2048, dtype=None):
|
| 39 |
+
"""
|
| 40 |
+
Loads the LLaMA model with the given configuration.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
model_path (str): Path or name of the pre-trained model.
|
| 44 |
+
max_seq_length (int): Maximum sequence length for the model.
|
| 45 |
+
dtype (torch.dtype or None): Data type for the model. Default is auto-detected.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
model, tokenizer, generation_config: Loaded model, tokenizer, and generation config.
|
| 49 |
+
"""
|
| 50 |
+
# Set default dtype based on available hardware
|
| 51 |
+
torch_dtype = torch.bfloat16 if dtype is None else dtype
|
| 52 |
+
|
| 53 |
+
# Load model with appropriate configuration
|
| 54 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 55 |
+
model_path,
|
| 56 |
+
torch_dtype=torch_dtype,
|
| 57 |
+
device_map='auto',
|
| 58 |
+
attn_implementation="flash_attention_2" # If you do not have access to GPU supporting flash_attention_2 you can commit this line
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 62 |
+
|
| 63 |
+
generation_config = GenerationConfig(
|
| 64 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 65 |
+
eos_token_id=tokenizer.convert_tokens_to_ids("</s>")
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
return model, tokenizer, generation_config
|
| 69 |
+
|
| 70 |
+
model_path = "RASMUS/AHMA-3B-RAG"
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
## Generating Prompts for RAG
|
| 74 |
+
To generate prompts that incorporate context for RAG-based queries, use the following function:
|
| 75 |
+
|
| 76 |
+
```python
|
| 77 |
+
def generate_rag_prompt_message(row):
|
| 78 |
+
prompt = f'Olet tekoälyavustaja joka vastaa annetun kontekstin perusteella asiantuntevasti ja ystävällisesti käyttäjän kysymyksiin\n\nKonteksti: {row["text"]}\n\nKysymys: {row["question"]}\n\nVastaa yllä olevaan kysymykseen annetun kontekstin perusteella.'
|
| 79 |
+
row["messages"] = [{'role': 'user', 'content': prompt}]
|
| 80 |
+
return row
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
## Generating Responses
|
| 84 |
+
Ahma-3B-RAG can be used to generate responses using the following inference setup:
|
| 85 |
+
|
| 86 |
+
```python
|
| 87 |
+
model, tokenizer, generation_config = load_llama_model(model_path)
|
| 88 |
+
|
| 89 |
+
row = {"text": "Rasmus Toivanen loi tämän mallin", "question": "Kuka loi tämän mallin?"}
|
| 90 |
+
row = generate_rag_prompt_message(row)
|
| 91 |
+
|
| 92 |
+
inputs = tokenizer(
|
| 93 |
+
[
|
| 94 |
+
tokenizer.apply_chat_template(row["messages"], tokenize=False)
|
| 95 |
+
] * 1, return_tensors="pt"
|
| 96 |
+
).to("cuda")
|
| 97 |
+
|
| 98 |
+
with torch.no_grad():
|
| 99 |
+
generated_ids = model.generate(
|
| 100 |
+
input_ids=inputs["input_ids"],
|
| 101 |
+
attention_mask=inputs["attention_mask"],
|
| 102 |
+
generation_config=generation_config, **{
|
| 103 |
+
"temperature": 0.1,
|
| 104 |
+
"penalty_alpha": 0.6,
|
| 105 |
+
"min_p": 0.3,
|
| 106 |
+
"do_sample": True,
|
| 107 |
+
"max_new_tokens": 300
|
| 108 |
+
}
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=False, clean_up_tokenization_spaces=True)[0]
|
| 112 |
+
generated_text_cleaned = generated_text.split('[/INST]')[1].replace('</s>', '').strip() if '[/INST]' in generated_text else generated_text.strip()
|
| 113 |
+
|
| 114 |
+
print(generated_text_cleaned)
|
| 115 |
+
```
|