Skip to main content

Use sentence-transformers for embeddings with LLM

Project description

llm-sentence-transformers

PyPI Changelog Tests License

LLM plugin for embedding models using sentence-transformers

Further reading:

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

llm_sentence_transformers-0.3.2.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_sentence_transformers-0.3.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file llm_sentence_transformers-0.3.2.tar.gz.

File metadata

File hashes

Hashes for llm_sentence_transformers-0.3.2.tar.gz
Algorithm Hash digest
SHA256 a39e4d3584d672d006d11873be6f902f564ce17f5174fddb50109d276000680f
MD5 29a4c40dc8ceff9c3ee76322666b6fb9
BLAKE2b-256 1d76ed7ebc9ce28210ae1946175551d7180749e3c83291e35430ea5afda151aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_sentence_transformers-0.3.2.tar.gz:

Publisher: publish.yml on simonw/llm-sentence-transformers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_sentence_transformers-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_sentence_transformers-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2ba8a21906820f4b165913b66a3d4446fb932f47c6364e74e9470298770594e6
MD5 efb1f930c02ecfbe67bfa150b8f70f66
BLAKE2b-256 f9f825db7460099d1491d73cf0b0633e5ffc44a244d2858d35d1ebbd4fc12edf

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page