File size: 3,837 Bytes
91652a5
e1efbfd
d2eeee4
 
 
 
 
 
 
 
 
 
 
 
2eb3f41
d2eeee4
 
 
bb4446f
 
 
d2eeee4
2feceb7
d2eeee4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5505102
d2eeee4
 
 
 
 
 
 
b13eaa4
d2eeee4
 
 
b13eaa4
d2eeee4
 
 
 
 
 
 
 
 
 
 
 
 
 
b13eaa4
25f45e5
b13eaa4
 
 
 
 
 
 
 
 
 
 
600e946
25f45e5
600e946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25f45e5
 
 
 
 
 
 
 
 
 
b13eaa4
25f45e5
 
 
 
 
 
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
---
license: apache-2.0
datasets:
- mmosiolek/pl_alpaca_data_cleaned
language:
- pl
tags:
- alpaca
- llama
- self-instruct
- casual language model
- llm
- gpt
- chat-gpt
pipeline_tag: question-answering
---
# Polpaca: The Alpaca Speaks Polish

The blogpost: https://medium.com/@mmosiolek/can-alpacas-learn-languages-df48a03b6d8


[LLaMA](https://ai.facebook.com/blog/large-language-model-llama-meta-ai/) is a state-of-the-art, foundational, open-source large language model designed to help engineers and researchers advance their work in NLP. 
For example, Stanford researchers have fine-tuned [LLaMA](https://ai.facebook.com/blog/large-language-model-llama-meta-ai/) to construct an alternative to the famous ChatGPT - a model called [Alpaca](https://crfm.stanford.edu/2023/03/13/alpaca.html).
Unfortunately, [LLaMA](https://ai.facebook.com/blog/large-language-model-llama-meta-ai/) was trained on a dataset consisting mainly of English texts, with only 4.5% of the data relating to other languages.
In addition, the [Alpaca](https://crfm.stanford.edu/2023/03/13/alpaca.html) instruction training dataset consists only of examples of English instructions.
So [Alpaca](https://crfm.stanford.edu/2023/03/13/alpaca.html) simply doesn't work for the other languages.

This repo makes [Alpaca-Lora-7B](https://huggingface.co/tloen/alpaca-lora-7b) speak Polish. 

### Usage

```python
from transformers import LlamaTokenizer, LlamaForCausalLM
from peft import PeftModel
import bitsandbytes as bnb

base = "decapoda-research/llama-7b-hf"
finetuned = "mmosiolek/polpaca-lora-7b"

tokenizer = LlamaTokenizer.from_pretrained(base)
tokenizer.pad_token_id = 0
tokenizer.padding_side = "left"

model = LlamaForCausalLM.from_pretrained(base)
model = PeftModel.from_pretrained(model, finetuned).to("cuda")
```

For output generation use the following code. Feel free to adjust the parameters according to your needs.


```python
from transformers import GenerationConfig

config = GenerationConfig(
  temperature=0.1,
  top_p=0.5,
  top_k=40,
  num_beams=4,
  max_new_tokens=128,
  repetition_penalty=1.2
)

def run(instruction, model, tokenizer):
    encodings = tokenizer(instruction, padding=True, return_tensors="pt").to('cuda')
    generated_ids = model.generate(
        **encodings,
        generation_config=GENERATION_CONFIG,
    )
    decoded = tokenizer.batch_decode(generated_ids)
    del encodings, generated_ids
    torch.cuda.empty_cache()
    return decoded[0].split("\n")[-1]
```

### Example input/output
instruction:
```
Wymyśl kilka zapytań w google na temat kodowania.
```

output
```
Jaki jest najpopularniejszy język programowania?
Jaki jest najbardziej powszechnie używany język programowania?
Jaki jest najnowszy język programowania?
Jaki jest najbardziej efektywny język programowania?
```
---
instruction
```
Zrób listę pożądanych umiejętności inżynierów oprogramowania, którą możesz dodać do LinkedIn.
```
output
```
Pożądane umiejętności dla inżyniera oprogramowania obejmują:
- Programowanie w językach takich jak Java, C++, C#, Python, JavaScript i HTML
- Wnioskowanie logiczne
- Rozwiązywanie problemów
- Inżynieria oprogramowania
- Uczenie maszynowe
- Przetwarzanie języka naturalnego (NLP)
- Systemy zarządzania bazami danych (DBMS)
- Architektura
```
---
instruction
```
W odniesieniu do danego scenariusza pogody podaj kilka wskazówek, jak dostosować do niego plany podróży.
```

input
```
nagłą zmianę temperatury
```

output
```
Kilka wskazówek dotyczących dostosowania planów podróży w przypadku nagłej zmiany temperatury obejmują:
1. Upewnij się, że masz odpowiednie ubrania i akcesoria, takie jak krem przeciwsłoneczny, kapelusze i okulary przeciwsłoneczne.
2. Upewnij się, że masz wystarczającą ilość płynów, takich jak woda i napoje gazowane.
```