Skip to content

Commit fbf8f63

Browse files
authored
docs(examples): add ollama example (#289)
* docs(examples): add ollama example * remove unncessary pkg details * return score * docs: update readme
1 parent b41fa4b commit fbf8f63

File tree

6 files changed

+515
-0
lines changed

6 files changed

+515
-0
lines changed

examples/ollama/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db

examples/ollama/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Ollama + Vector Search Example
2+
3+
This example demonstrates how to use libSQL vector search with a local database and Ollama.
4+
5+
## Install Dependencies
6+
7+
```bash
8+
npm i
9+
```
10+
11+
## Install Ollama
12+
13+
[Download Ollama](https://ollama.com/download) and install it.
14+
15+
## Running
16+
17+
Make sure Ollama is running with the model `mistral`:
18+
19+
```bash
20+
ollama run mistral
21+
```
22+
23+
Execute the example:
24+
25+
```bash
26+
node index.mjs
27+
```
28+
29+
This will setup a local SQLite database, generate embeddings using Ollama, and insert the data with embeddings, and then query the results using the vector similarity search function.

examples/ollama/index.mjs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { createClient } from "@libsql/client";
2+
import ollama from "ollama";
3+
4+
const client = createClient({
5+
url: "file:local.db",
6+
});
7+
8+
await client.batch(
9+
[
10+
"CREATE TABLE IF NOT EXISTS movies (id INTEGER PRIMARY KEY, title TEXT NOT NULL, description TEXT NOT NULL, embedding F32_BLOB(4096))",
11+
"CREATE INDEX IF NOT EXISTS movies_embedding_idx ON movies(libsql_vector_idx(embedding))",
12+
],
13+
"write",
14+
);
15+
16+
async function getEmbedding(prompt) {
17+
const response = await ollama.embeddings({
18+
model: "mistral",
19+
prompt,
20+
});
21+
22+
return response.embedding;
23+
}
24+
25+
async function insertMovie(id, title, description) {
26+
const embedding = await getEmbedding(description);
27+
28+
await client.execute({
29+
sql: `INSERT OR REPLACE INTO movies (id, title, description, embedding) VALUES (?, ?, ?, vector(?))`,
30+
args: [id, title, description, JSON.stringify(embedding)],
31+
});
32+
}
33+
34+
async function insertMovieIfNotExists(id, title, description) {
35+
const existing = await client.execute({
36+
sql: "SELECT id FROM movies WHERE id = ?",
37+
args: [id],
38+
});
39+
40+
if (existing.rows.length === 0) {
41+
await insertMovie(id, title, description);
42+
console.log(`Inserted: ${title} (ID: ${id})`);
43+
} else {
44+
console.log(`Movie already exists: ${title} (ID: ${id})`);
45+
}
46+
}
47+
48+
async function findSimilarMovies(description, limit = 3) {
49+
const queryEmbedding = await getEmbedding(description);
50+
51+
const results = await client.execute({
52+
sql: `
53+
WITH vector_scores AS (
54+
SELECT DISTINCT
55+
id,
56+
title,
57+
description,
58+
1 - vector_distance_cos(embedding, vector32(?)) AS similarity
59+
FROM movies
60+
ORDER BY similarity DESC
61+
LIMIT ?
62+
)
63+
SELECT id, title, description, similarity FROM vector_scores
64+
`,
65+
args: [JSON.stringify(queryEmbedding), limit],
66+
});
67+
68+
return results.rows;
69+
}
70+
71+
try {
72+
const sampleMovies = [
73+
{
74+
id: 1,
75+
title: "Inception",
76+
description:
77+
"A thief who enters the dreams of others to steal secrets from their subconscious.",
78+
},
79+
{
80+
id: 2,
81+
title: "The Matrix",
82+
description:
83+
"A computer programmer discovers that reality as he knows it is a simulation created by machines.",
84+
},
85+
{
86+
id: 3,
87+
title: "Interstellar",
88+
description:
89+
"Astronauts travel through a wormhole in search of a new habitable planet for humanity.",
90+
},
91+
];
92+
93+
for (const movie of sampleMovies) {
94+
await insertMovieIfNotExists(movie.id, movie.title, movie.description);
95+
}
96+
97+
const query =
98+
"A sci-fi movie about virtual reality and artificial intelligence";
99+
console.log("\nSearching for movies similar to:", query);
100+
101+
const similarMovies = await findSimilarMovies(query);
102+
console.log("\nSimilar movies found:");
103+
similarMovies.forEach((movie) => {
104+
console.log(`\nTitle: ${movie.title}`);
105+
console.log(`Description: ${movie.description}`);
106+
console.log(`Similarity: ${movie.similarity.toFixed(4)}`);
107+
});
108+
} catch (error) {
109+
console.error("Error:", error);
110+
}

0 commit comments

Comments
 (0)