Use sentence-transformers for embeddings with LLM
Project description
llm-sentence-transformers
LLM plugin for embedding models using sentence-transformers
Further reading:
- LLM now provides tools for working with embeddings
- Embedding paragraphs from my blog with E5-large-v2
Installation
Install this plugin in the same environment as LLM.
llm install llm-sentence-transformers
Configuration
After installing the plugin you need to register one or more models in order to use it. The all-MiniLM-L6-v2 model is registered by default, and will be downloaded the first time you use it.
You can try that model out like this:
llm embed -m mini-l6 -c 'hello'
This will return a JSON array of floating point numbers.
You can add more models using the llm sentence-transformers register command. Here is a list of available models.
Two good models to start experimenting with are all-MiniLM-L12-v2 - a 120MB download - and all-mpnet-base-v2, which is 420MB.
To install that all-mpnet-base-v2 model, run:
llm sentence-transformers register \
all-mpnet-base-v2 \
--alias mpnet
Some models may also require you to pass the --trust-remote-code flag when registering them. The model documentation will usually mention that in the Python example code on Hugging Face.
The --alias is optional, but can be used to configure one or more shorter aliases for the model.
You can run llm aliases to confirm which aliases you have configured, and llm aliases set to configure further aliases.
Usage
Once you have installed an embedding model you can use it like this:
llm embed -m sentence-transformers/all-mpnet-base-v2 \
-c "Hello world"
Or use its alias:
llm embed -m mpnet -c "Hello world"
Embeddings are more useful if you store them in a database - see the LLM documentation for instructions on doing that.
Be sure to review the documentation for the model you are using. Many models will silently truncate content beyond a certain number of tokens. all-mpnet-base-v2 says that "input text longer than 384 word pieces is truncated", for example.
Removing models
Models are stored in the Hugging Face cache directory, which can usually be found in ~/.cache/huggingface/hub.
To remove a model, first delete the directory for that model from the cache directory, then manually remove the model from the sentence-transformers.json file in the LLM configuration directory. The location of this directory can be found by running llm logs path:
llm logs path
Example output:
/Users/simon/Library/Application Support/io.datasette.llm/logs.db
In this case, sentence-transformers.json would be located at:
/Users/simon/Library/Application Support/io.datasette.llm/sentence-transformers.json
Development
To set up this plugin locally, first checkout the code. Then create a new virtual environment:
cd llm-sentence-transformers
python3 -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
pip install -e '.[test]'
To run the tests:
pytest
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_sentence_transformers-0.3.2.tar.gz.
File metadata
- Download URL: llm_sentence_transformers-0.3.2.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a39e4d3584d672d006d11873be6f902f564ce17f5174fddb50109d276000680f
|
|
| MD5 |
29a4c40dc8ceff9c3ee76322666b6fb9
|
|
| BLAKE2b-256 |
1d76ed7ebc9ce28210ae1946175551d7180749e3c83291e35430ea5afda151aa
|
Provenance
The following attestation bundles were made for llm_sentence_transformers-0.3.2.tar.gz:
Publisher:
publish.yml on simonw/llm-sentence-transformers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_sentence_transformers-0.3.2.tar.gz -
Subject digest:
a39e4d3584d672d006d11873be6f902f564ce17f5174fddb50109d276000680f - Sigstore transparency entry: 201552790
- Sigstore integration time:
-
Permalink:
simonw/llm-sentence-transformers@97c8013753690822acc6057057563473ebcdbc1f -
Branch / Tag:
refs/tags/0.3.2 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@97c8013753690822acc6057057563473ebcdbc1f -
Trigger Event:
release
-
Statement type:
File details
Details for the file llm_sentence_transformers-0.3.2-py3-none-any.whl.
File metadata
- Download URL: llm_sentence_transformers-0.3.2-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ba8a21906820f4b165913b66a3d4446fb932f47c6364e74e9470298770594e6
|
|
| MD5 |
efb1f930c02ecfbe67bfa150b8f70f66
|
|
| BLAKE2b-256 |
f9f825db7460099d1491d73cf0b0633e5ffc44a244d2858d35d1ebbd4fc12edf
|
Provenance
The following attestation bundles were made for llm_sentence_transformers-0.3.2-py3-none-any.whl:
Publisher:
publish.yml on simonw/llm-sentence-transformers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_sentence_transformers-0.3.2-py3-none-any.whl -
Subject digest:
2ba8a21906820f4b165913b66a3d4446fb932f47c6364e74e9470298770594e6 - Sigstore transparency entry: 201552791
- Sigstore integration time:
-
Permalink:
simonw/llm-sentence-transformers@97c8013753690822acc6057057563473ebcdbc1f -
Branch / Tag:
refs/tags/0.3.2 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@97c8013753690822acc6057057563473ebcdbc1f -
Trigger Event:
release
-
Statement type: