voyageai 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d127480aae597e6c15b398061d16beded7be8961fd433372b2550ea1b643d37
4
- data.tar.gz: 0a958352786d2994de9bdfbbb198850e15d245a9aed6971efae0f30518494878
3
+ metadata.gz: ac2d6a908abb5d1383cdf74f736b5aa7eae9fc9a66634ec1fd40ce8710571763
4
+ data.tar.gz: 7df317f435acc60bde69219f7dc50e19952433dce8d4dee9ebda10a8a6b0ba46
5
5
  SHA512:
6
- metadata.gz: 346dc805e9a46813198189b83805a6d2718cb1a4c244af08f8a1fb5cc4cc251f0fd7ff1b06104669b588da56b5d8fcda3f06795a64fac5a2ee7dc6e8e86faa1f
7
- data.tar.gz: 9cf273cdbf521f0b13fc9b30a58517ae7ebdfce1cb23801e64630bcf4887f17d4ed61058d0eb72533fe801e221a7a1fe91df84b802cb5266d7a405af7b68981a
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
- ### Generating Single Embedding
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
- ### Generating Multiple Embeddings
30
+ #### Generating Multiple Embeddings
29
31
 
30
32
  ```ruby
31
33
  require 'voyageai'
@@ -45,6 +47,28 @@ embed.usage # "#<VoyageAI::Usage total_tokens=...>"
45
47
  embed.embeddings # [[0.0, ...], ...]
46
48
  ```
47
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
+
48
72
  ## Configuration
49
73
 
50
74
  ```ruby
@@ -50,6 +50,20 @@ module VoyageAI
50
50
  Embed.parse(data: response.parse)
51
51
  end
52
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
+
53
67
  private
54
68
 
55
69
  # @return [HTTP::Client]
@@ -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
- Embed.new(model: model, usage: usage, embeddings: embeddings)
28
+ new(model: model, usage: usage, embeddings: embeddings)
29
29
  end
30
30
 
31
31
  # @param model [String]
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module VoyageAI
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voyageai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
@@ -55,6 +55,8 @@ files:
55
55
  - lib/voyageai/embed.rb
56
56
  - lib/voyageai/instrumentation.rb
57
57
  - lib/voyageai/model.rb
58
+ - lib/voyageai/rerank.rb
59
+ - lib/voyageai/reranking.rb
58
60
  - lib/voyageai/usage.rb
59
61
  - lib/voyageai/version.rb
60
62
  homepage: https://github.com/ksylvest/voyageai