Views
No views yet
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 in both fill-mask prediction and feature extraction for document embeddings."<|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.711 | 0.675 |
| Court Complaint vs. Credit Agreement | 0.847 | 0.833 |
| Consumer Terms vs. Credit Agreement | 0.828 | 0.709 |
1from transformers import pipeline
2
3# Load the fill-mask pipeline with the model
4fill_mask = pipeline('fill-mask', model="alea-institute/kl3m-doc-pico-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# - APPLICATION (score: 0.0384)
19# - PROCEDURES (score: 0.0164)
20# - HEARING (score: 0.0150)
21# - REGULATIONS (score: 0.0119)
22# - DEFINITIONS (score: 0.0117)1# Example: Defined term
2text2 = "<|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|>"
3results2 = fill_mask(text2)
4
5# Display predictions
6print("Top predictions:")
7for result in results2[:5]:
8 print(f"- {result['token_str']} (score: {result['score']:.4f})")
9
10# Output:
11# Top predictions:
12# - date (score: 0.3148)
13# - Date (score: 0.2616)
14# - Time (score: 0.0220)
15# - Order (score: 0.0213)
16# - Dates (score: 0.0198)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-pico-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.\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|>",
12
13 # Consumer Terms
14 "<|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|>",
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\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|>"
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.711 0.847]
35# [0.711 1. 0.828]
36# [0.847 0.828 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.675 0.833]
53# [0.675 1. 0.709]
54# [0.833 0.709 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.7112
68# - Mean pooling: 0.6749
69# Court Complaint vs. Credit Agreement:
70# - CLS token: 0.8474
71# - Mean pooling: 0.8331
72# Consumer Terms vs. Credit Agreement:
73# - CLS token: 0.8276
74# - Mean pooling: 0.7087<|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-pico-001,
2 author = {ALEA Institute},
3 title = {kl3m-doc-pico-001: A Domain-Specific Language Model for Legal and Financial Text Analysis},
4 year = {2024},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/alea-institute/kl3m-doc-pico-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}