Deep-dive on the Next Gen Platform. Join the Webinar!

Skip Navigation
Show nav
Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
    • .NET
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
Hide categories

Categories

  • Heroku Architecture
    • Compute (Dynos)
      • Dyno Management
      • Dyno Concepts
      • Dyno Behavior
      • Dyno Reference
      • Dyno Troubleshooting
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Developer Tools
    • Command Line
    • Heroku VS Code Extension
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery & Integration (Heroku Flow)
    • Continuous Integration
  • Language Support
    • Node.js
      • Working with Node.js
      • Troubleshooting Node.js Apps
      • Node.js Behavior in Heroku
    • Ruby
      • Rails Support
      • Working with Bundler
      • Working with Ruby
      • Ruby Behavior in Heroku
      • Troubleshooting Ruby Apps
    • Python
      • Working with Python
      • Background Jobs in Python
      • Python Behavior in Heroku
      • Working with Django
    • Java
      • Java Behavior in Heroku
      • Working with Java
      • Working with Maven
      • Working with Spring Boot
      • Troubleshooting Java Apps
    • PHP
      • PHP Behavior in Heroku
      • Working with PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
    • .NET
      • Working with .NET
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
      • Migrating to Heroku Postgres
    • Heroku Key-Value Store
    • Apache Kafka on Heroku
    • Other Data Stores
  • AI
    • Working with AI
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
      • Single Sign-on (SSO)
    • Private Spaces
      • Infrastructure Networking
    • Compliance
  • Heroku Enterprise
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Add-ons
  • All Add-ons
  • Ruby Quick Start Guide for v1-chat-completions API

Ruby Quick Start Guide for v1-chat-completions 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

  • Prerequisites
  • Ruby Example Code

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.

Our Claude chat models (Claude 3.5 Sonnet latest, Claude 3.5 Sonnet, Claude 3.5 Haiku,and Claude 3.0 Haiku) generate conversational completions for input messages. This guide walks you through how to use the v1-chat-completions API with Ruby.

Prerequisites

Before making requests, provision access to the model of your choice.

  1. If it’s not already installed, install the Heroku CLI. Then install the Heroku AI plugin:

    heroku plugins:install @heroku/plugin-ai
    
  2. Attach a chat 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 one of our chat models to your app, $APP_NAME:
    heroku ai:models:create -a $APP_NAME claude-3-5-sonnet --as INFERENCE
    # OR
    heroku ai:models:create -a $APP_NAME claude-3-haiku --as INFERENCE
    

Ruby Example Code

require 'net/http'
require 'json'
require 'uri'

# Fetch required environment variables or raise an error if missing
INFERENCE_URL = ENV.fetch('INFERENCE_URL') do
  raise <<~ERROR
    Environment variable 'INFERENCE_URL' is missing.
    Set it using:
      export INFERENCE_URL=$(heroku config:get -a $APP_NAME INFERENCE_URL)
  ERROR
end

INFERENCE_KEY = ENV.fetch('INFERENCE_KEY') do
  raise <<~ERROR
    Environment variable 'INFERENCE_KEY' is missing.
    Set it using:
      export INFERENCE_KEY=$(heroku config:get -a $APP_NAME INFERENCE_KEY)
  ERROR
end

INFERENCE_MODEL_ID = ENV.fetch('INFERENCE_MODEL_ID') do
  raise <<~ERROR
    Environment variable 'INFERENCE_MODEL_ID' is missing.
    Set it using:
      export INFERENCE_MODEL_ID=$(heroku config:get -a $APP_NAME INFERENCE_MODEL_ID)
  ERROR
end

##
# Parses and prints the API response for the chat completion request.
#
# @param response [Net::HTTPResponse] The response object from the API call.
def parse_chat_output(response)
  if response.is_a?(Net::HTTPSuccess)
    result = JSON.parse(response.body)
    content = result.dig('choices', 0, 'message', 'content')
    puts "Chat Completion: #{content}"
  else
    puts "Request failed: #{response.code}, #{response.body}"
  end
end

##
# Generates a chat completion using the Stability AI Chat Model.
#
# @param payload [Hash] Hash containing parameters for the chat completion request.
def generate_chat_completion(payload)
  uri = URI.join(INFERENCE_URL, '/v1/chat/completions')
  request = Net::HTTP::Post.new(uri)
  request['Authorization'] = "Bearer #{INFERENCE_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_chat_output(response)
end

# Example payload
payload = {
  model: INFERENCE_MODEL_ID,
  messages: [
    { role: 'user',      content: 'Hello!' },
    { role: 'assistant', content: 'Hi there! How can I assist you today?' },
    { role: 'user',      content: 'Why is Heroku so cool?' }
  ],
  temperature: 0.5,
  max_tokens: 100,
  stream: false
}

# Generate a chat completion with the given payload
generate_chat_completion(payload)

Keep reading

  • All Add-ons

Feedback

Log in to submit feedback.

Zara 4 S3 Hero Dev

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure
  • .NET

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing
  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Github
  • LinkedIn
  • © 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
  • heroku.com
  • Legal
  • Terms of Service
  • Privacy Information
  • Responsible Disclosure
  • Trust
  • Contact
  • Cookie Preferences
  • Your Privacy Choices