-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "libsql-examples-vector", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@xenova/transformers": "^2.17.1", | ||
"csv-parse": "^5.5.5", | ||
"libsql": "../../" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { pipeline } from "@xenova/transformers"; | ||
import { createReadStream } from "fs"; | ||
import { parse } from "csv-parse"; | ||
import Database from "libsql"; | ||
|
||
// Create a embeddings generator. | ||
const extractor = await pipeline( | ||
"feature-extraction", | ||
"Xenova/jina-embeddings-v2-small-en", | ||
{ quantized: false }, | ||
); | ||
|
||
// Open a database file. | ||
const db = new Database("movies.db"); | ||
|
||
// Create a table for movies with an embedding as a column. | ||
db.exec("CREATE TABLE movies (title TEXT, year INT, embedding VECTOR(512))"); | ||
|
||
// Create a vector index on the embedding column. | ||
db.exec("CREATE INDEX movies_idx USING vector ON movies (embedding)"); | ||
|
||
// Prepare a SQL `INSERT` statement. | ||
const stmt = db.prepare( | ||
"INSERT INTO movies (title, year, embedding) VALUES (?, ?, vector(?))", | ||
); | ||
|
||
// Process a CSV file of movies generating embeddings for plot synopsis. | ||
createReadStream("wiki_movie_plots_deduped.csv") | ||
.pipe(parse({ columns: true })) | ||
.on("data", async (data) => { | ||
const title = data.Title; | ||
const year = data.Year; | ||
const plot = data.Plot; | ||
const output = await extractor([plot], { pooling: "mean" }); | ||
const embedding = output[0].data; | ||
stmt.run([title, year, embedding]); | ||
}); |