Created
April 18, 2023 22:33
-
-
Save searls/0c8d0867ac2f78cdf6e195b2d4006e4a to your computer and use it in GitHub Desktop.
net/http's `read_body` is all you need to read streaming responses from the ChatGPT API. No need for a gem dependency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "../config/environment" | |
uri = URI("https://api.openai.com/v1/chat/completions") | |
req = Net::HTTP::Post.new(uri) | |
req["Content-Type"] = "application/json" | |
req["Authorization"] = "Bearer #{ENV["OPEN_AI_API_KEY"]}" | |
req.body = JSON.dump({ | |
model: "gpt-4", | |
max_tokens: 80, | |
messages: [{role: "user", content: "What's the best way to learn Ruby?"}], | |
stream: true | |
}) | |
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| | |
http.request(req) do |res| | |
if Net::HTTPSuccess === res | |
res.read_body do |chunk| | |
segments = chunk.split("data: ").map(&:strip).reject { |s| s.empty? || s == "[DONE]" } | |
segments.each do |segment| | |
payload = JSON.parse(segment).dig("choices", 0, "delta") | |
print payload["content"] | |
end | |
rescue | |
puts "Failed while reading:\n\n#{chunk}" | |
end | |
else | |
puts "Failed with #{res.code}" | |
puts res.body | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment