Views
No views yet
kl3m-doc-small-uncased-001 is a domain-specific masked language model (MLM) based on the RoBERTa architecture, specifically designed for legal and financial document analysis. With approximately 236M parameters, it provides a balanced model for specialized NLP tasks in both fill-mask prediction and feature extraction for document embeddings. This uncased variant is particularly useful for case-insensitive applications, maintaining strong performance while disregarding capitalization differences."<|cls|> 8. representations and<|mask|>. each party hereby represents and warrants to the other party as of the date hereof as follows: <|sep|>""<|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|>""<|cls|> all transactions shall comply with the requirements set forth in the truth in<|mask|> act and its implementing regulation z. <|sep|>"| Document Pair | Cosine Similarity (CLS token) | Cosine Similarity (Mean pooling) |
|---|---|---|
| Court Complaint vs. Consumer Terms | 0.429 | 0.440 |
| Court Complaint vs. Credit Agreement | 0.435 | 0.572 |
| Consumer Terms vs. Credit Agreement | 0.562 | 0.498 |
1from transformers import pipeline
2
3# Load the fill-mask pipeline with the model
4fill_mask = pipeline('fill-mask', model="alea-institute/kl3m-doc-small-uncased-001")
5
6# Example: Contract clause heading
7# Note the mask token placement - directly adjacent to "and" without space
8text = "<|cls|> 8. representations and<|mask|>. each party hereby represents and warrants to the other party as of the date hereof as follows: <|sep|>"
9results = fill_mask(text)
10
11# Display predictions
12print("Top predictions:")
13for result in results:
14 print(f"- {result['token_str']} (score: {result['score']:.3f})")
15
16# Output:
17# Top predictions:
18# - warranties (score: 0.940)
19# - warranty (score: 0.016)
20# - warranties (score: 0.015)
21# - covenants (score: 0.005)
22# - warrants (score: 0.004)1from transformers import pipeline
2import numpy as np
3from sklearn.metrics.pairwise import cosine_similarity
4
5# Load the feature-extraction pipeline
6extractor = pipeline('feature-extraction', model="alea-institute/kl3m-doc-small-uncased-001", return_tensors=True)
7
8# Example legal documents (truncated for brevity)
9texts = [
10 # Court Complaint
11 "<|cls|> in the united states district court for the eastern district of pennsylvania\n\njohn doe,\nplaintiff,\n\nvs.\n\nacme corporation,\ndefendant. <|sep|>",
12
13 # Consumer Terms
14 "<|cls|> terms and conditions\n\nlast updated: april 10, 2025\n\nthese terms and conditions govern your access to and use of the service. <|sep|>",
15
16 # Credit Agreement
17 "<|cls|> credit agreement\n\ndated as of april 10, 2025\n\namong\n\nacme borrower inc.,\nas the borrower,\n\nand bank of finance,\nas administrative agent. <|sep|>"
18]
19
20# Generate embeddings for each document
21embeddings = []
22for text in texts:
23 # Get features for the text
24 features = extractor(text)
25
26 # Extract the CLS token embedding (first token)
27 # Convert to numpy if needed
28 features_array = features[0].numpy() if hasattr(features[0], 'numpy') else features[0]
29 cls_embedding = features_array[0] # Shape: [hidden_size]
30
31 embeddings.append(cls_embedding)
32
33# Calculate cosine similarity between documents
34similarity_matrix = cosine_similarity(embeddings)
35print("\nDocument similarity matrix:")
36print(similarity_matrix)1# Mean pooling - taking average of all token embeddings
2def mean_pooling(features):
3 # Convert to numpy if needed
4 features_array = features[0].numpy() if hasattr(features[0], 'numpy') else features[0]
5 return np.mean(features_array, axis=0)
6
7# Generate mean-pooled embeddings
8mean_embeddings = [mean_pooling(extractor(text)) for text in texts]
9
10# Calculate similarity with mean pooling
11mean_similarity = cosine_similarity(mean_embeddings)
12print("\nMean pooling similarity matrix:")
13print(mean_similarity)<|cls|> (ID: 5) - Used for the beginning of input text<|mask|> (ID: 6) - Used to mark tokens for prediction<|sep|> (ID: 4) - Used for the end of input text<|pad|> (ID: 2) - Used for padding sequences to a uniform length<|start|> (ID: 0) - Beginning of sequence<|end|> (ID: 1) - End of sequence<|unk|> (ID: 3) - Unknown token"word<|mask|>" rather than "word <|mask|>".1@misc{kl3m-doc-small-uncased-001,
2 author = {ALEA Institute},
3 title = {kl3m-doc-small-uncased-001: A Domain-Specific Uncased Language Model for Legal and Financial Text Analysis},
4 year = {2025},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/alea-institute/kl3m-doc-small-uncased-001}}
7}
8
9@article{bommarito2025kl3m,
10 title={KL3M Tokenizers: A Family of Domain-Specific and Character-Level Tokenizers for Legal, Financial, and Preprocessing Applications},
11 author={Bommarito, Michael J and Katz, Daniel Martin and Bommarito, Jillian},
12 journal={arXiv preprint arXiv:2503.17247},
13 year={2025}
14}
15
16@misc{bommarito2025kl3mdata,
17 title={The KL3M Data Project: Copyright-Clean Training Resources for Large Language Models},
18 author={Bommarito II, Michael J. and Bommarito, Jillian and Katz, Daniel Martin},
19 year={2025},
20 eprint={2504.07854},
21 archivePrefix={arXiv},
22 primaryClass={cs.CL}
23}