Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add streaming with Faraday #234

Merged
merged 53 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
84d9eef
Switch to Faraday + Typhoeus
alexrudall Apr 1, 2023
6252467
Update timeout
alexrudall Apr 1, 2023
0e8c22a
Update specs
alexrudall Apr 2, 2023
e4cfc25
Add streaming to Chat
alexrudall Apr 2, 2023
072b667
Clean up
alexrudall Apr 2, 2023
14205c4
Fix for pre-3 Ruby
alexrudall Apr 2, 2023
170ef55
Rubocop
alexrudall Apr 2, 2023
a5cb6e0
Switch to Faraday as http gem
alexrudall Apr 22, 2023
c486807
Loosen gemspec requirements
alexrudall Apr 22, 2023
b1418ad
Revert default timeout
alexrudall Apr 22, 2023
433d011
Update README
alexrudall Apr 22, 2023
93b5a89
Refactor for legibility
alexrudall Apr 22, 2023
e57f7fa
Update spec
alexrudall Apr 22, 2023
fd25855
Rerecord specs
alexrudall Apr 22, 2023
ac26672
Parse JSON in client so this is less of a breaking change
alexrudall Apr 24, 2023
a83dd4c
Make spec more realistic
alexrudall Apr 24, 2023
1aeaec5
Rubocop
alexrudall Apr 24, 2023
74c5d57
Rubocop
alexrudall Apr 24, 2023
07d5820
Rubocop
alexrudall Apr 24, 2023
0dc081b
Fix streaming
alexrudall Apr 24, 2023
aaca0eb
Convert stream chunks to JSON as they come through
alexrudall Apr 24, 2023
0f63205
Refactor
alexrudall Apr 24, 2023
086e571
Remove DONE message from streamed response
alexrudall Apr 24, 2023
5d839b4
Always return an array of chunk(s)
alexrudall Apr 24, 2023
0894c4c
Allow Faraday 1 to work
alexrudall Apr 25, 2023
19eec23
Remove mime-types dependency!
alexrudall Apr 25, 2023
5ff0b89
Refactor
alexrudall Apr 25, 2023
5deff23
Rerun specs
alexrudall Apr 25, 2023
a3a5a6a
Refactor
alexrudall Apr 25, 2023
03148c2
Add Streaming to README
alexrudall Apr 25, 2023
bea6d0b
Update README examples
alexrudall Apr 25, 2023
cac7cc5
Update CHANGELOG
alexrudall Apr 25, 2023
ef4348b
Improve changelog
alexrudall Apr 25, 2023
0cb6860
Parse chunks of `data: {JSON}` and only call the user proc when valid.
rmontgomery429 Apr 25, 2023
33f6e18
Linting fixes based on feedback from Rubocop
rmontgomery429 Apr 25, 2023
ce82858
Do not send bytesize to the user's proc
rmontgomery429 Apr 25, 2023
683f661
Fix linting suggestions from Code Climate (Rubocop)
rmontgomery429 Apr 25, 2023
89e0cbf
Send error messages from OpenAI to the user
rmontgomery429 Apr 25, 2023
455e61a
Cleanup
rmontgomery429 Apr 25, 2023
7d77b47
Update method documentation
rmontgomery429 Apr 25, 2023
7b07133
Remove trailing space
rmontgomery429 Apr 25, 2023
cbbdd1b
Communicate what this heredoc is more correctly
rmontgomery429 Apr 25, 2023
c02665a
Merge pull request #248 from rmontgomery429/faraday-json-parsing
alexrudall Apr 26, 2023
9ee2111
Make method public
alexrudall Apr 26, 2023
0ab28f7
Tweak comments
alexrudall Apr 26, 2023
57dbfd5
Rubocop
alexrudall Apr 26, 2023
1ec477a
Fix timeouts
alexrudall Apr 26, 2023
82d6435
Try more aggressive timeout for CI
alexrudall Apr 26, 2023
1cd6a2c
Refactor
alexrudall Apr 26, 2023
a516b22
Rubocop
alexrudall Apr 26, 2023
58b4003
Rename spec
alexrudall Apr 26, 2023
b7df81c
Update README for streaming
alexrudall Apr 26, 2023
ef1e2bc
Point changelog readers to README
alexrudall Apr 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/openai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,42 @@ def translate(parameters: {})
end

def self.get(path:)
conn.get(uri(path: path), timeout: OpenAI.configuration.request_timeout) do |req|
json_parse(conn.get(uri(path: path), timeout: OpenAI.configuration.request_timeout) do |req|
req.headers = headers
end
end)
end

def self.json_post(path:, parameters:)
conn.post(uri(path: path), timeout: OpenAI.configuration.request_timeout) do |req|
json_parse(conn.post(uri(path: path), timeout: OpenAI.configuration.request_timeout) do |req|
req.headers = headers
if parameters[:stream] && parameters[:on_data].is_a?(Proc)
req.options.on_data = parameters[:on_data]
end
parameters.delete(:on_data)
req.body = parameters.to_json
end
end)
end

def self.multipart_post(path:, parameters: nil)
conn.post(uri(path: path), timeout: OpenAI.configuration.request_timeout) do |req|
json_parse(conn.post(uri(path: path), timeout: OpenAI.configuration.request_timeout) do |req|
req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
alexrudall marked this conversation as resolved.
Show resolved Hide resolved
req.body = multipart_parameters(parameters)
end
end)
end

def self.delete(path:)
conn.delete(uri(path: path), timeout: OpenAI.configuration.request_timeout) do |req|
json_parse(conn.delete(uri(path: path), timeout: OpenAI.configuration.request_timeout) do |req|
req.headers = headers
end
end)
end

def self.json_parse(response)
return unless response

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alexrudall, I come here now in October 😅. I was wondering when happens this case. I have seen that this happen to us when OpenAI returns empty string. Is this documented somewhere why it happens?

end

private_class_method def self.conn
Expand Down
Empty file added lib/openai/json.rb
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions spec/fixtures/cassettes/files_content.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions spec/fixtures/cassettes/files_content_upload.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions spec/fixtures/cassettes/files_delete.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions spec/fixtures/cassettes/files_delete_retrieve.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading