Skip to content

Commit

Permalink
doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
asg017 committed Jun 22, 2024
1 parent df48ac2 commit b62f6f1
Show file tree
Hide file tree
Showing 31 changed files with 751 additions and 97 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ See <a href="#sponsors">the Sponsors section</a> for more details.
</i>
</p>

<!--
## Installing
See [Installing `sqlite-vec`](https://alexgarcia.xyz/sqlite-vec/installing.html)
for more details.
| Language | Install | More Info | |
| -------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Python | `pip install sqlite-vec` | [`sqlite-vec` with Python](https://alexgarcia.xyz/sqlite-vec/python.html) | [![PyPI](https://img.shields.io/pypi/v/sqlite-vec.svg?color=blue&logo=python&logoColor=white)](https://pypi.org/project/sqlite-vec/) |
| Node.js | `npm install sqlite-vec` | [`sqlite-vec` with Node.js](https://alexgarcia.xyz/sqlite-vec/nodejs.html) | [![npm](https://img.shields.io/npm/v/sqlite-vec.svg?color=green&logo=nodedotjs&logoColor=white)](https://www.npmjs.com/package/sqlite-vec) |
| Ruby | `gem install sqlite-vec` | [`sqlite-vec` with Ruby](https://alexgarcia.xyz/sqlite-vec/ruby.html) | ![Gem](https://img.shields.io/gem/v/sqlite-vec?color=red&logo=rubygems&logoColor=white) |
| Go | `go get -u github.com/asg017/sqlite-vec/bindings/go` | [`sqlite-vec` with Go](https://alexgarcia.xyz/sqlite-vec/go.html) | [![Go Reference](https://pkg.go.dev/badge/github.com/asg017/sqlite-vec/bindings/go.svg)](https://pkg.go.dev/github.com/asg017/sqlite-vec/bindings/go) |
| Rust | `cargo add sqlite-vec` | [`sqlite-vec` with Rust](https://alexgarcia.xyz/sqlite-vec/rust.html) | [![Crates.io](https://img.shields.io/crates/v/sqlite-vec?logo=rust)](https://crates.io/crates/sqlite-vec) |
| Datasette | `datasette install datasette-sqlite-vec` | [`sqlite-vec` with Datasette](https://alexgarcia.xyz/sqlite-vec/datasette.html) | [![Datasette](https://img.shields.io/pypi/v/datasette-sqlite-vec.svg?color=B6B6D9&label=Datasette+plugin&logoColor=white&logo=python)](https://datasette.io/plugins/datasette-sqlite-vec) |
| `sqlite-utils` | `sqlite-utils install sqlite-utils-sqlite-vec` | [`sqlite-vec` with sqlite-utils](https://alexgarcia.xyz/sqlite-vec/sqlite-utils.html) | [![sqlite-utils](https://img.shields.io/pypi/v/sqlite-utils-sqlite-vec.svg?color=B6B6D9&label=sqlite-utils+plugin&logoColor=white&logo=python)](https://datasette.io/plugins/datasette-sqlite-vec) |
| Github Release | | | ![GitHub tag (latest SemVer pre-release)](https://img.shields.io/github/v/tag/asg017/sqlite-vec?color=lightgrey&include_prereleases&label=Github+release&logo=github) |
-->

## Sample usage

```sql
Expand Down
90 changes: 90 additions & 0 deletions examples/python-recipes/openai-sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# pip install openai sqlite-vec

from openai import OpenAI
import sqlite3
import sqlite_vec
import struct
from typing import List


def serialize(vector: List[float]) -> bytes:
"""serializes a list of floats into a compact "raw bytes" format"""
return struct.pack("%sf" % len(vector), *vector)


sentences = [
"Capri-Sun is a brand of juice concentrate–based drinks manufactured by the German company Wild and regional licensees.",
"George V was King of the United Kingdom and the British Dominions, and Emperor of India, from 6 May 1910 until his death in 1936.",
"Alaqua Cox is a Native American (Menominee) actress.",
"Shohei Ohtani is a Japanese professional baseball pitcher and designated hitter for the Los Angeles Dodgers of Major League Baseball.",
"Tamarindo, also commonly known as agua de tamarindo, is a non-alcoholic beverage made of tamarind, sugar, and water.",
]


client = OpenAI()

# change ':memory:' to a filepath to persist data
db = sqlite3.connect(":memory:")
db.enable_load_extension(True)
sqlite_vec.load(db)
db.enable_load_extension(False)

db.execute(
"""
CREATE TABLE sentences(
id INTEGER PRIMARY KEY,
sentence TEXT
);
"""
)

with db:
for i, sentence in enumerate(sentences):
db.execute("INSERT INTO sentences(id, sentence) VALUES(?, ?)", [i, sentence])

db.execute(
"""
CREATE VIRTUAL TABLE vec_sentences USING vec0(
id INTEGER PRIMARY KEY,
sentence_embedding FLOAT[1536]
);
"""
)


with db:
sentence_rows = db.execute("SELECT id, sentence FROM sentences").fetchall()
response = client.embeddings.create(
input=[row[1] for row in sentence_rows], model="text-embedding-3-small"
)
for (id, _), embedding in zip(sentence_rows, response.data):
db.execute(
"INSERT INTO vec_sentences(id, sentence_embedding) VALUES(?, ?)",
[id, serialize(embedding.embedding)],
)


query = "fruity liquids"
query_embedding = (
client.embeddings.create(input=query, model="text-embedding-3-small")
.data[0]
.embedding
)

results = db.execute(
"""
SELECT
vec_sentences.id,
distance,
sentence
FROM vec_sentences
LEFT JOIN sentences ON sentences.id = vec_sentences.id
WHERE sentence_embedding MATCH ?
AND k = 3
ORDER BY distance
""",
[serialize(query_embedding)],
).fetchall()

for row in results:
print(row)
93 changes: 61 additions & 32 deletions site/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ const VERSION = readFileSync(
"utf8"
);

const sqliteLanuage = JSON.parse(
readFileSync(
join(
dirname(fileURLToPath(import.meta.url)),
"..",
"sqlite.tmlanguage.json"
),
"utf8"
)
);

function head(): HeadConfig[] {
return [
[
"link",
{
rel: "shortcut icon",
type: "image/svg+xml",
href: "favicon.svg",
href: "./logo.light.svg",
},
],
[
Expand All @@ -36,22 +47,34 @@ const guides = {
text: "Guides",
collapsed: true,
items: [
{ text: "Binary Quantization", link: "/guides/binary-quant" },
{ text: "Scalar Quantization", link: "/guides/scalar-quant" },
{ text: "Performance", link: "/guides/performance" },
{
text: "Vector operations",
items: [
{ text: "Vector Arithmetic", link: "/guides/arithmetic" },
{ text: "Binary Quantization", link: "/guides/binary-quant" },
{ text: "Scalar Quantization", link: "/guides/scalar-quant" },
{
text: "Matryoshka Embeddings",
link: "/guides/matryoshka",
},
],
},

{
text: "Matryosha/Adaptive Length Embeddings",
link: "/guides/matryoshka",
text: "Build with sqlite-vec",
items: [
{ text: "Semantic Search", link: "/guides/semantic-search" },
{ text: "Hybrid Search", link: "/guides/hybrid-search" },
{ text: "Retrival Augmented Generation (RAG)", link: "/guides/rag" },
{ text: "Classifiers", link: "/guides/classifiers" },
],
},
{ text: "Semantic Search", link: "/guides/semantic-search" },
{ text: "Hybrid Search", link: "/guides/hybrid-search" },
{ text: "Classifiers", link: "/guides/classifiers" },
{ text: "Improving Performance", link: "/guides/improving-perf" },
],
};

function nav(): DefaultTheme.NavItem[] {
return [
guides,
{ text: "API Reference", link: "/api-reference" },
{ text: "♥ Sponsor", link: "https://github.com/sponsors/asg017" },
{
Expand Down Expand Up @@ -103,17 +126,25 @@ function sidebar(): DefaultTheme.SidebarItem[] {
return [
{
text: "Getting Started",
collapsed: false,
collapsed: true,
items: [
{
text: "Quickstart",
link: "/getting-started",
text: "Installation",
link: "/installation",
},
{
text: "Introduction",
link: "/introduction",
},
{
text: "Quick Start",
link: "/quickstart",
},
],
},
{
text: "Using with...",
collapsed: false,
collapsed: true,
items: [
{ text: "Python", link: "/python" },
{ text: "JavaScript", link: "/js" },
Expand All @@ -124,7 +155,6 @@ function sidebar(): DefaultTheme.SidebarItem[] {
{ text: "WebAssembly (Browser)", link: "/wasm" },
{ text: "Datasette", link: "/datasette" },
{ text: "sqlite-utils", link: "/sqlite-utils" },
{ text: "Loadable Extension", link: "/loadable" },
],
},
guides,
Expand All @@ -135,6 +165,10 @@ function sidebar(): DefaultTheme.SidebarItem[] {
{ text: "API Reference", link: "/api-reference" },
],
},
{
text: "Sponsors",
link: "/sponsors",
},
{
text: "See also",
items: [
Expand Down Expand Up @@ -163,13 +197,18 @@ export default defineConfig({
head: head(),
base: "/sqlite-vec/",
themeConfig: {
nav: nav(),
logo: {
light: "/logo.dark.svg",
dark: "/logo.light.svg",
alt: "sqlite-vec logo",
},

nav: nav(),
sidebar: sidebar(),

footer: {
message: "MIT License",
copyright: "Copyright © 2024 Alex Garcia",
message: "MIT/Apache-2 License",
copyright:
'Copyright © 2024 <a href="https://alexgarcia.xyz/">Alex Garcia</a>',
},
outline: "deep",
search: {
Expand All @@ -185,20 +224,10 @@ export default defineConfig({
},
rewrites: {
"using/:pkg.md": ":pkg.md",
"guides/:pkg.md": ":pkg.md",
"getting-started/:pkg.md": ":pkg.md",
//"guides/:pkg.md": ":pkg.md",
},
markdown: {
languages: [
JSON.parse(
readFileSync(
join(
dirname(fileURLToPath(import.meta.url)),
"..",
"sqlite.tmlanguage.json"
),
"utf8"
)
),
],
languages: [sqliteLanuage],
},
});
38 changes: 38 additions & 0 deletions site/.vitepress/theme/HeroImg.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script setup lang="ts"></script>

<template>
<div
style="
background: var(--vp-c-default-3);
padding: -4px 12px;
border-radius: 10px;
"
>
<div>
<div class="language-sqlite vp-adaptive-theme">
<pre
class="shiki shiki-themes github-light github-dark vp-code"
><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">-- store 768-dimensional vectors in a vec0 virtual table</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">create</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> virtual</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> table</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> vec_movies </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">using</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> vec0(</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> synopsis_embedding </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">float</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[768]</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">-- insert vectors into the table, as JSON or compact BLOBs</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">insert into</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> vec_movies(rowid, synopsis_embedding)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> select</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> rowid,</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> embed(synopsis) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">as</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> synopsis_embedding</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> movies;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">-- KNN search!</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">select</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> rowid,</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> distance</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> vec_movies</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">where</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> synopsis_embedding </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">match</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> embed(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'scary futuristic movies'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">order by</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> distance</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">limit</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 20</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span></code></pre>
</div>
</div>
</div>
</template>
32 changes: 17 additions & 15 deletions site/.vitepress/theme/Sponsors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const sponsors = computed(() => {
{
name: "Mozilla Builders",
url: "",
img: withBase("./mozilla.svg"),
img: withBase("/mozilla.svg"),
},
],
},
Expand All @@ -21,7 +21,7 @@ const sponsors = computed(() => {
{
name: "Fly.io",
url: "https://fly.io",
img: withBase("./flyio.svg"),
img: withBase("/flyio.svg"),
},
],
},
Expand All @@ -31,7 +31,7 @@ const sponsors = computed(() => {
{
name: "Turso",
url: "https://turso.tech",
img: withBase("./turso.svg"),
img: withBase("/turso.svg"),
},
],
},
Expand All @@ -41,7 +41,7 @@ const sponsors = computed(() => {
{
name: "SQLite Cloud",
url: "https://sqlitecloud.io",
img: withBase("./sqlitecloud.svg"),
img: withBase("/sqlitecloud.svg"),
},
],
},
Expand All @@ -51,18 +51,20 @@ const sponsors = computed(() => {

<template>
<!--<a class="sponsors-aside-text" href="/sponsor/">Sponsors</a>-->
<VPDocAsideSponsors :data="sponsors" />
<div
style="
font-size: 14px;
text-align: center;
font-style: italic;
margin-top: 4px;
"
>
<a href="https://github.com/asg017/sqlite-vec#sponsors"
>Become a sponsor! ↗</a
<div>
<VPDocAsideSponsors :data="sponsors" />
<div
style="
font-size: 14px;
text-align: center;
font-style: italic;
margin-top: 4px;
"
>
<a href="https://github.com/asg017/sqlite-vec#sponsors"
>Become a sponsor! ↗</a
>
</div>
</div>
</template>

Expand Down
Loading

0 comments on commit b62f6f1

Please sign in to comment.