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