Skip to content

Commit

Permalink
16 binary embedding for blob types eg using llm clip (#21)
Browse files Browse the repository at this point in the history
* Test binary with hazo

* Add image embedding example
  • Loading branch information
Florents-Tselai authored Jun 30, 2024
1 parent d991c6e commit 188d39f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pip install tsellm
Behind the scenes, **tsellm** is based on the beautiful [llm](https://llm.datasette.io) library,
so you can use any of its plugins:

## Generative

For example, to access `gpt4all` models

```shell
Expand All @@ -33,6 +35,21 @@ llm sentence-transformers register all-MiniLM-L12-v2
tsellm :memory: "select embed('Hello', 'sentence-transformers/all-MiniLM-L12-v2')"
```

### Embeddings for binary (`BLOB`) columns

```shell
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
```

```shell
llm install llm-clip
tsellm images.db "select embed(img, 'clip') from images"
```

## Examples

Things get more interesting if you
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_long_description():
"ruff",
"sqlite_utils",
"llm-markov",
"llm-embed-hazo",
"llm-embed-hazo==0.2.1",
]
},
python_requires=">=3.11",
Expand Down
17 changes: 11 additions & 6 deletions tests/test_tsellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,18 @@ def test_embed_hazo(self):
out,
)

def test_embed_hazo_binary(self):
self.assertTrue(llm.get_embedding_model("hazo").supports_binary)
self.expect_success(":memory:", "select embed(randomblob(16), 'hazo')")


def test_embed_default_hazo(self):
self.assertEqual(llm_cli.get_default_embedding_model(), "hazo")
out = self.expect_success(":memory:", "select embed('hello world')")
self.assertEqual(
"('[5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]',)\n",
out,
)
self.assertEqual(llm_cli.get_default_embedding_model(), "hazo")
out = self.expect_success(":memory:", "select embed('hello world')")
self.assertEqual(
"('[5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]',)\n",
out,
)


if __name__ == "__main__":
Expand Down

0 comments on commit 188d39f

Please sign in to comment.