ruby-amazon-bedrock 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +112 -18
  5. data/lib/amazon_bedrock/version.rb +1 -1
  6. data/lib/bedrock_runtime/client.rb +4 -4
  7. data/lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb +20 -8
  8. data/lib/bedrock_runtime/payload_builders/amazon/base.rb +11 -7
  9. data/lib/bedrock_runtime/payload_builders/amazon/titan_image_generator_v1.rb +1 -1
  10. data/lib/bedrock_runtime/payload_builders/amazon/titan_text_lite_v1.rb +7 -15
  11. data/lib/bedrock_runtime/payload_builders/anthropic/base.rb +16 -8
  12. data/lib/bedrock_runtime/payload_builders/base.rb +9 -2
  13. data/lib/bedrock_runtime/payload_builders/cohere/command_base.rb +25 -4
  14. data/lib/bedrock_runtime/payload_builders/cohere/embed_base.rb +10 -2
  15. data/lib/bedrock_runtime/payload_builders/meta/base.rb +12 -4
  16. data/lib/bedrock_runtime/payload_builders/stability_ai/base.rb +12 -4
  17. data/lib/bedrock_runtime/payload_factory.rb +3 -3
  18. data/lib/bedrock_runtime/response_builders/image.rb +4 -0
  19. data/spec/bedrock_runtime/client_spec.rb +2 -2
  20. data/spec/bedrock_runtime/payload_builders/ai_21_labs/base_spec.rb +9 -11
  21. data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1_spec.rb +9 -12
  22. data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_ultra_v1_spec.rb +10 -13
  23. data/spec/bedrock_runtime/payload_builders/amazon/base_spec.rb +7 -9
  24. data/spec/bedrock_runtime/payload_builders/amazon/titan_text_express_v1_spec.rb +7 -10
  25. data/spec/bedrock_runtime/payload_builders/amazon/titan_text_lite_v1_spec.rb +7 -10
  26. data/spec/bedrock_runtime/payload_builders/anthropic/base_spec.rb +8 -10
  27. data/spec/bedrock_runtime/payload_builders/anthropic/claude_instant_v1_spec.rb +8 -11
  28. data/spec/bedrock_runtime/payload_builders/anthropic/claude_v1_spec.rb +8 -11
  29. data/spec/bedrock_runtime/payload_builders/anthropic/claude_v2_spec.rb +8 -11
  30. data/spec/bedrock_runtime/payload_builders/base_spec.rb +1 -1
  31. data/spec/bedrock_runtime/payload_builders/cohere/command_base_spec.rb +16 -11
  32. data/spec/bedrock_runtime/payload_builders/cohere/command_light_text_v14_spec.rb +16 -12
  33. data/spec/bedrock_runtime/payload_builders/cohere/command_text_v14_spec.rb +16 -12
  34. data/spec/bedrock_runtime/payload_builders/cohere/embed_base_spec.rb +8 -10
  35. data/spec/bedrock_runtime/payload_builders/cohere/embed_english_v3_spec.rb +9 -11
  36. data/spec/bedrock_runtime/payload_builders/cohere/embed_multilingual_v3_spec.rb +9 -11
  37. data/spec/bedrock_runtime/payload_builders/meta/base_spec.rb +8 -10
  38. data/spec/bedrock_runtime/payload_builders/meta/llama213b_chat_v1_spec.rb +8 -11
  39. data/spec/bedrock_runtime/payload_builders/meta/llama270b_chat_v1_spec.rb +8 -11
  40. data/spec/bedrock_runtime/payload_builders/stability_ai/base_spec.rb +8 -10
  41. data/spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0_spec.rb +8 -11
  42. data/spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1_spec.rb +8 -11
  43. data/spec/bedrock_runtime/payload_factory_spec.rb +1 -1
  44. data/spec/spec_helper.rb +2 -0
  45. data/spec/support/helpers.rb +169 -0
  46. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3a872c53afed3f2fb371cbc1c4b661ad00d02583e7a31f1558f6081462d2740
4
- data.tar.gz: 5c5ed1f988239ecdf8a50e35c870544794f2c976572018feaf21404eb8097f01
3
+ metadata.gz: e34184a2246b1054a64d6d29bc7bb1144487a508c6d27f717d8f92edb4cb32a2
4
+ data.tar.gz: ae40e48b1107bf163bbb30e4c058d660784f1bf9cc6fcafa0bfa9843b526dd49
5
5
  SHA512:
6
- metadata.gz: 74db07e19c34b58d106c0e9847c9e244b48642f32eaf4b5f6b339aff974dcdbbad9b3949eb0076773f331a7843d87e6230e1cf2386fb9d8eecc8b6c3c2385fb2
7
- data.tar.gz: 307e7f513903954428303c8ab40c86df98d694770ce2022c405088a4042f2095504962102d43d50ec70124aafe9d7198e0db77cfd14c5e9e189693cb65866deb
6
+ metadata.gz: fcdfeb1017d3bf2cefeb1bf77b68a85bd36bd3a7906651b1a56f8ff2544309ba98eee08fc1f04ce80ed22b738302fd38e7811087f56a976c6eeea1f7cfae0010
7
+ data.tar.gz: f19b95ef5965317c53eda21c96c69e421abdb34ce441cc850731663aa8ed35bbc67000d02a34ebc2f192ce7fbe81e0d18afe3ab59d0dbea38e32c858625fcc89
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
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
+
1
6
  ## [0.2.1] - 2023-12-25
2
7
 
3
8
  - Make gem configurable
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-amazon-bedrock (0.2.1)
4
+ ruby-amazon-bedrock (0.2.2)
5
5
  aws-sdk-bedrockruntime (~> 1.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -14,7 +14,7 @@ Amazon Bedrock is a fully managed service that makes FMs from leading AI startup
14
14
  Add the following line to your application's Gemfile:
15
15
 
16
16
  ```ruby
17
- gem "ruby-amazon-bedrock", "~> 0.2.1"
17
+ gem "ruby-amazon-bedrock", "~> 0.2.2"
18
18
  ```
19
19
 
20
20
  And then execute:
@@ -75,6 +75,10 @@ end
75
75
  client = RubyAmazonBedrock::Client.new
76
76
  ```
77
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
+
78
82
  ## Model Providers
79
83
 
80
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.
@@ -83,16 +87,32 @@ Amazon Bedrock is a fully managed service that makes FMs from leading AI startup
83
87
 
84
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)
85
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
+
86
106
  How to call AI21 models:
87
107
 
88
108
  _*Jurassic-2 Ultra*_
89
109
 
90
- 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
91
111
 
92
112
  Languages: English, Spanish, French, German, Portuguese, Italian, Dutch
93
113
 
94
114
  ```ruby
95
- client.invoke_model(id: 'ai21.j2-ultra-v1', input: "What's natural language processing?")
115
+ client.invoke_model(id: 'ai21.j2-ultra-v1', prompt: "What's natural language processing?", options: {})
96
116
 
97
117
  # Response
98
118
  {:id=>1234,
@@ -111,7 +131,7 @@ client.invoke_model(id: 'ai21.j2-ultra-v1', input: "What's natural language proc
111
131
  _*Jurassic-2 Mid*_
112
132
 
113
133
  ```ruby
114
- client.invoke_model(id: 'ai21.j2-mid-v1', input: "What's GenAI?")
134
+ client.invoke_model(id: 'ai21.j2-mid-v1', prompt: "What's GenAI?", options: {})
115
135
 
116
136
  # Response
117
137
  {:id=>1234,
@@ -133,6 +153,19 @@ client.invoke_model(id: 'ai21.j2-mid-v1', input: "What's GenAI?")
133
153
 
134
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)
135
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
+
136
169
  How to call Amazon Titan text models:
137
170
 
138
171
  _*Titan Text G1 - Lite*_
@@ -142,7 +175,7 @@ _*Titan Text G1 - Lite*_
142
175
  Supports: Text generation, Code generation, Rich text formatting, Orchestration (Agents), Fine Tuning.
143
176
 
144
177
  ```ruby
145
- 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: {})
146
179
 
147
180
  # Response
148
181
  {:inputTextTokenCount=>8,
@@ -153,14 +186,12 @@ client.invoke_model(id: 'amazon.titan-text-lite-v1', input: 'Generate a story ab
153
186
  :completionReason=>"FINISH"}]}
154
187
  ```
155
188
 
156
- <br>
157
-
158
189
  _*Titan Text G1 - Express*_
159
190
 
160
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)
161
192
 
162
193
  ```ruby
163
- 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: {})
164
195
 
165
196
  # Response
166
197
  {:inputTextTokenCount=>9,
@@ -177,6 +208,20 @@ Supports: Text generation, Code generation, Rich text formatting, Orchestration
177
208
 
178
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.
179
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
+
180
225
  How to call Anthropic models:
181
226
 
182
227
  _*Claude Instant 1.2*_
@@ -204,7 +249,7 @@ _*Claude 1.3*_
204
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)
205
250
 
206
251
  ```ruby
207
- 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.")
208
253
 
209
254
  # Response
210
255
  {:completion=>
@@ -222,7 +267,7 @@ _*Claude 2*_
222
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)
223
268
 
224
269
  ```ruby
225
- 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
226
271
  ally identifying information from this text and replace it with XXX. It's very important that PII such as names, phone numbers,
227
272
  and home and email addresses, get replaced with XXX.")
228
273
 
@@ -237,6 +282,24 @@ ally identifying information from this text and replace it with XXX. It's very i
237
282
 
238
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.
239
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
+
240
303
  How to call Cohere command models:
241
304
 
242
305
  _*Command*_
@@ -246,7 +309,7 @@ _*Command*_
246
309
  Supports: Summarization, copywriting, dialogue, extraction, and question answering.
247
310
 
248
311
  ```ruby
249
- 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')
250
313
 
251
314
  # Response
252
315
  {:generations=>
@@ -267,7 +330,7 @@ _*Command Light*_
267
330
  Supports: Summarization, copywriting, dialogue, extraction, and question answering.
268
331
 
269
332
  ```ruby
270
- 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')
271
334
 
272
335
  # Response
273
336
  {:generations=>
@@ -279,6 +342,17 @@ client.invoke_model(id: 'cohere.command-light-text-v14', input: 'Generate a face
279
342
  :prompt=>"Generate a facebook post about GenAI models available at Amazon Bedrock:"}
280
343
  ```
281
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
+
282
356
  How to call Cohere embed models:
283
357
 
284
358
  _*Embed English*_
@@ -289,7 +363,7 @@ Supports: Semantic search, retrieval-augmented generation (RAG), classification,
289
363
 
290
364
  ```ruby
291
365
  # WIP
292
- # 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 ...')
293
367
  ```
294
368
 
295
369
  <br>
@@ -300,13 +374,23 @@ _*Embed Multilingual*_
300
374
 
301
375
  ```ruby
302
376
  # WIP
303
- # 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 ...')
304
378
  ```
305
379
 
306
380
  ### Meta
307
381
 
308
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.
309
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
+
310
394
  How to call Meta models:
311
395
 
312
396
  _*Llama 2 Chat 13B*_
@@ -316,7 +400,7 @@ _*Llama 2 Chat 13B*_
316
400
  Supports: Llama 2 is intended for commercial and research use in English. Fine-tuned chat models are intended for chat based applications.
317
401
 
318
402
  ```ruby
319
- 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')
320
404
 
321
405
  # Resopnse
322
406
  {:generation=>
@@ -335,7 +419,7 @@ _*Llama 2 Chat 70B*_
335
419
  Supports: Llama 2 is intended for commercial and research use in English. Fine-tuned chat models are intended for chat based applications.
336
420
 
337
421
  ```ruby
338
- 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')
339
423
 
340
424
  # Response
341
425
  {:generation=>
@@ -349,6 +433,16 @@ client.invoke_model(id: 'meta.llama2-70b-chat-v1', input: 'Generate a Facebook a
349
433
 
350
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.
351
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
+
352
446
  How to call Stability AI models:
353
447
 
354
448
  _*SDXL 0.8*_
@@ -358,7 +452,7 @@ _*SDXL 0.8*_
358
452
  Supports: image generation, image editing
359
453
 
360
454
  ```ruby
361
- 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' })
362
456
  # NOTE: If file_path is not provided the image will be saved at 'image.jpg'
363
457
 
364
458
  # Success Response
@@ -383,7 +477,7 @@ Example generated image
383
477
  _*SDXL 1.0*_
384
478
 
385
479
  ```ruby
386
- 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' })
387
481
  # NOTE: If file_path is not provided the image will be saved at 'image.jpg'
388
482
 
389
483
  # Success Response
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyAmazonBedrock
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
@@ -27,16 +27,16 @@ module RubyAmazonBedrock
27
27
  # Invokes a model using the Bedrock Runtime client.
28
28
  #
29
29
  # @param id [String] The ID of the model to be invoked.
30
- # @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.
31
31
  # @param options [Hash] Additional options for the model invocation.
32
32
  # @return [Aws::BedrockRuntime::Types::InvokeModelOutput] The output from invoking the model.
33
33
  # @example Invoke a model with specific ID and input
34
34
  # client = RubyAmazonBedrock::Client.new
35
35
  # client.invoke_model(
36
- # 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' }
37
37
  # )
38
- def invoke_model(id:, input:, options: {})
39
- 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
40
40
  response = @client.invoke_model(payload_builder_class.build)
41
41
 
42
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: 1.0,
41
- stopSequences: [],
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
@@ -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
@@ -33,14 +33,12 @@ module RubyAmazonBedrock
33
33
  content_type: 'application/json',
34
34
  accept: '*/*',
35
35
  body: {
36
- prompt: "\n\nHuman: #{@input}\n\nAssistant:",
37
- max_tokens_to_sample: 300,
38
- temperature: 0.5,
39
- top_k: 250,
40
- top_p: 1,
41
- stop_sequences: [
42
- '\n\nHuman'
43
- ],
36
+ prompt: "\n\nHuman: #{@prompt}\n\nAssistant:",
37
+ max_tokens_to_sample: parameters[:max_tokens_to_sample],
38
+ temperature: parameters[:temperature],
39
+ top_k: parameters[:top_k],
40
+ top_p: parameters[:top_p],
41
+ stop_sequences: parameters[:stop_sequences],
44
42
  anthropic_version: 'bedrock-2023-05-31'
45
43
  }.to_json
46
44
  }
@@ -49,6 +47,16 @@ module RubyAmazonBedrock
49
47
  def model_id
50
48
  # noop
51
49
  end
50
+
51
+ def parameters
52
+ {
53
+ max_tokens_to_sample: @options[:max_tokens] || 200,
54
+ temperature: @options[:temperature] || 0.5,
55
+ top_k: @options[:top_k] || 250,
56
+ top_p: @options[:top_p] || 1,
57
+ stop_sequences: @options[:stop_sequences] || ['\n\nHuman']
58
+ }
59
+ end
52
60
  end
53
61
  end
54
62
  end
@@ -10,8 +10,8 @@ module RubyAmazonBedrock
10
10
  # @param input [String] The input string for what needs to be generated.
11
11
  # @param options [Hash] optional parameters to customize payload building.
12
12
  # @option options [Any] :key Custom option key-value pairs.
13
- def initialize(input, options = {})
14
- @input = input
13
+ def initialize(prompt, options = {})
14
+ @prompt = prompt
15
15
  @options = options
16
16
  end
17
17
 
@@ -29,6 +29,13 @@ module RubyAmazonBedrock
29
29
  raise NotImplementedError
30
30
  end
31
31
 
32
+ # Abstract method to set the model parameters to be sent in the request.
33
+ # @raise [NotImplementedError] if the subclass does not implement this method.
34
+ # @return [Hash] the Amazon Bedrock model configuration parameters.
35
+ def parameters
36
+ raise NotImplementedError
37
+ end
38
+
32
39
  # Abstract method to retrieve the model type.
33
40
  # @return [Symbol] the model result type: :text (default) or :image.
34
41
  def type
@@ -29,10 +29,16 @@ module RubyAmazonBedrock
29
29
  content_type: 'application/json',
30
30
  accept: '*/*',
31
31
  body: {
32
- prompt: "#{@input}:",
33
- max_tokens: 100,
34
- temperature: 0.8
35
- # return_likelihood: 'GENERATION' NOTE: This was since it was giving an error
32
+ prompt: "#{@prompt}:",
33
+ temperature: parameters[:temperature],
34
+ p: parameters[:p],
35
+ k: parameters[:k],
36
+ max_tokens: parameters[:max_tokens],
37
+ num_generations: parameters[:num_generations],
38
+ return_likelihoods: parameters[:return_likelihoods],
39
+ stop_sequences: parameters[:stop_sequences],
40
+ stream: parameters[:stream],
41
+ truncate: parameters[:truncate]
36
42
  }.to_json
37
43
  }
38
44
  end
@@ -40,6 +46,21 @@ module RubyAmazonBedrock
40
46
  def model_id
41
47
  # noop
42
48
  end
49
+
50
+ def parameters # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
51
+ {
52
+ temperature: @options[:temperature] || 0.9,
53
+ p: @options[:top_p] || 0.75,
54
+ k: @options[:top_k] || 0,
55
+ max_tokens: @options[:max_tokens] || 20,
56
+ num_generations: @options[:num_generations] || 1,
57
+ return_likelihoods: @options[:return_likelihoods] || 'GENERATION',
58
+ stop_sequences: @options[:stop_sequences] || [],
59
+ stream: @options[:stream] || false,
60
+ # logit_bias: @options[:logit_bias] || {},
61
+ truncate: @options[:truncate] || 'NONE'
62
+ }
63
+ end
43
64
  end
44
65
  end
45
66
  end
@@ -26,8 +26,9 @@ module RubyAmazonBedrock
26
26
  content_type: 'application/json',
27
27
  accept: '*/*',
28
28
  body: {
29
- texts: [@input],
30
- input_type: 'search_document'
29
+ texts: [@prompt],
30
+ input_type: parameters[:input_type],
31
+ truncate: parameters[:truncate]
31
32
  }.to_json
32
33
  }
33
34
  end
@@ -35,6 +36,13 @@ module RubyAmazonBedrock
35
36
  def model_id
36
37
  # noop
37
38
  end
39
+
40
+ def parameters
41
+ {
42
+ input_type: @options[:input_type] || 'search_document',
43
+ truncate: @options[:truncate] || 'NONE'
44
+ }
45
+ end
38
46
  end
39
47
  end
40
48
  end
@@ -28,10 +28,10 @@ module RubyAmazonBedrock
28
28
  content_type: 'application/json',
29
29
  accept: '*/*',
30
30
  body: {
31
- prompt: @input,
32
- max_gen_len: 512,
33
- temperature: 0.2,
34
- top_p: 0.9
31
+ prompt: @prompt,
32
+ max_gen_len: parameters[:max_gen_len],
33
+ temperature: parameters[:temperature],
34
+ top_p: parameters[:top_p]
35
35
  }.to_json
36
36
  }
37
37
  end
@@ -39,6 +39,14 @@ module RubyAmazonBedrock
39
39
  def model_id
40
40
  # noop
41
41
  end
42
+
43
+ def parameters
44
+ {
45
+ max_gen_len: @options[:max_tokens] || 512,
46
+ temperature: @options[:temperature] || 0.5,
47
+ top_p: @options[:top_p] || 0.9
48
+ }
49
+ end
42
50
  end
43
51
  end
44
52
  end
@@ -30,11 +30,11 @@ module RubyAmazonBedrock
30
30
  accept: '*/*',
31
31
  body: {
32
32
  text_prompts: [
33
- { text: @input }
33
+ { text: @prompt }
34
34
  ],
35
- cfg_scale: 10,
36
- seed: 0,
37
- steps: 50
35
+ cfg_scale: parameters[:cfg_scale],
36
+ seed: parameters[:seed],
37
+ steps: parameters[:steps]
38
38
  }.to_json
39
39
  }
40
40
  end
@@ -43,6 +43,14 @@ module RubyAmazonBedrock
43
43
  # noop
44
44
  end
45
45
 
46
+ def parameters
47
+ {
48
+ cfg_scale: @options[:cfg_scale] || 10,
49
+ seed: @options[:seed] || 0,
50
+ steps: @options[:steps] || 30
51
+ }
52
+ end
53
+
46
54
  def type
47
55
  :image
48
56
  end