pinecone 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pinecone/vector/filter.rb +71 -0
- data/lib/pinecone/vector/query.rb +62 -0
- data/lib/pinecone/vector/sparse_vector.rb +40 -0
- data/lib/pinecone/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e278d71fc695cd9e6f0297cc0dd5e8fb6ec144cd1e599d394bb34f25dbcb4783
|
4
|
+
data.tar.gz: ecb5b591b5a8b3564c5b2790fbadf99f253925d60e25934f9fbcfec67a09b919
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b09cd21a242d82794ed5f6af15f9bd0f190d872f48a9ddce66e80eea9c9bdce191c09185d6e9d62058ca01d4ee32a98b2c4ee10460a4e726261eec1093d9da6
|
7
|
+
data.tar.gz: c3254d2c223308f0a0c56bb0c4b4f08fae0a807682f1ab502b7a622ebdea63a2cd719a691a1589d61888485938e5750fd4ea7d9b4e003d95d2115e9543e97a6d
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry-struct"
|
4
|
+
require "dry-validation"
|
5
|
+
|
6
|
+
module Types
|
7
|
+
include Dry.Types()
|
8
|
+
|
9
|
+
StringOrNumberOrBoolean = Dry::Types['string'] | Dry::Types['integer'] | Dry::Types['float'] | Dry::Types['bool']
|
10
|
+
StringOrNumber = Dry::Types['string'] | Dry::Types['integer'] | Dry::Types['float']
|
11
|
+
Number = Dry::Types['integer'] | Dry::Types['float']
|
12
|
+
end
|
13
|
+
|
14
|
+
module Pinecone
|
15
|
+
class Vector
|
16
|
+
class Filter < Dry::Struct
|
17
|
+
class FilterContract < Dry::Validation::Contract
|
18
|
+
schema do
|
19
|
+
optional(:$and).filled(:array)
|
20
|
+
optional(:$or).filled(:array)
|
21
|
+
optional(:$eq).filled(Types::StringOrNumberOrBoolean)
|
22
|
+
optional(:$ne).filled(Types::StringOrNumberOrBoolean)
|
23
|
+
optional(:$gt).filled(Types::Number)
|
24
|
+
optional(:$gte).filled(Types::Number)
|
25
|
+
optional(:$lt).filled(Types::Number)
|
26
|
+
optional(:$lte).filled(Types::Number)
|
27
|
+
optional(:$in).filled(:array).each(Types::StringOrNumber)
|
28
|
+
optional(:$nin).filled(:array).each(Types::StringOrNumber)
|
29
|
+
end
|
30
|
+
|
31
|
+
rule(:$and) do
|
32
|
+
if key?
|
33
|
+
key(:$and).failure("'$any' must be an array") unless value.is_a?(Array)
|
34
|
+
|
35
|
+
value.each do |v|
|
36
|
+
key(:$and).failure("'$any' must be an array of filters") unless v.is_a?(Filter) || to_filter(v).is_a?(Filter)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
rule(:$or) do
|
42
|
+
if key?
|
43
|
+
key(:$or).failure("'$or' must be an array") unless value.is_a?(Array)
|
44
|
+
|
45
|
+
value.each do |v|
|
46
|
+
key(:$or).failure("'$or' must be an array of filters") unless v.is_a?(Filter) || to_filter(v).is_a?(Filter)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_filter(input)
|
52
|
+
return false unless input.is_a?(Hash)
|
53
|
+
return Filter.new(input)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.new(input)
|
58
|
+
validation = FilterContract.new.call(input)
|
59
|
+
if validation.success?
|
60
|
+
super(input)
|
61
|
+
else
|
62
|
+
raise ArgumentError.new(validation.errors.to_h.inspect)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.default?
|
67
|
+
nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry-struct"
|
4
|
+
require "dry-validation"
|
5
|
+
|
6
|
+
require "pinecone/vector/filter"
|
7
|
+
require "pinecone/vector/sparse_vector"
|
8
|
+
|
9
|
+
module Types
|
10
|
+
include Dry.Types()
|
11
|
+
end
|
12
|
+
|
13
|
+
module Pinecone
|
14
|
+
class Vector
|
15
|
+
class Query < Dry::Struct
|
16
|
+
class QueryContract < Dry::Validation::Contract
|
17
|
+
params do
|
18
|
+
optional(:vector).filled(:array)
|
19
|
+
optional(:id).filled(:string)
|
20
|
+
end
|
21
|
+
|
22
|
+
rule(:vector, :id) do
|
23
|
+
if !values[:vector].nil? && !values[:id].nil?
|
24
|
+
key(:vector).failure("Only one of vector or id can be specified")
|
25
|
+
key(:id).failure("Only one of vector or id can be specified")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
schema schema.strict
|
31
|
+
|
32
|
+
attribute :namespace, Dry::Types['string'].default("")
|
33
|
+
attribute :include_values, Dry::Types['bool'].default(false)
|
34
|
+
attribute :include_metadata, Dry::Types['bool'].default(true)
|
35
|
+
attribute :top_k, Dry::Types['integer'].default(10)
|
36
|
+
attribute? :vector, Dry::Types['array'].of(Dry::Types['float'] | Dry::Types['integer'])
|
37
|
+
# Disabled contract since it wasn't carrying forward attributes to to_json
|
38
|
+
# See failing test in query_spec.rb
|
39
|
+
# attribute? :filter, Filter
|
40
|
+
attribute? :filter, Dry::Types['hash']
|
41
|
+
attribute? :sparse_vector, SparseVector
|
42
|
+
attribute? :id, Dry::Types['string']
|
43
|
+
|
44
|
+
def self.new(input)
|
45
|
+
validation = QueryContract.new.call(input)
|
46
|
+
if validation.success?
|
47
|
+
super(input)
|
48
|
+
else
|
49
|
+
raise ArgumentError.new(validation.errors.to_h.inspect)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_json
|
54
|
+
to_h.map do |key, value|
|
55
|
+
[key.to_s.split("_").map.with_index do |word, index|
|
56
|
+
index == 0 ? word : word.capitalize
|
57
|
+
end.join.to_sym, value]
|
58
|
+
end.to_h.to_json
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry-struct"
|
4
|
+
require "dry-validation"
|
5
|
+
|
6
|
+
module Types
|
7
|
+
include Dry.Types()
|
8
|
+
end
|
9
|
+
|
10
|
+
module Pinecone
|
11
|
+
class Vector
|
12
|
+
class SparseVector < Dry::Struct
|
13
|
+
class SparseVectorContract < Dry::Validation::Contract
|
14
|
+
params do
|
15
|
+
required(:indices).filled(:array)
|
16
|
+
required(:values).filled(:array)
|
17
|
+
end
|
18
|
+
|
19
|
+
rule(:indices, :values) do
|
20
|
+
unless values[:indices].size === values[:values].size
|
21
|
+
key(:indices).failure("Indices and values must be the same size")
|
22
|
+
key(:values).failure("Indices and values must be the same size")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
attribute :indices, Dry::Types['array'].of(Dry::Types['integer'])
|
28
|
+
attribute :values, Dry::Types['array'].of(Dry::Types['float'] | Dry::Types['integer'])
|
29
|
+
|
30
|
+
def self.new(input)
|
31
|
+
validation = SparseVectorContract.new.call(input)
|
32
|
+
if validation.success?
|
33
|
+
super(input)
|
34
|
+
else
|
35
|
+
raise ArgumentError.new(validation.errors.to_h.inspect)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/pinecone/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pinecone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Carleton
|
@@ -162,6 +162,9 @@ files:
|
|
162
162
|
- lib/pinecone/collection.rb
|
163
163
|
- lib/pinecone/index.rb
|
164
164
|
- lib/pinecone/vector.rb
|
165
|
+
- lib/pinecone/vector/filter.rb
|
166
|
+
- lib/pinecone/vector/query.rb
|
167
|
+
- lib/pinecone/vector/sparse_vector.rb
|
165
168
|
- lib/pinecone/version.rb
|
166
169
|
homepage: https://rubygems.org/gems/pinecone
|
167
170
|
licenses:
|