ruby-amazon-bedrock 0.2.0 → 0.2.1

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: 5cc2fda0ffade519b17acc4d278b6d4d55136c8df511a2da329dccbc3dc63bd9
4
- data.tar.gz: c6afb32b619515b886b5cb34ed01791a62ea1fda119407d8b72ce1455d65f149
3
+ metadata.gz: f3a872c53afed3f2fb371cbc1c4b661ad00d02583e7a31f1558f6081462d2740
4
+ data.tar.gz: 5c5ed1f988239ecdf8a50e35c870544794f2c976572018feaf21404eb8097f01
5
5
  SHA512:
6
- metadata.gz: bdadc9d45b3fac52916462f5e47e62cb91e4f25f949d4af9b7fe3ecca9a7bb151bdcfb0837c2fd511115affd49845441a6a964a0175cc1748583186f053de1e2
7
- data.tar.gz: 90b2adc4a2f8f49237803f81663ea3d4a54b0177bbd6075f6a86f8982dad6a23cac6266dc50cf92aa4902cef9241f9bf1f4ec22dbf6bf8b98269daa9d1102834
6
+ metadata.gz: 74db07e19c34b58d106c0e9847c9e244b48642f32eaf4b5f6b339aff974dcdbbad9b3949eb0076773f331a7843d87e6230e1cf2386fb9d8eecc8b6c3c2385fb2
7
+ data.tar.gz: 307e7f513903954428303c8ab40c86df98d694770ce2022c405088a4042f2095504962102d43d50ec70124aafe9d7198e0db77cfd14c5e9e189693cb65866deb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.2.1] - 2023-12-25
2
+
3
+ - Make gem configurable
4
+ - Fix A121 Labs payloads
5
+ - Update README.md with A121 Labs documentation
6
+
1
7
  ## [0.2.0] - 2023-12-14
2
8
 
3
9
  - Add Text and Image builders to be able to return text or save images depending on the model use case.
data/Gemfile CHANGED
@@ -11,6 +11,5 @@ gem "pry", "~> 0.13"
11
11
  gem "rake", "~> 13.0"
12
12
  gem "rspec", "~> 3.0"
13
13
  gem "rubocop", "~> 1.21"
14
- gem "simplecov", "~> 0.21"
15
14
  gem "vcr", "~> 6.0"
16
15
  gem "webmock", "~> 3.12"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-amazon-bedrock (0.2.0)
4
+ ruby-amazon-bedrock (0.2.1)
5
5
  aws-sdk-bedrockruntime (~> 1.0)
6
6
 
7
7
  GEM
@@ -29,7 +29,6 @@ GEM
29
29
  crack (0.4.5)
30
30
  rexml
31
31
  diff-lcs (1.5.0)
32
- docile (1.4.0)
33
32
  dotenv (2.8.1)
34
33
  hashdiff (1.0.1)
35
34
  jmespath (1.6.2)
@@ -76,12 +75,6 @@ GEM
76
75
  rubocop-ast (1.30.0)
77
76
  parser (>= 3.2.1.0)
78
77
  ruby-progressbar (1.13.0)
79
- simplecov (0.22.0)
80
- docile (~> 1.1)
81
- simplecov-html (~> 0.11)
82
- simplecov_json_formatter (~> 0.1)
83
- simplecov-html (0.12.3)
84
- simplecov_json_formatter (0.1.4)
85
78
  thor (1.3.0)
86
79
  unicode-display_width (2.5.0)
87
80
  vcr (6.2.0)
@@ -101,7 +94,6 @@ DEPENDENCIES
101
94
  rspec (~> 3.0)
102
95
  rubocop (~> 1.21)
103
96
  ruby-amazon-bedrock!
104
- simplecov (~> 0.21)
105
97
  vcr (~> 6.0)
106
98
  webmock (~> 3.12)
107
99
 
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Ruby Amazon Bedrock
2
2
 
3
- [![CircleCI](https://dl.circleci.com/status-badge/img/circleci/JP4R1PDyZ2Yax5GxVQoMZN/LAwrE89wMVTrgpfzaimon/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/circleci/JP4R1PDyZ2Yax5GxVQoMZN/LAwrE89wMVTrgpfzaimon/tree/main)
3
+ [![Gem Version](https://badge.fury.io/rb/ruby-amazon-bedrock.svg)](https://badge.fury.io/rb/ruby-amazon-bedrock)
4
+ [![CircleCI](https://dl.circleci.com/status-badge/img/circleci/JP4R1PDyZ2Yax5GxVQoMZN/LAwrE89wMVTrgpfzaimon/tree/main.svg?style=shield&circle-token=31edf35e4c3f323ce0044c6fceadf9ffb15f5d3c)](https://dl.circleci.com/status-badge/redirect/circleci/JP4R1PDyZ2Yax5GxVQoMZN/LAwrE89wMVTrgpfzaimon/tree/main)
4
5
 
5
6
  Seamless Integration with Amazon Bedrock API for AI-Powered Text and Image Generation in Ruby 🤖 + 💎. [Amazon Bedrock API](https://aws.amazon.com/es/bedrock/).
6
7
 
@@ -13,7 +14,7 @@ Amazon Bedrock is a fully managed service that makes FMs from leading AI startup
13
14
  Add the following line to your application's Gemfile:
14
15
 
15
16
  ```ruby
16
- gem "ruby-amazon-bedrock"
17
+ gem "ruby-amazon-bedrock", "~> 0.2.1"
17
18
  ```
18
19
 
19
20
  And then execute:
@@ -27,7 +28,7 @@ $ bundle install
27
28
  Or install with:
28
29
 
29
30
  ```bash
30
- $ gem install ruby-amazon-bedrock
31
+ $ gem install ruby-amazon-bedrock -v 0.2.1
31
32
  ```
32
33
 
33
34
  and require with:
@@ -55,34 +56,78 @@ Be aware that by using `ruby-amazon-bedrock` gem in conjunction with Amazon Bedr
55
56
  Instantiate a client by passing your AWS IAM credentials:
56
57
 
57
58
  ```ruby
58
- client = RubyAmazonBedrock::Client.new(region: "aws_region", access_key_id: "access_key_id", access_token: "access_token")
59
+ client = RubyAmazonBedrock::Client.new(
60
+ region: "AWS_REGION",
61
+ access_key_id: "AWS_ACCESS_KEY_ID",
62
+ access_token: "AWS_SECRET_ACCESS_KEY"
63
+ )
59
64
  ```
60
65
 
61
- ## Models Providers
66
+ ## With Configuration
67
+
68
+ ```ruby
69
+ RubyAmazonBedrock.configure do |config|
70
+ config.region = ENV.fetch('AWS_REGION', nil)
71
+ config.access_key_id = ENV.fetch('AWS_ACCESS_KEY_ID', nil)
72
+ config.secret_access_key = ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
73
+ end
74
+
75
+ client = RubyAmazonBedrock::Client.new
76
+ ```
77
+
78
+ ## Model Providers
62
79
 
63
80
  Amazon Bedrock is a fully managed service that makes FMs from leading AI startups and Amazon available via an API, so you can choose from a wide range of FMs to find the model that is best suited for your use case.
64
81
 
65
- <!-- ### AI21 Labs
82
+ ### AI21 Labs
66
83
 
67
84
  Businesses use AI21's Jurassic family of leading LLMs to build generative AI-driven applications and services leveraging existing organizational data. Jurassic supports cross-industry use cases including long and short-form text generation, contextual question answering, summarization, and classification. Designed to follow natural language instructions, Jurassic is trained on a massive corpus of web text and supports six languages in addition to English. [See more ...](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=ai21.j2-ultra-v1)
68
85
 
69
86
  How to call AI21 models:
70
87
 
71
- Jurassic-2 Ultra
88
+ _*Jurassic-2 Ultra*_
72
89
 
73
90
  Supported use cases: Open book question answering, summarization, draft generation, information extraction, ideation
74
91
 
75
92
  Languages: English, Spanish, French, German, Portuguese, Italian, Dutch
76
93
 
77
94
  ```ruby
78
- client.invoke_model('ai21.j2-ultra-v1', 'Your prompt goes here ...')
95
+ client.invoke_model(id: 'ai21.j2-ultra-v1', input: "What's natural language processing?")
96
+
97
+ # Response
98
+ {:id=>1234,
99
+ :prompt=>
100
+ {:text=>"Describe how an LLM works",
101
+ :tokens=>
102
+ [{:generatedToken=>{:token=>"▁Describe", :logprob=>-10.674324035644531, :raw_logprob=>-10.674324035644531},
103
+ :completions=>
104
+ [{:data=>
105
+ {:text=>
106
+ "\nNatural language processing (NLP) is a field of computer science, artificial intelligence, and linguistics concerned with the interactions between computers and human (natural) languages, in particular how to program computers to process and analyze large amounts of natural language data. The goal of NLP is to provide computers with the ability to read, understand, and generate human language.",
107
+ :tokens=>
108
+ [{:generatedToken=>{:token=>"<|newline|>", :logprob=>0.0, :raw_logprob=>-0.00046850196667946875},
79
109
  ```
80
110
 
81
- Jurassic-2 Mid
111
+ _*Jurassic-2 Mid*_
82
112
 
83
113
  ```ruby
84
- client.invoke_model('ai21.j2-mid-v1', 'Your prompt goes here ...')
85
- ``` -->
114
+ client.invoke_model(id: 'ai21.j2-mid-v1', input: "What's GenAI?")
115
+
116
+ # Response
117
+ {:id=>1234,
118
+ :prompt=>
119
+ {:text=>"What's GenAI?",
120
+ :tokens=>
121
+ [{:generatedToken=>{:token=>"▁What's", :logprob=>-9.553738594055176, :raw_logprob=>-9.553738594055176},
122
+ :topTokens=>nil,
123
+ :textRange=>{:start=>0, :end=>6}},
124
+ :completions=>
125
+ [{:data=>
126
+ {:text=>
127
+ "\nGenAI is a proposed standard for a generic AI language, which would allow AI systems to communicate and reason with each other in a common language. The goal of GenAI is to create a universal language that can be used by all AI systems, regardless of their specific task or underlying architecture. This would make it easier for AI systems to work together and learn from each other, and it would also make it easier for humans to interact with and understand AI systems.",
128
+ :tokens=>
129
+ [{:generatedToken=>{:token=>"<|newline|>", :logprob=>0.0, :raw_logprob=>-0.0009662011871114373},
130
+ ```
86
131
 
87
132
  ### Amazon Titan
88
133
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyAmazonBedrock
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -3,8 +3,41 @@
3
3
  require_relative "amazon_bedrock/version"
4
4
  require_relative "bedrock_runtime/client"
5
5
 
6
+ # The RubyAmazonBedrock module serves as a namespace for classes that
7
+ # interact with Amazon Bedrock Service. It includes configuration management
8
+ # and custom error classes for handling AWS service interactions.
6
9
  module RubyAmazonBedrock
7
10
  # Raised when an unknown model identifier is provided to PayloadFactory.
8
11
  class UnknownModelError < StandardError; end
9
12
  class Error < StandardError; end
13
+
14
+ # Configuration class for setting up AWS credentials and region.
15
+ class Configuration
16
+ attr_accessor :region, :access_key_id, :secret_access_key
17
+
18
+ # Initializes a new Configuration instance, loading values from
19
+ # environment variables or setting them to nil by default so the
20
+ # values can be dynamically set.
21
+ def initialize
22
+ @region = ENV.fetch('AWS_REGION', nil)
23
+ @access_key_id = ENV.fetch('AWS_ACCESS_KEY_ID', nil)
24
+ @secret_access_key = ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
25
+ end
26
+ end
27
+
28
+ class << self
29
+ # Returns the current configuration instance or creates a new one.
30
+ #
31
+ # @return [Configuration] the current configuration instance
32
+ def configuration
33
+ @configuration ||= Configuration.new
34
+ end
35
+
36
+ # Provides a block to configure the RubyAmazonBedrock settings.
37
+ #
38
+ # @yield [Configuration] Yields the current configuration to a block.
39
+ def configure
40
+ yield(configuration)
41
+ end
42
+ end
10
43
  end
@@ -4,7 +4,6 @@ require 'base64'
4
4
  require 'aws-sdk-bedrockruntime'
5
5
  require 'bedrock_runtime/payload_factory'
6
6
  require 'bedrock_runtime/response_factory'
7
- require 'pry'
8
7
 
9
8
  module RubyAmazonBedrock
10
9
  # Client for interfacing with the Amazon Bedrock Runtime.
@@ -15,11 +14,13 @@ module RubyAmazonBedrock
15
14
  # Initializes the AWS BedrockRuntime client.
16
15
  #
17
16
  # @note The AWS credentials and region are fetched from the environment variables.
18
- def initialize(region:, access_key_id:, secret_access_key:)
17
+ def initialize(region: nil, access_key_id: nil, secret_access_key: nil)
18
+ config = RubyAmazonBedrock.configuration || RubyAmazonBedrock::Configuration.new
19
+
19
20
  @client = Aws::BedrockRuntime::Client.new(
20
- region: region,
21
- access_key_id: access_key_id,
22
- secret_access_key: secret_access_key
21
+ region: region || config.region,
22
+ access_key_id: access_key_id || config.access_key_id,
23
+ secret_access_key: secret_access_key || config.secret_access_key
23
24
  )
24
25
  end
25
26
 
@@ -37,8 +37,8 @@ module RubyAmazonBedrock
37
37
  prompt: @input,
38
38
  maxTokens: 200,
39
39
  temperature: 0,
40
- topP: 250,
41
- stop_sequences: [],
40
+ topP: 1.0,
41
+ stopSequences: [],
42
42
  countPenalty: { scale: 0 },
43
43
  presencePenalty: { scale: 0 },
44
44
  frequencyPenalty: { scale: 0 }
@@ -14,7 +14,7 @@ module RubyAmazonBedrock
14
14
  #
15
15
  # @return [String] 'ai21labs.j2-mid-v1'
16
16
  def model_id
17
- 'ai21labs.j2-mid-v1'
17
+ 'ai21.j2-mid-v1'
18
18
  end
19
19
  end
20
20
  end
@@ -14,7 +14,7 @@ module RubyAmazonBedrock
14
14
  #
15
15
  # @return [String] 'ai21labs.j2-ultra-v1'
16
16
  def model_id
17
- 'ai21labs.j2-ultra-v1'
17
+ 'ai21.j2-ultra-v1'
18
18
  end
19
19
  end
20
20
  end
@@ -50,8 +50,8 @@ module RubyAmazonBedrock
50
50
  # @return [Hash] The mapping of model identifiers to builder classes.
51
51
  def models_to_builders
52
52
  {
53
- 'ai21labs.j2-mid-v1' => PayloadBuilders::Ai21Labs::J2MidV1,
54
- 'ai21labs.j2-ultra-v1' => PayloadBuilders::Ai21Labs::J2UltraV1,
53
+ 'ai21.j2-mid-v1' => PayloadBuilders::Ai21Labs::J2MidV1,
54
+ 'ai21.j2-ultra-v1' => PayloadBuilders::Ai21Labs::J2UltraV1,
55
55
  'amazon.titan-image-generator-v1' => PayloadBuilders::Amazon::TitanImageGeneratorV1,
56
56
  'amazon.titan-text-lite-v1' => PayloadBuilders::Amazon::TitanTextLiteV1,
57
57
  'amazon.titan-text-express-v1' => PayloadBuilders::Amazon::TitanTextExpressV1,
@@ -11,8 +11,8 @@ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::Base do
11
11
  prompt: input,
12
12
  maxTokens: 200,
13
13
  temperature: 0,
14
- topP: 250,
15
- stop_sequences: [],
14
+ topP: 1.0,
15
+ stopSequences: [],
16
16
  countPenalty: { scale: 0 },
17
17
  presencePenalty: { scale: 0 },
18
18
  frequencyPenalty: { scale: 0 }
@@ -11,8 +11,8 @@ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::J2MidV1 do
11
11
  prompt: input,
12
12
  maxTokens: 200,
13
13
  temperature: 0,
14
- topP: 250,
15
- stop_sequences: [],
14
+ topP: 1.0,
15
+ stopSequences: [],
16
16
  countPenalty: { scale: 0 },
17
17
  presencePenalty: { scale: 0 },
18
18
  frequencyPenalty: { scale: 0 }
@@ -24,7 +24,7 @@ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::J2MidV1 do
24
24
  payload_builder = described_class.new(input, options)
25
25
  payload = payload_builder.build
26
26
 
27
- expect(payload[:model_id]).to eq('ai21labs.j2-mid-v1')
27
+ expect(payload[:model_id]).to eq('ai21.j2-mid-v1')
28
28
  expect(payload[:content_type]).to eq('application/json')
29
29
  expect(payload[:accept]).to eq('*/*')
30
30
  expect(payload[:body]).to eq(body)
@@ -11,8 +11,8 @@ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::J2UltraV1 do
11
11
  prompt: input,
12
12
  maxTokens: 200,
13
13
  temperature: 0,
14
- topP: 250,
15
- stop_sequences: [],
14
+ topP: 1.0,
15
+ stopSequences: [],
16
16
  countPenalty: { scale: 0 },
17
17
  presencePenalty: { scale: 0 },
18
18
  frequencyPenalty: { scale: 0 }
@@ -24,7 +24,7 @@ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::J2UltraV1 do
24
24
  payload_builder = described_class.new(input, options)
25
25
  payload = payload_builder.build
26
26
 
27
- expect(payload[:model_id]).to eq('ai21labs.j2-ultra-v1')
27
+ expect(payload[:model_id]).to eq('ai21.j2-ultra-v1')
28
28
  expect(payload[:content_type]).to eq('application/json')
29
29
  expect(payload[:accept]).to eq('*/*')
30
30
  expect(payload[:body]).to eq(body)
@@ -5,8 +5,8 @@ require 'bedrock_runtime/payload_factory'
5
5
 
6
6
  RSpec.describe RubyAmazonBedrock::PayloadFactory do
7
7
  let(:bedrock_models) do
8
- ["ai21labs.j2-mid-v1",
9
- "ai21labs.j2-ultra-v1",
8
+ ["ai21.j2-mid-v1",
9
+ "ai21.j2-ultra-v1",
10
10
  "amazon.titan-image-generator-v1",
11
11
  "amazon.titan-text-lite-v1",
12
12
  "amazon.titan-text-express-v1",
data/spec/spec_helper.rb CHANGED
@@ -1,15 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'dotenv/load'
4
- require 'simplecov'
5
4
  require 'vcr'
6
5
 
7
6
  require 'amazon_bedrock'
8
7
  require 'bedrock_runtime/client'
9
8
  require 'bedrock_runtime/payload_factory'
10
9
 
11
- SimpleCov.start
12
-
13
10
  RSpec.configure do |config|
14
11
  # Enable flags like --only-failures and --next-failure
15
12
  config.example_status_persistence_file_path = '.rspec_status'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-amazon-bedrock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alvaro Delgado
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-14 00:00:00.000000000 Z
11
+ date: 2023-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-bedrockruntime