Skip to content

Instantly share code, notes, and snippets.

@timrwilliams
timrwilliams / README.md
Created September 19, 2023 09:17
OpenAI process streaming JSON in Ruby

Summary

This example shows how to process a JSON response from the OpenAI Chat API in streaming mode.

  • We are looking for a JSON key named questions which contains an array of JSON objects with the keys question and answer
  • As each question and answer pair is found we process it via the end_object block.

Dependencies

https://github.com/dgraham/json-stream

Keybase proof

I hereby claim:

  • I am timrwilliams on github.
  • I am timrwilliams (https://keybase.io/timrwilliams) on keybase.
  • I have a public key ASCF7rBSZKT4Ky7-WsaVO4KgpqhM2cxa8GzcTBfaIV4DZgo

To claim this, I am signing this object:

@timrwilliams
timrwilliams / clone_schema.sql
Created August 7, 2018 16:06
Clone Postgres schema
-- Function: clone_schema(text, text)
-- DROP FUNCTION clone_schema(text, text);
CREATE OR REPLACE FUNCTION clone_schema(
source_schema text,
dest_schema text,
include_recs boolean)
RETURNS void AS
$BODY$
@timrwilliams
timrwilliams / i18ncount.awk
Last active January 3, 2018 12:25
Count number of unique keys and words in a Rails translation file
awk '{ if( $1 != "#" ){ print $0 } }' en.yml | awk -F':' '{print $2}' | awk 'NF'
@timrwilliams
timrwilliams / .irbrc
Created October 4, 2017 14:24
Choose an Apartment Tenant on start of IRB/Rails console (works on Heroku)
puts "What tenant do you require? "
tenant_names = Apartment.tenant_names
tenant_names.each_with_index do | tenant, index |
puts "#{index}: #{tenant}"
end
tenant_index = gets.chomp.to_i
tenant = tenant_names[tenant_index]
tenant = "public" if tenant.blank?
Apartment::Tenant.switch!(tenant)
puts "Current tenant: #{Apartment::Tenant.current}"
module RailsAppName
class Application < Rails::Application
# .... your settings
require "#{Rails.root}/lib/cloud_flare_middleware"
config.middleware.insert_before(0, Rack::CloudFlareMiddleware)
# ... your settings
end
end
@timrwilliams
timrwilliams / designer.html
Last active August 29, 2015 14:08
designer
<link rel="import" href="../core-scaffold/core-scaffold.html">
<link rel="import" href="../core-header-panel/core-header-panel.html">
<link rel="import" href="../core-menu/core-menu.html">
<link rel="import" href="../core-item/core-item.html">
<link rel="import" href="../core-icon-button/core-icon-button.html">
<link rel="import" href="../core-toolbar/core-toolbar.html">
<link rel="import" href="../core-menu/core-submenu.html">
<link rel="import" href="../core-icons/core-icons.html">
<link rel="import" href="../paper-item/paper-item.html">
@timrwilliams
timrwilliams / qgoverride
Last active December 21, 2015 21:23
Override QuotaGuard Static Socksify env variable setup with your own socks.conf file
#!/bin/bash
# Script inspired by and derived from Proximo (https://github.com/pirateradio/proximo-stacklet)
# Usage ./qgoverride rails s
if [ "${QUOTAGUARDSTATIC_URL}" == "" ]; then
echo "No QUOTAGUARDSTATIC_URL env variable found. Have you installed QuotaGuard Static?"
echo "If using Heroku:"
echo " heroku addons:add QuotaGuardstatic:test"
echo "If using another platform sign-up at quotaguard.com and follow documentation to manually set variable"
exit 2
@timrwilliams
timrwilliams / unicorn.rb
Created October 8, 2014 09:21
Sample Unicorn.rb
#config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
@timrwilliams
timrwilliams / data.json
Last active November 27, 2019 09:21
Sample setup for refreshing a Rickshaw graph from remote JSON source
[{"color":"#DDDF0D","name":"maps.googleapis.com","data":[{"x":1397779200,"y":13},{"x":1397782800,"y":11},{"x":1397786400,"y":10},{"x":1397790000,"y":12},{"x":1397793600,"y":14},{"x":1397797200,"y":13},{"x":1397800800,"y":6},{"x":1397804400,"y":0},{"x":1397808000,"y":0},{"x":1397811600,"y":2},{"x":1397815200,"y":33},{"x":1397818800,"y":17},{"x":1397822400,"y":7},{"x":1397826000,"y":16},{"x":1397829600,"y":29},{"x":1397833200,"y":25},{"x":1397836800,"y":22},{"x":1397840400,"y":30},{"x":1397844000,"y":27},{"x":1397847600,"y":27},{"x":1397851200,"y":18},{"x":1397854800,"y":23},{"x":1397858400,"y":19},{"x":1397862000,"y":7},{"x":1397865600,"y":9},{"x":1397869200,"y":8},{"x":1397872800,"y":21},{"x":1397876400,"y":9},{"x":1397880000,"y":5},{"x":1397883600,"y":6},{"x":1397887200,"y":2},{"x":1397890800,"y":0},{"x":1397894400,"y":0},{"x":1397898000,"y":0},{"x":1397901600,"y":23},{"x":1397905200,"y":0},{"x":1397908800,"y":5},{"x":1397912400,"y":2},{"x":1397916000,"y":5},{"x":1397919600,"y":14},{"x":1397923200,"y":21},{"