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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for streaming chunks #4

Merged
merged 5 commits into from
Apr 25, 2024
Merged

Add support for streaming chunks #4

merged 5 commits into from
Apr 25, 2024

Conversation

drnic
Copy link
Owner

@drnic drnic commented Apr 25, 2024

Either pass a block to the chat() method with one or two arguments:

@client = Groq::Client.new(model_id: "llama3-70b-8192")

@client.chat("Write a long poem about patience") do |content|
  print content
end
puts

Or pass a Proc or callable object to stream: keyword argument:

class MessageBits
  def initialize(emoji)
    print "#{emoji} "
    @bits = []
  end

  def call(content)
    if content.nil?
      puts
    else
      print(content)
      @bits << content
    end
  end

  def to_s
    @bits.join("")
  end

  def to_assistant_message
    Assistant(to_s)
  end
end

bits = MessageBits.new("馃崟")
@client.chat("Write a long poem about pizza", stream: bits)

Credit to https://github.com/alexrudall/ruby-openai/blob/main/lib/openai/http.rb for borrowed code

@client = Groq::Client.new(model_id: "llama3-70b-8192")
@client.chat("Write a long poem about patience") do |content|
  print content
end
puts
@drnic drnic force-pushed the streaming branch 3 times, most recently from 3a8b945 to d787350 Compare April 25, 2024 08:51
@drnic drnic merged commit 0662e56 into develop Apr 25, 2024
2 checks passed
@drnic drnic deleted the streaming branch April 25, 2024 09:08
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

1 participant