Class: Ollama::JsonFragmentExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/json_fragment_extractor.rb

Overview

Pulls a single balanced JSON object or array out of a string that may contain surrounding prose (used when models wrap JSON in explanations).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ JsonFragmentExtractor

Returns a new instance of JsonFragmentExtractor.



13
14
15
# File 'lib/ollama/json_fragment_extractor.rb', line 13

def initialize(text)
  @text = text
end

Class Method Details

.call(text) ⇒ Object



9
10
11
# File 'lib/ollama/json_fragment_extractor.rb', line 9

def self.call(text)
  new(text).extract
end

Instance Method Details

#extractObject



17
18
19
20
21
22
23
# File 'lib/ollama/json_fragment_extractor.rb', line 17

def extract
  validate_present!
  stripped = @text.lstrip
  return try_parse_entire_document(stripped) if json_document_prefix?(stripped)

  extract_balanced_fragment
end