voyageai 1.3.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +78 -0
- data/lib/voyageai/client.rb +5 -3
- data/lib/voyageai/model.rb +1 -0
- data/lib/voyageai/version.rb +1 -1
- metadata +3 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 651f676fcb2fa068cf44f37fda02d1185b776ca83fc0f6099362d5e4deb7a250
|
4
|
+
data.tar.gz: f3a22402be82fd02ec94628aeab067aec45cfcc6c203f7d75af13a00f95b580b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b70798b6ee15febae3f263def8f118b4177f73e553bf3463e77f2638259b62d95025dc6336049eedec71fdccdac80512e54c55bd65b70333fc3f29518d97367
|
7
|
+
data.tar.gz: 2aa09dc2ea94771f309af4e40aa4340842b1181e1e82ddc270b75bddd5120044d0dd195bf687e302cf270da12c312261dc6a523e5b69a0184bacf0aafbb10da1
|
data/README.md
CHANGED
@@ -82,3 +82,81 @@ VoyageAI.configure do |config|
|
|
82
82
|
config.logger = Logger.new(STDOUT)
|
83
83
|
end
|
84
84
|
```
|
85
|
+
|
86
|
+
## Examples
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
require "voyageai"
|
90
|
+
|
91
|
+
Entry = Data.define(:document, :embedding)
|
92
|
+
|
93
|
+
VOYAGEAI = VoyageAI::Client.new
|
94
|
+
|
95
|
+
DOCUMENTS = [
|
96
|
+
"John is a musician.",
|
97
|
+
"Paul is a plumber.",
|
98
|
+
"George is a teacher.",
|
99
|
+
"Ringo is a doctor.",
|
100
|
+
"Lisa is a lawyer.",
|
101
|
+
"Stuart is a painter.",
|
102
|
+
"Brian is a writer.",
|
103
|
+
"Jane is a chef.",
|
104
|
+
"Bill is a nurse.",
|
105
|
+
"Susan is a carpenter.",
|
106
|
+
].freeze
|
107
|
+
|
108
|
+
embeddings = VOYAGEAI.embed(DOCUMENTS, input_type: "document").embeddings
|
109
|
+
|
110
|
+
ENTRIES = DOCUMENTS.zip(embeddings).map do |document, embedding|
|
111
|
+
Entry.new(document:, embedding:)
|
112
|
+
end
|
113
|
+
|
114
|
+
# @param src [Array<Float>]
|
115
|
+
# @param dst [Array<Float>]
|
116
|
+
#
|
117
|
+
# @return [Float]
|
118
|
+
def euclidean_distance(src, dst)
|
119
|
+
Math.sqrt(src.zip(dst).map { |a, b| (a - b)**2 }.reduce(:+))
|
120
|
+
end
|
121
|
+
|
122
|
+
# @param query [String]
|
123
|
+
# @param limit [Integer]
|
124
|
+
#
|
125
|
+
# @return [Array<String>]
|
126
|
+
def nearest_documents(query:, limit: 4)
|
127
|
+
embedding = VOYAGEAI.embed(query, input_type: "query").embedding
|
128
|
+
|
129
|
+
ENTRIES
|
130
|
+
.sort_by { |entry| euclidean_distance(entry.embedding, embedding) }
|
131
|
+
.first(limit)
|
132
|
+
.map(&:document)
|
133
|
+
end
|
134
|
+
|
135
|
+
# @param query [String]
|
136
|
+
def search(query:)
|
137
|
+
documents = nearest_documents(query:)
|
138
|
+
|
139
|
+
results = VOYAGEAI.rerank(query:, documents:, top_k: 2).results
|
140
|
+
|
141
|
+
puts "query=#{query.inspect}"
|
142
|
+
results.each do |reranking|
|
143
|
+
document = documents[reranking.index]
|
144
|
+
puts("document=#{document.inspect} relevance_score=#{reranking.relevance_score}")
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
search(query: "What do George and Ringo do?")
|
149
|
+
search(query: "Who works in the medical field?")
|
150
|
+
```
|
151
|
+
|
152
|
+
```
|
153
|
+
query="What do George and Ringo do?"
|
154
|
+
document="Ringo is a doctor." relevance_score=0.67968755
|
155
|
+
document="George is a teacher." relevance_score=0.58593755
|
156
|
+
```
|
157
|
+
|
158
|
+
```
|
159
|
+
query="Who works in the medical field?"
|
160
|
+
document="Bill is a nurse." relevance_score=0.55078125
|
161
|
+
document="Ringo is a doctor." relevance_score=0.50390625
|
162
|
+
```
|
data/lib/voyageai/client.rb
CHANGED
@@ -37,12 +37,14 @@ module VoyageAI
|
|
37
37
|
"#<#{self.class.name} api_key=#{masked_api_key.inspect} host=#{@host.inspect} version=#{@version.inspect}>"
|
38
38
|
end
|
39
39
|
|
40
|
-
# @param model [String] optional (e.g. VoyageAI::Model::VOYAGE or "voyage-3")
|
41
40
|
# @param input [String, Array<String>] required (e.g. "Sample" or ["Sample 1", "Sample 2", ...])
|
41
|
+
# @param model [String] optional (e.g. VoyageAI::Model::VOYAGE or "voyage-3")
|
42
|
+
# @param input_type [Symbol] optional (e.g. :query or :document)
|
43
|
+
# @param truncation [Boolean] optional
|
42
44
|
#
|
43
45
|
# @return [Embedding]
|
44
|
-
def embed(input, model: Model::VOYAGE)
|
45
|
-
payload = { input: arrayify(input), model: model }
|
46
|
+
def embed(input, model: Model::VOYAGE, input_type: nil, truncation: nil)
|
47
|
+
payload = { input: arrayify(input), model: model, truncation:, input_type: }.compact
|
46
48
|
response = http.accept(:json).post("/#{@version}/embeddings", json: payload)
|
47
49
|
|
48
50
|
raise RequestError.new(response:) unless response.status.ok?
|
data/lib/voyageai/model.rb
CHANGED
data/lib/voyageai/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voyageai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-01-08 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: http
|
@@ -66,7 +65,6 @@ metadata:
|
|
66
65
|
homepage_uri: https://github.com/ksylvest/voyageai
|
67
66
|
changelog_uri: https://github.com/ksylvest/voyageai/releases
|
68
67
|
rubygems_mfa_required: 'true'
|
69
|
-
post_install_message:
|
70
68
|
rdoc_options: []
|
71
69
|
require_paths:
|
72
70
|
- lib
|
@@ -81,8 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
79
|
- !ruby/object:Gem::Version
|
82
80
|
version: '0'
|
83
81
|
requirements: []
|
84
|
-
rubygems_version: 3.
|
85
|
-
signing_key:
|
82
|
+
rubygems_version: 3.6.2
|
86
83
|
specification_version: 4
|
87
84
|
summary: A client for voyageai.com.
|
88
85
|
test_files: []
|