Skip to content

Commit 5bd0adc

Browse files
committed
fixup! feat: implement streaming for OpenAI
1 parent 73b82fb commit 5bd0adc

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/llm/providers/openai.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ def complete(message, **params)
3030
params = DEFAULT_PARAMS.merge(params)
3131
body = {messages: messages.map(&:to_h)}.merge!(params)
3232
req = preflight(req, body)
33+
puts req.body
3334
if params[:stream]
3435
Fiber.new do
3536
@http.request(req) do |res|
3637
res.read_body do |chunk|
37-
Fiber.yield Response::Chunk.new(chunk[/{.*}/], self).extend(response_parser)
38+
chunk.scan(/^data:(.+)$/).each do |match|
39+
break if match[0].strip == "[DONE]"
40+
next if match[0].strip.empty?
41+
Fiber.yield Response::Chunk.new(match[0], self).extend(response_parser)
42+
end
3843
end
3944
end
4045
end

0 commit comments

Comments
 (0)