Skip to content

Commit

Permalink
Use Faraday's built in JSON parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudall committed Nov 6, 2023
1 parent 1e463b4 commit dc4d1c6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
26 changes: 9 additions & 17 deletions lib/openai/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,32 @@
module OpenAI
module HTTP
def get(path:)
to_json(conn.get(uri(path: path)) do |req|
conn.get(uri(path: path)) do |req|
req.headers = headers
end&.body)
end&.body
end

def json_post(path:, parameters:)
to_json(conn.post(uri(path: path)) do |req|
conn.post(uri(path: path)) do |req|
configure_json_post_request(req, parameters)
end&.body)
end&.body
end

def multipart_post(path:, parameters: nil)
to_json(conn(multipart: true).post(uri(path: path)) do |req|
conn(multipart: true).post(uri(path: path)) do |req|
req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
req.body = multipart_parameters(parameters)
end&.body)
end&.body
end

def delete(path:)
to_json(conn.delete(uri(path: path)) do |req|
conn.delete(uri(path: path)) do |req|
req.headers = headers
end&.body)
end&.body
end

private

def to_json(string)
return unless string

JSON.parse(string)
rescue JSON::ParserError
# Convert a multiline string of JSON objects to a JSON array.
JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
end

# Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.
# For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could
# be a data object or an error object as described in the OpenAI API documentation.
Expand All @@ -64,6 +55,7 @@ def conn(multipart: false)
f.options[:timeout] = @request_timeout
f.request(:multipart) if multipart
f.response :raise_error
f.response :json
end
end

Expand Down
21 changes: 18 additions & 3 deletions spec/openai/client/finetunes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@

it "succeeds" do
VCR.use_cassette(cassette) do
expect(response.dig("error", "code")).to eq("fine_tune_not_found")
response
rescue Faraday::ResourceNotFound => e
expect(e.response).to include(status: 404)
expect(e.response.dig(:body, "error", "code")).to eq("fine_tune_not_found")
else
raise "Expected to raise Faraday::ResourceNotFound"
end
end
end
Expand All @@ -64,7 +69,12 @@

it "succeeds" do
VCR.use_cassette(cassette) do
expect(response.dig("error", "code")).to eq("fine_tune_not_found")
response
rescue Faraday::ResourceNotFound => e
expect(e.response).to include(status: 404)
expect(e.response.dig(:body, "error", "code")).to eq("fine_tune_not_found")
else
raise "Expected to raise Faraday::ResourceNotFound"
end
end
end
Expand All @@ -75,7 +85,12 @@

it "succeeds" do
VCR.use_cassette(cassette) do
expect(response.dig("error", "code")).to eq("fine_tune_not_found")
response
rescue Faraday::ResourceNotFound => e
expect(e.response).to include(status: 404)
expect(e.response.dig(:body, "error", "code")).to eq("fine_tune_not_found")
else
raise "Expected to raise Faraday::ResourceNotFound"
end
end
end
Expand Down
9 changes: 0 additions & 9 deletions spec/openai/client/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,6 @@
end
end

describe ".to_json" do
context "with a jsonl string" do
let(:body) { "{\"prompt\":\":)\"}\n{\"prompt\":\":(\"}\n" }
let(:parsed) { OpenAI::Client.new.send(:to_json, body) }

it { expect(parsed).to eq([{ "prompt" => ":)" }, { "prompt" => ":(" }]) }
end
end

describe ".uri" do
let(:path) { "/chat" }
let(:uri) { OpenAI::Client.new.send(:uri, path: path) }
Expand Down

0 comments on commit dc4d1c6

Please sign in to comment.