Skip to content

Instantly share code, notes, and snippets.

@lucasluitjes
Created March 16, 2023 10:54
Show Gist options
  • Save lucasluitjes/0bf82de475ac91fe2ad8e71d5c2df164 to your computer and use it in GitHub Desktop.
Save lucasluitjes/0bf82de475ac91fe2ad8e71d5c2df164 to your computer and use it in GitHub Desktop.
sinatra app that streams openai chat responses
require 'bundler/setup'
require 'dotenv'
require 'sinatra'
require 'sinatra/streaming'
require 'json'
require 'typhoeus'
set :server, 'thin' # not sure if puma also has streaming nowadays?
Dotenv.load
API_BASE_URL = "https://api.openai.com/v1"
ACCESS_TOKEN = ENV["OPENAI_ACCESS_TOKEN"]
def streaming_completions_request(prompt)
headers = {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{ACCESS_TOKEN}"
}
request = Typhoeus::Request.new(
"https://api.openai.com/v1/completions",
method: :post,
headers: headers,
body: {
prompt: prompt,
model: "text-davinci-003",
stream: true,
max_tokens: 400
}.to_json
)
request
end
get '/stream_completions' do
content_type 'text/event-stream'
cache_control :no_cache
stream(:keep_open) do |out|
request = streaming_completions_request("Once upon a time...")
request.on_body do |chunk|
data_line = chunk.lines.find { |line| line.start_with?('data:') }
next unless data_line
json_str = data_line.sub('data:', '').strip
json = JSON.parse(json_str)
out << "data: #{json.to_json}\n\n"
end
request.on_complete do |response|
puts response.inspect
out << "event: close\n"
out << "data: {}\n\n"
out.close
end
request.run
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment