voyageai 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e168ee217dd0d4a6bd9ac0f220369bddc558d15cb157187d725f4ee4a2340d2
4
+ data.tar.gz: f2a891d5dcb4654f7ac9143eb0e6d95bd800f77b39d0acdcd38c05616a2c4cde
5
+ SHA512:
6
+ metadata.gz: fe1eaccbf9d80eb5a33dcb407f9bd6e2bc578a8bbdc5627c5aeaededde53493cb17f97fb07992e671151aad576ed39ee82b093330ab932c5cf0360e4d2ab6b12
7
+ data.tar.gz: 7ef2238c0073848e4d9bfcebda47d271806d1bde7801ffc180a7654f82f5d24b0bd3ecbd38a9a017ff04540cdc4057df4724d5a2b6b80b352ff3093533f10f8a
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "factory_bot"
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "rspec_junit_formatter"
11
+ gem "rubocop"
12
+ gem "rubocop-factory_bot"
13
+ gem "rubocop-rake"
14
+ gem "rubocop-rspec"
15
+ gem "simplecov"
16
+ gem "webmock"
17
+ gem "yard"
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # VoyageAI
2
+
3
+ `voyageai` is a ruby client for [VoyageAI](https://www.voyageai.com)
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ gem install voyageai
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ require 'voyageai'
15
+
16
+ input = [
17
+ 'John is a musician.',
18
+ 'Paul is a plumber.',
19
+ 'George is a teacher.',
20
+ 'Ringo is a doctor.',
21
+ ]
22
+
23
+ voyage_ai = VoyageAI::Client.new(api_key: 'pa-...')
24
+ result = voyage_ai.embed(input)
25
+ embed.model # "..."
26
+ embed.usage # "#<VoyageAI::Usage total_tokens=...>"
27
+ embed.embeddings.each do |embedding|
28
+ embedding # "#<VoyageAI::Embedding index=... embedding=...>
29
+ end
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/voyageai.
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "voyageai"
6
+
7
+ require "irb"
8
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VoyageAI
4
+ # A client for interacting with the VoyageAI API.
5
+ class Client
6
+ BASE_URL = "https://api.voyageai.com/v1"
7
+
8
+ # An error raised for any HTTP issues.
9
+ class RequestError < StandardError
10
+ attr_accessor :response
11
+
12
+ # @param response [HTTP::Response]
13
+ def initialize(response:)
14
+ super("status=#{response.status} body=#{response.body}")
15
+ @response = response
16
+ end
17
+ end
18
+
19
+ # @param api_key [String] optional if ENV['VOYAGE_API_KEY'] otherwise required
20
+ def initialize(api_key: ENV.fetch("VOYAGE_API_KEY", nil))
21
+ @api_key = api_key || raise(ArgumentError, "api_key is required or ENV['VOYAGE_API_KEY'] must be present")
22
+ end
23
+
24
+ # @return [String]
25
+ def inspect
26
+ masked_api_key = "#{@api_key[..4]}***"
27
+
28
+ "#<#{self.class.name} api_key=#{masked_api_key.inspect}>"
29
+ end
30
+
31
+ # @param model [String] optional (e.g. VoyageAI::Model::VOYAGE or "voyage-3")
32
+ # @param input [Array<String>] required (e.g. ["Sample 1", "Sample 2", ...])
33
+ #
34
+ # @return [Embedding]
35
+ def embed(input, model: Model::VOYAGE)
36
+ payload = { input: input, model: model }
37
+ response = HTTP
38
+ .accept(:json)
39
+ .auth(@api_key)
40
+ .post("#{BASE_URL}/embeddings", json: payload)
41
+
42
+ raise RequestError.new(response:) unless response.status.ok?
43
+
44
+ Embed.parse(data: response.parse)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VoyageAI
4
+ # The response for an embeddings request that wraps the model / usage / embeddings.
5
+ #
6
+ # @example
7
+ # VoyageAI::Embed.new(model: "voyage-3", usage: VoyageAI::Usage.new(total_tokens: 0), embeddings: [])
8
+ class Embed
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] embeddings
18
+ # @return [Array<Embedding>]
19
+ attr_accessor :embeddings
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
+ embeddings = data["data"].map do |embedding_data|
27
+ Embedding.parse(data: embedding_data)
28
+ end
29
+
30
+ Embed.new(model: model, usage: usage, embeddings: embeddings)
31
+ end
32
+
33
+ # @param model [String]
34
+ # @param usage [Usage]
35
+ # @param embeddings [Array<Embedding>]
36
+ def initialize(model:, usage:, embeddings:)
37
+ @model = model
38
+ @usage = usage
39
+ @embeddings = embeddings
40
+ end
41
+
42
+ # @return [String]
43
+ def inspect
44
+ "#<#{self.class.name} model=#{@model.inspect} embeddings=#{@embeddings.inspect} usage=#{@usage.inspect}>"
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VoyageAI
4
+ # An embedding returned by the VoyageAI API.
5
+ #
6
+ # @example
7
+ # VoyageAI::Embedding.new(index: 0, embedding: [0.0, 1.0, 2.0, 3.0])
8
+ class Embedding
9
+ # @!attribute [rw] model
10
+ # @return [Integer]
11
+ attr_accessor :index
12
+
13
+ # @!attribute [rw] embedding
14
+ # @return [Array<Float>]
15
+ attr_accessor :embedding
16
+
17
+ # @param data [Hash]
18
+ # @return [Embedding]
19
+ def self.parse(data:)
20
+ index = data["index"]
21
+ embedding = data["embedding"]
22
+ new(index:, embedding:)
23
+ end
24
+
25
+ # @param index [Integer]
26
+ # @param embedding [Array<Float>]
27
+ def initialize(index:, embedding:)
28
+ @index = index
29
+ @embedding = embedding
30
+ end
31
+
32
+ # @return [String]
33
+ def inspect
34
+ "#<#{self.class.name} index=#{index} embedding=#{embedding.inspect}>"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VoyageAI
4
+ module Model
5
+ VOYAGE_3 = "voyage-3"
6
+ VOYAGE_3_LITE = "voyage-3-lite"
7
+ VOYAGE_FINANCE_2 = "voyage-finance-2"
8
+ VOYAGE_MULTILINGUAL_2 = "age-multilingual-2"
9
+ VOYAGE_LAW_2 = "voyage-law-2"
10
+ VOYAGE_CODE_2 = "voyage-code-2"
11
+
12
+ VOYAGE = VOYAGE_3
13
+ VOYAGE_LITE = VOYAGE_3_LITE
14
+ VOYAGE_FINANCE = VOYAGE_FINANCE_2
15
+ VOYAGE_MULTILINGUAL = VOYAGE_MULTILINGUAL_2
16
+ VOYAGE_LAW = VOYAGE_LAW_2
17
+ VOYAGE_CODE = VOYAGE_CODE_2
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VoyageAI
4
+ # The usage returned by the VoyageAI API.
5
+ #
6
+ # @example
7
+ # VoyageAI::Usage.new(total_tokens: 0)
8
+ class Usage
9
+ # @!attribute [rw] total_tokens
10
+ # @return [Integer]
11
+ attr_accessor :total_tokens
12
+
13
+ # @param total_tokens [Integer]
14
+ def initialize(total_tokens:)
15
+ @total_tokens = total_tokens
16
+ end
17
+
18
+ # @return [String]
19
+ def inspect
20
+ "#<#{self.class.name} total_tokens=#{total_tokens}>"
21
+ end
22
+
23
+ # @param data [Hash]
24
+ # @return [Usage]
25
+ def self.parse(data:)
26
+ new(total_tokens: data["total_tokens"])
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VoyageAI
4
+ VERSION = "0.1.0"
5
+ end
data/lib/voyageai.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+
5
+ loader = Zeitwerk::Loader.for_gem
6
+ loader.inflector.inflect "voyageai" => "VoyageAI"
7
+ loader.setup
8
+
9
+ module VoyageAI
10
+ class Error < StandardError; end
11
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: voyageai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Sylvestre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: zeitwerk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A library for generating embeddings with https://voyageai.com.
42
+ email:
43
+ - kevin@ksylvest.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - README.md
50
+ - bin/console
51
+ - bin/setup
52
+ - lib/voyageai.rb
53
+ - lib/voyageai/client.rb
54
+ - lib/voyageai/embed.rb
55
+ - lib/voyageai/embedding.rb
56
+ - lib/voyageai/model.rb
57
+ - lib/voyageai/usage.rb
58
+ - lib/voyageai/version.rb
59
+ homepage: https://github.com/ksylvest/voyageai.
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/ksylvest/voyageai.
64
+ changelog_uri: https://github.com/ksylvest/voyageai./releases
65
+ rubygems_mfa_required: 'true'
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.3.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.5.21
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: A client for voyageai.com.
85
+ test_files: []