mirror of
https://github.com/asg017/sqlite-vec.git
synced 2026-04-25 00:36:56 +02:00
start simple examples
This commit is contained in:
parent
2572aa1413
commit
e6067e2711
28 changed files with 1354 additions and 2 deletions
36
examples/simple-ruby/demo.rb
Normal file
36
examples/simple-ruby/demo.rb
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
require 'sqlite3'
|
||||
require 'sqlite_vec'
|
||||
|
||||
|
||||
db = SQLite3::Database.new(':memory:')
|
||||
db.enable_load_extension(true)
|
||||
SqliteVec.load(db)
|
||||
db.enable_load_extension(false)
|
||||
|
||||
sqlite_version, vec_version = db.execute("select sqlite_version(), vec_version()").first
|
||||
puts "sqlite_version=#{sqlite_version}, vec_version=#{vec_version}"
|
||||
|
||||
db.execute("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[8])")
|
||||
|
||||
items = [
|
||||
[1, [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]],
|
||||
[2, [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]]
|
||||
]
|
||||
db.transaction do
|
||||
items.each do |item|
|
||||
db.execute("INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)", [item[0], item[1].pack("f*")])
|
||||
end
|
||||
end
|
||||
|
||||
query = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
|
||||
rows = db.execute(<<-SQL, [query.pack("f*")])
|
||||
SELECT
|
||||
rowid,
|
||||
distance
|
||||
FROM vec_items
|
||||
WHERE embedding MATCH ?
|
||||
ORDER BY distance
|
||||
LIMIT 5
|
||||
SQL
|
||||
|
||||
puts rows
|
||||
Loading…
Add table
Add a link
Reference in a new issue