Use LLMs in SQLite and DuckDB
Project description
tsellm: Use LLMs in SQLite
tsellm is the easiest way to access LLMs through your SQLite database.
pip install tsellm
usage: tsellm [-h] [--sqlite | --duckdb] [-v] [filename] [sql]
Use LLMs in SQLite and DuckDB
positional arguments:
filename SQLite/DuckDB database to open (defaults to SQLite ':memory:').
A new database is created if the file does not previously exist.
sql An SQL query to execute. Any returned rows are printed to
stdout.
options:
-h, --help show this help message and exit
--sqlite SQLite mode
--duckdb DuckDB mode
-v, --version Print tsellm version
Behind the scenes, tsellm is based on the beautiful llm library, so you can use any of its plugins:
Generative
For example, to access gpt4all
models
llm install llm-gpt4all
# Then pick any gpt4all (it will be downloaded automatically the first time you use any model
tsellm :memory: "select prompt('What is the capital of Greece?', 'orca-mini-3b-gguf2-q4_0')"
tsellm :memory: "select prompt('What is the capital of Greece?', 'orca-2-7b')"
Embeddings
llm install llm-sentence-transformers
llm sentence-transformers register all-MiniLM-L12-v2
tsellm :memory: "select embed('Hello', 'sentence-transformers/all-MiniLM-L12-v2')"
Embeddings for binary (BLOB
) columns
wget https://tselai.com/img/flo.jpg
sqlite3 images.db <<EOF
CREATE TABLE images(name TEXT, type TEXT, img BLOB);
INSERT INTO images(name,type,img) VALUES('flo','jpg',readfile('flo.jpg'));
EOF
llm install llm-clip
tsellm images.db "select embed(img, 'clip') from images"
Examples
Things get more interesting if you combine models in your standard queries.
First, create a db with some data. You can easily toggle between SQLite and DuckDB, and tsellm will pick this up automatically.
SQLite
sqlite3 prompts.db <<EOF
CREATE TABLE prompts (
p TEXT
);
INSERT INTO prompts VALUES('hello world!');
INSERT INTO prompts VALUES('how are you?');
INSERT INTO prompts VALUES('is this real life?');
INSERT INTO prompts VALUES('1+1=?');
EOF
With a single query you can access get prompt responses from different LLMs:
tsellm prompts.db "
select p,
prompt(p, 'orca-2-7b'),
prompt(p, 'orca-mini-3b-gguf2-q4_0'),
embed(p, 'sentence-transformers/all-MiniLM-L12-v2')
from prompts"
DuckDB
duckdb prompts.duckdb <<EOF
CREATE TABLE prompts (
p TEXT
);
INSERT INTO prompts VALUES('hello world!');
INSERT INTO prompts VALUES('how are you?');
INSERT INTO prompts VALUES('is this real life?');
INSERT INTO prompts VALUES('1+1=?');
EOF
With a single query you can access get prompt responses from different LLMs:
tsellm prompts.duckdb "
select p,
prompt(p, 'orca-2-7b'),
prompt(p, 'orca-mini-3b-gguf2-q4_0'),
embed(p, 'sentence-transformers/all-MiniLM-L12-v2')
from prompts"
Interactive Shell
If you don't provide an SQL query, you'll enter an interactive shell instead.
tsellm prompts.db
Installation
pip install tsellm
How
tsellm relies on the following facts:
- SQLite is bundled with the standard Python library (
import sqlite3
) - Python 3.12 ships with a SQLite interactive shell
- one can create Python-written user-defined functions to be used in SQLite queries (see create_function)
- Simon Willison has gone through the process of creating the beautiful llm Python library and CLI
Development
pip install -e '.[test]'
pytest
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file tsellm-0.1.0a11.tar.gz
.
File metadata
- Download URL: tsellm-0.1.0a11.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03e972c1d17fc67644fdd2df18ea6b8544e6c476fb78100602a65fa72dab5c27 |
|
MD5 | 105b03225bd350c66a2eaf5f661a1fbb |
|
BLAKE2b-256 | ceace8e45c189ae45d65bdfc61cb5703da492af002220c4e86b1c16c5f4ef01f |
File details
Details for the file tsellm-0.1.0a11-py3-none-any.whl
.
File metadata
- Download URL: tsellm-0.1.0a11-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ef9a82a91ef96e56a0c6d21da9d151a085c297299388edf384c46baab300289 |
|
MD5 | bb45e1d59b14892805cc32b4c14bf29f |
|
BLAKE2b-256 | 1f2b6d69ee89ca138922d8f1e642307fe8c097ca8af456311bfa72829df4cd77 |