Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(examples): add ollama example #289

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/ollama/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local.db
29 changes: 29 additions & 0 deletions examples/ollama/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Ollama + Vector Search Example

This example demonstrates how to use libSQL vector search with a local database and Ollama.

## Install Dependencies

```bash
npm i
```

## Install Ollama

[Download Ollama](https://ollama.com/download) and install it.

## Running

Make sure Ollama is running with the model `mistral`:

```bash
ollama run mistral
```

Execute the example:

```bash
node index.mjs
```

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.
97 changes: 97 additions & 0 deletions examples/ollama/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { createClient } from "@libsql/client";
import ollama from "ollama";

const client = createClient({
url: "file:local.db",
});

await client.batch(
[
"CREATE TABLE IF NOT EXISTS movies (id INTEGER PRIMARY KEY, title TEXT NOT NULL, description TEXT NOT NULL, embedding F32_BLOB(4096))",
"CREATE INDEX IF NOT EXISTS movies_embedding_idx ON movies(libsql_vector_idx(embedding))",
],
"write",
);

async function getEmbedding(prompt) {
const response = await ollama.embeddings({
model: "mistral",
prompt,
});

return response.embedding;
}

async function insertMovie(title, description) {
const embedding = await getEmbedding(description);

await client.execute({
sql: `
INSERT INTO movies (title, description, embedding)
VALUES (?, ?, vector(?))
`,
args: [title, description, JSON.stringify(embedding)],
});
}

async function findSimilarMovies(description, limit = 3) {
const queryEmbedding = await getEmbedding(description);

const results = await client.execute({
sql: `
WITH vector_scores AS (
SELECT
rowid as id,
title,
description,
embedding,
1 - vector_distance_cos(embedding, vector32(?)) AS similarity
FROM movies
ORDER BY similarity DESC
LIMIT ?
)
SELECT id, title, description, similarity FROM vector_scores
`,
args: [JSON.stringify(queryEmbedding), limit],
});

return results.rows;
}

try {
const sampleMovies = [
{
title: "Inception",
description:
"A thief who enters the dreams of others to steal secrets from their subconscious.",
},
{
title: "The Matrix",
description:
"A computer programmer discovers that reality as he knows it is a simulation created by machines.",
},
{
title: "Interstellar",
description:
"Astronauts travel through a wormhole in search of a new habitable planet for humanity.",
},
];

for (const movie of sampleMovies) {
await insertMovie(movie.title, movie.description);
console.log(`Inserted: ${movie.title}`);
}

const query =
"A sci-fi movie about virtual reality and artificial intelligence";
console.log("\nSearching for movies similar to:", query);

const similarMovies = await findSimilarMovies(query);
console.log("\nSimilar movies found:");
similarMovies.forEach((movie) => {
console.log(`\nTitle: ${movie.title}`);
console.log(`Description: ${movie.description}`);
});
} catch (error) {
console.error("Error:", error);
}
Loading
Loading