Class: Nukitori::SchemaGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/nukitori/schema_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil, &block) ⇒ SchemaGenerator

Returns a new instance of SchemaGenerator.

Examples:

generator = Nukitori::SchemaGenerator.new do
  array :repos do
    object do
      string :name
      string :url
    end
  end
end

Parameters:

  • model (String, nil) (defaults to: nil) —

    LLM model to use

  • block (Proc) —

    Schema definition block

Raises:

  • (ArgumentError)


22
23
24
25
26
27
# File 'lib/nukitori/schema_generator.rb', line 22

def initialize(model: nil, &block)
  raise ArgumentError, 'Block required for schema definition' unless block_given?

  @model = model
  @schema_block = block
end

Instance Attribute Details

#model ⇒ Object (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/nukitori/schema_generator.rb', line 7

def model
  @model
end

Instance Method Details

#create_extraction_schema_for(html) ⇒ Hash

Create extraction schema for given HTML

Examples:

extraction_schema = generator.create_extraction_schema_for(html)

Parameters:

  • html (String, Nokogiri::HTML::Document) —

    HTML string or Nokogiri document

Returns:

  • (Hash) —

    Generated extraction schema



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nukitori/schema_generator.rb', line 36

def create_extraction_schema_for(html)
  doc = html.is_a?(Nokogiri::HTML::Document) ? html : Nokogiri::HTML(html)
  requirements = build_requirements(&@schema_block)
  processed_html = HtmlPreprocessor.process(doc)
  normalized_requirements = normalize_requirements(requirements)
  prompt = build_prompt(normalized_requirements)

  chat = ChatFactory.create(model:)
  chat.with_instructions(prompt)

  response = chat.ask(processed_html)
  ResponseParser.parse(response.content)
end