Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
semantic-chunking
Advanced tools
Semantically create chunks from large texts. Useful for workflows involving large language models (LLMs).
NPM Package for Semantically creating chunks from large texts. Useful for workflows involving large language models (LLMs).
how it works
npm install semantic-chunking
Basic usage:
import { chunkit } from 'semantic-chunking';
const documents = [
{ document_name: "document1", document_text: "contents of document 1..." },
{ document_name: "document2", document_text: "contents of document 2..." },
...
];
const chunkitOptions = {};
const myChunks = await chunkit(documents, chunkitOptions);
NOTE 🚨 The Embedding model (onnxEmbeddingModel
) will be downloaded to this package's cache directory the first it is run (file size will depend on the specified model; see the model's table ).
chunkit
accepts an array of document objects and an optional configuration object. Here are the details for each parameter:
documents
: array of documents. each document is an object containing document_name
and document_text
.
documents = [
{ document_name: "document1", document_text: "..." },
{ document_name: "document2", document_text: "..." },
...
]
Chunkit Options Object:
logging
: Boolean (optional, default false
) - Enables logging of detailed processing steps.maxTokenSize
: Integer (optional, default 500
) - Maximum token size for each chunk.similarityThreshold
: Float (optional, default 0.5
) - Threshold to determine if sentences are similar enough to be in the same chunk. A higher value demands higher similarity.dynamicThresholdLowerBound
: Float (optional, default 0.45
) - Minimum possible dynamic similarity threshold.dynamicThresholdUpperBound
: Float (optional, default 0.75
) - Maximum possible dynamic similarity threshold.numSimilaritySentencesLookahead
: Integer (optional, default 2
) - Number of sentences to look ahead for calculating similarity.combineChunks
: Boolean (optional, default true
) - Determines whether to reblance and combine chunks into larger ones up to the max token limit.combineChunksSimilarityThreshold
: Float (optional, default 0.5
) - Threshold for combining chunks based on similarity during the rebalance and combining phase.onnxEmbeddingModel
: String (optional, default Xenova/all-MiniLM-L6-v2
) - ONNX model used for creating embeddings.onnxEmbeddingModelQuantized
: Boolean (optional, default true
) - Indicates whether to use a quantized version of the embedding model.localModelPath
: String (optional, default null
) - Local path to save and load models (example: ./models
).modelCacheDir
: String (optional, default null
) - Directory to cache downloaded models (example: ./models
).returnEmbedding
: Boolean (optional, default false
) - If set to true
, each chunk will include an embedding vector. This is useful for applications that require semantic understanding of the chunks. The embedding model will be the same as the one specified in onnxEmbeddingModel
.returnTokenLength
: Boolean (optional, default false
) - If set to true
, each chunk will include the token length. This can be useful for understanding the size of each chunk in terms of tokens, which is important for token-based processing limits. The token length is calculated using the tokenizer specified in onnxEmbeddingModel
.chunkPrefix
: String (optional, default null
) - A prefix to add to each chunk (e.g., "search_document: "). This is particularly useful when using embedding models that are trained with specific task prefixes, like the nomic-embed-text-v1.5 model. The prefix is added before calculating embeddings or token lengths.excludeChunkPrefixInResults
: Boolean (optional, default false
) - If set to true
, the chunk prefix will be removed from the results. This is useful when you want to remove the prefix from the results while still maintaining the prefix for embedding calculations.The output is an array of chunks, each containing the following properties:
document_id
: Integer - A unique identifier for the document (current timestamp in milliseconds).document_name
: String - The name of the document being chunked (if provided).number_of_chunks
: Integer - The total number of final chunks returned from the input text.chunk_number
: Integer - The number of the current chunk.model_name
: String - The name of the embedding model used.is_model_quantized
: Boolean - Indicates whether the embedding model is quantized.text
: String - The chunked text.embedding
: Array - The embedding vector (if returnEmbedding
is true
).token_length
: Integer - The token length (if returnTokenLength
is true
).Example 1: Basic usage with custom similarity threshold:
import { chunkit } from 'semantic-chunking';
import fs from 'fs';
async function main() {
const documents = [
{
document_name: "test document",
document_text: await fs.promises.readFile('./test.txt', 'utf8')
}
];
let myChunks = await chunkit(documents, { similarityThreshold: 0.4 });
myChunks.forEach((chunk, index) => {
console.log(`\n-- Chunk ${index + 1} --`);
console.log(chunk);
});
}
main();
Example 2: Chunking with a small max token size:
import { chunkit } from 'semantic-chunking';
const frogText = "A frog hops into a deli and croaks to the cashier, \"I'll have a sandwich, please.\" The cashier, surprised, quickly makes the sandwich and hands it over. The frog takes a big bite, looks around, and then asks, \"Do you have any flies to go with this?\" The cashier, taken aback, replies, \"Sorry, we're all out of flies today.\" The frog shrugs and continues munching on its sandwich, clearly unfazed by the lack of fly toppings. Just another day in the life of a sandwich-loving amphibian! 🐸🥪";
const documents = [
{
document_name: "frog document",
document_text: frogText
}
];
async function main() {
let myFrogChunks = await chunkit(documents, { maxTokenSize: 65 });
console.log("myFrogChunks", myFrogChunks);
}
main();
Look at the example\example-chunkit.js
file for a more complex example of using all the optional parameters.
The behavior of the chunkit
function can be finely tuned using several optional parameters in the options object. Understanding how each parameter affects the function can help you optimize the chunking process for your specific requirements.
logging
false
maxTokenSize
500
similarityThreshold
0.456
dynamicThresholdLowerBound
0.2
dynamicThresholdUpperBound
0.8
numSimilaritySentencesLookahead
2
combineChunks
true
maxTokenSize
. This can enhance the readability of the output by grouping closely related content more effectively.combineChunksSimilarityThreshold
0.4
similarityThreshold
, but specifically for rebalancing existing chunks. Adjusting this parameter can help in fine-tuning the granularity of the final chunks.onnxEmbeddingModel
Xenova/paraphrase-multilingual-MiniLM-L12-v2
onnxEmbeddingModelQuantized
true
Model | Quantized | Link | Size |
---|---|---|---|
nomic-ai/nomic-embed-text-v1.5 | true | https://huggingface.co/nomic-ai/nomic-embed-text-v1.5 | 138 MB |
nomic-ai/nomic-embed-text-v1.5 | false | https://huggingface.co/nomic-ai/nomic-embed-text-v1.5 | 548 MB |
Xenova/all-MiniLM-L6-v2 | true | https://huggingface.co/Xenova/all-MiniLM-L6-v2 | 23 MB |
Xenova/all-MiniLM-L6-v2 | false | https://huggingface.co/Xenova/all-MiniLM-L6-v2 | 90.4 MB |
Xenova/paraphrase-multilingual-MiniLM-L12-v2 | true | https://huggingface.co/Xenova/paraphrase-multilingual-MiniLM-L12-v2 | 118 MB |
thenlper/gte-base | false | https://huggingface.co/thenlper/gte-base | 436 MB |
Xenova/all-distilroberta-v1 | true | https://huggingface.co/Xenova/all-distilroberta-v1 | 82.1 MB |
Xenova/all-distilroberta-v1 | false | https://huggingface.co/Xenova/all-distilroberta-v1 | 326 MB |
BAAI/bge-base-en-v1.5 | false | https://huggingface.co/BAAI/bge-base-en-v1.5 | 436 MB |
BAAI/bge-small-en-v1.5 | false | https://huggingface.co/BAAI/bge-small-en-v1.5 | 133 MB |
Each of these parameters allows you to customize the chunkit
function to better fit the text size, content complexity, and performance requirements of your application.
The Semantic Chunking Web UI allows you to experiment with the chunking parameters and see the results in real-time. This tool provides a visual way to test and configure the semantic-chunking
library's settings to get optimal results for your specific use case. Once you've found the best settings, you can generate code to implement them in your project.
cramit
- 🧼 The Quick & DirtyThere is an additional function you can import to just "cram" sentences together till they meet your target token size for when you just need quick, high desity chunks.
Basic usage:
import { cramit } from 'semantic-chunking';
let frogText = "A frog hops into a deli and croaks to the cashier, \"I'll have a sandwich, please.\" The cashier, surprised, quickly makes the sandwich and hands it over. The frog takes a big bite, looks around, and then asks, \"Do you have any flies to go with this?\" The cashier, taken aback, replies, \"Sorry, we're all out of flies today.\" The frog shrugs and continues munching on its sandwich, clearly unfazed by the lack of fly toppings. Just another day in the life of a sandwich-loving amphibian! 🐸🥪";
// initialize documents array and add the frog text to it
let documents = [];
documents.push({
document_name: "frog document",
document_text: frogText
});
// call the cramit function passing in the documents array and the options object
async function main() {
let myFrogChunks = await cramit(documents, { maxTokenSize: 65 });
console.log("myFrogChunks", myFrogChunks);
}
main();
Look at the example\example-cramit.js
file in the root of this project for a more complex example of using all the optional parameters.
The behavior of the chunkit
function can be finely tuned using several optional parameters in the options object. Understanding how each parameter affects the function can help you optimize the chunking process for your specific requirements.
logging
false
maxTokenSize
500
onnxEmbeddingModel
Xenova/paraphrase-multilingual-MiniLM-L12-v2
onnxEmbeddingModelQuantized
true
Fill out the tools/download-models.list.json
file with a list of models you want pre-downloaded, and if they are quantized or not (See the Curated ONNX Embedding Models section above for a list of models to try)
If you are using this library for a RAG application, consider using the chunkPrefix
option to add a prefix to each chunk. This can help improve the quality of the embeddings and reduce the amount of context needed to be passed to the LLM for embedding models that support task prefixes.
Chunk your large document like this:
const largeDocumentText = await fs.promises.readFile('./large-document.txt', 'utf8');
const documents = [
{
document_name: "large document",
document_text: largeDocumentText
}
];
const myDocumentChunks = await chunkit(documents, { chunkPrefix: "search_document" });
Get your search queries ready like this (use cramit for a quick large chunk):
const documents = [
{ document_text: "What is the capital of France?" }
];
const mySearchQueryChunk = await chunkit(documents, { chunkPrefix: "search_query" });
Now you can use the myDocumentChunks
and mySearchQueryChunk
results in your RAG application, feed them to a vector database, or find the closest match using cosine similarity in memory. The possibilities are many!
Happy Chunking!
If you enjoy this library please consider sending me a tip to support my work 😀
[2.2.1] - 2024-11-06
FAQs
Semantically create chunks from large texts. Useful for workflows involving large language models (LLMs).
The npm package semantic-chunking receives a total of 82 weekly downloads. As such, semantic-chunking popularity was classified as not popular.
We found that semantic-chunking demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.