How can you use invoke a model in AWS Bedrock using Ruby?

268 Views Asked by At

I found it difficult to understand in the docs how to use the AWS Ruby SDK to invoke models on AWS Bedrock.

2

There are 2 best solutions below

0
On

Add this gem: gem "aws-sdk-bedrockruntime"

Invoke models using the BedrockRuntime client.

client = Aws::BedrockRuntime::Client.new
resp = client.invoke_model(
  body: { texts: ["data"], input_type: "search_document" }.to_json,
  model_id: "cohere.embed-english-v3",
  content_type: "application/json",
  accept: "*/*"
)

embeddings = JSON.parse(resp.body)["embeddings"]

The specific structure of the params to invoke_model can be found in the 'Providers' tab of the Bedrock section in AWS, under the specific model you're interested in.

0
On

I created the ruby-amazon-bedrock gem https://github.com/AAlvAAro/ruby-amazon-bedrock, which is designed to enhance the ease of leveraging the aws-sdk-bedrockruntime gem. It provides a more intuitive interface and abstracts some of the complexities involved in dealing with different payload structures.

Here are a few examples of how you can use it:

client = RubyAmazonBedrock::Client.new(
  region: "aws_region",
  access_key_id: "access_key_id",
  access_token: "access_token"
)

# Claude Instant 1.2
client.invoke_model(
  id: "anthropic.claude-instant-v1",
  input: "What is a neural network?"
)

# Meta: Llama 2 Chat 70B
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"
)

# Stability AI: SDXL 1.0
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: "ruby-ring.jpg" }
)

I've tried to make the documentation as clear as possible, but I'm always looking for feedback to improve it. I hope this gem makes your work with AWS Bedrock a bit smoother. Let me know if it helps or if you have any further questions!