zvec-ruby 0.1.0-arm64-darwin
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +21 -0
- data/LICENSE +190 -0
- data/README.md +189 -0
- data/Rakefile +83 -0
- data/examples/basic.rb +63 -0
- data/examples/with_ruby_llm.rb +79 -0
- data/lib/zvec/3.4/zvec_ext.bundle +0 -0
- data/lib/zvec/active_record.rb +113 -0
- data/lib/zvec/collection.rb +165 -0
- data/lib/zvec/data_types.rb +63 -0
- data/lib/zvec/doc.rb +107 -0
- data/lib/zvec/query.rb +28 -0
- data/lib/zvec/ruby_llm.rb +108 -0
- data/lib/zvec/schema.rb +70 -0
- data/lib/zvec/version.rb +3 -0
- data/lib/zvec.rb +22 -0
- data/test/test_active_record.rb +55 -0
- data/test/test_collection.rb +312 -0
- data/test/test_data_types.rb +165 -0
- data/test/test_doc.rb +271 -0
- data/test/test_ext_bindings.rb +313 -0
- data/test/test_helper.rb +170 -0
- data/test/test_query.rb +64 -0
- data/test/test_ruby_llm_store.rb +166 -0
- data/test/test_schema.rb +133 -0
- data/test/test_version.rb +19 -0
- data/zvec.gemspec +43 -0
- metadata +113 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 754211611493745c6264cd16852224cc98fcb1e7a03876b611856e7bfdbd0cd5
|
|
4
|
+
data.tar.gz: a8d64e325f72c290a9f124b7ae1debfa8ee4699db478ece033873a30abfa44a2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 969a7a8ba45cc0fe0beca62b7ae92a8b7dd5a1d74fe6a2d469001367ebf131516abc85fadce8a3dc64ee6b1237d66bf3b208571367fc64b2cd876f0258921fd4
|
|
7
|
+
data.tar.gz: efdb348a90b11c2340a62138346a4b5b19ac732170289a3439101bbe4ba6ccde8d76272c92d33626be7463f9cc385b817f7479dff55954b86e915b3a16665a14
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2026-03-06
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- Initial release
|
|
12
|
+
- Rice-based C++ bindings for zvec vector database
|
|
13
|
+
- Ruby DSL for schema definition (`Zvec::Schema`)
|
|
14
|
+
- High-level `Zvec::Collection` with `add`, `search`, `query`, `fetch`, `delete`
|
|
15
|
+
- `Zvec::Doc` with typed getters/setters and hash-like access
|
|
16
|
+
- `Zvec::VectorQuery` for configuring vector similarity searches
|
|
17
|
+
- Support for HNSW, Flat, and IVF index types
|
|
18
|
+
- Support for L2, IP, and Cosine distance metrics
|
|
19
|
+
- `Zvec::RubyLLM::Store` integration for ruby_llm gem
|
|
20
|
+
- `Zvec::ActiveRecord::Vectorize` concern for Rails models
|
|
21
|
+
- Data type constants for all scalar, vector, sparse vector, and array types
|
data/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2025 zvec-ruby contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# zvec-ruby
|
|
2
|
+
|
|
3
|
+
Ruby bindings for [zvec](https://github.com/alibaba/zvec), a high-performance C++ vector database from Alibaba. Built with [Rice](https://github.com/jasonroelofs/rice) (Ruby's C++ binding library).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### Precompiled (recommended)
|
|
8
|
+
|
|
9
|
+
Precompiled native gems are available for:
|
|
10
|
+
|
|
11
|
+
| Platform | Architectures |
|
|
12
|
+
|---|---|
|
|
13
|
+
| Linux | x86_64, aarch64 |
|
|
14
|
+
| macOS | x86_64 (Intel), arm64 (Apple Silicon) |
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
# Gemfile
|
|
18
|
+
gem "zvec"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
gem install zvec
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
No compiler or build tools needed — the gem ships with the native extension and all zvec dependencies statically linked.
|
|
26
|
+
|
|
27
|
+
### From source
|
|
28
|
+
|
|
29
|
+
If no precompiled gem is available for your platform, you'll need to build zvec first:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# 1. Build zvec from source
|
|
33
|
+
git clone --depth 1 https://github.com/alibaba/zvec /tmp/zvec
|
|
34
|
+
cd /tmp/zvec && mkdir build && cd build
|
|
35
|
+
cmake .. -DCMAKE_BUILD_TYPE=Release
|
|
36
|
+
make -j$(nproc)
|
|
37
|
+
|
|
38
|
+
# 2. Install the gem with ZVEC_DIR pointing to the build
|
|
39
|
+
ZVEC_DIR=/tmp/zvec gem install zvec
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or using the included helper script:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
git clone https://github.com/johannesdwicahyo/zvec-ruby
|
|
46
|
+
cd zvec-ruby
|
|
47
|
+
./script/build_zvec.sh
|
|
48
|
+
ZVEC_DIR=/tmp/zvec bundle install && bundle exec rake compile
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Quick Start
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
require "zvec"
|
|
55
|
+
|
|
56
|
+
# Define a schema
|
|
57
|
+
schema = Zvec::Schema.new("my_collection") do
|
|
58
|
+
string "title"
|
|
59
|
+
int32 "year"
|
|
60
|
+
vector "embedding", dimension: 128,
|
|
61
|
+
index: Zvec::Ext::HnswIndexParams.new(Zvec::COSINE)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Create and populate a collection
|
|
65
|
+
collection = Zvec::Collection.create_and_open("/tmp/my_vectors", schema)
|
|
66
|
+
|
|
67
|
+
collection.add(pk: "doc1", title: "Hello", year: 2025,
|
|
68
|
+
embedding: Array.new(128) { rand })
|
|
69
|
+
|
|
70
|
+
# Search
|
|
71
|
+
results = collection.search([0.1] * 128, top_k: 5)
|
|
72
|
+
results.each { |doc| puts "#{doc.pk}: #{doc.score}" }
|
|
73
|
+
|
|
74
|
+
# Clean up
|
|
75
|
+
collection.destroy
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Schema DSL
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
schema = Zvec::Schema.new("name") do
|
|
82
|
+
string "title"
|
|
83
|
+
int32 "count"
|
|
84
|
+
int64 "big_count"
|
|
85
|
+
float "score"
|
|
86
|
+
double "precise_score"
|
|
87
|
+
bool "active"
|
|
88
|
+
vector "embedding", dimension: 1536
|
|
89
|
+
field "tags", Zvec::ARRAY_STRING
|
|
90
|
+
end
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Index Types
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
# HNSW (default for vector search)
|
|
97
|
+
Zvec::Ext::HnswIndexParams.new(Zvec::COSINE, m: 16, ef_construction: 200)
|
|
98
|
+
|
|
99
|
+
# Flat (brute-force, exact)
|
|
100
|
+
Zvec::Ext::FlatIndexParams.new(Zvec::L2)
|
|
101
|
+
|
|
102
|
+
# IVF (inverted file index)
|
|
103
|
+
Zvec::Ext::IVFIndexParams.new(Zvec::IP, n_list: 1024)
|
|
104
|
+
|
|
105
|
+
# Inverted index (for scalar fields)
|
|
106
|
+
Zvec::Ext::InvertIndexParams.new
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Collection API
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
# CRUD
|
|
113
|
+
collection.add(pk: "id", title: "text", embedding: [...])
|
|
114
|
+
collection.upsert(doc)
|
|
115
|
+
collection.delete("id1", "id2")
|
|
116
|
+
collection.fetch("id1")
|
|
117
|
+
|
|
118
|
+
# Search
|
|
119
|
+
collection.search(vector, top_k: 10, filter: "year > 2024")
|
|
120
|
+
|
|
121
|
+
# Full query control
|
|
122
|
+
collection.query(
|
|
123
|
+
field_name: "embedding",
|
|
124
|
+
vector: [...],
|
|
125
|
+
topk: 10,
|
|
126
|
+
filter: "category='tech'",
|
|
127
|
+
include_vector: true,
|
|
128
|
+
output_fields: ["title", "url"]
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
# Management
|
|
132
|
+
collection.flush
|
|
133
|
+
collection.optimize
|
|
134
|
+
collection.doc_count
|
|
135
|
+
collection.destroy
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## ruby_llm Integration
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
require "zvec/ruby_llm"
|
|
142
|
+
|
|
143
|
+
store = Zvec::RubyLLM::Store.new("/tmp/vectors", dimension: 1536, metric: :cosine)
|
|
144
|
+
|
|
145
|
+
store.add("doc-1", embedding: [...], content: "Some text")
|
|
146
|
+
store.search(query_embedding, top_k: 5)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## ActiveRecord Integration
|
|
150
|
+
|
|
151
|
+
```ruby
|
|
152
|
+
require "zvec/active_record"
|
|
153
|
+
|
|
154
|
+
class Article < ApplicationRecord
|
|
155
|
+
include Zvec::ActiveRecord::Vectorize
|
|
156
|
+
|
|
157
|
+
vectorize :content,
|
|
158
|
+
dimensions: 1536,
|
|
159
|
+
embed_with: ->(text) { MyEmbedder.embed(text) }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Automatic embedding on save
|
|
163
|
+
article = Article.create!(title: "Hello", content: "World")
|
|
164
|
+
|
|
165
|
+
# Vector similarity search
|
|
166
|
+
Article.vector_search("similar articles", top_k: 5)
|
|
167
|
+
Article.vector_search([0.1, 0.2, ...], top_k: 10, embed: false)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Development
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
# Build zvec
|
|
174
|
+
./script/build_zvec.sh
|
|
175
|
+
|
|
176
|
+
# Compile and test
|
|
177
|
+
ZVEC_DIR=/tmp/zvec bundle exec rake compile
|
|
178
|
+
ZVEC_DIR=/tmp/zvec bundle exec rake test
|
|
179
|
+
|
|
180
|
+
# Run pure Ruby tests only (no native extension needed)
|
|
181
|
+
bundle exec rake test_pure
|
|
182
|
+
|
|
183
|
+
# Package a native gem for your platform
|
|
184
|
+
ruby script/package_native_gem.rb
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## License
|
|
188
|
+
|
|
189
|
+
Apache-2.0 (same as zvec)
|
data/Rakefile
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require "rake/extensiontask"
|
|
2
|
+
require "rake/testtask"
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
|
|
5
|
+
GEMSPEC = Gem::Specification.load("zvec.gemspec")
|
|
6
|
+
|
|
7
|
+
CROSS_RUBIES = %w[3.1.0 3.2.0 3.3.0 3.4.0]
|
|
8
|
+
CROSS_PLATFORMS = %w[
|
|
9
|
+
x86_64-linux
|
|
10
|
+
aarch64-linux
|
|
11
|
+
x86_64-darwin
|
|
12
|
+
arm64-darwin
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
Rake::ExtensionTask.new("zvec_ext", GEMSPEC) do |ext|
|
|
16
|
+
ext.lib_dir = "lib/zvec"
|
|
17
|
+
ext.ext_dir = "ext/zvec"
|
|
18
|
+
ext.cross_compile = true
|
|
19
|
+
ext.cross_platform = CROSS_PLATFORMS
|
|
20
|
+
ext.cross_compiling do |spec|
|
|
21
|
+
# Precompiled gems don't need ext/ sources or rice dependency
|
|
22
|
+
spec.files.reject! { |f| f.start_with?("ext/") }
|
|
23
|
+
spec.extensions.clear
|
|
24
|
+
spec.dependencies.reject! { |d| d.name == "rice" }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
Rake::TestTask.new(:test) do |t|
|
|
29
|
+
t.libs << "test" << "lib"
|
|
30
|
+
t.pattern = "test/test_*.rb"
|
|
31
|
+
t.warning = true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
desc "Run pure Ruby tests (no native extension required)"
|
|
35
|
+
Rake::TestTask.new(:test_pure) do |t|
|
|
36
|
+
t.libs << "test" << "lib"
|
|
37
|
+
t.test_files = FileList[
|
|
38
|
+
"test/test_version.rb",
|
|
39
|
+
"test/test_data_types.rb",
|
|
40
|
+
"test/test_schema.rb",
|
|
41
|
+
"test/test_doc.rb",
|
|
42
|
+
"test/test_query.rb",
|
|
43
|
+
"test/test_active_record.rb",
|
|
44
|
+
]
|
|
45
|
+
t.warning = true
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
namespace :gem do
|
|
49
|
+
desc "Build native gem for the current platform"
|
|
50
|
+
task :native do
|
|
51
|
+
platform = Gem::Platform.local.to_s
|
|
52
|
+
sh "gem build zvec.gemspec"
|
|
53
|
+
# Repackage with the precompiled .so/.bundle
|
|
54
|
+
puts "\nTo build a platform-specific gem, use:"
|
|
55
|
+
puts " rake native gem RUBY_CC_VERSION=#{CROSS_RUBIES.join(':')}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
desc "Build precompiled gems for all platforms using rake-compiler-dock"
|
|
59
|
+
task :precompile do
|
|
60
|
+
require "rake_compiler_dock"
|
|
61
|
+
|
|
62
|
+
CROSS_PLATFORMS.each do |plat|
|
|
63
|
+
RakeCompilerDock.sh <<~SCRIPT, platform: plat
|
|
64
|
+
set -e
|
|
65
|
+
# Build zvec inside the container
|
|
66
|
+
git clone --depth 1 https://github.com/alibaba/zvec /tmp/zvec
|
|
67
|
+
cd /tmp/zvec && mkdir build && cd build
|
|
68
|
+
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local
|
|
69
|
+
make -j$(nproc)
|
|
70
|
+
make install
|
|
71
|
+
ldconfig 2>/dev/null || true
|
|
72
|
+
|
|
73
|
+
# Build the native gem
|
|
74
|
+
cd /payload
|
|
75
|
+
export ZVEC_DIR=/tmp/zvec
|
|
76
|
+
bundle install
|
|
77
|
+
rake native:#{plat} gem RUBY_CC_VERSION=#{CROSS_RUBIES.join(':')}
|
|
78
|
+
SCRIPT
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
task default: :compile
|
data/examples/basic.rb
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
require "zvec"
|
|
3
|
+
|
|
4
|
+
# Define schema with a vector field and scalar fields
|
|
5
|
+
schema = Zvec::Schema.new("my_docs") do
|
|
6
|
+
string "title"
|
|
7
|
+
string "category", nullable: true
|
|
8
|
+
int32 "year"
|
|
9
|
+
vector "embedding", dimension: 4,
|
|
10
|
+
index: Zvec::Ext::HnswIndexParams.new(Zvec::COSINE)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Create collection
|
|
14
|
+
path = "/tmp/zvec_ruby_example"
|
|
15
|
+
FileUtils.rm_rf(path) if Dir.exist?(path)
|
|
16
|
+
|
|
17
|
+
collection = Zvec::Collection.create_and_open(path, schema)
|
|
18
|
+
|
|
19
|
+
# Insert documents
|
|
20
|
+
docs = [
|
|
21
|
+
{ pk: "doc1", title: "Ruby Gems", category: "programming", year: 2024,
|
|
22
|
+
embedding: [0.1, 0.2, 0.3, 0.4] },
|
|
23
|
+
{ pk: "doc2", title: "Vector Databases", category: "databases", year: 2025,
|
|
24
|
+
embedding: [0.4, 0.3, 0.2, 0.1] },
|
|
25
|
+
{ pk: "doc3", title: "Machine Learning", category: "ai", year: 2025,
|
|
26
|
+
embedding: [0.2, 0.4, 0.1, 0.3] },
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
docs.each do |d|
|
|
30
|
+
collection.add(pk: d[:pk], **d.except(:pk))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
puts "Inserted #{collection.doc_count} documents"
|
|
34
|
+
|
|
35
|
+
# Search
|
|
36
|
+
query_vector = [0.1, 0.2, 0.3, 0.4]
|
|
37
|
+
results = collection.search(query_vector, top_k: 2)
|
|
38
|
+
|
|
39
|
+
puts "\nSearch results (top 2):"
|
|
40
|
+
results.each do |doc|
|
|
41
|
+
puts " #{doc.pk}: score=#{doc.score}, title=#{doc["title"]}"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Fetch by ID
|
|
45
|
+
fetched = collection.fetch("doc2")
|
|
46
|
+
puts "\nFetched doc2: #{fetched["doc2"]&.to_h}"
|
|
47
|
+
|
|
48
|
+
# Filter query
|
|
49
|
+
results = collection.query(
|
|
50
|
+
field_name: "embedding",
|
|
51
|
+
vector: query_vector,
|
|
52
|
+
topk: 10,
|
|
53
|
+
filter: "year == 2025"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
puts "\nFiltered results (year=2025):"
|
|
57
|
+
results.each do |doc|
|
|
58
|
+
puts " #{doc.pk}: #{doc["title"]}"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Cleanup
|
|
62
|
+
collection.destroy
|
|
63
|
+
puts "\nDone!"
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
require "zvec/ruby_llm"
|
|
3
|
+
# require "ruby_llm" # Uncomment when ruby_llm is installed
|
|
4
|
+
|
|
5
|
+
# This example shows how to use Zvec as a vector store with the ruby_llm gem.
|
|
6
|
+
#
|
|
7
|
+
# Prerequisites:
|
|
8
|
+
# gem install ruby_llm
|
|
9
|
+
# export OPENAI_API_KEY=your_key
|
|
10
|
+
#
|
|
11
|
+
# The Zvec::RubyLLM::Store provides a simple interface for storing and
|
|
12
|
+
# retrieving embeddings that can be used with any LLM framework.
|
|
13
|
+
|
|
14
|
+
DIMENSION = 4 # Use 1536 for OpenAI text-embedding-3-small
|
|
15
|
+
|
|
16
|
+
# Create a vector store
|
|
17
|
+
store = Zvec::RubyLLM::Store.new(
|
|
18
|
+
"/tmp/zvec_ruby_llm_example",
|
|
19
|
+
dimension: DIMENSION,
|
|
20
|
+
metric: :cosine
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# --- Option A: Manual embeddings (for demo without API keys) ---
|
|
24
|
+
|
|
25
|
+
store.add("chunk-1",
|
|
26
|
+
embedding: [0.1, 0.2, 0.3, 0.4],
|
|
27
|
+
content: "Ruby is a dynamic, open source programming language."
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
store.add("chunk-2",
|
|
31
|
+
embedding: [0.4, 0.3, 0.2, 0.1],
|
|
32
|
+
content: "Vector databases store and search high-dimensional vectors."
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
store.add_many([
|
|
36
|
+
{
|
|
37
|
+
id: "chunk-3",
|
|
38
|
+
embedding: [0.2, 0.4, 0.1, 0.3],
|
|
39
|
+
content: "Machine learning models generate embeddings from text."
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: "chunk-4",
|
|
43
|
+
embedding: [0.3, 0.1, 0.4, 0.2],
|
|
44
|
+
content: "Retrieval-augmented generation improves LLM responses."
|
|
45
|
+
}
|
|
46
|
+
])
|
|
47
|
+
|
|
48
|
+
store.flush
|
|
49
|
+
|
|
50
|
+
puts "Stored #{store.count} documents"
|
|
51
|
+
|
|
52
|
+
# Search
|
|
53
|
+
results = store.search([0.1, 0.2, 0.3, 0.4], top_k: 3)
|
|
54
|
+
|
|
55
|
+
puts "\nSearch results:"
|
|
56
|
+
results.each do |r|
|
|
57
|
+
puts " [#{r[:score].round(4)}] #{r[:id]}: #{r[:content]}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# --- Option B: With ruby_llm (uncomment below) ---
|
|
61
|
+
#
|
|
62
|
+
# RubyLLM.configure do |config|
|
|
63
|
+
# config.openai_api_key = ENV["OPENAI_API_KEY"]
|
|
64
|
+
# end
|
|
65
|
+
#
|
|
66
|
+
# # Use OpenAI to generate real embeddings
|
|
67
|
+
# embed = ->(text) { RubyLLM.embed(text).vectors.first }
|
|
68
|
+
#
|
|
69
|
+
# store.add("real-1",
|
|
70
|
+
# embedding: embed.call("The quick brown fox"),
|
|
71
|
+
# content: "The quick brown fox jumps over the lazy dog."
|
|
72
|
+
# )
|
|
73
|
+
#
|
|
74
|
+
# query_embedding = embed.call("fast animal")
|
|
75
|
+
# results = store.search(query_embedding, top_k: 5)
|
|
76
|
+
|
|
77
|
+
# Cleanup
|
|
78
|
+
store.collection.destroy
|
|
79
|
+
puts "\nDone!"
|
|
Binary file
|