Semantic-Split
A Python library to chunk/group your text based on semantic similarity - ideal for pre-processing data for Language Models or Vector Databases. Leverages SentenceTransformers and spaCy.
Why?
-
Better Context: Providing more relevant context to your prompts enhances the LLM's performance (arXiv:2005.14165 [cs.CL]). Semantic-Split groups related sentences together, ensuring your prompts have relevant context.
-
Improved Results: Short, precise prompts often yield the best results from LLMs (arXiv:2004.04906 [cs.CL]). By grouping semantically similar sentences, Semantic-Split helps you craft such efficient prompts.
-
Cost Savings: LLMs like GPT-4 charge costs per token and have a token limit (e.g., 8K tokens). With Semantic-Split, you can make your prompts shorter and more meaningful, leading to potential cost savings.
Real world example:
Imagine you're building an application where users ask questions about articles:
- A. We want to only add parts in the artcile that are relevant to our query (for better results).
- B. We want to be able to query the article quickly (pre-processing).
- We want to pre-process the article - so each query is fast (point B). So we split it into semantic chunks using
semantic-splitand store it in a Vector DB as embeddings. - Each time the user asks something we calculate the embedding for their question and find the top 3 similar chunks in our Vector DB.
- We add those 3 chunks to our pompt, to get better results for our user's questions.
As you can see, in part 1, which involves semantic sentence splitting (grouping), is crucial. If we don't split or group the sentences semantically, we risk losing essential information. This can diminish the effectiveness of our Vector DB in identifying the most suitable chunks. Consequently, we may end up with poorer context for our prompts, negatively impacting the quality of our responses.
Install
- To use most of the functionality you will need to install some pre-requisists
- Spacy sm dataset:
python -m spacy download en_core_web_sm poetry install- See examples
Examples
Sentence Split by Semantic Similarity
from semantic_split import SimilarSentenceSplitter, SentenceTransformersSimilarity, SpacySentenceSplitter
text = """
I dogs are amazing.
Cats must be the easiest pets around.
Robots are advanced now with AI.
Flying in space can only be done by Artificial intelligence."""
model = SentenceTransformersSimilarity()
sentence_splitter = SpacySentenceSplitter()
splitter = SimilarSentenceSplitter(model, sentence_splitter)
res = splitter.split(text)
Result:
[["I dogs are amazing.", "Cats must be the easiest pets around."],
["Robots are advanced now with AI.", "Flying in space can only be done by Artificial intelligence."]]
Tests
poetry run pytest
Metadata
Release files for semantic-split 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| semantic_split-0.1.0.tar.gz | 3.3 kB | Without provenance | Details for semantic_split-0.1.0.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| semantic_split-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for semantic_split-0.1.0-py3-none-any.whl |
| semantic_split-0.1.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for semantic_split-0.1.0-py2.py3-none-any.whl |
Total release size: 12.9 kB
Release files / semantic_split-0.1.0.tar.gz
| Download URL | semantic_split-0.1.0.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0ce362864b34d4642f88e2bf45b05e17fb7ebfb9555cb6e7d0703e2cb3512c43
|
|
BLAKE2b-256 checksum How to use checksums |
def9abaf967304741740c217e23d3a6adcee8b2353527db82ce0bb1e25d9b231
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.6 Linux/5.15.90.1-microsoft-standard-WSL2
|
Release files / semantic_split-0.1.0-py3-none-any.whl
| Download URL | semantic_split-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c02eedbec9da7c510b9e0ad42f6aa117bb4077e7d4ffe491e3d8940d534774e1
|
|
BLAKE2b-256 checksum How to use checksums |
ae0a61798449f783fa2c6d0c05a39150b3957146a2999b5fab8130ca6de4c6e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.6 Linux/5.15.90.1-microsoft-standard-WSL2
|
Release files / semantic_split-0.1.0-py2.py3-none-any.whl
| Download URL | semantic_split-0.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
6e6bd8168ac6574805f5e7aac37274cd73c94a568107a07e9d7ac129948978b7
|
|
BLAKE2b-256 checksum How to use checksums |
e7d7db87e57d4fbc42586a35739da07444ca76c480337fa096fe9ec50781259d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|
Security for semantic-split 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.