Skip to content

WIP: Implement streaming completions #32

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 14 additions & 2 deletions lib/llm/providers/openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,20 @@ def complete(message, **params)
params = DEFAULT_PARAMS.merge(params)
body = {messages: messages.map(&:to_h)}.merge!(params)
req = preflight(req, body)
res = request(@http, req)
Response::Completion.new(res.body, self).extend(response_parser)
if params[:stream]
Copy link
Member

@0x1eef 0x1eef Nov 8, 2024

Choose a reason for hiding this comment

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

We could offload the complexity to another method

stream!(req, body) if params[:stream]
stream_completion!(req, body) if params[:stream]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@0x1eef I'm still not sure about we go about this

Fiber.new do
@http.request(req) do |res|
res.read_body do |chunk|
chunk.scan(/^data:(.+)$/).each do |match|
Fiber.yield Response::Chunk.new(match[0], self).extend(response_parser)
end
end
end
end
else
res = request(@http, req)
Response::Completion.new(res.body, self).extend(response_parser)
end
end

private
Expand Down
13 changes: 13 additions & 0 deletions lib/llm/providers/openai/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,18 @@ def parse_completion(raw)
total_tokens: raw.dig("usage", "total_tokens")
}
end

##
# @param [Hash] raw
# The raw response from the LLM provider
# @return [Hash]
def parse_completion_chunk(raw)
{
model: raw["model"],
choices: raw["choices"].map do
LLM::Message.new(*_1["delta"].values_at("role", "content"))
end
}
end
end
end
1 change: 1 addition & 0 deletions lib/llm/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module LLM
class Response
require "json"
require_relative "response/completion"
require_relative "response/chunk"
require_relative "response/embedding"

##
Expand Down
15 changes: 15 additions & 0 deletions lib/llm/response/chunk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module LLM
class Response::Chunk < Response::Completion
private

##
# @private
# @return [Hash]
# Returns the parsed completion response from the provider
def parsed
@parsed ||= parse_completion_chunk(raw)
end
end
end