Update README and config files - README.md
Browse files
README.md
CHANGED
@@ -9,7 +9,10 @@ tags:
|
|
9 |
- financial
|
10 |
- mlm
|
11 |
- roberta
|
|
|
12 |
pipeline_tag: fill-mask
|
|
|
|
|
13 |
widget:
|
14 |
- text: "<|cls|> Under the Migratory<|mask|> Treaty Act, the <|sep|>"
|
15 |
- example: "<|cls|> This<|mask|> Credit Agreement is hereby <|sep|>"
|
@@ -20,7 +23,7 @@ date: '2024-11-07T00:00:00.000Z'
|
|
20 |
|
21 |
# kl3m-doc-pico-001
|
22 |
|
23 |
-
`kl3m-doc-pico-001` is a domain-specific masked language model (MLM) based on the RoBERTa architecture, specifically designed for legal and financial document analysis. With approximately 40M parameters, it provides a compact yet effective model for specialized NLP tasks.
|
24 |
|
25 |
## Model Details
|
26 |
|
@@ -28,8 +31,13 @@ date: '2024-11-07T00:00:00.000Z'
|
|
28 |
- **Size**: 40M parameters
|
29 |
- **Hidden Size**: 256
|
30 |
- **Layers**: 8
|
|
|
|
|
|
|
31 |
- **Max Sequence Length**: 509
|
32 |
- **Tokenizer**: [alea-institute/kl3m-004-128k-cased](https://huggingface.co/alea-institute/kl3m-004-128k-cased)
|
|
|
|
|
33 |
|
34 |
## Use Cases
|
35 |
|
@@ -40,107 +48,273 @@ This model is particularly useful for:
|
|
40 |
- Understanding legal citations and references
|
41 |
- Filling in missing terms in legal documents
|
42 |
- Feature extraction for downstream legal analysis tasks
|
|
|
|
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
## Usage
|
47 |
|
48 |
-
|
|
|
|
|
49 |
|
50 |
```python
|
51 |
-
from transformers import
|
52 |
-
|
|
|
|
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
print(f"
|
72 |
|
73 |
# Output:
|
74 |
-
#
|
75 |
-
#
|
76 |
-
#
|
77 |
-
#
|
78 |
-
#
|
79 |
-
#
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
inputs = tokenizer(text, return_tensors="pt")
|
84 |
-
outputs = model(**inputs)
|
85 |
-
|
86 |
-
# Get top predictions for masked token
|
87 |
-
masked_index = torch.where(inputs.input_ids[0] == tokenizer.mask_token_id)[0].item()
|
88 |
-
probs = outputs.logits[0, masked_index].softmax(dim=0)
|
89 |
-
top_5 = torch.topk(probs, 5)
|
90 |
-
|
91 |
-
print("\nExample 2 - Top 5 predictions:")
|
92 |
-
for i, (score, idx) in enumerate(zip(top_5.values, top_5.indices)):
|
93 |
-
token = tokenizer.decode(idx).strip()
|
94 |
-
print(f"{i+1}. {token} ({score.item():.3f})")
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
# Output:
|
97 |
-
#
|
98 |
-
#
|
99 |
-
#
|
100 |
-
#
|
101 |
-
#
|
102 |
-
#
|
|
|
|
|
|
|
|
|
103 |
```
|
104 |
|
105 |
## Training
|
106 |
|
107 |
The model was trained on a diverse corpus of legal and financial documents, ensuring high-quality performance in these domains. It leverages the KL3M tokenizer which provides 9-17% more efficient tokenization for domain-specific content than cl100k_base or the LLaMA/Mistral tokenizers.
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
## Special Tokens
|
110 |
|
111 |
-
This model
|
112 |
|
113 |
-
- CLS token: `<|cls|>` (ID: 5) -
|
114 |
- MASK token: `<|mask|>` (ID: 6) - Used to mark tokens for prediction
|
115 |
-
- SEP token: `<|sep|>` (ID: 4) -
|
116 |
- PAD token: `<|pad|>` (ID: 2) - Used for padding sequences to a uniform length
|
117 |
- BOS token: `<|start|>` (ID: 0) - Beginning of sequence
|
118 |
- EOS token: `<|end|>` (ID: 1) - End of sequence
|
119 |
- UNK token: `<|unk|>` (ID: 3) - Unknown token
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
## Limitations
|
122 |
|
123 |
While compact compared to larger language models, this model has some limitations:
|
124 |
|
125 |
-
- Limited
|
126 |
- Primarily focused on English legal and financial texts
|
127 |
- Best suited for domain-specific rather than general-purpose tasks
|
|
|
|
|
128 |
|
129 |
## References
|
130 |
|
131 |
- [KL3M Tokenizers: A Family of Domain-Specific and Character-Level Tokenizers for Legal, Financial, and Preprocessing Applications](https://arxiv.org/abs/2503.17247)
|
132 |
-
- [The KL3M Data Project: Copyright-Clean Training Resources for Large Language Models]()
|
133 |
|
134 |
## Citation
|
135 |
|
136 |
If you use this model in your research, please cite:
|
137 |
|
138 |
```bibtex
|
139 |
-
@misc{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
title={KL3M Tokenizers: A Family of Domain-Specific and Character-Level Tokenizers for Legal, Financial, and Preprocessing Applications},
|
141 |
-
author={Bommarito
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
year={2025},
|
143 |
-
eprint={
|
144 |
archivePrefix={arXiv},
|
145 |
primaryClass={cs.CL}
|
146 |
}
|
@@ -156,5 +330,6 @@ The KL3M model family is maintained by the [ALEA Institute](https://aleainstitut
|
|
156 |
|
157 |
- Email: [email protected]
|
158 |
- Website: https://aleainstitute.ai
|
|
|
159 |
|
160 |
-

|
39 |
+
- **Vector Dimension**: 256 (hidden_size)
|
40 |
+
- **Pooling Strategy**: CLS token or mean pooling
|
41 |
|
42 |
## Use Cases
|
43 |
|
|
|
48 |
- Understanding legal citations and references
|
49 |
- Filling in missing terms in legal documents
|
50 |
- Feature extraction for downstream legal analysis tasks
|
51 |
+
- Document similarity and retrieval tasks
|
52 |
+
- Semantic search across legal and financial corpora
|
53 |
|
54 |
+
## Performance
|
55 |
+
|
56 |
+
The model demonstrates strong performance on domain-specific tasks compared to general-purpose models of similar size. It shows particular strength in masked token prediction for legal and regulatory terminology.
|
57 |
+
|
58 |
+
### Examples of Fill-Mask Performance
|
59 |
+
|
60 |
+
1. Environmental Law:
|
61 |
+
- "Under the Migratory Bird Treaty Act, the..." → 93.4% confidence
|
62 |
+
|
63 |
+
2. Financial Contracts:
|
64 |
+
- "This Farm Credit Agreement is hereby..." → 30.0% confidence
|
65 |
+
|
66 |
+
## Standard Test Examples
|
67 |
+
|
68 |
+
Using our standardized test examples for comparing embedding models:
|
69 |
+
|
70 |
+
### Fill-Mask Results
|
71 |
+
|
72 |
+
1. **Contract Clause Heading**:
|
73 |
+
`"<|cls|> 8. REPRESENTATIONS AND<|mask|>. Each party hereby represents and warrants to the other party as of the date hereof as follows: <|sep|>"`
|
74 |
+
|
75 |
+
Top 5 predictions:
|
76 |
+
1. APPLICATION (0.0384)
|
77 |
+
2. PROCEDURES (0.0164)
|
78 |
+
3. HEARING (0.0150)
|
79 |
+
4. REGULATIONS (0.0119)
|
80 |
+
5. DEFINITIONS (0.0117)
|
81 |
+
|
82 |
+
Note: The contract-specialized models show stronger performance on this task, with kl3m-doc-pico-contracts-001 predicting "WARRANTIES" with higher confidence and kl3m-doc-nano-001 with 0.927 confidence.
|
83 |
+
|
84 |
+
2. **Defined Term Example**:
|
85 |
+
`"<|cls|> \"Effective<|mask|>\" means the date on which all conditions precedent set forth in Article V are satisfied or waived by the Administrative Agent. <|sep|>"`
|
86 |
+
|
87 |
+
Top 5 predictions:
|
88 |
+
1. date (0.3148)
|
89 |
+
2. Date (0.2616)
|
90 |
+
3. Time (0.0220)
|
91 |
+
4. Order (0.0213)
|
92 |
+
5. Dates (0.0198)
|
93 |
+
|
94 |
+
3. **Regulation Example**:
|
95 |
+
`"<|cls|> All transactions shall comply with the requirements set forth in the Truth in<|mask|> Act and its implementing Regulation Z. <|sep|>"`
|
96 |
+
|
97 |
+
Top 5 predictions:
|
98 |
+
1. Lending (0.5241)
|
99 |
+
2. the (0.1538)
|
100 |
+
3. Disabilities (0.0708)
|
101 |
+
4. America (0.0228)
|
102 |
+
5. Truth (0.0147)
|
103 |
+
|
104 |
+
### Document Similarity Results
|
105 |
+
|
106 |
+
Using the standardized document examples for embeddings:
|
107 |
+
|
108 |
+
| Document Pair | Cosine Similarity (CLS token) | Cosine Similarity (Mean pooling) |
|
109 |
+
|---------------|-------------------------------|----------------------------------|
|
110 |
+
| Court Complaint vs. Consumer Terms | 0.711 | 0.675 |
|
111 |
+
| Court Complaint vs. Credit Agreement | 0.847 | 0.833 |
|
112 |
+
| Consumer Terms vs. Credit Agreement | 0.828 | 0.709 |
|
113 |
+
|
114 |
+
Note how different pooling strategies affect similarity measurements. Mean pooling tends to capture more comprehensive document-level similarity, particularly between legal documents like complaints and agreements (0.833), while CLS token embeddings in this model show generally higher similarity values, particularly between Court Complaint and Credit Agreement (0.847).
|
115 |
|
116 |
## Usage
|
117 |
|
118 |
+
### Masked Language Modeling
|
119 |
+
|
120 |
+
You can use this model for masked language modeling with the simple pipeline approach:
|
121 |
|
122 |
```python
|
123 |
+
from transformers import pipeline
|
124 |
+
|
125 |
+
# Load the fill-mask pipeline with the model
|
126 |
+
fill_mask = pipeline('fill-mask', model="alea-institute/kl3m-doc-pico-001")
|
127 |
|
128 |
+
# Example: Contract clause heading
|
129 |
+
# Note the mask token placement - directly adjacent to "AND" without space
|
130 |
+
text = "<|cls|> 8. REPRESENTATIONS AND<|mask|>. Each party hereby represents and warrants to the other party as of the date hereof as follows: <|sep|>"
|
131 |
+
results = fill_mask(text)
|
132 |
|
133 |
+
# Display predictions
|
134 |
+
print("Top predictions:")
|
135 |
+
for result in results:
|
136 |
+
print(f"- {result['token_str']} (score: {result['score']:.4f})")
|
137 |
|
138 |
+
# Output:
|
139 |
+
# Top predictions:
|
140 |
+
# - APPLICATION (score: 0.0384)
|
141 |
+
# - PROCEDURES (score: 0.0164)
|
142 |
+
# - HEARING (score: 0.0150)
|
143 |
+
# - REGULATIONS (score: 0.0119)
|
144 |
+
# - DEFINITIONS (score: 0.0117)
|
145 |
+
```
|
146 |
+
|
147 |
+
You can also try other examples:
|
148 |
+
|
149 |
+
```python
|
150 |
+
# Example: Defined term
|
151 |
+
text2 = "<|cls|> \"Effective<|mask|>\" means the date on which all conditions precedent set forth in Article V are satisfied or waived by the Administrative Agent. <|sep|>"
|
152 |
+
results2 = fill_mask(text2)
|
153 |
|
154 |
+
# Display predictions
|
155 |
+
print("Top predictions:")
|
156 |
+
for result in results2[:5]:
|
157 |
+
print(f"- {result['token_str']} (score: {result['score']:.4f})")
|
158 |
|
159 |
# Output:
|
160 |
+
# Top predictions:
|
161 |
+
# - date (score: 0.3148)
|
162 |
+
# - Date (score: 0.2616)
|
163 |
+
# - Time (score: 0.0220)
|
164 |
+
# - Order (score: 0.0213)
|
165 |
+
# - Dates (score: 0.0198)
|
166 |
+
```
|
167 |
+
|
168 |
+
### Feature Extraction for Embeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
+
```python
|
171 |
+
from transformers import pipeline
|
172 |
+
import numpy as np
|
173 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
174 |
+
|
175 |
+
# Load the feature-extraction pipeline
|
176 |
+
extractor = pipeline('feature-extraction', model="alea-institute/kl3m-doc-pico-001", return_tensors=True)
|
177 |
+
|
178 |
+
# Example legal documents
|
179 |
+
texts = [
|
180 |
+
# Court Complaint
|
181 |
+
"<|cls|> IN THE UNITED STATES DISTRICT COURT FOR THE EASTERN DISTRICT OF PENNSYLVANIA\n\nJOHN DOE,\nPlaintiff,\n\nvs.\n\nACME CORPORATION,\nDefendant.\n\nCIVIL ACTION NO. 21-12345\n\nCOMPLAINT\n\nPlaintiff John Doe, by and through his undersigned counsel, hereby files this Complaint against Defendant Acme Corporation, and in support thereof, alleges as follows: <|sep|>",
|
182 |
+
|
183 |
+
# Consumer Terms
|
184 |
+
"<|cls|> TERMS AND CONDITIONS\n\nLast Updated: April 10, 2025\n\nThese Terms and Conditions (\"Terms\") govern your access to and use of the Service. By accessing or using the Service, you agree to be bound by these Terms. If you do not agree to these Terms, you may not access or use the Service. These Terms constitute a legally binding agreement between you and the Company. <|sep|>",
|
185 |
+
|
186 |
+
# Credit Agreement
|
187 |
+
"<|cls|> CREDIT AGREEMENT\n\nDated as of April 10, 2025\n\nAmong\n\nACME BORROWER INC.,\nas the Borrower,\n\nBANK OF FINANCE,\nas Administrative Agent,\n\nand\n\nTHE LENDERS PARTY HERETO\n\nThis CREDIT AGREEMENT (\"Agreement\") is entered into as of April 10, 2025, among ACME BORROWER INC., a Delaware corporation (the \"Borrower\"), each lender from time to time party hereto (collectively, the \"Lenders\"), and BANK OF FINANCE, as Administrative Agent. <|sep|>"
|
188 |
+
]
|
189 |
+
|
190 |
+
# Strategy 1: CLS token embeddings
|
191 |
+
cls_embeddings = []
|
192 |
+
for text in texts:
|
193 |
+
features = extractor(text)
|
194 |
+
# Get the CLS token (first token) embedding
|
195 |
+
features_array = features[0].numpy() if hasattr(features[0], 'numpy') else features[0]
|
196 |
+
cls_embedding = features_array[0]
|
197 |
+
cls_embeddings.append(cls_embedding)
|
198 |
+
|
199 |
+
# Calculate similarity between documents using CLS tokens
|
200 |
+
cls_similarity = cosine_similarity(np.vstack(cls_embeddings))
|
201 |
+
print("\nDocument similarity (CLS token):")
|
202 |
+
print(np.round(cls_similarity, 3))
|
203 |
+
# Output:
|
204 |
+
# [[1. 0.711 0.847]
|
205 |
+
# [0.711 1. 0.828]
|
206 |
+
# [0.847 0.828 1. ]]
|
207 |
+
|
208 |
+
# Strategy 2: Mean pooling
|
209 |
+
mean_embeddings = []
|
210 |
+
for text in texts:
|
211 |
+
features = extractor(text)
|
212 |
+
# Average over all tokens
|
213 |
+
features_array = features[0].numpy() if hasattr(features[0], 'numpy') else features[0]
|
214 |
+
mean_embedding = np.mean(features_array, axis=0)
|
215 |
+
mean_embeddings.append(mean_embedding)
|
216 |
+
|
217 |
+
# Calculate similarity using mean pooling
|
218 |
+
mean_similarity = cosine_similarity(np.vstack(mean_embeddings))
|
219 |
+
print("\nDocument similarity (Mean pooling):")
|
220 |
+
print(np.round(mean_similarity, 3))
|
221 |
+
# Output:
|
222 |
+
# [[1. 0.675 0.833]
|
223 |
+
# [0.675 1. 0.709]
|
224 |
+
# [0.833 0.709 1. ]]
|
225 |
+
|
226 |
+
# Print pairwise similarities
|
227 |
+
doc_names = ["Court Complaint", "Consumer Terms", "Credit Agreement"]
|
228 |
+
print("\nPairwise similarities:")
|
229 |
+
for i in range(len(doc_names)):
|
230 |
+
for j in range(i+1, len(doc_names)):
|
231 |
+
print(f"{doc_names[i]} vs. {doc_names[j]}:")
|
232 |
+
print(f" - CLS token: {cls_similarity[i, j]:.4f}")
|
233 |
+
print(f" - Mean pooling: {mean_similarity[i, j]:.4f}")
|
234 |
# Output:
|
235 |
+
# Pairwise similarities:
|
236 |
+
# Court Complaint vs. Consumer Terms:
|
237 |
+
# - CLS token: 0.7112
|
238 |
+
# - Mean pooling: 0.6749
|
239 |
+
# Court Complaint vs. Credit Agreement:
|
240 |
+
# - CLS token: 0.8474
|
241 |
+
# - Mean pooling: 0.8331
|
242 |
+
# Consumer Terms vs. Credit Agreement:
|
243 |
+
# - CLS token: 0.8276
|
244 |
+
# - Mean pooling: 0.7087
|
245 |
```
|
246 |
|
247 |
## Training
|
248 |
|
249 |
The model was trained on a diverse corpus of legal and financial documents, ensuring high-quality performance in these domains. It leverages the KL3M tokenizer which provides 9-17% more efficient tokenization for domain-specific content than cl100k_base or the LLaMA/Mistral tokenizers.
|
250 |
|
251 |
+
Training included both masked language modeling (MLM) objectives and attention to dense document representation for retrieval and classification tasks.
|
252 |
+
|
253 |
+
## Intended Usage
|
254 |
+
|
255 |
+
This model is intended for both:
|
256 |
+
|
257 |
+
1. **Masked Language Modeling**: Filling in missing words/terms in legal and financial documents
|
258 |
+
2. **Document Embedding**: Generating fixed-length vector representations for document similarity and classification
|
259 |
+
|
260 |
## Special Tokens
|
261 |
|
262 |
+
This model includes the following special tokens:
|
263 |
|
264 |
+
- CLS token: `<|cls|>` (ID: 5) - Used for the beginning of input text
|
265 |
- MASK token: `<|mask|>` (ID: 6) - Used to mark tokens for prediction
|
266 |
+
- SEP token: `<|sep|>` (ID: 4) - Used for the end of input text
|
267 |
- PAD token: `<|pad|>` (ID: 2) - Used for padding sequences to a uniform length
|
268 |
- BOS token: `<|start|>` (ID: 0) - Beginning of sequence
|
269 |
- EOS token: `<|end|>` (ID: 1) - End of sequence
|
270 |
- UNK token: `<|unk|>` (ID: 3) - Unknown token
|
271 |
|
272 |
+
Important usage notes:
|
273 |
+
|
274 |
+
When using the MASK token for predictions, be aware that this model uses a **space-prefixed BPE tokenizer**. The <|mask|> token should be placed IMMEDIATELY after the previous token with NO space, because most tokens in this tokenizer have an initial space encoded within them. For example: `"word<|mask|>"` rather than `"word <|mask|>"`.
|
275 |
+
|
276 |
+
This space-aware placement is crucial for getting accurate predictions, as demonstrated in our test examples.
|
277 |
+
|
278 |
## Limitations
|
279 |
|
280 |
While compact compared to larger language models, this model has some limitations:
|
281 |
|
282 |
+
- Limited parameter count (40M) means it captures less nuance than larger language models
|
283 |
- Primarily focused on English legal and financial texts
|
284 |
- Best suited for domain-specific rather than general-purpose tasks
|
285 |
+
- Maximum sequence length of 512 tokens may require chunking for lengthy documents
|
286 |
+
- Requires domain expertise to interpret results effectively
|
287 |
|
288 |
## References
|
289 |
|
290 |
- [KL3M Tokenizers: A Family of Domain-Specific and Character-Level Tokenizers for Legal, Financial, and Preprocessing Applications](https://arxiv.org/abs/2503.17247)
|
291 |
+
- [The KL3M Data Project: Copyright-Clean Training Resources for Large Language Models](https://arxiv.org/abs/2504.07854)
|
292 |
|
293 |
## Citation
|
294 |
|
295 |
If you use this model in your research, please cite:
|
296 |
|
297 |
```bibtex
|
298 |
+
@misc{kl3m-doc-pico-001,
|
299 |
+
author = {ALEA Institute},
|
300 |
+
title = {kl3m-doc-pico-001: A Domain-Specific Language Model for Legal and Financial Text Analysis},
|
301 |
+
year = {2024},
|
302 |
+
publisher = {Hugging Face},
|
303 |
+
howpublished = {\url{https://huggingface.co/alea-institute/kl3m-doc-pico-001}}
|
304 |
+
}
|
305 |
+
|
306 |
+
@article{bommarito2025kl3m,
|
307 |
title={KL3M Tokenizers: A Family of Domain-Specific and Character-Level Tokenizers for Legal, Financial, and Preprocessing Applications},
|
308 |
+
author={Bommarito, Michael J and Katz, Daniel Martin and Bommarito, Jillian},
|
309 |
+
journal={arXiv preprint arXiv:2503.17247},
|
310 |
+
year={2025}
|
311 |
+
}
|
312 |
+
|
313 |
+
@misc{bommarito2025kl3mdata,
|
314 |
+
title={The KL3M Data Project: Copyright-Clean Training Resources for Large Language Models},
|
315 |
+
author={Bommarito II, Michael J. and Bommarito, Jillian and Katz, Daniel Martin},
|
316 |
year={2025},
|
317 |
+
eprint={2504.07854},
|
318 |
archivePrefix={arXiv},
|
319 |
primaryClass={cs.CL}
|
320 |
}
|
|
|
330 |
|
331 |
- Email: [email protected]
|
332 |
- Website: https://aleainstitute.ai
|
333 |
+
- GitHub: https://github.com/alea-institute/kl3m-model-research
|
334 |
|
335 |
+

|