Skip to content

Commit

Permalink
Move Chunk under Response namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ksylvest committed Jul 12, 2024
1 parent 2a17e69 commit 8f91b21
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 47 deletions.
44 changes: 0 additions & 44 deletions lib/omniai/chat/chunk.rb

This file was deleted.

46 changes: 46 additions & 0 deletions lib/omniai/chat/response/chunk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

module OmniAI
class Chat
module Response
# A chunk returned by the API.
class Chunk < Resource
# @return [String]
def inspect
"#<#{self.class.name} id=#{id.inspect} model=#{model.inspect} choices=#{choices.inspect}>"
end

# @return [String]
def id
@data['id']
end

# @return [Time]
def created
Time.at(@data['created']) if @data['created']
end

# @return [Time]
def updated
Time.at(@data['updated']) if @data['updated']
end

# @return [String]
def model
@data['model']
end

# @return [Array<OmniAI::Chat::Response::DeltaChoice>]
def choices
@choices ||= @data['choices'].map { |data| DeltaChoice.new(data:) }
end

# @param index [Integer]
# @return [OmniAI::Chat::Response::DeltaChoice]
def choice(index: 0)
choices[index]
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/omniai/chat/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def stream!
@parser.feed(chunk) do |_, data|
next if data.eql?('[DONE]')

yield(Chunk.new(data: JSON.parse(data)))
yield(Response::Chunk.new(data: JSON.parse(data)))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe OmniAI::Chat::Chunk do
RSpec.describe OmniAI::Chat::Response::Chunk do
subject(:chunk) { described_class.new(data:) }

let(:data) do
Expand Down Expand Up @@ -34,6 +34,6 @@
end

describe '#inspect' do
it { expect(chunk.inspect).to eq('#<OmniAI::Chat::Chunk id="fake_id" model="fake_model" choices=[]>') }
it { expect(chunk.inspect).to eq('#<OmniAI::Chat::Response::Chunk id="fake_id" model="fake_model" choices=[]>') }
end
end

0 comments on commit 8f91b21

Please sign in to comment.