llmflowstack 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- llmflowstack-1.0.0/.github/workflows/python-publish.yml +70 -0
- llmflowstack-1.0.0/.gitignore +4 -0
- llmflowstack-1.0.0/LICENSE +21 -0
- llmflowstack-1.0.0/PKG-INFO +229 -0
- llmflowstack-1.0.0/README.md +193 -0
- llmflowstack-1.0.0/llmflowstack/__init__.py +19 -0
- llmflowstack-1.0.0/llmflowstack/base/__init__.py +0 -0
- llmflowstack-1.0.0/llmflowstack/base/base.py +527 -0
- llmflowstack-1.0.0/llmflowstack/callbacks/__init__.py +0 -0
- llmflowstack-1.0.0/llmflowstack/callbacks/log_collector.py +21 -0
- llmflowstack-1.0.0/llmflowstack/callbacks/stop_on_token.py +16 -0
- llmflowstack-1.0.0/llmflowstack/models/GPT_OSS.py +265 -0
- llmflowstack-1.0.0/llmflowstack/models/Gemma.py +247 -0
- llmflowstack-1.0.0/llmflowstack/models/LLaMA3.py +213 -0
- llmflowstack-1.0.0/llmflowstack/models/__init__.py +9 -0
- llmflowstack-1.0.0/llmflowstack/rag/__iinit__.py +5 -0
- llmflowstack-1.0.0/llmflowstack/rag/pipeline.py +114 -0
- llmflowstack-1.0.0/llmflowstack/schemas/__init__.py +9 -0
- llmflowstack-1.0.0/llmflowstack/schemas/params.py +39 -0
- llmflowstack-1.0.0/llmflowstack/utils/__init__.py +11 -0
- llmflowstack-1.0.0/llmflowstack/utils/evaluation_methods.py +92 -0
- llmflowstack-1.0.0/llmflowstack/utils/exceptions.py +2 -0
- llmflowstack-1.0.0/llmflowstack/utils/generation_utils.py +30 -0
- llmflowstack-1.0.0/pyproject.toml +43 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# This workflow will upload a Python Package to PyPI when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
|
|
3
|
+
|
|
4
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
5
|
+
# They are provided by a third-party and are governed by
|
|
6
|
+
# separate terms of service, privacy policy, and support
|
|
7
|
+
# documentation.
|
|
8
|
+
|
|
9
|
+
name: Upload Python Package
|
|
10
|
+
|
|
11
|
+
on:
|
|
12
|
+
release:
|
|
13
|
+
types: [published]
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
release-build:
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
|
|
25
|
+
- uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: "3.x"
|
|
28
|
+
|
|
29
|
+
- name: Build release distributions
|
|
30
|
+
run: |
|
|
31
|
+
# NOTE: put your own distribution build steps here.
|
|
32
|
+
python -m pip install build
|
|
33
|
+
python -m build
|
|
34
|
+
|
|
35
|
+
- name: Upload distributions
|
|
36
|
+
uses: actions/upload-artifact@v4
|
|
37
|
+
with:
|
|
38
|
+
name: release-dists
|
|
39
|
+
path: dist/
|
|
40
|
+
|
|
41
|
+
pypi-publish:
|
|
42
|
+
runs-on: ubuntu-latest
|
|
43
|
+
needs:
|
|
44
|
+
- release-build
|
|
45
|
+
permissions:
|
|
46
|
+
# IMPORTANT: this permission is mandatory for trusted publishing
|
|
47
|
+
id-token: write
|
|
48
|
+
|
|
49
|
+
# Dedicated environments with protections for publishing are strongly recommended.
|
|
50
|
+
# For more information, see: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#deployment-protection-rules
|
|
51
|
+
environment:
|
|
52
|
+
name: pypi
|
|
53
|
+
# OPTIONAL: uncomment and update to include your PyPI project URL in the deployment status:
|
|
54
|
+
# url: https://pypi.org/p/YOURPROJECT
|
|
55
|
+
#
|
|
56
|
+
# ALTERNATIVE: if your GitHub Release name is the PyPI project version string
|
|
57
|
+
# ALTERNATIVE: exactly, uncomment the following line instead:
|
|
58
|
+
# url: https://pypi.org/project/YOURPROJECT/${{ github.event.release.name }}
|
|
59
|
+
|
|
60
|
+
steps:
|
|
61
|
+
- name: Retrieve release distributions
|
|
62
|
+
uses: actions/download-artifact@v4
|
|
63
|
+
with:
|
|
64
|
+
name: release-dists
|
|
65
|
+
path: dist/
|
|
66
|
+
|
|
67
|
+
- name: Publish release distributions to PyPI
|
|
68
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
69
|
+
with:
|
|
70
|
+
packages-dir: dist/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Gustavo Henrique Ferreira Cruz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llmflowstack
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: LLMFlowStack is a framework for training and using LLMs (LLaMA, GPT-OSS, Gemma). Supports DAPT, fine-tuning, and distributed inference. Public fork without institution-specific components.
|
|
5
|
+
Author-email: Gustavo Henrique Ferreira Cruz <gustavohferreiracruz@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.13
|
|
9
|
+
Requires-Dist: accelerate
|
|
10
|
+
Requires-Dist: bert-score
|
|
11
|
+
Requires-Dist: bitsandbytes
|
|
12
|
+
Requires-Dist: chromadb
|
|
13
|
+
Requires-Dist: colorama
|
|
14
|
+
Requires-Dist: datasets
|
|
15
|
+
Requires-Dist: evaluate
|
|
16
|
+
Requires-Dist: huggingface-hub
|
|
17
|
+
Requires-Dist: langchain-chroma
|
|
18
|
+
Requires-Dist: langchain-community
|
|
19
|
+
Requires-Dist: nltk
|
|
20
|
+
Requires-Dist: numpy
|
|
21
|
+
Requires-Dist: openai-harmony
|
|
22
|
+
Requires-Dist: pandas
|
|
23
|
+
Requires-Dist: peft
|
|
24
|
+
Requires-Dist: rouge-score
|
|
25
|
+
Requires-Dist: safetensors
|
|
26
|
+
Requires-Dist: scikit-learn
|
|
27
|
+
Requires-Dist: scipy
|
|
28
|
+
Requires-Dist: sentence-transformers
|
|
29
|
+
Requires-Dist: torch
|
|
30
|
+
Requires-Dist: torchvision
|
|
31
|
+
Requires-Dist: tqdm
|
|
32
|
+
Requires-Dist: transformers
|
|
33
|
+
Requires-Dist: triton
|
|
34
|
+
Requires-Dist: trl
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# LLMFlow
|
|
38
|
+
|
|
39
|
+
**LLMFlow** is a lightweight framework designed to simplify the use of LLMs (LLaMA, GPT-OSS, and Gemma) for NLP tasks.
|
|
40
|
+
|
|
41
|
+
> **Note:** LLMFlow is intended for high-performance machines with **one or more NVIDIA H100 GPUs**.
|
|
42
|
+
|
|
43
|
+
It provides:
|
|
44
|
+
|
|
45
|
+
- **Training pipelines** with **fine-tuning** or **DAPT** in distributed setups β just provide the data and the process runs automatically;
|
|
46
|
+
- **Distributed inference** made simple;
|
|
47
|
+
- **Evaluation** with standard metrics (BERTScore, ROUGE, Cosine Similarity).
|
|
48
|
+
|
|
49
|
+
The goal is to make experimentation with LLMs more accessible, without the need to build complex infrastructure from scratch.
|
|
50
|
+
|
|
51
|
+
## Supported Models
|
|
52
|
+
|
|
53
|
+
This framework is designed to provide flexibility when working with different open-source and commercial LLMs. Currently, the following models are supported:
|
|
54
|
+
|
|
55
|
+
- **GPT-OSS**
|
|
56
|
+
|
|
57
|
+
- [`GPT-OSS 20B`](https://huggingface.co/openai/gpt-oss-20b)
|
|
58
|
+
- [`GPT-OSS 120B`](https://huggingface.co/openai/gpt-oss-120b)
|
|
59
|
+
|
|
60
|
+
- **LLaMA**
|
|
61
|
+
|
|
62
|
+
- [`LLaMA 3.1 8B - Instruct`](https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct)
|
|
63
|
+
- [`LLaMA 3.1 70B - Instruct`](https://huggingface.co/meta-llama/Llama-3.1-70B-Instruct)
|
|
64
|
+
- [`LLaMA 3.3 70B - Instruct`](https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct)
|
|
65
|
+
- [`LLaMA 3.3 405B - Instruct`](https://huggingface.co/meta-llama/Llama-3.1-405B-Instruct)
|
|
66
|
+
|
|
67
|
+
- **Gemma**
|
|
68
|
+
- [`MedGemma 27B Text - It`](https://huggingface.co/google/medgemma-27b-text-it)
|
|
69
|
+
|
|
70
|
+
> Compatibility includes both inference and training (Domain-Adaptive Pre-Training β DAPT β and Supervised Fine-Tuning)
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Installation
|
|
75
|
+
|
|
76
|
+
You can install the package directly from [PyPI](https://pypi.org/project/llmflow/):
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pip install llmflow
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Usage
|
|
83
|
+
|
|
84
|
+
This section presents a bit of what you can do with the framework.
|
|
85
|
+
|
|
86
|
+
### Loading models
|
|
87
|
+
|
|
88
|
+
You can load as many models as your hardware allows (H100 GPU recommended)...
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from llmflow import GPT_OSS, LLaMA3
|
|
92
|
+
|
|
93
|
+
# Loading a LLaMA model
|
|
94
|
+
first_model = LLaMA3()
|
|
95
|
+
first_model.load_checkpoint(
|
|
96
|
+
checkpoint="/llama-3.1-8b-Instruct",
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
# Loading a quantized LLaMA model
|
|
100
|
+
second_model = LLaMA3(
|
|
101
|
+
checkpoint="/llama-3.3-70b-Instruct",
|
|
102
|
+
quantization="4bit"
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# Loading a GPT-OSS, quantized and with seed
|
|
106
|
+
thrid_model = GPT_OSS(
|
|
107
|
+
checkpoint="/gpt-oss-20b",
|
|
108
|
+
quantization=True,
|
|
109
|
+
seed=1234
|
|
110
|
+
)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Inference Examples
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
> gpt_oss_model = GPT_OSS(checkpoint="/gpt-oss-120b")
|
|
117
|
+
|
|
118
|
+
> gpt_oss_model.generate("Tell me a joke!")
|
|
119
|
+
'Why did the scarecrow become a successful motivational speaker? Because he was outstanding **in** his field! πΎπ'
|
|
120
|
+
|
|
121
|
+
# Exclusive for GPT-OSS
|
|
122
|
+
> gpt_oss_model.set_reasoning_level("High")
|
|
123
|
+
|
|
124
|
+
> custom_input = gpt_oss_model.build_input(
|
|
125
|
+
input_text="Tell me another joke!",
|
|
126
|
+
developer_message="You are a clown and after every joke, you should say 'HONK HONK'"
|
|
127
|
+
)
|
|
128
|
+
> gpt_oss_model.generate(
|
|
129
|
+
input=custom_input,
|
|
130
|
+
params=GenerationParams(
|
|
131
|
+
max_new_tokens=1024,
|
|
132
|
+
sample=GenerationSampleParams(
|
|
133
|
+
temperature=0.3
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
'Why did the scarecrow win an award? Because he was outstanding in his field! \n\nHONK HONK'
|
|
138
|
+
|
|
139
|
+
> llama_model = LLaMA3(checkpoint="/llama-3.3-70B-Instruct", quantization="4bit")
|
|
140
|
+
> llama_model.generate("Why is the sky blue?")
|
|
141
|
+
'The sky appears blue because of a phenomenon called Rayleigh scattering, which is the scattering of light'
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Training Examples (DAPT & Fine-tune)
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from llmflow import LLaMA3
|
|
148
|
+
from llmflow.schemas import TrainParams
|
|
149
|
+
|
|
150
|
+
model = LLaMA3(
|
|
151
|
+
checkpoint="llama-3.1-8b-Instruct"
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
# Creating the dataset
|
|
155
|
+
dataset = []
|
|
156
|
+
dataset.append(model.build_input(
|
|
157
|
+
input_text="Chico is a cat, which color he is?",
|
|
158
|
+
expected_answer="Black!"
|
|
159
|
+
))
|
|
160
|
+
|
|
161
|
+
dataset.append(model.build_input(
|
|
162
|
+
input_text="Fred is a dog, which color he is?",
|
|
163
|
+
expected_answer="White!"
|
|
164
|
+
))
|
|
165
|
+
|
|
166
|
+
# Does the DAPT in the full model
|
|
167
|
+
model.dapt(
|
|
168
|
+
train_dataset=dataset,
|
|
169
|
+
params=TrainParams(
|
|
170
|
+
batch_size=1,
|
|
171
|
+
epochs=3,
|
|
172
|
+
gradient_accumulation=1,
|
|
173
|
+
lr=2e-5
|
|
174
|
+
)
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
# Does the fine-tune this time
|
|
178
|
+
model.fine_tune(
|
|
179
|
+
train_dataset=dataset,
|
|
180
|
+
params=TrainParams(
|
|
181
|
+
batch_size=1,
|
|
182
|
+
gradient_accumulation=1,
|
|
183
|
+
lr=2e-5,
|
|
184
|
+
epochs=50
|
|
185
|
+
),
|
|
186
|
+
save_at_end=True,
|
|
187
|
+
# It will save the model
|
|
188
|
+
save_path="./output"
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
# Saving the final result
|
|
192
|
+
model.save_checkpoint(
|
|
193
|
+
path="./model-output"
|
|
194
|
+
)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### NLP Evaluation
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
> from llmflow import text_evaluation
|
|
201
|
+
> from llmflow.utils import (bert_score_evaluation, cosine_similarity_evaluation, rouge_evaluation)
|
|
202
|
+
|
|
203
|
+
# Predictions from some model
|
|
204
|
+
> predictions = ["Chico is a dog, and he is orange!", "Fred is a cat, and he is white!"]
|
|
205
|
+
# References text (ground truth)
|
|
206
|
+
> references = ["Chico is a cat, and he is black!", "Fred is a dog, and he is white!"]
|
|
207
|
+
|
|
208
|
+
# BERTScore Evaluation
|
|
209
|
+
> bert_score_evaluation(predictions, references)
|
|
210
|
+
{'bertscore_precision': 0.9772549867630005, 'bertscore_recall': 0.9772549867630005, 'bertscore_f1': 0.9772549867630005}
|
|
211
|
+
|
|
212
|
+
# Cosine Similarity Evaluation
|
|
213
|
+
> cosine_similarity_evaluation(predictions, references)
|
|
214
|
+
{'cosine_similarity': 0.7443363666534424}
|
|
215
|
+
|
|
216
|
+
# RougeScore Evaluation
|
|
217
|
+
> rouge_evaluation(predictions, references)
|
|
218
|
+
{'rouge1': 0.8125, 'rouge2': 0.6428571428571428, 'rougeL': 0.8125}
|
|
219
|
+
|
|
220
|
+
# All-in-one function
|
|
221
|
+
> text_evaluation(predictions, references, bert=True, cosine=True, rouge=True)
|
|
222
|
+
{'rouge1': 0.8125, 'rouge2': 0.6428571428571428, 'rougeL': 0.8125, 'bertscore_precision': 0.9772549867630005, 'bertscore_recall': 0.9772549867630005, 'bertscore_f1': 0.9772549867630005, 'cosine_similarity': 0.7443363666534424}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
> **Disclaimer**
|
|
228
|
+
> This is a public fork of a framework originally developed in a research setting.
|
|
229
|
+
> Institution-specific components have been removed for confidentiality reasons.
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# LLMFlow
|
|
2
|
+
|
|
3
|
+
**LLMFlow** is a lightweight framework designed to simplify the use of LLMs (LLaMA, GPT-OSS, and Gemma) for NLP tasks.
|
|
4
|
+
|
|
5
|
+
> **Note:** LLMFlow is intended for high-performance machines with **one or more NVIDIA H100 GPUs**.
|
|
6
|
+
|
|
7
|
+
It provides:
|
|
8
|
+
|
|
9
|
+
- **Training pipelines** with **fine-tuning** or **DAPT** in distributed setups β just provide the data and the process runs automatically;
|
|
10
|
+
- **Distributed inference** made simple;
|
|
11
|
+
- **Evaluation** with standard metrics (BERTScore, ROUGE, Cosine Similarity).
|
|
12
|
+
|
|
13
|
+
The goal is to make experimentation with LLMs more accessible, without the need to build complex infrastructure from scratch.
|
|
14
|
+
|
|
15
|
+
## Supported Models
|
|
16
|
+
|
|
17
|
+
This framework is designed to provide flexibility when working with different open-source and commercial LLMs. Currently, the following models are supported:
|
|
18
|
+
|
|
19
|
+
- **GPT-OSS**
|
|
20
|
+
|
|
21
|
+
- [`GPT-OSS 20B`](https://huggingface.co/openai/gpt-oss-20b)
|
|
22
|
+
- [`GPT-OSS 120B`](https://huggingface.co/openai/gpt-oss-120b)
|
|
23
|
+
|
|
24
|
+
- **LLaMA**
|
|
25
|
+
|
|
26
|
+
- [`LLaMA 3.1 8B - Instruct`](https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct)
|
|
27
|
+
- [`LLaMA 3.1 70B - Instruct`](https://huggingface.co/meta-llama/Llama-3.1-70B-Instruct)
|
|
28
|
+
- [`LLaMA 3.3 70B - Instruct`](https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct)
|
|
29
|
+
- [`LLaMA 3.3 405B - Instruct`](https://huggingface.co/meta-llama/Llama-3.1-405B-Instruct)
|
|
30
|
+
|
|
31
|
+
- **Gemma**
|
|
32
|
+
- [`MedGemma 27B Text - It`](https://huggingface.co/google/medgemma-27b-text-it)
|
|
33
|
+
|
|
34
|
+
> Compatibility includes both inference and training (Domain-Adaptive Pre-Training β DAPT β and Supervised Fine-Tuning)
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
You can install the package directly from [PyPI](https://pypi.org/project/llmflow/):
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install llmflow
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
This section presents a bit of what you can do with the framework.
|
|
49
|
+
|
|
50
|
+
### Loading models
|
|
51
|
+
|
|
52
|
+
You can load as many models as your hardware allows (H100 GPU recommended)...
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from llmflow import GPT_OSS, LLaMA3
|
|
56
|
+
|
|
57
|
+
# Loading a LLaMA model
|
|
58
|
+
first_model = LLaMA3()
|
|
59
|
+
first_model.load_checkpoint(
|
|
60
|
+
checkpoint="/llama-3.1-8b-Instruct",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# Loading a quantized LLaMA model
|
|
64
|
+
second_model = LLaMA3(
|
|
65
|
+
checkpoint="/llama-3.3-70b-Instruct",
|
|
66
|
+
quantization="4bit"
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
# Loading a GPT-OSS, quantized and with seed
|
|
70
|
+
thrid_model = GPT_OSS(
|
|
71
|
+
checkpoint="/gpt-oss-20b",
|
|
72
|
+
quantization=True,
|
|
73
|
+
seed=1234
|
|
74
|
+
)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Inference Examples
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
> gpt_oss_model = GPT_OSS(checkpoint="/gpt-oss-120b")
|
|
81
|
+
|
|
82
|
+
> gpt_oss_model.generate("Tell me a joke!")
|
|
83
|
+
'Why did the scarecrow become a successful motivational speaker? Because he was outstanding **in** his field! πΎπ'
|
|
84
|
+
|
|
85
|
+
# Exclusive for GPT-OSS
|
|
86
|
+
> gpt_oss_model.set_reasoning_level("High")
|
|
87
|
+
|
|
88
|
+
> custom_input = gpt_oss_model.build_input(
|
|
89
|
+
input_text="Tell me another joke!",
|
|
90
|
+
developer_message="You are a clown and after every joke, you should say 'HONK HONK'"
|
|
91
|
+
)
|
|
92
|
+
> gpt_oss_model.generate(
|
|
93
|
+
input=custom_input,
|
|
94
|
+
params=GenerationParams(
|
|
95
|
+
max_new_tokens=1024,
|
|
96
|
+
sample=GenerationSampleParams(
|
|
97
|
+
temperature=0.3
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
'Why did the scarecrow win an award? Because he was outstanding in his field! \n\nHONK HONK'
|
|
102
|
+
|
|
103
|
+
> llama_model = LLaMA3(checkpoint="/llama-3.3-70B-Instruct", quantization="4bit")
|
|
104
|
+
> llama_model.generate("Why is the sky blue?")
|
|
105
|
+
'The sky appears blue because of a phenomenon called Rayleigh scattering, which is the scattering of light'
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Training Examples (DAPT & Fine-tune)
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from llmflow import LLaMA3
|
|
112
|
+
from llmflow.schemas import TrainParams
|
|
113
|
+
|
|
114
|
+
model = LLaMA3(
|
|
115
|
+
checkpoint="llama-3.1-8b-Instruct"
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# Creating the dataset
|
|
119
|
+
dataset = []
|
|
120
|
+
dataset.append(model.build_input(
|
|
121
|
+
input_text="Chico is a cat, which color he is?",
|
|
122
|
+
expected_answer="Black!"
|
|
123
|
+
))
|
|
124
|
+
|
|
125
|
+
dataset.append(model.build_input(
|
|
126
|
+
input_text="Fred is a dog, which color he is?",
|
|
127
|
+
expected_answer="White!"
|
|
128
|
+
))
|
|
129
|
+
|
|
130
|
+
# Does the DAPT in the full model
|
|
131
|
+
model.dapt(
|
|
132
|
+
train_dataset=dataset,
|
|
133
|
+
params=TrainParams(
|
|
134
|
+
batch_size=1,
|
|
135
|
+
epochs=3,
|
|
136
|
+
gradient_accumulation=1,
|
|
137
|
+
lr=2e-5
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# Does the fine-tune this time
|
|
142
|
+
model.fine_tune(
|
|
143
|
+
train_dataset=dataset,
|
|
144
|
+
params=TrainParams(
|
|
145
|
+
batch_size=1,
|
|
146
|
+
gradient_accumulation=1,
|
|
147
|
+
lr=2e-5,
|
|
148
|
+
epochs=50
|
|
149
|
+
),
|
|
150
|
+
save_at_end=True,
|
|
151
|
+
# It will save the model
|
|
152
|
+
save_path="./output"
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# Saving the final result
|
|
156
|
+
model.save_checkpoint(
|
|
157
|
+
path="./model-output"
|
|
158
|
+
)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### NLP Evaluation
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
> from llmflow import text_evaluation
|
|
165
|
+
> from llmflow.utils import (bert_score_evaluation, cosine_similarity_evaluation, rouge_evaluation)
|
|
166
|
+
|
|
167
|
+
# Predictions from some model
|
|
168
|
+
> predictions = ["Chico is a dog, and he is orange!", "Fred is a cat, and he is white!"]
|
|
169
|
+
# References text (ground truth)
|
|
170
|
+
> references = ["Chico is a cat, and he is black!", "Fred is a dog, and he is white!"]
|
|
171
|
+
|
|
172
|
+
# BERTScore Evaluation
|
|
173
|
+
> bert_score_evaluation(predictions, references)
|
|
174
|
+
{'bertscore_precision': 0.9772549867630005, 'bertscore_recall': 0.9772549867630005, 'bertscore_f1': 0.9772549867630005}
|
|
175
|
+
|
|
176
|
+
# Cosine Similarity Evaluation
|
|
177
|
+
> cosine_similarity_evaluation(predictions, references)
|
|
178
|
+
{'cosine_similarity': 0.7443363666534424}
|
|
179
|
+
|
|
180
|
+
# RougeScore Evaluation
|
|
181
|
+
> rouge_evaluation(predictions, references)
|
|
182
|
+
{'rouge1': 0.8125, 'rouge2': 0.6428571428571428, 'rougeL': 0.8125}
|
|
183
|
+
|
|
184
|
+
# All-in-one function
|
|
185
|
+
> text_evaluation(predictions, references, bert=True, cosine=True, rouge=True)
|
|
186
|
+
{'rouge1': 0.8125, 'rouge2': 0.6428571428571428, 'rougeL': 0.8125, 'bertscore_precision': 0.9772549867630005, 'bertscore_recall': 0.9772549867630005, 'bertscore_f1': 0.9772549867630005, 'cosine_similarity': 0.7443363666534424}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
> **Disclaimer**
|
|
192
|
+
> This is a public fork of a framework originally developed in a research setting.
|
|
193
|
+
> Institution-specific components have been removed for confidentiality reasons.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from .models.Gemma import Gemma
|
|
2
|
+
from .models.GPT_OSS import GPT_OSS
|
|
3
|
+
from .models.LLaMA3 import LLaMA3
|
|
4
|
+
from .rag.pipeline import RAGPipeline
|
|
5
|
+
from .schemas.params import (GenerationBeamsParams, GenerationParams,
|
|
6
|
+
GenerationSampleParams, TrainParams)
|
|
7
|
+
from .utils.evaluation_methods import text_evaluation
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Gemma",
|
|
11
|
+
"LLaMA3",
|
|
12
|
+
"GPT_OSS",
|
|
13
|
+
"RAGPipeline",
|
|
14
|
+
"GenerationBeamsParams",
|
|
15
|
+
"GenerationParams",
|
|
16
|
+
"GenerationSampleParams",
|
|
17
|
+
"TrainParams",
|
|
18
|
+
"text_evaluation"
|
|
19
|
+
]
|
|
File without changes
|