Skip to content

Commit

Permalink
Merge pull request #2 from gbaptista/gb-utf8
Browse files Browse the repository at this point in the history
Fixing encoding issues (incompatible character encodings: UTF-8 and ASCII-8BIT (Encoding::CompatibilityError))
  • Loading branch information
gbaptista authored Apr 13, 2024
2 parents 37dd4b3 + 756ec0f commit 24d05b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions controllers/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def request(path, payload = nil, server_sent_events: nil, request_method: 'POST'

method_to_call = request_method.to_s.strip.downcase.to_sym

partial_json = ''
partial_json = String.new.force_encoding('UTF-8')

response = Faraday.new(request: @request_options) do |faraday|
faraday.adapter @faraday_adapter
Expand All @@ -110,7 +110,13 @@ def request(path, payload = nil, server_sent_events: nil, request_method: 'POST'
raise_error.on_complete(env.merge(body: chunk))
end

partial_json += chunk
utf8_chunk = chunk.force_encoding('UTF-8')

partial_json += if utf8_chunk.valid_encoding?
utf8_chunk
else
utf8_chunk.encode('UTF-8', invalid: :replace, undef: :replace)
end

parsed_json = safe_parse_json(partial_json)

Expand All @@ -121,7 +127,7 @@ def request(path, payload = nil, server_sent_events: nil, request_method: 'POST'

results << result

partial_json = ''
partial_json = String.new.force_encoding('UTF-8')
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/tasks/test-encoding.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require_relative '../../ports/dsl/ollama-ai'

client = Ollama.new(
credentials: { address: 'http://localhost:11434' },
options: { server_sent_events: true }
)

puts client.show({ name: 'yi:latest' })[0]['license']

0 comments on commit 24d05b1

Please sign in to comment.