Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,112 @@
|
|
1 |
---
|
2 |
license: apache-2.0
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: apache-2.0
|
3 |
+
tags:
|
4 |
+
- MerlynMind
|
5 |
+
- education
|
6 |
+
inference: false
|
7 |
---
|
8 |
+
|
9 |
+
# Merlyn-Education Corpus QA
|
10 |
+
|
11 |
+
merlyn-education-corpus-qa-v2 is a 13b parameter decoder-style transformer model for the education domain. It is fine-tuned from a [llama2-13b](https://huggingface.co/meta-llama/Llama-2-13b-hf) base-model.
|
12 |
+
|
13 |
+
This model was trained by [Merlyn Mind](https://www.merlyn.org/).
|
14 |
+
|
15 |
+
It is a model that provides an answer to a question based on the given context.
|
16 |
+
|
17 |
+
## Model Date
|
18 |
+
|
19 |
+
August 21, 2023
|
20 |
+
|
21 |
+
## Model License
|
22 |
+
|
23 |
+
Apache-2.0
|
24 |
+
|
25 |
+
|
26 |
+
## Usage
|
27 |
+
|
28 |
+
Loading model and tokenizer:
|
29 |
+
|
30 |
+
```python
|
31 |
+
import torch
|
32 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
33 |
+
|
34 |
+
model_path = "MerlynMind/merlyn-education-corpus-qa-v2"
|
35 |
+
device = torch.device("cuda:0") # change device id as necessary
|
36 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
37 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, fast_tokenizer=True)
|
38 |
+
model.to(device) # move to device
|
39 |
+
|
40 |
+
```
|
41 |
+
|
42 |
+
Prompt example:
|
43 |
+
|
44 |
+
```python
|
45 |
+
info = '''Information:\tThe Solar System is about 4.6 billion years old. The Sun formed by gravity in a large molecular cloud. It is mainly hydrogen, which it converts into helium.
|
46 |
+
Information:\tThe formation and evolution of the Solar System began 4.6 billion years ago with the gravitational collapse of a small part of a giant molecular cloud.
|
47 |
+
Information:\tAstronomers are now more or less certain that the order of the planets was not always as it is today. Knowing what we know today, we can see the Solar System is strange. All other planetary system we are able to study have their largest planet close to their star. Also we have noticed other oddities in the Solar System. Mars is smaller than it ought to be, and the asteroid belt has been disturbed.
|
48 |
+
Information:\tFor thousands of years, people had no need for a name for the "Solar System". They thought the Earth stayed still at the center of everything (geocentrism). The Greek philosopher Aristarchus of Samos suggested that there was a special order in the sky. Nicolaus Copernicus was the first to develop a mathematical system that described what we now call the "Solar System". This was called a "new system of the world". In the 17th century, Galileo Galilei, Johannes Kepler and Isaac Newton began to understand physics more clearly. People began to accept the idea that the Earth is a planet that moves around the Sun, and that the planets are worlds, and that all worlds are governed by the same same physical laws. More recently, telescopes and space probes sometimes let us see details directly. All inner planets have surface features. The gas giants (as the name suggests) have surfaces whose make-up is gradually being discovered.
|
49 |
+
Information:\tThere are eight planets in the Solar System. From closest to farthest from the Sun, they are: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus and Neptune. The first four planets are called terrestrial planets. They are mostly made of rock and metal, and they are mostly solid. The last four planets are called gas giants. This is because they are much larger than other planets and are mostly made of gas.
|
50 |
+
'''
|
51 |
+
qs = "Question:\tHow old is the Solar System?"
|
52 |
+
|
53 |
+
prompt = tokenizer.bos_token
|
54 |
+
prompt += '''Instruction:\tYou are to try to answer the following question using only the pieces of information given.
|
55 |
+
Instruction:\tYour response should be a well formed JSON object with an 'answerable' property followed by an 'answer' property.
|
56 |
+
Instruction:\tIf you cannot answer the question given the information, the value of the 'answerable' should be 'false' and the 'answer' should be an empty string.
|
57 |
+
Instruction:\tIf you can answer the question given the information, the value of the 'answerable' should be 'true' and your answer should be the string value of the 'answer' property.
|
58 |
+
''' + info + qs
|
59 |
+
|
60 |
+
```
|
61 |
+
|
62 |
+
We recommend using newline character for stopping criterion, as follows:
|
63 |
+
|
64 |
+
```python
|
65 |
+
from transformers import StoppingCriteria, StoppingCriteriaList
|
66 |
+
|
67 |
+
eos_tokens = [tokenizer.eos_token,'\n']
|
68 |
+
eos_token_ids = [tokenizer.encode(token)[0] for token in eos_tokens]
|
69 |
+
|
70 |
+
class MultipleEOSTokensStoppingCriteria(StoppingCriteria):
|
71 |
+
def __init__(self, eos_token_ids):
|
72 |
+
self.eos_token_ids = set(eos_token_ids)
|
73 |
+
def __call__(self, input_ids, scores) -> bool:
|
74 |
+
if input_ids.shape[-1] <= 1:
|
75 |
+
return False
|
76 |
+
for eos_token_id in self.eos_token_ids:
|
77 |
+
if eos_token_id == input_ids[0, -1].item():
|
78 |
+
return True
|
79 |
+
return False
|
80 |
+
|
81 |
+
# Define stopping criteria
|
82 |
+
multiple_eos_tokens_processor = MultipleEOSTokensStoppingCriteria(eos_token_ids)
|
83 |
+
stopping_criteria = StoppingCriteriaList([multiple_eos_tokens_processor])
|
84 |
+
```
|
85 |
+
|
86 |
+
Inference:
|
87 |
+
|
88 |
+
```python
|
89 |
+
inputs = tokenizer(prompt, return_tensors="pt", return_token_type_ids=False).to(device)
|
90 |
+
generate_ids = model.generate(
|
91 |
+
**inputs,
|
92 |
+
max_new_tokens=1024,
|
93 |
+
temperature=0.0,
|
94 |
+
num_beams=2,
|
95 |
+
top_p=1,
|
96 |
+
stopping_criteria=stopping_criteria
|
97 |
+
)
|
98 |
+
response = tokenizer.decode(generate_ids[0],
|
99 |
+
skip_special_tokens=True,
|
100 |
+
clean_up_tokenization_spaces=True)
|
101 |
+
```
|
102 |
+
|
103 |
+
Example output (after response processing):
|
104 |
+
|
105 |
+
```json
|
106 |
+
[{"answerable": "true", "answer": "4.6 billion years"}]
|
107 |
+
```
|
108 |
+
|
109 |
+
## Evaluation
|
110 |
+
This model is trained on a larger dataset compared to the [pythia-based v1 model](https://huggingface.co/MerlynMind/merlyn-education-corpus-qa), yielding better correctness and reduced hallucinations on a larger and more diverse benchmarking dataset.
|
111 |
+
|
112 |
+
|