2024-06-11 23:58:00 -07:00
|
|
|
# Using `sqlite-vec` in Ruby
|
|
|
|
|
|
2024-07-31 12:55:03 -07:00
|
|
|

|
|
|
|
|
|
|
|
|
|
Ruby developers can use `sqlite-vec` with the [`sqlite-vec` Gem](https://rubygems.org/gems/sqlite-vec).
|
|
|
|
|
|
2024-06-22 16:46:33 -07:00
|
|
|
|
2024-06-11 23:58:00 -07:00
|
|
|
```bash
|
|
|
|
|
gem install sqlite-vec
|
|
|
|
|
```
|
|
|
|
|
|
2024-08-01 02:45:36 -07:00
|
|
|
You can then use `SqliteVec.load()` to load `sqlite-vec` SQL functions in a given SQLite connection.
|
2024-07-31 12:55:03 -07:00
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2024-08-01 02:45:36 -07:00
|
|
|
See
|
|
|
|
|
[`simple-ruby/demo.rb`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-ruby/demo.rb)
|
|
|
|
|
for a more complete Ruby demo.
|
2024-07-31 12:55:03 -07:00
|
|
|
|
2024-06-11 23:58:00 -07:00
|
|
|
## Working with vectors in Ruby
|
2024-07-31 12:55:03 -07:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
|
embedding = [0.1, 0.2, 0.3, 0.4]
|
|
|
|
|
result = db.execute("SELECT vec_length(?)", [query.pack("f*")]])
|
|
|
|
|
puts result.first.first # 4
|
|
|
|
|
```
|