Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 1.25 KB

File metadata and controls

45 lines (31 loc) · 1.25 KB

Using sqlite-vec in Ruby

Gem

Ruby developers can use sqlite-vec with the sqlite-vec Gem.

gem install sqlite-vec

You can then use SqliteVec.load() to load sqlite-vec SQL functions in a given SQLite connection.

require 'sqlite3'
require 'sqlite_vec'

db = SQLite3::Database.new(':memory:')
db.enable_load_extension(true)
SqliteVec.load(db)
db.enable_load_extension(false)

result = db.execute('SELECT vec_version()')
puts result.first.first

See simple-ruby/demo.rb for a more complete Ruby demo.

Working with vectors in Ruby

If your embeddings are provided as a list of numbers, use .pack("f*") to convert them into the compact BLOB format that sqlite-vec uses.

embedding = [0.1, 0.2, 0.3, 0.4]
result = db.execute("SELECT vec_length(?)", [query.pack("f*")])
puts result.first.first # 4

# Or, if using Active Record:
embedding_blob = embedding.pack("f*").unpack1('H*') # Hex string representation
result = ActiveRecord::Base.connection.execute("SELECT vec_length(x'#{embedding_blob}')")
result.first.first.last