Skip to content
Nagendra Dhanakeerthi edited this page Oct 30, 2024 · 1 revision

Frequently Asked Questions (FAQ)

General Questions

What is chatgpt-ruby?

A Ruby gem that provides a simple and intuitive interface to OpenAI's GPT models, supporting both chat and completion endpoints.

Which Ruby versions are supported?

Ruby 3.0 and above are supported. The gem is tested on Ruby 3.0, 3.1, and 3.2.

Which OpenAI models are supported?

Currently supports:

  • GPT-3.5-turbo (default for chat)
  • text-davinci-002 (default for completions)
  • Other GPT-3.x models

Installation & Setup

How do I install the gem?

# In your Gemfile
gem 'chatgpt-ruby'

# Or install directly
gem install chatgpt-ruby

Why am I getting authentication errors?

Common reasons:

  1. API key not set correctly
  2. Invalid API key
  3. API key not loaded from environment variables

Solution:

# Check your API key
puts ENV['OPENAI_API_KEY'].nil? ? "API key missing" : "API key present"

# Set explicitly
client = ChatGPT::Client.new('your-api-key')

How do I configure the gem?

ChatGPT.configure do |config|
  config.api_key = ENV['OPENAI_API_KEY']
  config.request_timeout = 30
  config.max_retries = 3
end

Usage Questions

How do I make a simple chat request?

client = ChatGPT::Client.new
response = client.chat([
  { role: "user", content: "Hello!" }
])

How do I handle streaming responses?

client.chat_stream(messages) do |chunk|
  print chunk.dig("choices", 0, "delta", "content")
end

What's the difference between chat and completions?

  • chat: Modern endpoint for conversational AI (recommended)
  • completions: Legacy endpoint for text completion tasks

Error Handling

How do I handle rate limits?

begin
  response = client.chat(messages)
rescue ChatGPT::RateLimitError => e
  sleep(5)  # Wait before retrying
  retry
end

What do I do if requests time out?

# Increase timeout in configuration
ChatGPT.configure do |config|
  config.request_timeout = 60  # 60 seconds
end

How do I debug request issues?

begin
  response = client.chat(messages)
rescue ChatGPT::APIError => e
  puts "Status: #{e.status_code}"
  puts "Error: #{e.message}"
  puts "Type: #{e.error_type}"
end

Performance

How do I optimize response times?

  1. Use appropriate max_tokens
  2. Keep messages concise
  3. Use streaming for long responses
  4. Implement proper error handling

How do I handle concurrent requests?

The gem is thread-safe. Use normal Ruby concurrency patterns:

threads = messages.map do |msg|
  Thread.new { client.chat([msg]) }
end
responses = threads.map(&:value)

Testing

How do I test code using this gem?

# In test_helper.rb
require 'webmock/minitest'

# In your tests
def test_chat
  stub_request(:post, "https://api.openai.com/v1/chat/completions")
    .to_return(status: 200, body: response_json)
    
  response = @client.chat(messages)
  assert_equal expected_response, response
end

How do I mock responses in tests?

# Helper method for stubbing
def stub_chat_response(content)
  {
    "choices" => [{
      "message" => {
        "role" => "assistant",
        "content" => content
      }
    }]
  }.to_json
end

Security

Is it safe to store API keys in code?

No, always use environment variables or secure credential management:

# Good
api_key = ENV['OPENAI_API_KEY']

# Bad
api_key = 'sk-...'  # Never do this

How do I secure API keys in Rails?

Use Rails credentials:

# config/credentials.yml.enc
openai:
  api_key: your_key_here

# Usage
ChatGPT.configure do |config|
  config.api_key = Rails.application.credentials.openai[:api_key]
end

Troubleshooting

Why is my request failing?

Common reasons:

  1. Invalid API key
  2. Rate limits exceeded
  3. Invalid request parameters
  4. Network issues

How do I get better error messages?

begin
  response = client.chat(messages)
rescue => e
  puts "Error Class: #{e.class}"
  puts "Message: #{e.message}"
  puts "Backtrace: #{e.backtrace.first(5)}"
end

Support

Where can I get help?

  1. GitHub Issues
  2. Documentation
  3. OpenAI API Documentation

How do I report a bug?

Create a GitHub issue with:

  1. Ruby version (ruby -v)
  2. Gem version
  3. Minimal reproduction code
  4. Error message
  5. Expected behavior
Clone this wiki locally