Skip to content

Commit

Permalink
Move HTTP logging to custom
Browse files Browse the repository at this point in the history
HTTP.rb  has issues when logging with streaming
  • Loading branch information
ksylvest committed Jun 21, 2024
1 parent 5fe854c commit f97e25e
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Metrics/ParameterLists:
RSpec/NestedGroups:
Enabled: false

RSpec/MultipleMemoizedHelpers:
Enabled: false

RSpec/SpecFilePathFormat:
CustomTransform:
OmniAI: omniai
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
omniai (1.3.0)
omniai (1.3.1)
event_stream_parser
http
zeitwerk
Expand Down
3 changes: 3 additions & 0 deletions lib/omniai/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def initialize(messages, client:, model:, temperature: nil, stream: nil, format:
# @raise [HTTPError]
def process!
response = request!

@client.log(response:)

raise HTTPError, response.flush unless response.status.ok?

parse!(response:)
Expand Down
11 changes: 10 additions & 1 deletion lib/omniai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@ def masked_api_key
# @return [HTTP::Client]
def connection
http = HTTP.persistent(@host)
http = http.use(logging: { logger: @logger }) if @logger
http = http.timeout(@timeout) if @timeout
http
end

# @param response [HTTP::Response]
def log(response:)
return if @logger.nil?

request = response.request

@logger.info("#{request.verb.upcase} #{request.uri}")
@logger.info("#{response.status.code} #{response.status.reason}")
end

# @raise [OmniAI::Error]
#
# @param messages [String, Array, Hash]
Expand Down
3 changes: 3 additions & 0 deletions lib/omniai/speak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def initialize(input, client:, model:, voice:, speed: nil, format: nil)
# @return [Tempfile]
def process!(&block)
response = request!

@client.log(response:)

raise HTTPError, response.flush unless response.status.ok?

if block
Expand Down
3 changes: 3 additions & 0 deletions lib/omniai/transcribe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def initialize(io, client:, model:, language: nil, prompt: nil, temperature: nil
# @return [OmniAI::Transcribe::Transcription]
def process!
response = request!

@client.log(response:)

raise HTTPError, response.flush unless response.status.ok?

text = @format.nil? || @format.eql?(Format::JSON) ? response.parse['text'] : String(response.body)
Expand Down
2 changes: 1 addition & 1 deletion lib/omniai/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module OmniAI
VERSION = '1.3.0'
VERSION = '1.3.1'
end
20 changes: 19 additions & 1 deletion spec/omniai/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:api_key) { 'abcdef' }
let(:host) { 'http://localhost:8080' }
let(:timeout) { 5 }
let(:logger) { Logger.new($stdout) }
let(:logger) { instance_double(Logger) }

describe '#api_key' do
it { expect(client.api_key).to eq(api_key) }
Expand Down Expand Up @@ -35,4 +35,22 @@
describe '#inspect' do
it { expect(client.inspect).to eq('#<OmniAI::Client api_key="abc***" host="http://localhost:8080">') }
end

describe '#log' do
let(:response) { instance_double(HTTP::Response, request:, status:) }
let(:request) { instance_double(HTTP::Request, uri: '/chat', verb: 'POST') }
let(:status) { instance_double(HTTP::Response::Status, code: 200, reason: 'OK') }

it 'logs the request' do
allow(logger).to receive(:info)
client.log(response:)
expect(logger).to have_received(:info).with('POST /chat')
end

it 'logs the response' do
allow(logger).to receive(:info)
client.log(response:)
expect(logger).to have_received(:info).with('200 OK')
end
end
end

0 comments on commit f97e25e

Please sign in to comment.