Update README.md
Browse files
README.md
CHANGED
@@ -84,6 +84,43 @@ generation_output = model.generate(
|
|
84 |
# tokenizer.decode(generation_output.flatten())
|
85 |
```
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
### Acknowledgements and Special Thanks
|
88 |
|
89 |
- Thank you [malteos](https://https://huggingface.co/malteos/) for hermeo, without this it would not be possible! (and all your other contributions)
|
|
|
84 |
# tokenizer.decode(generation_output.flatten())
|
85 |
```
|
86 |
|
87 |
+
### FAQ
|
88 |
+
#### The model continues after the reply with user inputs:
|
89 |
+
To solve this, you need to implement a custom stopping criteria:
|
90 |
+
|
91 |
+
```python
|
92 |
+
from transformers import StoppingCriteria
|
93 |
+
class GermeoStoppingCriteria(StoppingCriteria):
|
94 |
+
def __init__(self, target_sequence, prompt):
|
95 |
+
self.target_sequence = target_sequence
|
96 |
+
self.prompt=prompt
|
97 |
+
|
98 |
+
def __call__(self, input_ids, scores, **kwargs):
|
99 |
+
# Get the generated text as a string
|
100 |
+
generated_text = tokenizer.decode(input_ids[0])
|
101 |
+
generated_text = generated_text.replace(self.prompt,'')
|
102 |
+
# Check if the target sequence appears in the generated text
|
103 |
+
if self.target_sequence in generated_text:
|
104 |
+
return True # Stop generation
|
105 |
+
|
106 |
+
return False # Continue generation
|
107 |
+
|
108 |
+
def __len__(self):
|
109 |
+
return 1
|
110 |
+
|
111 |
+
def __iter__(self):
|
112 |
+
yield self
|
113 |
+
```
|
114 |
+
This then expects your input prompt (formatted as given into the model), and a stopping criteria, in this case the im_end token. Simply add it to the generation:
|
115 |
+
|
116 |
+
```python
|
117 |
+
generation_output = model.generate(
|
118 |
+
tokens,
|
119 |
+
streamer=streamer,
|
120 |
+
max_new_tokens=1012,
|
121 |
+
stopping_criteria=GermeoStoppingCriteria("<|im_end|>", prompt_template.format(prompt=prompt))
|
122 |
+
)
|
123 |
+
```
|
124 |
### Acknowledgements and Special Thanks
|
125 |
|
126 |
- Thank you [malteos](https://https://huggingface.co/malteos/) for hermeo, without this it would not be possible! (and all your other contributions)
|