voyageai 1.1.1 → 1.3.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 +40 -2
- data/lib/voyageai/client.rb +38 -8
- data/lib/voyageai/config.rb +57 -0
- data/lib/voyageai/embed.rb +1 -1
- data/lib/voyageai/instrumentation.rb +35 -0
- data/lib/voyageai/model.rb +6 -0
- data/lib/voyageai/rerank.rb +46 -0
- data/lib/voyageai/reranking.rb +46 -0
- data/lib/voyageai/version.rb +1 -1
- data/lib/voyageai.rb +10 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac2d6a908abb5d1383cdf74f736b5aa7eae9fc9a66634ec1fd40ce8710571763
|
4
|
+
data.tar.gz: 7df317f435acc60bde69219f7dc50e19952433dce8d4dee9ebda10a8a6b0ba46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af23bbd90f54d69d912223071a4a9ca3ba9d4128c10e7d5e60ce57632c54a244f4dc656ae6dd3a923d8b8273e1836ae670401a8de7de155350d1a1a5e2e24a1b
|
7
|
+
data.tar.gz: a778faecde1b31e4c7a9c8955019af8ec618076dff925b3d26dce0bceeaae054174f24cfeb120d4c076750fcc730679338014c50c5ffdfe47ee2bad9a10700a4
|
data/README.md
CHANGED
@@ -10,7 +10,9 @@ gem install voyageai
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
###
|
13
|
+
### Embedding
|
14
|
+
|
15
|
+
#### Generating Single Embedding
|
14
16
|
|
15
17
|
```ruby
|
16
18
|
require 'voyageai'
|
@@ -25,7 +27,7 @@ embed.usage # "#<VoyageAI::Usage total_tokens=...>"
|
|
25
27
|
embed.embedding # [0.0, ...]
|
26
28
|
```
|
27
29
|
|
28
|
-
|
30
|
+
#### Generating Multiple Embeddings
|
29
31
|
|
30
32
|
```ruby
|
31
33
|
require 'voyageai'
|
@@ -44,3 +46,39 @@ embed.model # "..."
|
|
44
46
|
embed.usage # "#<VoyageAI::Usage total_tokens=...>"
|
45
47
|
embed.embeddings # [[0.0, ...], ...]
|
46
48
|
```
|
49
|
+
|
50
|
+
### Reranking
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
require 'voyageai'
|
54
|
+
|
55
|
+
query = 'Who is the best person to call for a toilet?'
|
56
|
+
|
57
|
+
documents = [
|
58
|
+
'John is a musician.',
|
59
|
+
'Paul is a plumber.',
|
60
|
+
'George is a teacher.',
|
61
|
+
'Ringo is a doctor.',
|
62
|
+
]
|
63
|
+
|
64
|
+
voyageai = VoyageAI::Client.new(api_key: 'pa-...') # or configure ENV['VOYAGEAI_API_KEY']
|
65
|
+
|
66
|
+
rerank = voyageai.rerank(query:, documents:, top_k: 3)
|
67
|
+
rerank.model # "..."
|
68
|
+
rerank.usage # "#<VoyageAI::Usage total_tokens=...>"
|
69
|
+
rerank.results # [#<VoyageAI::Reranking index=0 relevance_score=0.5>]
|
70
|
+
```
|
71
|
+
|
72
|
+
## Configuration
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
require 'voyageai'
|
76
|
+
|
77
|
+
VoyageAI.configure do |config|
|
78
|
+
config.api_key = 'pa-...' # defaults to ENV['VOYAGEAI_API_KEY']
|
79
|
+
config.host = 'https://api.voyageai.com'
|
80
|
+
config.version = 'v1'
|
81
|
+
config.timeout = 15 # seconds
|
82
|
+
config.logger = Logger.new(STDOUT)
|
83
|
+
end
|
84
|
+
```
|
data/lib/voyageai/client.rb
CHANGED
@@ -3,8 +3,6 @@
|
|
3
3
|
module VoyageAI
|
4
4
|
# A client for interacting with the VoyageAI API.
|
5
5
|
class Client
|
6
|
-
BASE_URL = "https://api.voyageai.com/v1"
|
7
|
-
|
8
6
|
# An error raised for any HTTP issues.
|
9
7
|
class RequestError < StandardError
|
10
8
|
attr_accessor :response
|
@@ -17,15 +15,26 @@ module VoyageAI
|
|
17
15
|
end
|
18
16
|
|
19
17
|
# @param api_key [String] optional if ENV['VOYAGEAI_API_KEY'] otherwise required
|
20
|
-
|
18
|
+
# @param host [String] optional
|
19
|
+
# @param version [String] optional
|
20
|
+
# @param logger [Logger] optional
|
21
|
+
def initialize(
|
22
|
+
api_key: VoyageAI.config.api_key,
|
23
|
+
host: VoyageAI.config.host,
|
24
|
+
version: VoyageAI.config.version,
|
25
|
+
logger: VoyageAI.config.logger
|
26
|
+
)
|
21
27
|
@api_key = api_key || raise(ArgumentError, "api_key is required or ENV['VOYAGEAI_API_KEY'] must be present")
|
28
|
+
@host = host
|
29
|
+
@version = version
|
30
|
+
@logger = logger
|
22
31
|
end
|
23
32
|
|
24
33
|
# @return [String]
|
25
34
|
def inspect
|
26
35
|
masked_api_key = "#{@api_key[..4]}***"
|
27
36
|
|
28
|
-
"#<#{self.class.name} api_key=#{masked_api_key.inspect}>"
|
37
|
+
"#<#{self.class.name} api_key=#{masked_api_key.inspect} host=#{@host.inspect} version=#{@version.inspect}>"
|
29
38
|
end
|
30
39
|
|
31
40
|
# @param model [String] optional (e.g. VoyageAI::Model::VOYAGE or "voyage-3")
|
@@ -34,18 +43,39 @@ module VoyageAI
|
|
34
43
|
# @return [Embedding]
|
35
44
|
def embed(input, model: Model::VOYAGE)
|
36
45
|
payload = { input: arrayify(input), model: model }
|
37
|
-
response =
|
38
|
-
.accept(:json)
|
39
|
-
.auth("Bearer #{@api_key}")
|
40
|
-
.post("#{BASE_URL}/embeddings", json: payload)
|
46
|
+
response = http.accept(:json).post("/#{@version}/embeddings", json: payload)
|
41
47
|
|
42
48
|
raise RequestError.new(response:) unless response.status.ok?
|
43
49
|
|
44
50
|
Embed.parse(data: response.parse)
|
45
51
|
end
|
46
52
|
|
53
|
+
# @param query [String] required
|
54
|
+
# @param documents [Array<String>] required
|
55
|
+
# @param model [String] optional (e.g. VoyageAI::Model::RERANK or "rerank-2")
|
56
|
+
# @param top_k [Integer] optional
|
57
|
+
# @param truncation [Boolean] optional
|
58
|
+
def rerank(query:, documents:, model: Model::RERANK, top_k: nil, truncation: nil)
|
59
|
+
payload = { query:, documents:, model:, top_k:, truncation: }.compact
|
60
|
+
response = http.accept(:json).post("/#{@version}/rerank", json: payload)
|
61
|
+
|
62
|
+
raise RequestError.new(response:) unless response.status.ok?
|
63
|
+
|
64
|
+
Rerank.parse(data: response.parse)
|
65
|
+
end
|
66
|
+
|
47
67
|
private
|
48
68
|
|
69
|
+
# @return [HTTP::Client]
|
70
|
+
def http
|
71
|
+
@http ||= begin
|
72
|
+
http = HTTP.auth("Bearer #{@api_key}").persistent(@host)
|
73
|
+
http = http.use(instrumentation: { instrumenter: Instrumentation.new(logger: @logger) }) if @logger
|
74
|
+
http = http.timeout(@timeout) if @timeout
|
75
|
+
http
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
49
79
|
def arrayify(input)
|
50
80
|
input.is_a?(Array) ? input : [input]
|
51
81
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module VoyageAI
|
4
|
+
# A configuration for VoyageAI.
|
5
|
+
#
|
6
|
+
# @example:
|
7
|
+
# VoyageAI.configure do |config|
|
8
|
+
# config.api_key = '...'
|
9
|
+
# config.host = 'https://api.voyageai.com'
|
10
|
+
# config.version = 'v1'
|
11
|
+
# config.logger = Logger.new(STDOUT)
|
12
|
+
# config.timeout = 15
|
13
|
+
# end
|
14
|
+
class Config
|
15
|
+
DEFAULT_HOST = "https://api.voyageai.com"
|
16
|
+
DEFAULT_VERSION = "v1"
|
17
|
+
|
18
|
+
# @!attribute [rw] api_key
|
19
|
+
# @return [String, nil]
|
20
|
+
attr_accessor :api_key
|
21
|
+
|
22
|
+
# @!attribute [rw] host
|
23
|
+
# @return [String, nil]
|
24
|
+
attr_accessor :host
|
25
|
+
|
26
|
+
# @!attribute [rw] version
|
27
|
+
# @return [String, nil]
|
28
|
+
attr_accessor :version
|
29
|
+
|
30
|
+
# @!attribute [rw] logger
|
31
|
+
# @return [Logger, nil]
|
32
|
+
attr_accessor :logger
|
33
|
+
|
34
|
+
# @!attribute [rw] timeout
|
35
|
+
# @return [Integer, nil]
|
36
|
+
attr_accessor :timeout
|
37
|
+
|
38
|
+
# @param api_key [String] optional - defaults to `ENV['VOYAGE_API_KEY']`
|
39
|
+
# @param host [String] optional - defaults to `ENV['VOYAGEAI_HOST']`` w/ fallback to `https://api.voyageai.com`
|
40
|
+
# @param version [String] optional - defaults to `ENV['DEFAULT_VERSION']` w/ fallback to `v1`
|
41
|
+
# @param logger [Logger] optional
|
42
|
+
# @param timeout [Integer] optional
|
43
|
+
def initialize(
|
44
|
+
api_key: ENV.fetch("VOYAGEAI_API_KEY", nil),
|
45
|
+
host: ENV.fetch("VOYAGEAI_HOST", DEFAULT_HOST),
|
46
|
+
version: ENV.fetch("VOYAGEAI_VERSION", DEFAULT_VERSION),
|
47
|
+
logger: nil,
|
48
|
+
timeout: nil
|
49
|
+
)
|
50
|
+
@api_key = api_key
|
51
|
+
@host = host
|
52
|
+
@version = version
|
53
|
+
@logger = logger
|
54
|
+
@timeout = timeout
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/voyageai/embed.rb
CHANGED
@@ -25,7 +25,7 @@ module VoyageAI
|
|
25
25
|
usage = Usage.parse(data: data["usage"])
|
26
26
|
embeddings = data["data"].map { |embedding_data| embedding_data["embedding"] }
|
27
27
|
|
28
|
-
|
28
|
+
new(model: model, usage: usage, embeddings: embeddings)
|
29
29
|
end
|
30
30
|
|
31
31
|
# @param model [String]
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module VoyageAI
|
4
|
+
# Used for logging via HTTP.
|
5
|
+
class Instrumentation
|
6
|
+
# @param logger [Logger]
|
7
|
+
def initialize(logger:)
|
8
|
+
@logger = logger
|
9
|
+
end
|
10
|
+
|
11
|
+
# @param name [String]
|
12
|
+
# @param payload [Hash]
|
13
|
+
# @option payload [Exception] :error
|
14
|
+
def instrument(name, payload = {})
|
15
|
+
error = payload[:error]
|
16
|
+
return unless error
|
17
|
+
|
18
|
+
@logger.error("#{name}: #{error.message}")
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param payload [Hash]
|
22
|
+
# @option payload [HTTP::Request] :request
|
23
|
+
def start(_, payload)
|
24
|
+
request = payload[:request]
|
25
|
+
@logger.info("#{request.verb.upcase} #{request.uri}")
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param payload [Hash]
|
29
|
+
# @option payload [HTTP::Response] :response
|
30
|
+
def finish(_, payload)
|
31
|
+
response = payload[:response]
|
32
|
+
@logger.info("#{response.status.code} #{response.status.reason}")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/voyageai/model.rb
CHANGED
@@ -9,11 +9,17 @@ module VoyageAI
|
|
9
9
|
VOYAGE_LAW_2 = "voyage-law-2"
|
10
10
|
VOYAGE_CODE_2 = "voyage-code-2"
|
11
11
|
|
12
|
+
RERANK_2 = "rerank-2"
|
13
|
+
RERANK_2_LITE = "rerank-2-lite"
|
14
|
+
|
12
15
|
VOYAGE = VOYAGE_3
|
13
16
|
VOYAGE_LITE = VOYAGE_3_LITE
|
14
17
|
VOYAGE_FINANCE = VOYAGE_FINANCE_2
|
15
18
|
VOYAGE_MULTILINGUAL = VOYAGE_MULTILINGUAL_2
|
16
19
|
VOYAGE_LAW = VOYAGE_LAW_2
|
17
20
|
VOYAGE_CODE = VOYAGE_CODE_2
|
21
|
+
|
22
|
+
RERANK = RERANK_2
|
23
|
+
RERANK_LITE = RERANK_2_LITE
|
18
24
|
end
|
19
25
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module VoyageAI
|
4
|
+
# The response for an rerank request that wraps the model / usage / results.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# VoyageAI::Rerank.new(model: "rerank-2", usage: VoyageAI::Usage.new(total_tokens: 0), results: [])
|
8
|
+
class Rerank
|
9
|
+
# @!attribute [rw] model
|
10
|
+
# @return [String]
|
11
|
+
attr_accessor :model
|
12
|
+
|
13
|
+
# @!attribute [rw] usage
|
14
|
+
# @return [Usage]
|
15
|
+
attr_accessor :usage
|
16
|
+
|
17
|
+
# @!attribute [rw] reranking
|
18
|
+
# @return [Array<Array<Float>>]
|
19
|
+
attr_accessor :results
|
20
|
+
|
21
|
+
# @param data [Hash]
|
22
|
+
# @return [Embed]
|
23
|
+
def self.parse(data:)
|
24
|
+
model = data["model"]
|
25
|
+
usage = Usage.parse(data: data["usage"])
|
26
|
+
|
27
|
+
results = data["data"].map { |entry| Reranking.parse(data: entry) }
|
28
|
+
|
29
|
+
new(model:, usage:, results:)
|
30
|
+
end
|
31
|
+
|
32
|
+
# @param model [String]
|
33
|
+
# @param usage [Usage]
|
34
|
+
# @param results [Array<Reranking>]
|
35
|
+
def initialize(model:, usage:, results:)
|
36
|
+
@model = model
|
37
|
+
@usage = usage
|
38
|
+
@results = results
|
39
|
+
end
|
40
|
+
|
41
|
+
# @return [String]
|
42
|
+
def inspect
|
43
|
+
"#<#{self.class.name} model=#{@model.inspect} usage=#{@usage.inspect}>"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module VoyageAI
|
4
|
+
# The response of an individual re-ranking.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# VoyageAI::Reranking.new(index: 0, document: "Sample", relevance_score: 0.0)
|
8
|
+
class Reranking
|
9
|
+
# @!attribute [rw] model
|
10
|
+
# @return [Integer]
|
11
|
+
attr_accessor :index
|
12
|
+
|
13
|
+
# @!attribute [rw] document
|
14
|
+
# @return [String]
|
15
|
+
attr_accessor :document
|
16
|
+
|
17
|
+
# @!attribute [rw] relevance_score
|
18
|
+
# @return [Float]
|
19
|
+
attr_accessor :relevance_score
|
20
|
+
|
21
|
+
# @param index [Integer]
|
22
|
+
# @param document [String]
|
23
|
+
# @param relevance_score [Float]
|
24
|
+
def initialize(index:, document:, relevance_score:)
|
25
|
+
@index = index
|
26
|
+
@document = document
|
27
|
+
@relevance_score = relevance_score
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [String]
|
31
|
+
def inspect
|
32
|
+
"#<#{self.class.name} index=#{@index} relevance_score=#{@relevance_score}>"
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param data [Hash]
|
36
|
+
#
|
37
|
+
# @return [Reranking]
|
38
|
+
def self.parse(data:)
|
39
|
+
index = data["index"]
|
40
|
+
document = data["document"]
|
41
|
+
relevance_score = data["relevance_score"]
|
42
|
+
|
43
|
+
new(index: index, document: document, relevance_score: relevance_score)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/voyageai/version.rb
CHANGED
data/lib/voyageai.rb
CHANGED
@@ -7,6 +7,15 @@ loader = Zeitwerk::Loader.for_gem
|
|
7
7
|
loader.inflector.inflect "voyageai" => "VoyageAI"
|
8
8
|
loader.setup
|
9
9
|
|
10
|
+
# The main entrypoint for VoyageAI.
|
10
11
|
module VoyageAI
|
11
|
-
|
12
|
+
# @return [VoyageAI::Config]
|
13
|
+
def self.config
|
14
|
+
@config ||= Config.new
|
15
|
+
end
|
16
|
+
|
17
|
+
# @yield [VoyageAI::Config]
|
18
|
+
def self.configure
|
19
|
+
yield config
|
20
|
+
end
|
12
21
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voyageai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -51,8 +51,12 @@ files:
|
|
51
51
|
- bin/setup
|
52
52
|
- lib/voyageai.rb
|
53
53
|
- lib/voyageai/client.rb
|
54
|
+
- lib/voyageai/config.rb
|
54
55
|
- lib/voyageai/embed.rb
|
56
|
+
- lib/voyageai/instrumentation.rb
|
55
57
|
- lib/voyageai/model.rb
|
58
|
+
- lib/voyageai/rerank.rb
|
59
|
+
- lib/voyageai/reranking.rb
|
56
60
|
- lib/voyageai/usage.rb
|
57
61
|
- lib/voyageai/version.rb
|
58
62
|
homepage: https://github.com/ksylvest/voyageai
|