Inference Activity for Ruby

A Ruby gem that provides Net::HTTP extensions for tracking inference activities on Heroku AI. This gem is the Ruby equivalent of inference-activity-axios.

Installation

Add this line to your application's Gemfile:

gem 'inference_activity'

And then execute:

bundle install

Or install it yourself as:

gem install inference_activity

Usage

The gem automatically extends Net::HTTP to track inference activities. Simply require it in your code:

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

# Set up your environment variables
ENV['INFERENCE_ACTIVITY_URL'] = 'your-activity-logging-endpoint'
ENV['INFERENCE_ACTIVITY_KEY'] = 'your-api-key'

# Make requests as usual with Net::HTTP
uri = URI('https://api.heroku.ai/v1/chat/completions')
request = Net::HTTP::Post.new(uri)
request['Authorization'] = "Bearer #{ENV['API_KEY']}"
request['Content-Type'] = 'application/json'
request.body = {
  messages: [
    { role: 'user', content: 'Hello!' }
  ],
  model: 'gpt-3.5-turbo'
}.to_json

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
  http.request(request)
end

The gem will automatically:

  • Track the request/response
  • Measure response time
  • Redact sensitive information
  • Send activity logs to your specified endpoint

Features

  • Automatically tracks inference activities for Heroku AI endpoints
  • Redacts sensitive information from requests and responses
  • Tracks response times and logs activity data
  • Handles the following endpoints:
    • /v1/chat/completions
    • /v1/embeddings
    • /v1/images/generations

Environment Variables

  • INFERENCE_ACTIVITY_URL: The endpoint where activity logs will be sent
  • INFERENCE_ACTIVITY_KEY: The API key for authentication when sending activity logs

Activity Data Format

The gem sends the following data structure to your activity logging endpoint:

{
  timestamp: 1234567890123,  # Unix timestamp in milliseconds
  response_time: 500,        # Response time in milliseconds
  status_code: 200,          # HTTP status code
  status_message: "OK",      # HTTP status message
  request: {
    method: "POST",
    url: "https://api.heroku.ai/v1/chat/completions",
    params: {},
    body: {
      messages: "[REDACTED]",
      model: "gpt-3.5-turbo"
    }
  },
  response: {
    headers: {
      "content-type" => "application/json",
      # ...
    },
    data: {
      choices: [{
        message: {
          content: "[REDACTED]",
          role: "assistant"
        }
      }],
      # ...
    }
  }
}

Differences from inference-activity-axios

This gem provides the same functionality as inference-activity-axios but is adapted for Ruby's Net::HTTP:

  1. Uses Ruby's Net::HTTP extension system instead of Axios interceptors
  2. Automatically applies to all Net::HTTP requests in the application
  3. Uses Ruby's Time class for timestamps
  4. Maintains consistent millisecond-precision timestamps for compatibility

Development

After checking out the repo, run bundle install to install dependencies. Then, run rake spec to run the tests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/add-co/inference-activity-ruby.

License

The gem is available as open source under the terms of the MIT License.