ruby-amazon-bedrock 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/Gemfile +0 -1
  4. data/Gemfile.lock +1 -9
  5. data/README.md +165 -26
  6. data/lib/amazon_bedrock/version.rb +1 -1
  7. data/lib/amazon_bedrock.rb +33 -0
  8. data/lib/bedrock_runtime/client.rb +10 -9
  9. data/lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb +20 -8
  10. data/lib/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1.rb +1 -1
  11. data/lib/bedrock_runtime/payload_builders/ai_21_labs/j2_ultra_v1.rb +1 -1
  12. data/lib/bedrock_runtime/payload_builders/amazon/base.rb +11 -7
  13. data/lib/bedrock_runtime/payload_builders/amazon/titan_image_generator_v1.rb +1 -1
  14. data/lib/bedrock_runtime/payload_builders/amazon/titan_text_lite_v1.rb +7 -15
  15. data/lib/bedrock_runtime/payload_builders/anthropic/base.rb +16 -8
  16. data/lib/bedrock_runtime/payload_builders/base.rb +9 -2
  17. data/lib/bedrock_runtime/payload_builders/cohere/command_base.rb +25 -4
  18. data/lib/bedrock_runtime/payload_builders/cohere/embed_base.rb +10 -2
  19. data/lib/bedrock_runtime/payload_builders/meta/base.rb +12 -4
  20. data/lib/bedrock_runtime/payload_builders/stability_ai/base.rb +12 -4
  21. data/lib/bedrock_runtime/payload_factory.rb +5 -5
  22. data/lib/bedrock_runtime/response_builders/image.rb +4 -0
  23. data/spec/bedrock_runtime/client_spec.rb +2 -2
  24. data/spec/bedrock_runtime/payload_builders/ai_21_labs/base_spec.rb +10 -12
  25. data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1_spec.rb +10 -13
  26. data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_ultra_v1_spec.rb +11 -14
  27. data/spec/bedrock_runtime/payload_builders/amazon/base_spec.rb +7 -9
  28. data/spec/bedrock_runtime/payload_builders/amazon/titan_text_express_v1_spec.rb +7 -10
  29. data/spec/bedrock_runtime/payload_builders/amazon/titan_text_lite_v1_spec.rb +7 -10
  30. data/spec/bedrock_runtime/payload_builders/anthropic/base_spec.rb +8 -10
  31. data/spec/bedrock_runtime/payload_builders/anthropic/claude_instant_v1_spec.rb +8 -11
  32. data/spec/bedrock_runtime/payload_builders/anthropic/claude_v1_spec.rb +8 -11
  33. data/spec/bedrock_runtime/payload_builders/anthropic/claude_v2_spec.rb +8 -11
  34. data/spec/bedrock_runtime/payload_builders/base_spec.rb +1 -1
  35. data/spec/bedrock_runtime/payload_builders/cohere/command_base_spec.rb +16 -11
  36. data/spec/bedrock_runtime/payload_builders/cohere/command_light_text_v14_spec.rb +16 -12
  37. data/spec/bedrock_runtime/payload_builders/cohere/command_text_v14_spec.rb +16 -12
  38. data/spec/bedrock_runtime/payload_builders/cohere/embed_base_spec.rb +8 -10
  39. data/spec/bedrock_runtime/payload_builders/cohere/embed_english_v3_spec.rb +9 -11
  40. data/spec/bedrock_runtime/payload_builders/cohere/embed_multilingual_v3_spec.rb +9 -11
  41. data/spec/bedrock_runtime/payload_builders/meta/base_spec.rb +8 -10
  42. data/spec/bedrock_runtime/payload_builders/meta/llama213b_chat_v1_spec.rb +8 -11
  43. data/spec/bedrock_runtime/payload_builders/meta/llama270b_chat_v1_spec.rb +8 -11
  44. data/spec/bedrock_runtime/payload_builders/stability_ai/base_spec.rb +8 -10
  45. data/spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0_spec.rb +8 -11
  46. data/spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1_spec.rb +8 -11
  47. data/spec/bedrock_runtime/payload_factory_spec.rb +3 -3
  48. data/spec/spec_helper.rb +1 -2
  49. data/spec/support/helpers.rb +169 -0
  50. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cc2fda0ffade519b17acc4d278b6d4d55136c8df511a2da329dccbc3dc63bd9
4
- data.tar.gz: c6afb32b619515b886b5cb34ed01791a62ea1fda119407d8b72ce1455d65f149
3
+ metadata.gz: e34184a2246b1054a64d6d29bc7bb1144487a508c6d27f717d8f92edb4cb32a2
4
+ data.tar.gz: ae40e48b1107bf163bbb30e4c058d660784f1bf9cc6fcafa0bfa9843b526dd49
5
5
  SHA512:
6
- metadata.gz: bdadc9d45b3fac52916462f5e47e62cb91e4f25f949d4af9b7fe3ecca9a7bb151bdcfb0837c2fd511115affd49845441a6a964a0175cc1748583186f053de1e2
7
- data.tar.gz: 90b2adc4a2f8f49237803f81663ea3d4a54b0177bbd6075f6a86f8982dad6a23cac6266dc50cf92aa4902cef9241f9bf1f4ec22dbf6bf8b98269daa9d1102834
6
+ metadata.gz: fcdfeb1017d3bf2cefeb1bf77b68a85bd36bd3a7906651b1a56f8ff2544309ba98eee08fc1f04ce80ed22b738302fd38e7811087f56a976c6eeea1f7cfae0010
7
+ data.tar.gz: f19b95ef5965317c53eda21c96c69e421abdb34ce441cc850731663aa8ed35bbc67000d02a34ebc2f192ce7fbe81e0d18afe3ab59d0dbea38e32c858625fcc89
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## [0.2.2] - 2024-01-10
2
+
3
+ - Fix bug for Amazon Titan Text Lite
4
+ - Allow payload builders to receive and set Model parameters as options
5
+
6
+ ## [0.2.1] - 2023-12-25
7
+
8
+ - Make gem configurable
9
+ - Fix A121 Labs payloads
10
+ - Update README.md with A121 Labs documentation
11
+
1
12
  ## [0.2.0] - 2023-12-14
2
13
 
3
14
  - 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.2)
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.2"
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,39 +56,116 @@ 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
+ ## Options
79
+
80
+ The options argument enhances the method's flexibility by allowing additional, model-specific configurations. If not explicitly provided, the method defaults to a set of standard Model parameters. You can see more details about a Model's optional parameters.
81
+
82
+ ## Model Providers
62
83
 
63
84
  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
85
 
65
- <!-- ### AI21 Labs
86
+ ### AI21 Labs
66
87
 
67
88
  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
89
 
90
+ Options for these models are:
91
+
92
+ ```ruby
93
+ {
94
+ temperature: 0.5, # float
95
+ max_tokens: 200, # integer
96
+ stop_sequences: [], # [string]
97
+ top_p: 0.5, # float
98
+ count_penalty: 0, # integer
99
+ presence_penalty: 0, # integer
100
+ frequency_penalty: 0 # integer
101
+ }
102
+ ```
103
+
104
+ For more documentation about the parameter data types and values you can [See more details...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html]
105
+
69
106
  How to call AI21 models:
70
107
 
71
- Jurassic-2 Ultra
108
+ _*Jurassic-2 Ultra*_
72
109
 
73
- Supported use cases: Open book question answering, summarization, draft generation, information extraction, ideation
110
+ Supports: Open book question answering, summarization, draft generation, information extraction, ideation
74
111
 
75
112
  Languages: English, Spanish, French, German, Portuguese, Italian, Dutch
76
113
 
77
114
  ```ruby
78
- client.invoke_model('ai21.j2-ultra-v1', 'Your prompt goes here ...')
115
+ client.invoke_model(id: 'ai21.j2-ultra-v1', prompt: "What's natural language processing?", options: {})
116
+
117
+ # Response
118
+ {:id=>1234,
119
+ :prompt=>
120
+ {:text=>"Describe how an LLM works",
121
+ :tokens=>
122
+ [{:generatedToken=>{:token=>"▁Describe", :logprob=>-10.674324035644531, :raw_logprob=>-10.674324035644531},
123
+ :completions=>
124
+ [{:data=>
125
+ {:text=>
126
+ "\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.",
127
+ :tokens=>
128
+ [{:generatedToken=>{:token=>"<|newline|>", :logprob=>0.0, :raw_logprob=>-0.00046850196667946875},
79
129
  ```
80
130
 
81
- Jurassic-2 Mid
131
+ _*Jurassic-2 Mid*_
82
132
 
83
133
  ```ruby
84
- client.invoke_model('ai21.j2-mid-v1', 'Your prompt goes here ...')
85
- ``` -->
134
+ client.invoke_model(id: 'ai21.j2-mid-v1', prompt: "What's GenAI?", options: {})
135
+
136
+ # Response
137
+ {:id=>1234,
138
+ :prompt=>
139
+ {:text=>"What's GenAI?",
140
+ :tokens=>
141
+ [{:generatedToken=>{:token=>"▁What's", :logprob=>-9.553738594055176, :raw_logprob=>-9.553738594055176},
142
+ :topTokens=>nil,
143
+ :textRange=>{:start=>0, :end=>6}},
144
+ :completions=>
145
+ [{:data=>
146
+ {:text=>
147
+ "\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.",
148
+ :tokens=>
149
+ [{:generatedToken=>{:token=>"<|newline|>", :logprob=>0.0, :raw_logprob=>-0.0009662011871114373},
150
+ ```
86
151
 
87
152
  ### Amazon Titan
88
153
 
89
154
  Amazon Titan Foundation Models are pre-trained on large datasets, making them powerful, general-purpose models. Use them as is, or customize them by fine tuning the models with your own data for a particular task without annotating large volumes of data. [See more ...](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-embed-text-v1)
90
155
 
156
+ Options for these models are:
157
+
158
+ ```ruby
159
+ {
160
+ temperature: 0.5, # float
161
+ top_p: 0.5, # float
162
+ max_tokens: 512, # integer
163
+ stop_sequences: [] # [string]
164
+ }
165
+ ```
166
+
167
+ For more documentation about the parameter data types and values you can [See more details...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-text.html]
168
+
91
169
  How to call Amazon Titan text models:
92
170
 
93
171
  _*Titan Text G1 - Lite*_
@@ -97,7 +175,7 @@ _*Titan Text G1 - Lite*_
97
175
  Supports: Text generation, Code generation, Rich text formatting, Orchestration (Agents), Fine Tuning.
98
176
 
99
177
  ```ruby
100
- client.invoke_model(id: 'amazon.titan-text-lite-v1', input: 'Generate a story about rubies and gems')
178
+ client.invoke_model(id: 'amazon.titan-text-lite-v1', prompt: 'Generate a story about rubies and gems', options: {})
101
179
 
102
180
  # Response
103
181
  {:inputTextTokenCount=>8,
@@ -108,14 +186,12 @@ client.invoke_model(id: 'amazon.titan-text-lite-v1', input: 'Generate a story ab
108
186
  :completionReason=>"FINISH"}]}
109
187
  ```
110
188
 
111
- <br>
112
-
113
189
  _*Titan Text G1 - Express*_
114
190
 
115
191
  [https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-express-v1](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-express-v1)
116
192
 
117
193
  ```ruby
118
- client.invoke_model(id: 'amazon.titan-text-express-v1', input: 'Generate a post about cats formatted with HTML tags')
194
+ client.invoke_model(id: 'amazon.titan-text-express-v1', prompt: 'Generate a post about cats formatted with HTML tags', options: {})
119
195
 
120
196
  # Response
121
197
  {:inputTextTokenCount=>9,
@@ -132,6 +208,20 @@ Supports: Text generation, Code generation, Rich text formatting, Orchestration
132
208
 
133
209
  Anthropic offers the Claude family of large language models purpose built for conversations, summarization, Q&A, workflow automation, coding and more. Early customers report that Claude is much less likely to produce harmful outputs, easier to converse with, and more steerable - so you can get your desired output with less effort. Claude can also take direction on personality, tone, and behavior.
134
210
 
211
+ Options for these models are:
212
+
213
+ ```ruby
214
+ {
215
+ temperature: 0.3, # float
216
+ top_p: 0.5, # float
217
+ top_k: 0.5, # float
218
+ max_tokens: 1000, # integer
219
+ stop_sequences: [] # [string]
220
+ }
221
+ ```
222
+
223
+ For more documentation about the parameter data types and values you can [See more details...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html]
224
+
135
225
  How to call Anthropic models:
136
226
 
137
227
  _*Claude Instant 1.2*_
@@ -159,7 +249,7 @@ _*Claude 1.3*_
159
249
  Supports: Question answering, [information extraction](https://docs.anthropic.com/claude/docs/text-processing), removing PII, content generation, [multiple choice classification](https://docs.anthropic.com/claude/docs/multiple-choice-and-classification), [Roleplay](https://docs.anthropic.com/claude/docs/roleplay-dialogue), comparing text, summarization, [document Q&A with citation](https://docs.anthropic.com/claude/docs/advanced-text-analysis)
160
250
 
161
251
  ```ruby
162
- client.invoke_model(id: 'anthropic.claude-instant-v1', input: "You will be acting as a AI customer success agent for a company called Acme Dynamics.")
252
+ client.invoke_model(id: 'anthropic.claude-v1', prompt: "You will be acting as a AI customer success agent for a company called Acme Dynamics.")
163
253
 
164
254
  # Response
165
255
  {:completion=>
@@ -177,7 +267,7 @@ _*Claude 2*_
177
267
  Supports: Question answering, [information extraction](https://docs.anthropic.com/claude/docs/text-processing), removing PII, content generation, [multiple choice classification](https://docs.anthropic.com/claude/docs/multiple-choice-and-classification), [Roleplay](https://docs.anthropic.com/claude/docs/roleplay-dialogue), comparing text, summarization, [document Q&A with citation](https://docs.anthropic.com/claude/docs/advanced-text-analysis)
178
268
 
179
269
  ```ruby
180
- client.invoke_model(id: 'anthropic.claude-v2', input: "I'm going to provide some text. I want to remove all person
270
+ client.invoke_model(id: 'anthropic.claude-v2', prompt: "I'm going to provide some text. I want to remove all person
181
271
  ally identifying information from this text and replace it with XXX. It's very important that PII such as names, phone numbers,
182
272
  and home and email addresses, get replaced with XXX.")
183
273
 
@@ -192,6 +282,24 @@ ally identifying information from this text and replace it with XXX. It's very i
192
282
 
193
283
  Cohere models are text generation models for business use cases. Cohere models are trained on data that supports reliable business applications, like text generation, summarization, copywriting, dialogue, extraction, and question answering.
194
284
 
285
+ Options for command models are:
286
+
287
+ ```ruby
288
+ {
289
+ temperature: 0.3, # float
290
+ top_p: 0.5, # float
291
+ top_k: 0.5, # float
292
+ max_tokens: 1000, # integer
293
+ stop_sequences: [], # [string]
294
+ num_generations: 2, # integer
295
+ return_likelihoods: 'ALL', # string
296
+ stream: true, # boolean
297
+ truncate: 'END' # srtring
298
+ }
299
+ ```
300
+
301
+ For more documentation about the parameter data types and values you can [See more details...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-cohere-command.html]
302
+
195
303
  How to call Cohere command models:
196
304
 
197
305
  _*Command*_
@@ -201,7 +309,7 @@ _*Command*_
201
309
  Supports: Summarization, copywriting, dialogue, extraction, and question answering.
202
310
 
203
311
  ```ruby
204
- client.invoke_model(id: 'cohere.command-text-v14', input: 'Generate a twit about why Ruby on Rails is a great tool for building a startup. Write a few hashtags')
312
+ client.invoke_model(id: 'cohere.command-text-v14', prompt: 'Generate a twit about why Ruby on Rails is a great tool for building a startup. Write a few hashtags')
205
313
 
206
314
  # Response
207
315
  {:generations=>
@@ -222,7 +330,7 @@ _*Command Light*_
222
330
  Supports: Summarization, copywriting, dialogue, extraction, and question answering.
223
331
 
224
332
  ```ruby
225
- client.invoke_model(id: 'cohere.command-light-text-v14', input: 'Generate a facebook post about GenAI models available at Amazon Bedrock')
333
+ client.invoke_model(id: 'cohere.command-light-text-v14', prompt: 'Generate a facebook post about GenAI models available at Amazon Bedrock')
226
334
 
227
335
  # Response
228
336
  {:generations=>
@@ -234,6 +342,17 @@ client.invoke_model(id: 'cohere.command-light-text-v14', input: 'Generate a face
234
342
  :prompt=>"Generate a facebook post about GenAI models available at Amazon Bedrock:"}
235
343
  ```
236
344
 
345
+ Options for embed models are:
346
+
347
+ ```ruby
348
+ {
349
+ input_type: 'classification', # string
350
+ truncate: 'END' # string
351
+ }
352
+ ```
353
+
354
+ For more documentation about the parameter data types and values you can [See more details...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-embed.html]
355
+
237
356
  How to call Cohere embed models:
238
357
 
239
358
  _*Embed English*_
@@ -244,7 +363,7 @@ Supports: Semantic search, retrieval-augmented generation (RAG), classification,
244
363
 
245
364
  ```ruby
246
365
  # WIP
247
- # client.invoke_model(id: 'cohere.embed-english-v3', input: 'Your prompt goes here ...')
366
+ # client.invoke_model(id: 'cohere.embed-english-v3', prompt: 'Your prompt goes here ...')
248
367
  ```
249
368
 
250
369
  <br>
@@ -255,13 +374,23 @@ _*Embed Multilingual*_
255
374
 
256
375
  ```ruby
257
376
  # WIP
258
- # client.invoke_model(id: 'cohere.embed-multilingual-v3', input: 'Your prompt goes here ...')
377
+ # client.invoke_model(id: 'cohere.embed-multilingual-v3', prompt: 'Your prompt goes here ...')
259
378
  ```
260
379
 
261
380
  ### Meta
262
381
 
263
382
  Meta is looking to unlock the power of large language models. Our latest version of Llama is now accessible to individuals, creators, researchers and businesses of all sizes so that they can experiment, innovate and scale their ideas responsibly.
264
383
 
384
+ Options for these models are:
385
+
386
+ ```ruby
387
+ {
388
+ max_tokens: 128, # integer
389
+ temperature: 0.9, # float
390
+ top_p: 0.7 # float
391
+ }
392
+ ```
393
+
265
394
  How to call Meta models:
266
395
 
267
396
  _*Llama 2 Chat 13B*_
@@ -271,7 +400,7 @@ _*Llama 2 Chat 13B*_
271
400
  Supports: Llama 2 is intended for commercial and research use in English. Fine-tuned chat models are intended for chat based applications.
272
401
 
273
402
  ```ruby
274
- client.invoke_model(id: 'meta.llama2-13b-chat-v1', input: 'Generate an Instagram Post about the Metaverse')
403
+ client.invoke_model(id: 'meta.llama2-13b-chat-v1', prompt: 'Generate an Instagram Post about the Metaverse')
275
404
 
276
405
  # Resopnse
277
406
  {:generation=>
@@ -290,7 +419,7 @@ _*Llama 2 Chat 70B*_
290
419
  Supports: Llama 2 is intended for commercial and research use in English. Fine-tuned chat models are intended for chat based applications.
291
420
 
292
421
  ```ruby
293
- client.invoke_model(id: 'meta.llama2-70b-chat-v1', input: 'Generate a Facebook add to promote a new website that is selling Ruby on Rails and AI courses')
422
+ client.invoke_model(id: 'meta.llama2-70b-chat-v1', prompt: 'Generate a Facebook add to promote a new website that is selling Ruby on Rails and AI courses')
294
423
 
295
424
  # Response
296
425
  {:generation=>
@@ -304,6 +433,16 @@ client.invoke_model(id: 'meta.llama2-70b-chat-v1', input: 'Generate a Facebook a
304
433
 
305
434
  Stability AI is the world's leading open-source generative artificial intelligence company, collaborating with public and private sector partners to bring next generation infrastructure to a global audience.
306
435
 
436
+ Options for these models are:
437
+
438
+ ```ruby
439
+ {
440
+ cfg_scale: 20, # integer
441
+ seed: 1, # integer
442
+ steps: 10 # integer
443
+ }
444
+ ```
445
+
307
446
  How to call Stability AI models:
308
447
 
309
448
  _*SDXL 0.8*_
@@ -313,7 +452,7 @@ _*SDXL 0.8*_
313
452
  Supports: image generation, image editing
314
453
 
315
454
  ```ruby
316
- client.invoke_model(id: 'stability.stable-diffusion-xl-v0', input: 'Generate an image of an orca jumping out of the water', options: { file_path: 'path/to/your/image.jpg' })
455
+ client.invoke_model(id: 'stability.stable-diffusion-xl-v0', prompt: 'Generate an image of an orca jumping out of the water', options: { file_path: 'path/to/your/image.jpg' })
317
456
  # NOTE: If file_path is not provided the image will be saved at 'image.jpg'
318
457
 
319
458
  # Success Response
@@ -338,7 +477,7 @@ Example generated image
338
477
  _*SDXL 1.0*_
339
478
 
340
479
  ```ruby
341
- client.invoke_model(id: 'stability.stable-diffusion-xl-v1', input: 'Generate an image of a white gold ring with a ruby on it', options: { file_path: 'path/to/your/image.jpg' })
480
+ client.invoke_model(id: 'stability.stable-diffusion-xl-v1', prompt: 'Generate an image of a white gold ring with a ruby on it', options: { file_path: 'path/to/your/image.jpg' })
342
481
  # NOTE: If file_path is not provided the image will be saved at 'image.jpg'
343
482
 
344
483
  # Success Response
@@ -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.2"
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,27 +14,29 @@ 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
 
26
27
  # Invokes a model using the Bedrock Runtime client.
27
28
  #
28
29
  # @param id [String] The ID of the model to be invoked.
29
- # @param input [String] The input string for what needs to be generated.
30
+ # @param prompt [String] The prompt string for what needs to be generated.
30
31
  # @param options [Hash] Additional options for the model invocation.
31
32
  # @return [Aws::BedrockRuntime::Types::InvokeModelOutput] The output from invoking the model.
32
33
  # @example Invoke a model with specific ID and input
33
34
  # client = RubyAmazonBedrock::Client.new
34
35
  # client.invoke_model(
35
- # id: 'model_id', input: 'This is what you want to generate', options: { option_key: 'option_value' }
36
+ # id: 'model_id', prompt: 'This is what you want to generate', options: { option_key: 'option_value' }
36
37
  # )
37
- def invoke_model(id:, input:, options: {})
38
- payload_builder_class = RubyAmazonBedrock::PayloadFactory.new(id, input, options).create
38
+ def invoke_model(id:, prompt:, options: {})
39
+ payload_builder_class = RubyAmazonBedrock::PayloadFactory.new(id, prompt, options).create
39
40
  response = @client.invoke_model(payload_builder_class.build)
40
41
 
41
42
  response_builder_class = RubyAmazonBedrock::ResponseFactory.new(payload_builder_class.type, response,
@@ -34,14 +34,14 @@ module RubyAmazonBedrock
34
34
  content_type: 'application/json',
35
35
  accept: '*/*',
36
36
  body: {
37
- prompt: @input,
38
- maxTokens: 200,
39
- temperature: 0,
40
- topP: 250,
41
- stop_sequences: [],
42
- countPenalty: { scale: 0 },
43
- presencePenalty: { scale: 0 },
44
- frequencyPenalty: { scale: 0 }
37
+ prompt: @prompt,
38
+ maxTokenCount: parameters[:maxTokenCount],
39
+ temperature: parameters[:temperature],
40
+ topP: parameters[:topP],
41
+ stopSequences: parameters[:stopSequences],
42
+ countPenalty: { scale: parameters[:countPenalty] },
43
+ presencePenalty: { scale: parameters[:presencePenalty] },
44
+ frequencyPenalty: { scale: parameters[:frequencyPenalty] }
45
45
  }.to_json
46
46
  }
47
47
  end
@@ -49,6 +49,18 @@ module RubyAmazonBedrock
49
49
  def model_id
50
50
  # noop
51
51
  end
52
+
53
+ def parameters # rubocop:disable Metrics/CyclomaticComplexity
54
+ {
55
+ maxTokenCount: @options[:max_tokens] || 200,
56
+ stopSequences: @options[:stop_sequences] || [],
57
+ temperature: @options[:temperature] || 0,
58
+ topP: @options[:top_p] || 1,
59
+ countPenalty: @options[:count_penalty] || 0,
60
+ presencePenalty: @options[:presence_penalty] || 0,
61
+ frequencyPenalty: @options[:frequency_penalty] || 0
62
+ }
63
+ end
52
64
  end
53
65
  end
54
66
  end
@@ -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
@@ -31,13 +31,8 @@ module RubyAmazonBedrock
31
31
  content_type: 'application/json',
32
32
  accept: '*/*',
33
33
  body: {
34
- inputText: @input,
35
- textGenerationConfig: {
36
- maxTokenCount: 4096,
37
- stopSequences: [],
38
- temperature: 0,
39
- topP: 1
40
- }
34
+ inputText: @prompt,
35
+ textGenerationConfig: parameters
41
36
  }.to_json
42
37
  }
43
38
  end
@@ -45,6 +40,15 @@ module RubyAmazonBedrock
45
40
  def model_id
46
41
  # noop
47
42
  end
43
+
44
+ def parameters
45
+ {
46
+ maxTokenCount: @options[:max_tokens] || 4096,
47
+ stopSequences: @options[:stop_sequences] || [],
48
+ temperature: @options[:temperature] || 0,
49
+ topP: @options[:top_p] || 1
50
+ }
51
+ end
48
52
  end
49
53
  end
50
54
  end
@@ -18,7 +18,7 @@ module RubyAmazonBedrock
18
18
  body: {
19
19
  taskType: "TEXT_IMAGE",
20
20
  textToImageParams: {
21
- text: @input,
21
+ text: @prompt,
22
22
  negativeTtext: @options[:negative_text]
23
23
  },
24
24
  imageGenerationConfig: {
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'base'
4
+
3
5
  module RubyAmazonBedrock
4
6
  module PayloadBuilders
5
7
  module Amazon
@@ -9,21 +11,11 @@ module RubyAmazonBedrock
9
11
  # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-lite-v1
10
12
  # for more information about the Amazon model.
11
13
  class TitanTextLiteV1 < Base
12
- def build
13
- {
14
- model_id: 'amazon.titan-text-lite-v1',
15
- content_type: 'application/json',
16
- accept: '*/*',
17
- body: {
18
- inputText: @input,
19
- textGenerationConfig: {
20
- maxTokenCount: 4096,
21
- stopSequences: [],
22
- temperature: 0,
23
- topP: 1
24
- }
25
- }.to_json
26
- }
14
+ # Returns the model ID for the Amazon Titan Text G1 - Lite model.
15
+ #
16
+ # @return [String] 'amazon.titan-text-lite-v1'
17
+ def model_id
18
+ 'amazon.titan-text-lite-v1'
27
19
  end
28
20
  end
29
21
  end