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

Add streaming with Faraday #234

merged 53 commits into from
Apr 26, 2023

Conversation

alexrudall
Copy link
Owner

@alexrudall alexrudall commented Apr 2, 2023

  • WIP on v4 of the gem - main breaking change is switching HTTP library to improve performance and allow streaming and parallel requests
  • Switch to Faraday
  • Add streaming to Chat as MVP

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 22336 lines exceeds the maximum allowed for the inline comments feature.

@alexrudall alexrudall mentioned this pull request Apr 2, 2023
Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 22335 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 22338 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 22338 lines exceeds the maximum allowed for the inline comments feature.

This was referenced Apr 2, 2023
@alexrudall alexrudall changed the title Add streaming with Faraday & Typhoeus WIP: Add streaming with Faraday & Typhoeus Apr 2, 2023
@@ -25,7 +25,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "httparty", ">= 0.18.1"
spec.add_dependency "faraday", "2.7.4"
spec.add_dependency "faraday-typhoeus", "1.0.0"
Copy link

Choose a reason for hiding this comment

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

I personally have no interest in using or install typhoeus on my system. The advantage of Faraday is you can leave those decisions to someone else.

If you want to test Faraday with typhoeus in the gem, the pattern for that would be to put typhoeus in the Gemfile but not the gemspec, that way it's not required.

Also, our Faraday requirement in our app

gem "faraday", ">= 2"

We happen to be on 2.7.4, but I'd prefer not to be locked in to that version

-  spec.add_dependency "faraday", "2.7.4"
+  spec.add_dependency "faraday", ">= 2"

Copy link
Owner Author

Choose a reason for hiding this comment

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

Great points, thanks for review! Experimenting with Faraday only version.

lib/openai/client.rb Outdated Show resolved Hide resolved
lib/openai.rb Outdated Show resolved Hide resolved
@codenamev
Copy link
Contributor

This looks great! How can I help push this through the finish line?

@alexrudall alexrudall changed the title WIP: Add streaming with Faraday & Typhoeus WIP: Add streaming with Faraday Apr 22, 2023
Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 23814 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 23814 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 23812 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 23812 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 23810 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 24977 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 26402 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 26402 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 26397 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 26397 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 26397 lines exceeds the maximum allowed for the inline comments feature.

@alexrudall alexrudall changed the title WIP: Add streaming with Faraday Add streaming with Faraday Apr 24, 2023
Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 26998 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 27377 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 27377 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 27380 lines exceeds the maximum allowed for the inline comments feature.

@alexrudall
Copy link
Owner Author

Streaming getting there :)

Apr-24-2023.18-38-28.mp4

@alexrudall
Copy link
Owner Author

alexrudall commented Apr 25, 2023

Thanks for your input all. Would appreciate any final reviews 👍

  • This PR switches the HTTP library from HTTParty to Faraday and adds streaming functionality on the Chat endpoint.
  • Breaking change: endpoints now return JSON rather than HTTParty objects. You need to update your code to handle this change, changing code like JSON.parse(response.body)["key"] and response.parsed_response["key"] to just response["key"].
  • README examples should all be updated and working and including Chat streaming.
  • Edit; To use this branch in your Gemfile do this: gem "ruby-openai", github: "alexrudall/ruby-openai", branch: "faraday", and then bundle update ruby-openai to get the latest

@alexrudall alexrudall mentioned this pull request Apr 25, 2023
lib/openai/client.rb Outdated Show resolved Hide resolved
@fwitzke fwitzke mentioned this pull request Apr 25, 2023
3 tasks
lib/openai/client.rb Outdated Show resolved Hide resolved
@alexrudall alexrudall merged commit 5683eb9 into main Apr 26, 2023
@kmcphillips
Copy link
Contributor

This is rad. Thanks for this change! ❤️

@gastonmorixe
Copy link

Thank you everyone!

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants