Ruby SDK

Official Ruby SDK for HINOW API with streaming helpers

Ruby SDK

Install and configure the HINOW Ruby SDK to integrate over 100 AI models into Ruby applications with idiomatic Ruby patterns.


The HINOW Ruby SDK provides full access to the HINOW REST API with Ruby idioms, streaming support, and comprehensive error handling.

> Note: For API resource documentation with code examples, see the API Reference. This page covers Ruby SDK-specific features and configurations.

Installation

bash
gem install hinow

Or add to your Gemfile:

ruby
gem 'hinow'

Requirements

  • Ruby 3.0+

Configuration

Basic Setup

ruby
require 'hinow'

client = Hinow::Client.new(api_key: 'your-api-key')

Environment Variable

bash
export HINOW_API_KEY=your-api-key

ruby
# API key loaded automatically from environment
client = Hinow::Client.new

Advanced Configuration

ruby
client = Hinow::Client.new(
  api_key: 'your-api-key',
  base_url: 'https://api.hinow.ai',
  timeout: 120,
  max_retries: 3
)

Basic Usage

Chat Completions

ruby
require 'hinow'

client = Hinow::Client.new

response = client.chat.completions.create(
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'What is the capital of France?' }
  ],
  temperature: 0.7,
  max_tokens: 1024
)

puts response.choices[0].message.content

Using Different Models

ruby
# OpenAI GPT-4o
response = client.chat.completions.create(
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Explain machine learning' }]
)

# Anthropic Claude
response = client.chat.completions.create(
  model: 'claude-sonnet-4-20250514',
  messages: [{ role: 'user', content: 'Explain machine learning' }]
)

# DeepSeek
response = client.chat.completions.create(
  model: 'deepseek-ai/deepseek-v3.2',
  messages: [{ role: 'user', content: 'Explain machine learning' }]
)

Streaming

ruby
client.chat.completions.create(
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Write a story about a robot' }],
  stream: true
) do |chunk|
  content = chunk.choices[0].delta.content
  print content if content
end

Function Calling (Tool Use)

ruby
response = client.chat.completions.create(
  model: 'gpt-4o',
  messages: [
    { role: 'user', content: 'What is the weather in New York?' }
  ],
  tools: [
    {
      type: 'function',
      function: {
        name: 'get_weather',
        description: 'Get the current weather for a location',
        parameters: {
          type: 'object',
          properties: {
            location: {
              type: 'string',
              description: 'City and state, e.g., New York, NY'
            },
            unit: {
              type: 'string',
              enum: %w[celsius fahrenheit]
            }
          },
          required: ['location']
        }
      }
    }
  ],
  tool_choice: 'auto'
)

message = response.choices[0].message

if message.tool_calls
  message.tool_calls.each do |tool_call|
    puts "Function: #{tool_call.function.name}"
    puts "Arguments: #{tool_call.function.arguments}"
  end
end

Image Generation

ruby
response = client.images.generate(
  model: 'black-forest-labs/flux-1-schnell',
  prompt: 'A programmer cat wearing glasses, cartoon style',
  size: '1024x1024',
  quality: 'hd'
)

response.data.each do |image|
  puts "URL: #{image.url}"
end

Embeddings

ruby
response = client.embeddings.create(
  model: 'BAAI/bge-base-en-v1.5',
  input: 'Machine learning is fascinating'
)

embedding = response.data[0].embedding
puts "Dimensions: #{embedding.length}"

Error Handling

ruby
require 'hinow'

begin
  response = client.chat.completions.create(
    model: 'nonexistent-model',
    messages: [{ role: 'user', content: 'Hello' }]
  )
rescue Hinow::AuthenticationError
  puts 'Invalid API key'
rescue Hinow::InsufficientBalanceError
  puts 'Insufficient balance'
rescue Hinow::RateLimitError => e
  puts "Rate limit reached. Retry after: #{e.retry_after}"
rescue Hinow::BadRequestError => e
  puts "Invalid request: #{e.message}"
rescue Hinow::APIError => e
  puts "API Error [#{e.status_code}]: #{e.message}"
end

Rails Integration

ruby
# config/initializers/hinow.rb
Rails.application.config.hinow = Hinow::Client.new

# In your controller
class ChatController < ApplicationController
  def create
    client = Rails.application.config.hinow

    response = client.chat.completions.create(
      model: 'gpt-4o',
      messages: [{ role: 'user', content: params[:message] }]
    )

    render json: { message: response.choices[0].message.content }
  end
end

Additional Resources