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
6
examples/simple-ruby/Gemfile
Normal file
6
examples/simple-ruby/Gemfile
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
source 'https://rubygems.org'
|
||||
|
||||
ruby '>= 3.0'
|
||||
|
||||
gem 'sqlite3', '~> 2.0', '>= 2.0.1'
|
||||
gem 'sqlite-vec'
|
||||
18
examples/simple-ruby/Gemfile.lock
Normal file
18
examples/simple-ruby/Gemfile.lock
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
sqlite-vec (0.0.1.alpha.3-x86_64-darwin)
|
||||
sqlite3 (2.0.1-x86_64-darwin)
|
||||
|
||||
PLATFORMS
|
||||
x86_64-darwin-21
|
||||
|
||||
DEPENDENCIES
|
||||
sqlite-vec
|
||||
sqlite3 (~> 2.0, >= 2.0.1)
|
||||
|
||||
RUBY VERSION
|
||||
ruby 3.2.2p53
|
||||
|
||||
BUNDLED WITH
|
||||
2.4.10
|
||||
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