Views
No views yet
kl3m-doc-micro-uncased-001 is a domain-specific masked language model (MLM) based on the DeBERTa-v2 architecture, specifically designed for legal and financial document analysis. With approximately 118M parameters, it provides a specialized model for 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.607 | 0.723 |
| Court Complaint vs. Credit Agreement | 0.631 | 0.837 |
| Consumer Terms vs. Credit Agreement | 0.744 | 0.762 |
1from transformers import pipeline
2
3# Load the fill-mask pipeline with the model
4fill_mask = pipeline('fill-mask', model="alea-institute/kl3m-doc-micro-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']:.4f})")
15
16# Output:
17# Top predictions:
18# - warranties (score: 0.8665)
19# - warrants (score: 0.0857)
20# - warrant (score: 0.0108)
21# - covenants (score: 0.0079)
22# - agreements (score: 0.0057)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-micro-uncased-001", return_tensors=True)
7
8# Example legal documents
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# Strategy 1: CLS token embeddings
21cls_embeddings = []
22for text in texts:
23 features = extractor(text)
24 # Get the CLS token (first token) embedding
25 features_array = features[0].numpy() if hasattr(features[0], 'numpy') else features[0]
26 cls_embedding = features_array[0]
27 cls_embeddings.append(cls_embedding)
28
29# Calculate similarity between documents using CLS tokens
30cls_similarity = cosine_similarity(np.vstack(cls_embeddings))
31print("\nDocument similarity (CLS token):")
32print(np.round(cls_similarity, 3))
33# Output:
34# [[1. 0.607 0.631]
35# [0.607 1. 0.744]
36# [0.631 0.744 1. ]]
37
38# Strategy 2: Mean pooling
39mean_embeddings = []
40for text in texts:
41 features = extractor(text)
42 # Average over all tokens
43 features_array = features[0].numpy() if hasattr(features[0], 'numpy') else features[0]
44 mean_embedding = np.mean(features_array, axis=0)
45 mean_embeddings.append(mean_embedding)
46
47# Calculate similarity using mean pooling
48mean_similarity = cosine_similarity(np.vstack(mean_embeddings))
49print("\nDocument similarity (Mean pooling):")
50print(np.round(mean_similarity, 3))
51# Output:
52# [[1. 0.723 0.837]
53# [0.723 1. 0.762]
54# [0.837 0.762 1. ]]
55
56# Print pairwise similarities
57doc_names = ["Court Complaint", "Consumer Terms", "Credit Agreement"]
58print("\nPairwise similarities:")
59for i in range(len(doc_names)):
60 for j in range(i+1, len(doc_names)):
61 print(f"{doc_names[i]} vs. {doc_names[j]}:")
62 print(f" - CLS token: {cls_similarity[i, j]:.4f}")
63 print(f" - Mean pooling: {mean_similarity[i, j]:.4f}")
64# Output:
65# Pairwise similarities:
66# Court Complaint vs. Consumer Terms:
67# - CLS token: 0.6073
68# - Mean pooling: 0.7233
69# Court Complaint vs. Credit Agreement:
70# - CLS token: 0.6314
71# - Mean pooling: 0.8370
72# Consumer Terms vs. Credit Agreement:
73# - CLS token: 0.7435
74# - Mean pooling: 0.7620<|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-micro-uncased-001,
2 author = {ALEA Institute},
3 title = {kl3m-doc-micro-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-micro-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}