Ruby Quick Start Guide for v1-embeddings API
Last updated January 27, 2025
This article is a work in progress, or documents a feature that is not yet released to all users. This article is unlisted. Only those with the link can access it.
Table of Contents
The Heroku Managed Inference and Agent add-on is currently in pilot. The products offered as part of the pilot aren’t intended for production use and are considered as a Beta Service and are subject to the Beta Services terms at https://www.salesforce.com/company/legal/agreements.jsp.
The Cohere Embed Multilingual (cohere-embed-multilingual
) model generates vector embeddings (lists of numbers) for provided text inputs. These embeddings can be used in various applications, such as search, classification, and clustering. This guide shows how to access the v1-embeddings API using Ruby.
Prerequisites
Before making requests, provision access to the model of your choice.
If it’s not already installed, install the Heroku CLI. Then install the Heroku AI plugin:
heroku plugins:install @heroku/plugin-ai
Attach the embedding model to an app of yours:
# If you don't have an app yet, you can create one with: heroku create $APP_NAME # specify the name you want for your app (or skip this step to use an existing app you have) # Create and attach the embedding model to your app, $APP_NAME. heroku ai:models:create -a $APP_NAME cohere-multilingual --as EMBEDDING
Ruby Example Code
# frozen_string_literal: true
require 'net/http'
require 'json'
require 'uri'
EMBEDDING_URL = ENV.fetch('EMBEDDING_URL') do
raise <<~ERROR
Environment variable 'EMBEDDING_URL' is missing.
Set it using:
export EMBEDDING_URL=$(heroku config:get -a $APP_NAME EMBEDDING_URL)
ERROR
end
EMBEDDING_KEY = ENV.fetch('EMBEDDING_KEY') do
raise <<~ERROR
Environment variable 'EMBEDDING_KEY' is missing.
Set it using:
export EMBEDDING_KEY=$(heroku config:get -a $APP_NAME EMBEDDING_KEY)
ERROR
end
EMBEDDING_MODEL_ID = ENV.fetch('EMBEDDING_MODEL_ID') do
raise <<~ERROR
Environment variable 'EMBEDDING_MODEL_ID' is missing.
Set it using:
export EMBEDDING_MODEL_ID=$(heroku config:get -a $APP_NAME EMBEDDING_MODEL_ID)
ERROR
end
##
# Parses and prints the API response for the embedding request.
#
# @param response [Net::HTTPResponse] The response object from the API call.
def parse_embedding_output(response)
if response.is_a?(Net::HTTPSuccess)
result = JSON.parse(response.body)
puts "Embeddings: #{result['data']}"
else
puts "Request failed: #{response.code}, #{response.body}"
end
end
##
# Generates embeddings using the Stability AI Embeddings model.
#
# @param payload [Hash] Hash containing parameters for the embedding generation.
def generate_embeddings(payload)
uri = URI.join(EMBEDDING_URL, '/v1/embeddings')
request = Net::HTTP::Post.new(uri)
request['Authorization'] = "Bearer #{EMBEDDING_KEY}"
request['Content-Type'] = 'application/json'
request.body = payload.to_json
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(request)
end
parse_embedding_output(response)
end
# Example payload
payload = {
model: EMBEDDING_MODEL_ID,
input: [
'Hello, I am a blob of text.',
"How's the weather in Portland?"
],
input_type: 'search_document',
truncate: 'END',
encoding_format: 'float'
}
# Generate embeddings with the given payload
generate_embeddings(payload)